View Javadoc

1   /**
2    *  Copyright 2003-2006 Greg Luck
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  
18  package net.sf.ehcache.config;
19  
20  import net.sf.ehcache.AbstractCacheTest;
21  import net.sf.ehcache.Cache;
22  import net.sf.ehcache.CacheException;
23  import net.sf.ehcache.CacheManager;
24  import net.sf.ehcache.distribution.CacheManagerPeerListener;
25  import net.sf.ehcache.distribution.CacheManagerPeerProvider;
26  import net.sf.ehcache.distribution.MulticastRMICacheManagerPeerProvider;
27  import net.sf.ehcache.distribution.RMIAsynchronousCacheReplicator;
28  import net.sf.ehcache.distribution.RMICacheManagerPeerListener;
29  import net.sf.ehcache.event.CacheEventListener;
30  import net.sf.ehcache.event.CacheManagerEventListener;
31  import net.sf.ehcache.event.CountingCacheEventListener;
32  import net.sf.ehcache.event.CountingCacheManagerEventListener;
33  
34  import java.io.BufferedInputStream;
35  import java.io.BufferedOutputStream;
36  import java.io.File;
37  import java.io.FileInputStream;
38  import java.io.FileNotFoundException;
39  import java.io.FileOutputStream;
40  import java.io.IOException;
41  import java.io.InputStream;
42  import java.net.URI;
43  import java.net.URL;
44  import java.util.Iterator;
45  import java.util.Set;
46  import java.util.jar.JarEntry;
47  import java.util.jar.JarOutputStream;
48  
49  /**
50   * Tests for Store Configuration
51   *
52   * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
53   * @version $Id: ConfigurationFactoryTest.java 30 2006-04-16 09:23:03Z gregluck $
54   */
55  public class ConfigurationFactoryTest extends AbstractCacheTest {
56  
57  
58      /**
59       * setup test
60       */
61      protected void setUp() throws Exception {
62          super.setUp();
63          manager.removalAll();
64      }
65  
66      /**
67       * Tests that the loader successfully loads from ehcache.xml.
68       * ehcache.xml should be found in the classpath. In our ant configuration
69       * this should be from build/test-classes/ehcache.xml
70       * <p/>
71       * <defaultCache
72       * maxElementsInMemory="10000"
73       * eternal="false"
74       * timeToIdleSeconds="3600"
75       * timeToLiveSeconds="10"
76       * overflowToDisk="true"
77       * />
78       */
79      public void testLoadConfigurationFromClasspath() throws Exception {
80  
81          Configuration configuration = ConfigurationFactory.parseConfiguration();
82          ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
83  
84          //Check disk path  <diskStore path="/tmp"/>
85          assertEquals(System.getProperty("java.io.tmpdir"), configurationHelper.getDiskStorePath());
86  
87          //Check CacheManagerEventListener
88          assertEquals(null, configurationHelper.createCacheManagerEventListener());
89  
90          //Check default cache
91          Cache defaultCache = configurationHelper.createDefaultCache();
92          assertEquals("default", defaultCache.getName());
93          assertEquals(false, defaultCache.isEternal());
94          assertEquals(5, defaultCache.getTimeToIdleSeconds());
95          assertEquals(10, defaultCache.getTimeToLiveSeconds());
96          assertEquals(true, defaultCache.isOverflowToDisk());
97  
98          //Check caches
99          assertEquals(8, configurationHelper.createCaches().size());
100 
101         /*
102         <cache name="sampleCache1"
103         maxElementsInMemory="10000"
104         eternal="false"
105         timeToIdleSeconds="360"
106         timeToLiveSeconds="1000"
107         overflowToDisk="true"
108         />
109         */
110         Cache sampleCache1 = configurationHelper.createCacheFromName("sampleCache1");
111         assertEquals("sampleCache1", sampleCache1.getName());
112         assertEquals(false, sampleCache1.isEternal());
113         assertEquals(360, sampleCache1.getTimeToIdleSeconds());
114         assertEquals(1000, sampleCache1.getTimeToLiveSeconds());
115         assertEquals(true, sampleCache1.isOverflowToDisk());
116 
117         /** A cache which overflows to disk. The disk store is persistent
118          between cache and VM restarts. The disk expiry thread interval is set to 10 minutes, overriding
119          the default of 2 minutes.
120          <cache name="persistentLongExpiryIntervalCache"
121          maxElementsInMemory="500"
122          eternal="false"
123          timeToIdleSeconds="300"
124          timeToLiveSeconds="600"
125          overflowToDisk="true"
126          diskPersistent="true"
127          diskExpiryThreadIntervalSeconds="600"
128          /> */
129         Cache persistentLongExpiryIntervalCache = configurationHelper.createCacheFromName("persistentLongExpiryIntervalCache");
130         assertEquals("persistentLongExpiryIntervalCache", persistentLongExpiryIntervalCache.getName());
131         assertEquals(false, persistentLongExpiryIntervalCache.isEternal());
132         assertEquals(300, persistentLongExpiryIntervalCache.getTimeToIdleSeconds());
133         assertEquals(600, persistentLongExpiryIntervalCache.getTimeToLiveSeconds());
134         assertEquals(true, persistentLongExpiryIntervalCache.isOverflowToDisk());
135         assertEquals(true, persistentLongExpiryIntervalCache.isDiskPersistent());
136         assertEquals(600, persistentLongExpiryIntervalCache.getDiskExpiryThreadIntervalSeconds());
137     }
138 
139 
140     /**
141      * Tests that the loader successfully loads from ehcache.xml
142      * given as a {@link File}
143      * <p/>
144      * <defaultCache
145      * maxElementsInMemory="10000"
146      * eternal="false"
147      * timeToIdleSeconds="120"
148      * timeToLiveSeconds="120"
149      * overflowToDisk="true"
150      * />
151      */
152     public void testLoadConfigurationFromFile() throws Exception {
153 
154         File file = new File(SRC_CONFIG_DIR + "ehcache.xml");
155         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
156         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
157 
158         //Check disk path  <diskStore path="/tmp"/>
159         assertEquals(System.getProperty("java.io.tmpdir"), configurationHelper.getDiskStorePath());
160 
161         //Check default cache
162         Cache defaultCache = configurationHelper.createDefaultCache();
163         assertEquals("default", defaultCache.getName());
164         assertEquals(false, defaultCache.isEternal());
165         assertEquals(120, defaultCache.getTimeToIdleSeconds());
166         assertEquals(120, defaultCache.getTimeToLiveSeconds());
167         assertEquals(true, defaultCache.isOverflowToDisk());
168 
169         //Check caches
170         assertEquals(5, configurationHelper.createCaches().size());
171 
172         //  <cache name="sampleCache1"
173         //  maxElementsInMemory="10000"
174         //  eternal="false"
175         //  timeToIdleSeconds="300"
176         //  timeToLiveSeconds="600"
177         //  overflowToDisk="true"
178         //  />
179         Cache sampleCache1 = configurationHelper.createCacheFromName("sampleCache1");
180         assertEquals("sampleCache1", sampleCache1.getName());
181         assertEquals(false, sampleCache1.isEternal());
182         assertEquals(300, sampleCache1.getTimeToIdleSeconds());
183         assertEquals(600, sampleCache1.getTimeToLiveSeconds());
184         assertEquals(true, sampleCache1.isOverflowToDisk());
185     }
186 
187 
188     /**
189      * Tests that the loader successfully loads from ehcache-1.1.xml
190      * given as a {@link File}. This is a backward compatibility test.
191      * <p/>
192      * <defaultCache
193      * maxElementsInMemory="10000"
194      * eternal="false"
195      * timeToIdleSeconds="120"
196      * timeToLiveSeconds="120"
197      * overflowToDisk="true"
198      * />
199      */
200     public void testLoadConfigurationFromEhcache11File() throws Exception {
201 
202         File file = new File(TEST_CONFIG_DIR + "ehcache-1_1.xml");
203         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
204         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
205 
206         //Check disk path  <diskStore path="/tmp"/>
207         assertEquals(System.getProperty("java.io.tmpdir"), configurationHelper.getDiskStorePath());
208 
209         //Check default cache
210         Cache defaultCache = configurationHelper.createDefaultCache();
211         assertEquals("default", defaultCache.getName());
212         assertEquals(false, defaultCache.isEternal());
213         assertEquals(5, defaultCache.getTimeToIdleSeconds());
214         assertEquals(10, defaultCache.getTimeToLiveSeconds());
215         assertEquals(true, defaultCache.isOverflowToDisk());
216 
217         //Check caches
218         assertEquals(8, configurationHelper.createCaches().size());
219 
220         //  <cache name="sampleCache1"
221         //  maxElementsInMemory="10000"
222         //  eternal="false"
223         //  timeToIdleSeconds="300"
224         //  timeToLiveSeconds="600"
225         //  overflowToDisk="true"
226         //  />
227         Cache sampleCache1 = configurationHelper.createCacheFromName("sampleCache1");
228         assertEquals("sampleCache1", sampleCache1.getName());
229         assertEquals(false, sampleCache1.isEternal());
230         assertEquals(360, sampleCache1.getTimeToIdleSeconds());
231         assertEquals(1000, sampleCache1.getTimeToLiveSeconds());
232         assertEquals(true, sampleCache1.isOverflowToDisk());
233     }
234 
235     /**
236      * Tests that the CacheManagerEventListener is null when
237      * no CacheManagerEventListener class is specified.
238      */
239     public void testLoadConfigurationFromFileNoCacheManagerListenerDefault() throws Exception {
240         File file = new File(TEST_CONFIG_DIR + "ehcache-nolisteners.xml");
241         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
242         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
243 
244         //Check CacheManagerEventListener
245         CacheManagerEventListener listener = configurationHelper.createCacheManagerEventListener();
246         assertEquals(null, listener);
247 
248         //Check caches. Configuration should have completed
249         assertEquals(10, configurationHelper.createCaches().size());
250     }
251 
252     /**
253      * Tests that the CacheManagerEventListener class is set as the CacheManagerEventListener
254      * when the class is unloadable.
255      */
256     public void testLoadConfigurationFromFileUnloadableCacheManagerListenerDefault() throws Exception {
257         File file = new File(TEST_CONFIG_DIR + "ehcache-unloadablecachemanagerlistenerclass.xml");
258         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
259         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
260 
261         //Check CacheManagerEventListener
262         CacheManagerEventListener listener = null;
263         try {
264             listener = configurationHelper.createCacheManagerEventListener();
265             fail();
266         } catch (CacheException e) {
267             //expected
268         }
269     }
270 
271     /**
272      * Positive and negative Tests for setting a list of CacheEventListeners in the configuration
273      */
274     public void testLoadConfigurationFromFileCountingCacheListener() throws Exception {
275         File file = new File(TEST_CONFIG_DIR + "ehcache-countinglisteners.xml");
276         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
277         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
278 
279         //Check CacheManagerEventListener
280         Class actualClass = configurationHelper.createCacheManagerEventListener().getClass();
281         assertEquals(CountingCacheManagerEventListener.class, actualClass);
282 
283         //Check caches. Configuration should have completed
284         assertEquals(10, configurationHelper.createCaches().size());
285 
286         //Should have null and counting
287         Cache sampleCache1 = configurationHelper.createCacheFromName("sampleCache1");
288         Set registeredListeners = sampleCache1.getCacheEventNotificationService().getCacheEventListeners();
289         assertEquals(2, registeredListeners.size());
290 
291         //Should have null and counting
292         Cache sampleCache2 = configurationHelper.createCacheFromName("sampleCache2");
293         registeredListeners = sampleCache2.getCacheEventNotificationService().getCacheEventListeners();
294         assertEquals(1, registeredListeners.size());
295 
296         //Should have null and counting
297         Cache sampleCache3 = configurationHelper.createCacheFromName("sampleCache3");
298         registeredListeners = sampleCache3.getCacheEventNotificationService().getCacheEventListeners();
299         assertEquals(1, registeredListeners.size());
300 
301         //Should have none. None set.
302         Cache footerPageCache = configurationHelper.createCacheFromName("FooterPageCache");
303         registeredListeners = footerPageCache.getCacheEventNotificationService().getCacheEventListeners();
304         assertEquals(0, registeredListeners.size());
305 
306         //Should have one. null listener set.
307         Cache persistentLongExpiryIntervalCache = configurationHelper.createCacheFromName("persistentLongExpiryIntervalCache");
308         registeredListeners = persistentLongExpiryIntervalCache.getCacheEventNotificationService()
309                 .getCacheEventListeners();
310         assertEquals(1, registeredListeners.size());
311     }
312 
313     /**
314      * Tests for Distributed Cache config
315      */
316     public void testLoadConfigurationFromFileDistribution() throws Exception {
317         File file = new File(TEST_CONFIG_DIR + "distribution/ehcache-distributed1.xml");
318         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
319         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
320 
321         //Check CacheManagerPeerProvider
322         CacheManagerPeerProvider peerProvider = configurationHelper.createCachePeerProvider();
323         assertTrue(peerProvider instanceof MulticastRMICacheManagerPeerProvider);
324 
325         //check CacheManagerPeerListener
326         CacheManagerPeerListener peerListener = configurationHelper.createCachePeerListener();
327         assertTrue(peerListener instanceof RMICacheManagerPeerListener);
328 
329         //Check caches. Configuration should have completed
330         assertEquals(61, configurationHelper.createCaches().size());
331 
332         Cache sampleCache1 = configurationHelper.createCacheFromName("sampleCache1");
333         Set listeners = sampleCache1.getCacheEventNotificationService().getCacheEventListeners();
334         assertEquals(2, listeners.size());
335         for (Iterator iterator = listeners.iterator(); iterator.hasNext();) {
336             CacheEventListener cacheEventListener = (CacheEventListener) iterator.next();
337             assertTrue(cacheEventListener instanceof RMIAsynchronousCacheReplicator || cacheEventListener
338                     instanceof CountingCacheEventListener);
339         }
340 
341 
342     }
343 
344 
345     /**
346      * Tests that the loader successfully loads from ehcache-nodefault.xml
347      * given as a {@link File}
348      * <p/>
349      * <defaultCache
350      * maxElementsInMemory="10000"
351      * eternal="false"
352      * timeToIdleSeconds="120"
353      * timeToLiveSeconds="120"
354      * overflowToDisk="true"
355      * />
356      */
357     public void testLoadConfigurationFromFileNoDefault() throws Exception {
358         File file = new File(TEST_CONFIG_DIR + "ehcache-nodefault.xml");
359         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
360         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
361 
362         //Check disk path  <diskStore path="/tmp"/>
363         assertEquals(System.getProperty("java.io.tmpdir"), configurationHelper.getDiskStorePath());
364 
365         //Check default cache
366         try {
367             configurationHelper.createDefaultCache();
368             fail();
369         } catch (CacheException e) {
370             //noop
371         }
372 
373         //Check caches
374         assertEquals(3, configurationHelper.createCaches().size());
375 
376         //  <cache name="sampleCache1"
377         //  maxElementsInMemory="10000"
378         //  eternal="false"
379         //  timeToIdleSeconds="300"
380         //  timeToLiveSeconds="600"
381         //  overflowToDisk="true"
382         //  />
383         Cache sampleCache1 = configurationHelper.createCacheFromName("sampleCache1");
384         assertEquals("sampleCache1", sampleCache1.getName());
385         assertEquals(false, sampleCache1.isEternal());
386         assertEquals(300, sampleCache1.getTimeToIdleSeconds());
387         assertEquals(600, sampleCache1.getTimeToLiveSeconds());
388         assertEquals(true, sampleCache1.isOverflowToDisk());
389     }
390 
391     /**
392      * Tests that the loader successfully loads from ehcache-nodefault.xml
393      * given as a {@link File}
394      * <p/>
395      * /**
396      * Tests that the loader successfully loads from ehcache-nodefault.xml
397      * given as a {@link File}
398      * <p/>
399      * <cache name="sampleCacheNoOptionalAttributes"
400      * maxElementsInMemory="1000"
401      * eternal="true"
402      * overflowToDisk="false"
403      * />
404      */
405     public void testDefaultValues() throws Exception {
406         File file = new File(TEST_CONFIG_DIR + "ehcache-nodefault.xml");
407         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
408         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
409 
410         Cache sampleCacheNoOptionalAttributes = configurationHelper.createCacheFromName("sampleCacheNoOptionalAttributes");
411         assertEquals("sampleCacheNoOptionalAttributes", sampleCacheNoOptionalAttributes.getName());
412         assertEquals(1000, sampleCacheNoOptionalAttributes.getMaxElementsInMemory());
413         assertEquals(true, sampleCacheNoOptionalAttributes.isEternal());
414         assertEquals(false, sampleCacheNoOptionalAttributes.isOverflowToDisk());
415         assertEquals(0, sampleCacheNoOptionalAttributes.getTimeToIdleSeconds());
416         assertEquals(0, sampleCacheNoOptionalAttributes.getTimeToLiveSeconds());
417         assertEquals(false, sampleCacheNoOptionalAttributes.isDiskPersistent());
418         assertEquals(120, sampleCacheNoOptionalAttributes.getDiskExpiryThreadIntervalSeconds());
419     }
420 
421 
422     /**
423      * Tests that the loader successfully loads from ehcache-nodisk.xml
424      * given as a {@link File}
425      * <p/>
426      * <defaultCache
427      * maxElementsInMemory="10000"
428      * eternal="false"
429      * timeToIdleSeconds="120"
430      * timeToLiveSeconds="120"
431      * overflowToDisk="false"
432      * <p/>
433      * No disk store path specified as disk store not being used
434      * />
435      */
436     public void testLoadConfigurationFromFileNoDisk() throws Exception {
437         File file = new File(TEST_CONFIG_DIR + "ehcache-nodisk.xml");
438         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
439         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
440 
441         //Check disk path  <diskStore path="/tmp"/>
442         assertEquals(null, configurationHelper.getDiskStorePath());
443 
444         //Check default cache
445         Cache defaultCache = configurationHelper.createDefaultCache();
446         assertEquals("default", defaultCache.getName());
447         assertEquals(false, defaultCache.isEternal());
448         assertEquals(5, defaultCache.getTimeToIdleSeconds());
449         assertEquals(10, defaultCache.getTimeToLiveSeconds());
450         assertEquals(false, defaultCache.isOverflowToDisk());
451 
452         //Check caches
453         assertEquals(2, configurationHelper.createCaches().size());
454 
455         //  <cache name="sampleCache1"
456         //  maxElementsInMemory="10000"
457         //  eternal="false"
458         //  timeToIdleSeconds="300"
459         //  timeToLiveSeconds="600"
460         //  overflowToDisk="true"
461         //  />
462         Cache sampleCache1 = configurationHelper.createCacheFromName("sampleCache1");
463         assertEquals("sampleCache1", sampleCache1.getName());
464         assertEquals(false, sampleCache1.isEternal());
465         assertEquals(360, sampleCache1.getTimeToIdleSeconds());
466         assertEquals(1000, sampleCache1.getTimeToLiveSeconds());
467         assertEquals(false, sampleCache1.isOverflowToDisk());
468     }
469 
470     /**
471      * Tests the default values for optional attributes
472      * <p/>
473      * <!-- Sample cache. Optional attributes are removed -->
474      * <cache name="sampleRequiredAttributesOnly"
475      * maxElementsInMemory="1000"
476      * eternal="true"
477      * overflowToDisk="false"
478      * />
479      * <p/>
480      * No disk store path specified as disk store not being used
481      * />
482      */
483     public void testOptionalAttributeDefaultValues() throws Exception {
484         File file = new File(TEST_CONFIG_DIR + "ehcache-nodisk.xml");
485         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
486         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
487 
488         //  <cache name="sampleCache1"
489         //  maxElementsInMemory="10000"
490         //  eternal="false"
491         //  timeToIdleSeconds="300"
492         //  timeToLiveSeconds="600"
493         //  overflowToDisk="true"
494         //  />
495         Cache sampleCache1 = configurationHelper.createCacheFromName("sampleCache1");
496         assertEquals("sampleCache1", sampleCache1.getName());
497         assertEquals(false, sampleCache1.isEternal());
498         assertEquals(360, sampleCache1.getTimeToIdleSeconds());
499         assertEquals(1000, sampleCache1.getTimeToLiveSeconds());
500         assertEquals(false, sampleCache1.isOverflowToDisk());
501     }
502 
503     /**
504      * Regression test for bug 1432074 - NullPointer on RMICacheManagerPeerProviderFactory
505      * If manual peer provider configuration is selected then a CacheException should be
506      * thrown if there is no list.
507      */
508     public void testBadManualDistributedConfiguration() {
509         try {
510             new CacheManager(TEST_CONFIG_DIR + "distribution/ehcache-bad-manual-distributed.xml");
511             fail();
512         } catch (CacheException e) {
513             assertEquals("rmiUrls must be specified when peerDiscovery is manual", e.getMessage());
514         }
515     }
516 
517 
518     /**
519      * Tests that the loader successfully loads from ehcache.xml
520      * given as an {@link URL}.
521      * <p/>
522      * is found first
523      * <p/>
524      * <defaultCache
525      * maxElementsInMemory="10"
526      * eternal="false"
527      * timeToIdleSeconds="5"
528      * timeToLiveSeconds="10"
529      * overflowToDisk="true"
530      * />
531      */
532     public void testLoadConfigurationFromURL() throws Exception {
533         URL url = getClass().getResource("/ehcache.xml");
534         testDefaultConfiguration(url);
535     }
536 
537     /**
538      * Exposes a bug where the default configuration could not be loaded from a Jar URL
539      * (a common scenario when ehcache is deployed, and always used for failsafe config).
540      *
541      * @throws Exception When the test fails.
542      */
543     public void testLoadConfigurationFromJarURL() throws Exception {
544 
545         // first, create the jar
546         File tempJar = createTempConfigJar();
547 
548         // convert it to a URL
549         URL tempUrl = tempJar.toURI().toURL();
550 
551         // create a jar url that points to the configuration file
552         String entry = "jar:" + tempUrl + "!/ehcache.xml";
553 
554         // create a URL object from the string, going through the URI class so it's encoded
555         URL entryUrl = new URI(entry).toURL();
556 
557         testDefaultConfiguration(entryUrl);
558     }
559 
560     /**
561      * Given a URL, parse the configuration and test that the config read corresponds
562      * to that which exists in the ehcache.xml file.
563      *
564      * @param url The URL to load.
565      */
566     private void testDefaultConfiguration(URL url) {
567         Configuration configuration = ConfigurationFactory.parseConfiguration(url);
568         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
569 
570         //Check disk path  <diskStore path="/tmp"/>
571         assertEquals(System.getProperty("java.io.tmpdir"), configurationHelper.getDiskStorePath());
572 
573         //Check default cache
574         Cache defaultCache = configurationHelper.createDefaultCache();
575         assertEquals("default", defaultCache.getName());
576         assertEquals(false, defaultCache.isEternal());
577         assertEquals(5, defaultCache.getTimeToIdleSeconds());
578         assertEquals(10, defaultCache.getTimeToLiveSeconds());
579         assertEquals(true, defaultCache.isOverflowToDisk());
580 
581         //Check caches
582         assertEquals(8, configurationHelper.createCaches().size());
583 
584         //  <cache name="sampleCache1"
585         //  maxElementsInMemory="10000"
586         //  eternal="false"
587         //  timeToIdleSeconds="300"
588         //  timeToLiveSeconds="600"
589         //  overflowToDisk="true"
590         //  />
591         Cache sampleCache1 = configurationHelper.createCacheFromName("sampleCache1");
592         assertEquals("sampleCache1", sampleCache1.getName());
593         assertEquals(false, sampleCache1.isEternal());
594         assertEquals(360, sampleCache1.getTimeToIdleSeconds());
595         assertEquals(1000, sampleCache1.getTimeToLiveSeconds());
596         assertEquals(true, sampleCache1.isOverflowToDisk());
597     }
598 
599     /**
600      * Creates a jar file that contains only ehcache.xml (a supplied configuration file).
601      *
602      * @return The jar file created with the configuration file as its only entry.
603      * @throws IOException If the jar could not be created.
604      */
605     private File createTempConfigJar() throws IOException, FileNotFoundException {
606         File tempJar = File.createTempFile("config_", ".jar");
607         tempJar.deleteOnExit();
608 
609         // write the default config to the jar
610         JarOutputStream jos = null;
611         try {
612             jos = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(tempJar)));
613 
614             jos.putNextEntry(new JarEntry("ehcache.xml"));
615 
616             InputStream defaultCfg = null;
617             try {
618                 defaultCfg = new BufferedInputStream(getClass().getResource("/ehcache.xml").openStream());
619                 byte[] buf = new byte[1024];
620                 int read = 0;
621                 while ((read = defaultCfg.read(buf)) > 0) {
622                     jos.write(buf, 0, read);
623                 }
624             } finally {
625                 try {
626                     if (defaultCfg != null) {
627                         defaultCfg.close();
628                     }
629                 } catch (IOException ioEx) {
630                     // swallow this exception
631                 }
632             }
633 
634         } finally {
635             try {
636                 if (jos != null) {
637                     jos.closeEntry();
638 
639                     jos.flush();
640                     jos.close();
641                 }
642             } catch (IOException ioEx) {
643                 // swallow this exception
644             }
645         }
646 
647         return tempJar;
648     }
649 
650     /**
651      * Tests that the loader successfully loads from ehcache.xml
652      * given as a {@link InputStream}
653      * <p/>
654      * <defaultCache
655      * maxElementsInMemory="10000"
656      * eternal="false"
657      * timeToIdleSeconds="120"
658      * timeToLiveSeconds="120"
659      * overflowToDisk="true"
660      * />
661      */
662     public void testLoadConfigurationFromInputStream() throws Exception {
663         InputStream fis = new FileInputStream(new File(SRC_CONFIG_DIR + "ehcache.xml").getAbsolutePath());
664         ConfigurationHelper configurationHelper;
665         try {
666             Configuration configuration = ConfigurationFactory.parseConfiguration(fis);
667             configurationHelper = new ConfigurationHelper(manager, configuration);
668         } finally {
669             fis.close();
670         }
671 
672         //Check disk path  <diskStore path="/tmp"/>
673         assertEquals(System.getProperty("java.io.tmpdir"), configurationHelper.getDiskStorePath());
674 
675         //Check default cache
676         Cache defaultCache = configurationHelper.createDefaultCache();
677         assertEquals("default", defaultCache.getName());
678         assertEquals(false, defaultCache.isEternal());
679         assertEquals(120, defaultCache.getTimeToIdleSeconds());
680         assertEquals(120, defaultCache.getTimeToLiveSeconds());
681         assertEquals(true, defaultCache.isOverflowToDisk());
682 
683         //Check caches
684         assertEquals(5, configurationHelper.createCaches().size());
685 
686         //  <cache name="sampleCache1"
687         //  maxElementsInMemory="10000"
688         //  eternal="false"
689         //  timeToIdleSeconds="300"
690         //  timeToLiveSeconds="600"
691         //  overflowToDisk="true"
692         //  />
693         Cache sampleCache1 = configurationHelper.createCacheFromName("sampleCache1");
694         assertEquals("sampleCache1", sampleCache1.getName());
695         assertEquals(false, sampleCache1.isEternal());
696         assertEquals(300, sampleCache1.getTimeToIdleSeconds());
697         assertEquals(600, sampleCache1.getTimeToLiveSeconds());
698         assertEquals(true, sampleCache1.isOverflowToDisk());
699     }
700 
701     /**
702      * Tests that the loader successfully loads from ehcache-failsafe.xml
703      * found in the classpath.
704      * ehcache.xml should be found in the classpath. In our ant configuration
705      * this should be from build/classes/ehcache-failsafe.xml
706      * <p/>
707      * We delete ehcache.xml from build/test-classes/ first, as failsafe only
708      * kicks in when ehcache.xml is not in the classpath.
709      * <p/>
710      * <defaultCache
711      * maxElementsInMemory="10000"
712      * eternal="false"
713      * timeToIdleSeconds="120"
714      * timeToLiveSeconds="120"
715      * overflowToDisk="true"
716      * />
717      */
718     public void testLoadConfigurationFromFailsafe() throws Exception {
719         try {
720             File file = new File(AbstractCacheTest.TEST_CLASSES_DIR + "ehcache.xml");
721             file.renameTo(new File(AbstractCacheTest.TEST_CLASSES_DIR + "hideehcache.xml"));
722             Configuration configuration = ConfigurationFactory.parseConfiguration();
723             ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
724 
725             //Check disk path  <diskStore path="/tmp"/>
726             assertEquals(System.getProperty("java.io.tmpdir"), configurationHelper.getDiskStorePath());
727 
728             //Check default cache
729             Cache defaultCache = configurationHelper.createDefaultCache();
730             assertEquals("default", defaultCache.getName());
731             assertEquals(false, defaultCache.isEternal());
732             assertEquals(120, defaultCache.getTimeToIdleSeconds());
733             assertEquals(120, defaultCache.getTimeToLiveSeconds());
734             assertEquals(true, defaultCache.isOverflowToDisk());
735 
736             //Check caches
737             assertEquals(0, configurationHelper.createCaches().size());
738         } finally {
739             //Put ehcache.xml back
740             File hiddenFile = new File(AbstractCacheTest.TEST_CLASSES_DIR + "hideehcache.xml");
741             hiddenFile.renameTo(new File(AbstractCacheTest.TEST_CLASSES_DIR + "ehcache.xml"));
742         }
743 
744     }
745 
746     /**
747      * Make sure that the empty Configuration constructor remains public for those wishing to create CacheManagers
748      * purely programmatically.
749      */
750     public void testCreateEmptyConfiguration() {
751         Configuration configuration = new Configuration();
752         configuration.setSource("programmatic");
753     }
754 }