1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
54
55
56
57
58
59
60
61 public class ConfigurationFactoryTest extends AbstractCacheTest {
62
63
64
65
66
67 protected void setUp() throws Exception {
68 super.setUp();
69 manager.removalAll();
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 public void testLoadConfigurationFromClasspath() throws Exception {
86
87 Configuration configuration = ConfigurationFactory.parseConfiguration();
88 ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
89
90
91 assertEquals(System.getProperty("java.io.tmpdir"), configurationHelper.getDiskStorePath());
92
93
94 CacheManagerPeerProvider peerProvider = configurationHelper.createCachePeerProvider();
95
96
97 assertTrue(peerProvider instanceof MulticastRMICacheManagerPeerProvider);
98 assertEquals(new Integer(1), ((MulticastRMICacheManagerPeerProvider) peerProvider).getHeartBeatSender().getTimeToLive());
99
100
101 assertEquals(null, configurationHelper.createCacheManagerEventListener());
102
103
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
112 assertEquals(12, configurationHelper.createCaches().size());
113
114
115
116
117
118
119
120
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
132
133
134
135
136
137
138
139
140
141
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
154 }
155
156
157
158
159
160
161
162
163
164
165
166
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
176 assertEquals(System.getProperty("java.io.tmpdir"), configurationHelper.getDiskStorePath());
177
178
179 CacheManagerPeerProvider peerProvider = configurationHelper.createCachePeerProvider();
180
181
182 assertTrue(peerProvider instanceof MulticastRMICacheManagerPeerProvider);
183 assertEquals(new Integer(1), ((MulticastRMICacheManagerPeerProvider) peerProvider).getHeartBeatSender().getTimeToLive());
184
185
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
197 assertEquals(6, configurationHelper.createCaches().size());
198
199
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
209
210
211
212
213
214
215
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
228
229
230
231
232
233
234
235
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
245 assertEquals(System.getProperty("java.io.tmpdir"), configurationHelper.getDiskStorePath());
246
247
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
258 assertEquals(8, configurationHelper.createCaches().size());
259
260
261
262
263
264
265
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
277
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
285 CacheManagerEventListener listener = configurationHelper.createCacheManagerEventListener();
286 assertEquals(null, listener);
287
288
289 assertEquals(10, configurationHelper.createCaches().size());
290 }
291
292
293
294
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
302 CacheManagerEventListener listener = null;
303 try {
304 listener = configurationHelper.createCacheManagerEventListener();
305 fail();
306 } catch (CacheException e) {
307
308 }
309 }
310
311
312
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
320 Class actualClass = configurationHelper.createCacheManagerEventListener().getClass();
321 assertEquals(CountingCacheManagerEventListener.class, actualClass);
322
323
324 assertEquals(10, configurationHelper.createCaches().size());
325
326
327 Ehcache sampleCache1 = configurationHelper.createCacheFromName("sampleCache1");
328 Set registeredListeners = sampleCache1.getCacheEventNotificationService().getCacheEventListeners();
329 assertEquals(2, registeredListeners.size());
330
331
332 Ehcache sampleCache2 = configurationHelper.createCacheFromName("sampleCache2");
333 registeredListeners = sampleCache2.getCacheEventNotificationService().getCacheEventListeners();
334 assertEquals(1, registeredListeners.size());
335
336
337 Ehcache sampleCache3 = configurationHelper.createCacheFromName("sampleCache3");
338 registeredListeners = sampleCache3.getCacheEventNotificationService().getCacheEventListeners();
339 assertEquals(1, registeredListeners.size());
340
341
342 Ehcache footerPageCache = configurationHelper.createCacheFromName("FooterPageCache");
343 registeredListeners = footerPageCache.getCacheEventNotificationService().getCacheEventListeners();
344 assertEquals(0, registeredListeners.size());
345
346
347 Ehcache persistentLongExpiryIntervalCache = configurationHelper.createCacheFromName("persistentLongExpiryIntervalCache");
348 registeredListeners = persistentLongExpiryIntervalCache.getCacheEventNotificationService()
349 .getCacheEventListeners();
350 assertEquals(1, registeredListeners.size());
351 }
352
353
354
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
362 CacheManagerPeerProvider peerProvider = configurationHelper.createCachePeerProvider();
363
364
365 assertTrue(peerProvider instanceof MulticastRMICacheManagerPeerProvider);
366 assertEquals(new Integer(64), ((MulticastRMICacheManagerPeerProvider) peerProvider).getHeartBeatSender().getTimeToLive());
367
368
369 CacheManagerPeerListener peerListener = configurationHelper.createCachePeerListener();
370 assertTrue(peerListener instanceof RMICacheManagerPeerListener);
371
372
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
394
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
409
410
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
425
426
427
428
429
430
431
432
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
441 assertEquals(System.getProperty("java.io.tmpdir"), configurationHelper.getDiskStorePath());
442
443
444 try {
445 configurationHelper.createDefaultCache();
446 fail();
447 } catch (CacheException e) {
448
449 }
450
451
452 assertEquals(3, configurationHelper.createCaches().size());
453
454
455
456
457
458
459
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
471
472
473
474
475
476
477
478
479
480
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
502
503
504
505
506
507
508
509
510
511
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
520 assertEquals(null, configurationHelper.getDiskStorePath());
521
522
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
531 assertEquals(2, configurationHelper.createCaches().size());
532
533
534
535
536
537
538
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
550
551
552
553
554
555
556
557
558
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
567
568
569
570
571
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
583
584
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
596
597
598
599
600
601
602
603
604
605
606
607
608 public void testLoadConfigurationFromURL() throws Exception {
609 URL url = getClass().getResource("/ehcache.xml");
610 testDefaultConfiguration(url);
611 }
612
613
614
615
616
617
618
619 public void testLoadConfigurationFromJarURL() throws Exception {
620
621
622 File tempJar = createTempConfigJar();
623
624
625 URL tempUrl = tempJar.toURI().toURL();
626
627
628 String entry = "jar:" + tempUrl + "!/ehcache.xml";
629
630
631 URL entryUrl = new URI(entry).toURL();
632
633 testDefaultConfiguration(entryUrl);
634 }
635
636
637
638
639
640
641
642 private void testDefaultConfiguration(URL url) {
643 Configuration configuration = ConfigurationFactory.parseConfiguration(url);
644 ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
645
646
647 assertEquals(System.getProperty("java.io.tmpdir"), configurationHelper.getDiskStorePath());
648
649
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
658 assertEquals(12, configurationHelper.createCaches().size());
659
660
661
662
663
664
665
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
677
678
679
680
681 private File createTempConfigJar() throws IOException, FileNotFoundException {
682 File tempJar = File.createTempFile("config_", ".jar");
683 tempJar.deleteOnExit();
684
685
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
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
720 }
721 }
722
723 return tempJar;
724 }
725
726
727
728
729
730
731
732
733
734
735
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
749 assertEquals(System.getProperty("java.io.tmpdir"), configurationHelper.getDiskStorePath());
750
751
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
760 assertEquals(6, configurationHelper.createCaches().size());
761
762
763
764
765
766
767
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
779
780
781
782
783
784
785
786
787
788
789
790
791
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
802 assertEquals(System.getProperty("java.io.tmpdir"), configurationHelper.getDiskStorePath());
803
804
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
813 assertEquals(0, configurationHelper.createCaches().size());
814 } finally {
815
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
824
825
826 public void testCreateEmptyConfiguration() {
827 Configuration configuration = new Configuration();
828 configuration.setSource("programmatic");
829 }
830
831
832
833
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 }