View Javadoc

1   /*
2    * $Id: ConcurrentHashMapCacheTest.java 1188000 2011-10-23 23:10:24Z mcucchiara $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.commons.ognl.internal;
23  
24  import org.apache.commons.ognl.internal.entry.CacheEntryFactory;
25  import org.apache.commons.ognl.test.objects.Bean2;
26  import org.junit.Test;
27  
28  import java.lang.reflect.Method;
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  import static junit.framework.Assert.assertNotNull;
33  import static org.junit.Assert.assertFalse;
34  
35  public class ConcurrentHashMapCacheTest
36  {
37      private ConcurrentHashMapCacheTest.DummyEntryFactory entryFactory=new DummyEntryFactory( );
38      private Cache<CacheEntry, List<Method>> cache = new ConcurrentHashMapCache<CacheEntry, List<Method>>( entryFactory );
39  
40  
41      @Test
42      public void testGet( )
43          throws Exception
44      {
45  
46          getMethods( new CacheEntry( Bean2.class, "bean3" ) );
47          getMethods( new CacheEntry( Bean2.class, "id" ) );
48      }
49  
50      private void getMethods( CacheEntry entry )
51          throws CacheException
52      {
53          List<Method> methods = cache.get( entry);
54          assertNotNull( methods );
55          assertFalse( methods.isEmpty( ) );
56      }
57  
58      private class CacheEntry
59      {
60          private Class<?> clazz;
61  
62          private String methodName;
63  
64          private CacheEntry( Class<?> clazz, String methodName )
65          {
66              this.clazz = clazz;
67              this.methodName = methodName;
68          }
69  
70          public Class<?> getClazz( )
71          {
72              return clazz;
73          }
74  
75          public String getMethodName( )
76          {
77              return methodName;
78          }
79      }
80  
81      private class DummyEntryFactory
82          implements CacheEntryFactory<CacheEntry, List<Method>>
83      {
84          public List<Method> create( CacheEntry key )
85              throws CacheException
86          {
87              Method[] methods = key.getClazz( ).getMethods( );
88              List<Method> list = new ArrayList<Method>( );
89              for ( Method method : methods )
90              {
91                  String name = method.getName( );
92                  boolean isGet = name.substring( 3, name.length( ) ).equalsIgnoreCase( key.getMethodName( ) );
93                  if ( isGet )
94                  {
95                      list.add( method );
96                  }
97              }
98              return list;
99          }
100     }
101 }