1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 }