View Javadoc
1   package org.apache.commons.jcs3.jcache.cdi;
2   
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  import java.lang.reflect.Constructor;
23  import java.lang.reflect.Method;
24  import java.lang.reflect.Proxy;
25  import java.util.Map;
26  import javax.cache.annotation.CacheDefaults;
27  import javax.cache.annotation.CacheResult;
28  import javax.interceptor.InvocationContext;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.fail;
32  
33  import org.junit.Test;
34  
35  public class CDIJCacheHelperTest
36  {
37      @Test
38      public void proxyCacheDefaults()
39      {
40          final CDIJCacheHelper helper = new CDIJCacheHelper();
41  
42          final MyParent child1 = MyParent.class.cast(Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
43                  new Class<?>[]{MyChild1.class}, (proxy, method, args) -> null));
44          final CDIJCacheHelper.MethodMeta meta1 = helper.findMeta(newContext(child1));
45          assertEquals("child", meta1.getCacheResultCacheName());
46  
47          final MyParent child2 = MyParent.class.cast(Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
48                  new Class<?>[]{MyChild2.class}, (proxy, method, args) -> null));
49          final CDIJCacheHelper.MethodMeta meta2 = helper.findMeta(newContext(child2));
50          assertEquals("child2", meta2.getCacheResultCacheName());
51      }
52  
53      private InvocationContext newContext(final MyParent child1) {
54          return new InvocationContext()
55          {
56              @Override
57              public Object getTarget()
58              {
59                  return child1;
60              }
61  
62              @Override
63              public Method getMethod()
64              {
65                  try {
66                      return MyParent.class.getMethod("foo");
67                  } catch (final NoSuchMethodException e) {
68                      fail(e.getMessage());
69                      return null;
70                  }
71              }
72  
73              @Override
74              public Constructor<?> getConstructor()
75              {
76                  return null;
77              }
78  
79              @Override
80              public Object[] getParameters()
81              {
82                  return new Object[0];
83              }
84  
85              @Override
86              public void setParameters(final Object[] objects)
87              {
88  
89              }
90  
91              @Override
92              public Map<String, Object> getContextData()
93              {
94                  return null;
95              }
96  
97              @Override
98              public Object proceed() throws Exception
99              {
100                 return null;
101             }
102 
103             @Override
104             public Object getTimer()
105             {
106                 return null;
107             }
108         };
109     }
110 
111     public interface MyParent
112     {
113         @CacheResult
114         String foo();
115     }
116 
117     @CacheDefaults(cacheName = "child")
118     public interface MyChild1 extends MyParent
119     {
120     }
121 
122     @CacheDefaults(cacheName = "child2")
123     public interface MyChild2 extends MyParent
124     {
125     }
126 }