001 /*
002 * $Id: ConcurrentHashMapCacheTest.java 1188000 2011-10-23 23:10:24Z mcucchiara $
003 *
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022 package org.apache.commons.ognl.internal;
023
024 import org.apache.commons.ognl.internal.entry.CacheEntryFactory;
025 import org.apache.commons.ognl.test.objects.Bean2;
026 import org.junit.Test;
027
028 import java.lang.reflect.Method;
029 import java.util.ArrayList;
030 import java.util.List;
031
032 import static junit.framework.Assert.assertNotNull;
033 import static org.junit.Assert.assertFalse;
034
035 public class ConcurrentHashMapCacheTest
036 {
037 private ConcurrentHashMapCacheTest.DummyEntryFactory entryFactory=new DummyEntryFactory( );
038 private Cache<CacheEntry, List<Method>> cache = new ConcurrentHashMapCache<CacheEntry, List<Method>>( entryFactory );
039
040
041 @Test
042 public void testGet( )
043 throws Exception
044 {
045
046 getMethods( new CacheEntry( Bean2.class, "bean3" ) );
047 getMethods( new CacheEntry( Bean2.class, "id" ) );
048 }
049
050 private void getMethods( CacheEntry entry )
051 throws CacheException
052 {
053 List<Method> methods = cache.get( entry);
054 assertNotNull( methods );
055 assertFalse( methods.isEmpty( ) );
056 }
057
058 private class CacheEntry
059 {
060 private Class<?> clazz;
061
062 private String methodName;
063
064 private CacheEntry( Class<?> clazz, String methodName )
065 {
066 this.clazz = clazz;
067 this.methodName = methodName;
068 }
069
070 public Class<?> getClazz( )
071 {
072 return clazz;
073 }
074
075 public String getMethodName( )
076 {
077 return methodName;
078 }
079 }
080
081 private class DummyEntryFactory
082 implements CacheEntryFactory<CacheEntry, List<Method>>
083 {
084 public List<Method> create( CacheEntry key )
085 throws CacheException
086 {
087 Method[] methods = key.getClazz( ).getMethods( );
088 List<Method> list = new ArrayList<Method>( );
089 for ( Method method : methods )
090 {
091 String name = method.getName( );
092 boolean isGet = name.substring( 3, name.length( ) ).equalsIgnoreCase( key.getMethodName( ) );
093 if ( isGet )
094 {
095 list.add( method );
096 }
097 }
098 return list;
099 }
100 }
101 }