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.jcs3.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                  try (CachingProvider provider = Caching.getCachingProvider();
52                          CacheManager manager = provider.getCacheManager())
53                  {
54                      Field cache = null;
55                      CompleteConfiguration<?, ?> config = null;
56                      for (final Field f : test.getClass().getDeclaredFields())
57                      {
58                          if (Cache.class.isAssignableFrom(f.getType()))
59                          {
60                              f.setAccessible(true);
61                              cache = f;
62                          }
63                          else if (Configuration.class.isAssignableFrom(f.getType()))
64                          {
65                              f.setAccessible(true);
66                              config = (CompleteConfiguration<?, ?>) f.get(test);
67                          }
68                      }
69                      if (cache != null)
70                      {
71                          if (config == null)
72                          {
73                              throw new IllegalStateException("Define a Configuration field");
74                          }
75                          cache.set(test, manager.createCache(cache.getName(), config));
76                      }
77                      base.evaluate();
78                  }
79              }
80          };
81      }
82  }