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