/*
 * $Id$
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.commons.ognl.internal;

import org.apache.commons.ognl.internal.entry.MethodPermCacheEntryFactory;
import org.apache.commons.ognl.test.objects.Root;
import org.junit.Assert;
import org.junit.Test;

import java.lang.reflect.Method;
import java.security.Permission;

/**
 * User: Maurizio Cucchiara
 * Date: 10/17/11
 * Time: 12:13 AM
 */
public class MethodPermCacheTest
{
    private SecurityManager securityManager =new DummySecurityManager();

    Cache<Method, Boolean> cache =
        new ConcurrentHashMapCache<Method, Boolean>( new MethodPermCacheEntryFactory( securityManager ) );
    @Test
    public void testGetPublicMethod( )
        throws CacheException, NoSuchMethodException
    {
        Method method = Root.class.getMethod( "getArray");
        Assert.assertTrue( cache.get( method ) );
    }

    private class DummySecurityManager
        extends SecurityManager
    {
        @Override
        public void checkPermission( Permission perm )
        {
        }
    }
}

@@ -1201,17 +1201,17 @@
 acheTest
-%0A
+ 
 %7B%0A    pr
@@ -1232,17 +1232,25 @@
 Manager 
-s
+allowAllS
 ecurityM
@@ -1261,17 +1261,104 @@
 er =
+ 
 new 
-Dummy
+AllowAllSecurityManager();%0A    private SecurityManager denyAllSecurityManager = new DenyAll
 Secu
@@ -1377,16 +1377,24 @@
 );%0A%0A    
+private 
 Cache%3CMe
@@ -1413,14 +1413,53 @@
 n%3E c
-ache =
+reateCache(SecurityManager securityManager) %7B
 %0A   
@@ -1463,16 +1463,23 @@
         
+return 
 new Conc
@@ -1514,17 +1514,16 @@
 oolean%3E(
- 
 new Meth
@@ -1546,17 +1546,16 @@
 Factory(
- 
 security
@@ -1561,22 +1561,28 @@
 yManager
- 
 ) );%0A
+    %7D%0A%0A
     @Tes
@@ -1618,16 +1618,28 @@
 icMethod
+_returnsTrue
 ( )%0A    
@@ -1689,24 +1689,102 @@
 ption%0A    %7B%0A
+        Cache%3CMethod, Boolean%3E cache = createCache(allowAllSecurityManager);%0A%0A
         Meth
@@ -1895,27 +1895,528 @@
     
-private class Dummy
+@Test%0A    public void testGetPublicMethod_returnsFalse( )%0A            throws CacheException, NoSuchMethodException%0A    %7B%0A        Cache%3CMethod, Boolean%3E cache = createCache(denyAllSecurityManager);%0A        Method method = Root.class.getMethod( %22getArray%22);%0A%0A        Assert.assertFalse( cache.get( method ) );%0A    %7D%0A%0A    private class AllowAllSecurityManager%0A        extends SecurityManager%0A    %7B%0A        @Override%0A        public void checkPermission( Permission perm )%0A        %7B%0A        %7D%0A    %7D%0A%0A    private class DenyAll
 Secu
@@ -2419,32 +2419,36 @@
 SecurityManager%0A
+    
         extends 
@@ -2544,32 +2544,84 @@
 erm )%0A        %7B%0A
+            throw new SecurityException(%22Denied.%22);%0A
         %7D%0A    %7D%0A

enhance MethodPermCacheTest