View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.jcs.jcache.extras;
20  
21  import org.junit.rules.TestRule;
22  import org.junit.runner.Description;
23  import org.junit.runners.model.Statement;
24  
25  import javax.cache.Cache;
26  import javax.cache.CacheManager;
27  import javax.cache.Caching;
28  import javax.cache.configuration.CompleteConfiguration;
29  import javax.cache.configuration.Configuration;
30  import javax.cache.spi.CachingProvider;
31  import java.lang.reflect.Field;
32  
33  // TODO: *if needed* define @CacheDeifnition instead of relying on field types
34  public class InternalCacheRule implements TestRule
35  {
36      private final Object test;
37  
38      public InternalCacheRule(final Object test)
39      {
40          this.test = test;
41      }
42  
43      @Override
44      public Statement apply(final Statement base, final Description description)
45      {
46          return new Statement()
47          {
48              @Override
49              public void evaluate() throws Throwable
50              {
51                  final CachingProvider provider = Caching.getCachingProvider();
52                  final CacheManager manager = provider.getCacheManager();
53                  try
54                  {
55                      Field cache = null;
56                      CompleteConfiguration<?, ?> config = null;
57                      for (final Field f : test.getClass().getDeclaredFields())
58                      {
59                          if (Cache.class.isAssignableFrom(f.getType()))
60                          {
61                              f.setAccessible(true);
62                              cache = f;
63                          }
64                          else if (Configuration.class.isAssignableFrom(f.getType()))
65                          {
66                              f.setAccessible(true);
67                              config = (CompleteConfiguration<?, ?>) f.get(test);
68                          }
69                      }
70                      if (cache != null)
71                      {
72                          if (config == null)
73                          {
74                              throw new IllegalStateException("Define a Configuration field");
75                          }
76                          cache.set(test, manager.createCache(cache.getName(), config));
77                      }
78                      base.evaluate();
79                  }
80                  finally
81                  {
82                      manager.close();
83                      provider.close();
84                  }
85              }
86          };
87      }
88  }