View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
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.Ehcache;
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.RMIBootstrapCacheLoader;
31  import net.sf.ehcache.distribution.RMICacheManagerPeerListener;
32  import net.sf.ehcache.distribution.RMICacheReplicatorFactory;
33  import net.sf.ehcache.event.CacheEventListener;
34  import net.sf.ehcache.event.CacheManagerEventListener;
35  import net.sf.ehcache.event.CountingCacheEventListener;
36  import net.sf.ehcache.event.CountingCacheManagerEventListener;
37  import net.sf.ehcache.event.NotificationScope;
38  import net.sf.ehcache.exceptionhandler.CacheExceptionHandler;
39  import net.sf.ehcache.exceptionhandler.CountingExceptionHandler;
40  import net.sf.ehcache.store.compound.SerializationCopyStrategy;
41  import net.sf.ehcache.writer.TestCacheWriter;
42  import org.junit.Before;
43  import org.junit.Test;
44  import org.slf4j.Logger;
45  import org.slf4j.LoggerFactory;
46  
47  import java.io.BufferedInputStream;
48  import java.io.BufferedOutputStream;
49  import java.io.File;
50  import java.io.FileInputStream;
51  import java.io.FileNotFoundException;
52  import java.io.FileOutputStream;
53  import java.io.IOException;
54  import java.io.InputStream;
55  import java.net.URI;
56  import java.net.URL;
57  import java.util.Iterator;
58  import java.util.List;
59  import java.util.Map;
60  import java.util.Properties;
61  import java.util.Set;
62  import java.util.jar.JarEntry;
63  import java.util.jar.JarOutputStream;
64  import java.util.regex.Matcher;
65  
66  import static org.junit.Assert.assertEquals;
67  import static org.junit.Assert.assertFalse;
68  import static org.junit.Assert.assertNotNull;
69  import static org.junit.Assert.assertNotSame;
70  import static org.junit.Assert.assertTrue;
71  import static org.junit.Assert.fail;
72  
73  /***
74   * Tests for Store Configuration
75   * <p/>
76   * Make sure ant compile has been executed before running these tests, as they rely on the test ehcache.xml being
77   * in the classpath.
78   *
79   * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
80   * @version $Id: ConfigurationFactoryTest.java 2530 2010-06-28 11:30:26Z gbevin $
81   */
82  public class ConfigurationFactoryTest extends AbstractCacheTest {
83      private static final int CACHES_IN_TEST_EHCACHE = 14;
84  
85      private static final Logger LOG = LoggerFactory.getLogger(ConfigurationFactoryTest.class.getName());
86  
87  
88      /***
89       * setup test
90       */
91      @Override
92      @Before
93      public void setUp() throws Exception {
94          super.setUp();
95          manager.removalAll();
96      }
97  
98      /***
99       * Tests that the loader successfully loads from ehcache.xml.
100      * ehcache.xml should be found in the classpath. In our ant configuration
101      * this should be from build/test-classes/ehcache.xml
102      * <p/>
103      * <defaultCache
104      * maxElementsInMemory="10000"
105      * eternal="false"
106      * timeToIdleSeconds="3600"
107      * timeToLiveSeconds="10"
108      * overflowToDisk="true"
109      * />
110      */
111     @Test
112     public void testLoadConfigurationFromClasspath() throws Exception {
113 
114         Configuration configuration = ConfigurationFactory.parseConfiguration();
115         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
116 
117         //Check core attributes
118         assertEquals(null, configurationHelper.getConfigurationBean().getName());
119         assertEquals(true, configurationHelper.getConfigurationBean().getUpdateCheck());
120         assertEquals(Configuration.Monitoring.AUTODETECT, configurationHelper.getConfigurationBean().getMonitoring());
121 
122         //Check disk store
123         assertEquals(System.getProperty("java.io.tmpdir"), configurationHelper.getDiskStorePath());
124 
125 
126         //Check CacheManagerPeerProvider
127         Map<String, CacheManagerPeerProvider> peerProviders = configurationHelper.createCachePeerProviders();
128         CacheManagerPeerProvider peerProvider = peerProviders.get("RMI");
129 
130 
131         //Check TTL
132         assertTrue(peerProvider instanceof MulticastRMICacheManagerPeerProvider);
133         assertEquals(Integer.valueOf(0), ((MulticastRMICacheManagerPeerProvider) peerProvider).getHeartBeatSender().getTimeToLive());
134 
135         //Check CacheManagerEventListener
136         assertEquals(null, configurationHelper.createCacheManagerEventListener());
137 
138         //Check default cache
139         Ehcache defaultCache = configurationHelper.createDefaultCache();
140         assertEquals("default", defaultCache.getName());
141         assertEquals(false, defaultCache.getCacheConfiguration().isEternal());
142         assertEquals(5, defaultCache.getCacheConfiguration().getTimeToIdleSeconds());
143         assertEquals(10, defaultCache.getCacheConfiguration().getTimeToLiveSeconds());
144         assertEquals(true, defaultCache.getCacheConfiguration().isOverflowToDisk());
145 
146         //Check caches
147         assertEquals(CACHES_IN_TEST_EHCACHE, configurationHelper.createCaches().size());
148 
149         /*
150         <cache name="sampleCache1"
151         maxElementsInMemory="10000"
152         eternal="false"
153         timeToIdleSeconds="360"
154         timeToLiveSeconds="1000"
155         overflowToDisk="true"
156         />
157         */
158         Ehcache sampleCache1 = configurationHelper.createCacheFromName("sampleCache1");
159         assertEquals("sampleCache1", sampleCache1.getName());
160         assertEquals(false, sampleCache1.getCacheConfiguration().isEternal());
161         assertEquals(360, sampleCache1.getCacheConfiguration().getTimeToIdleSeconds());
162         assertEquals(1000, sampleCache1.getCacheConfiguration().getTimeToLiveSeconds());
163         assertEquals(true, sampleCache1.getCacheConfiguration().isOverflowToDisk());
164         assertEquals(1000, sampleCache1.getCacheConfiguration().getMaxElementsOnDisk());
165 
166         /*** A cache which overflows to disk. The disk store is persistent
167          between cache and VM restarts. The disk expiry thread interval is set to 10 minutes, overriding
168          the default of 2 minutes.
169          <cache name="persistentLongExpiryIntervalCache"
170          maxElementsInMemory="500"
171          eternal="false"
172          timeToIdleSeconds="300"
173          timeToLiveSeconds="600"
174          overflowToDisk="true"
175          diskPersistent="true"
176          diskExpiryThreadIntervalSeconds="600"
177          /> */
178         Ehcache persistentLongExpiryIntervalCache = configurationHelper.createCacheFromName("persistentLongExpiryIntervalCache");
179         assertEquals("persistentLongExpiryIntervalCache", persistentLongExpiryIntervalCache.getName());
180         assertEquals(false, persistentLongExpiryIntervalCache.getCacheConfiguration().isEternal());
181         assertEquals(300, persistentLongExpiryIntervalCache.getCacheConfiguration().getTimeToIdleSeconds());
182         assertEquals(600, persistentLongExpiryIntervalCache.getCacheConfiguration().getTimeToLiveSeconds());
183         assertEquals(true, persistentLongExpiryIntervalCache.getCacheConfiguration().isOverflowToDisk());
184         assertEquals(true, persistentLongExpiryIntervalCache.getCacheConfiguration().isDiskPersistent());
185         assertEquals(600, persistentLongExpiryIntervalCache.getCacheConfiguration().getDiskExpiryThreadIntervalSeconds());
186 
187         /*
188            <!--
189             A cache which has a CacheExtension
190             -->
191             <cache name="testCacheExtensionCache"
192                    maxElementsInMemory="10"
193                    eternal="false"
194                    timeToIdleSeconds="100"
195                    timeToLiveSeconds="100"
196                    overflowToDisk="false">
197                 <cacheExtensionFactory
198                         class="net.sf.ehcache.extension.TestCacheExtensionFactory"
199                         properties="propertyA=valueA"/>
200             </cache>CacheExtension cache
201         */
202         Ehcache exceptionHandlingCache = configurationHelper.createCacheFromName("exceptionHandlingCache");
203         assertEquals("exceptionHandlingCache", exceptionHandlingCache.getName());
204         assertTrue(exceptionHandlingCache.getCacheExceptionHandler() != null);
205         assertTrue(exceptionHandlingCache.getCacheExceptionHandler() instanceof CountingExceptionHandler);
206         assertTrue(exceptionHandlingCache.getCacheExceptionHandler() instanceof CacheExceptionHandler);
207     }
208 
209 
210     /***
211      * Tests that the loader successfully loads from ehcache.xml
212      * given as a {@link File}
213      * <p/>
214      * <defaultCache
215      * maxElementsInMemory="10000"
216      * eternal="false"
217      * timeToIdleSeconds="120"
218      * timeToLiveSeconds="120"
219      * overflowToDisk="true"
220      * />
221      */
222     @Test
223     public void testLoadConfigurationFromFile() throws Exception {
224 
225         File file = new File(SRC_CONFIG_DIR + "ehcache.xml");
226         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
227         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
228 
229         assertEquals(null, configurationHelper.getConfigurationBean().getName());
230         assertEquals(true, configurationHelper.getConfigurationBean().getUpdateCheck());
231         assertEquals(Configuration.Monitoring.AUTODETECT, configurationHelper.getConfigurationBean().getMonitoring());
232 
233         //Check disk store  <diskStore path="/tmp"/>
234         assertEquals(System.getProperty("java.io.tmpdir"), configurationHelper.getDiskStorePath());
235 
236         //Check CacheManagerPeerProvider
237         Map<String, CacheManagerPeerProvider> peerProviders = configurationHelper.createCachePeerProviders();
238         CacheManagerPeerProvider peerProvider = peerProviders.get("RMI");
239 
240 
241         //Check TTL
242         assertTrue(peerProvider instanceof MulticastRMICacheManagerPeerProvider);
243         assertEquals(Integer.valueOf(1), ((MulticastRMICacheManagerPeerProvider) peerProvider).getHeartBeatSender().getTimeToLive());
244 
245 
246         //Check default cache
247         Ehcache defaultCache = configurationHelper.createDefaultCache();
248         assertEquals("default", defaultCache.getName());
249         assertEquals(false, defaultCache.getCacheConfiguration().isEternal());
250         assertEquals(120, defaultCache.getCacheConfiguration().getTimeToIdleSeconds());
251         assertEquals(120, defaultCache.getCacheConfiguration().getTimeToLiveSeconds());
252         assertEquals(true, defaultCache.getCacheConfiguration().isOverflowToDisk());
253         assertEquals(10000, defaultCache.getCacheConfiguration().getMaxElementsInMemory());
254         assertEquals(10000000, defaultCache.getCacheConfiguration().getMaxElementsOnDisk());
255 
256         //Check caches
257         assertEquals(6, configurationHelper.createCaches().size());
258 
259         //check config
260         CacheConfiguration sampleCache1Config = configuration.getCacheConfigurations().get("sampleCache1");
261         assertEquals("sampleCache1", sampleCache1Config.getName());
262         assertEquals(false, sampleCache1Config.isEternal());
263         assertEquals(300, sampleCache1Config.getTimeToIdleSeconds());
264         assertEquals(600, sampleCache1Config.getTimeToLiveSeconds());
265         assertEquals(true, sampleCache1Config.isOverflowToDisk());
266         assertEquals(20, sampleCache1Config.getDiskSpoolBufferSizeMB());
267 
268         //  <cache name="sampleCache1"
269         //  maxElementsInMemory="10000"
270         //  eternal="false"
271         //  timeToIdleSeconds="300"
272         //  timeToLiveSeconds="600"
273         //  overflowToDisk="true"
274         //  />
275         //Check created cache
276         Ehcache sampleCache1 = configurationHelper.createCacheFromName("sampleCache1");
277         assertEquals("sampleCache1", sampleCache1.getName());
278         assertEquals(false, sampleCache1.getCacheConfiguration().isEternal());
279         assertEquals(300, sampleCache1.getCacheConfiguration().getTimeToIdleSeconds());
280         assertEquals(1000, sampleCache1.getCacheConfiguration().getMaxElementsOnDisk());
281         assertEquals(600, sampleCache1.getCacheConfiguration().getTimeToLiveSeconds());
282         assertEquals(true, sampleCache1.getCacheConfiguration().isOverflowToDisk());
283     }
284 
285     /***
286      * Can we read from a UTF8 encoded file which uses Japanese characters
287      */
288     @Test
289     public void testLoadUTF8ConfigurationFromFile() throws Exception {
290 
291         File file = new File(TEST_CONFIG_DIR + "ehcacheUTF8.xml");
292         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
293         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
294     }
295 
296 
297     /***
298      * Tests that the loader successfully loads from ehcache-1.1.xml
299      * given as a {@link File}. This is a backward compatibility test.
300      * <p/>
301      * <defaultCache
302      * maxElementsInMemory="10000"
303      * eternal="false"
304      * timeToIdleSeconds="120"
305      * timeToLiveSeconds="120"
306      * overflowToDisk="true"
307      * />
308      */
309     @Test
310     public void testLoadConfigurationFromEhcache11File() throws Exception {
311 
312         File file = new File(TEST_CONFIG_DIR + "ehcache-1_1.xml");
313         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
314         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
315 
316         assertEquals(null, configurationHelper.getConfigurationBean().getName());
317         assertEquals(true, configurationHelper.getConfigurationBean().getUpdateCheck());
318         assertEquals(Configuration.Monitoring.AUTODETECT, configurationHelper.getConfigurationBean().getMonitoring());
319 
320         //Check disk path  <diskStore path="/tmp"/>
321         assertEquals(System.getProperty("java.io.tmpdir"), configurationHelper.getDiskStorePath());
322 
323         //Check default cache
324         Ehcache defaultCache = configurationHelper.createDefaultCache();
325         assertEquals("default", defaultCache.getName());
326         assertEquals(false, defaultCache.getCacheConfiguration().isEternal());
327         assertEquals(5, defaultCache.getCacheConfiguration().getTimeToIdleSeconds());
328         assertEquals(10, defaultCache.getCacheConfiguration().getTimeToLiveSeconds());
329         assertEquals(true, defaultCache.getCacheConfiguration().isOverflowToDisk());
330         assertEquals(10, defaultCache.getCacheConfiguration().getMaxElementsInMemory());
331         assertEquals(0, defaultCache.getCacheConfiguration().getMaxElementsOnDisk());
332 
333         //Check caches
334         assertEquals(8, configurationHelper.createCaches().size());
335 
336         //  <cache name="sampleCache1"
337         //  maxElementsInMemory="10000"
338         //  eternal="false"
339         //  timeToIdleSeconds="300"
340         //  timeToLiveSeconds="600"
341         //  overflowToDisk="true"
342         //  />
343         Ehcache sampleCache1 = configurationHelper.createCacheFromName("sampleCache1");
344         assertEquals("sampleCache1", sampleCache1.getName());
345         assertEquals(false, sampleCache1.getCacheConfiguration().isEternal());
346         assertEquals(360, sampleCache1.getCacheConfiguration().getTimeToIdleSeconds());
347         assertEquals(1000, sampleCache1.getCacheConfiguration().getTimeToLiveSeconds());
348         assertEquals(true, sampleCache1.getCacheConfiguration().isOverflowToDisk());
349     }
350 
351     /***
352      * Tests that the CacheManagerEventListener is null when
353      * no CacheManagerEventListener class is specified.
354      */
355     @Test
356     public void testLoadConfigurationFromFileNoCacheManagerListenerDefault() throws Exception {
357         File file = new File(TEST_CONFIG_DIR + "ehcache-nolisteners.xml");
358         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
359         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
360 
361         //Check CacheManagerEventListener
362         CacheManagerEventListener listener = configurationHelper.createCacheManagerEventListener();
363         assertEquals(null, listener);
364 
365         //Check caches. Configuration should have completed
366         assertEquals(10, configurationHelper.createCaches().size());
367     }
368 
369     /***
370      * Tests that the CacheManagerEventListener class is set as the CacheManagerEventListener
371      * when the class is unloadable.
372      */
373     @Test
374     public void testLoadConfigurationFromFileUnloadableCacheManagerListenerDefault() throws Exception {
375         File file = new File(TEST_CONFIG_DIR + "ehcache-unloadablecachemanagerlistenerclass.xml");
376         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
377         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
378 
379         //Check CacheManagerEventListener
380         CacheManagerEventListener listener = null;
381         try {
382             listener = configurationHelper.createCacheManagerEventListener();
383             fail();
384         } catch (CacheException e) {
385             //expected
386         }
387     }
388 
389     /***
390      * Positive and negative Tests for setting a list of CacheEventListeners in the configuration
391      */
392     @Test
393     public void testLoadConfigurationFromFileCountingCacheListener() throws Exception {
394         File file = new File(TEST_CONFIG_DIR + "ehcache-countinglisteners.xml");
395         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
396         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
397 
398         //Check CacheManagerEventListener
399         Class actualClass = configurationHelper.createCacheManagerEventListener().getClass();
400         assertEquals(CountingCacheManagerEventListener.class, actualClass);
401 
402         //Check caches. Configuration should have completed
403         assertEquals(10, configurationHelper.createCaches().size());
404 
405         //Should have null and counting
406         Ehcache sampleCache1 = configurationHelper.createCacheFromName("sampleCache1");
407         Set registeredListeners = sampleCache1.getCacheEventNotificationService().getCacheEventListeners();
408         assertEquals(2, registeredListeners.size());
409 
410         //Should have null and counting
411         Ehcache sampleCache2 = configurationHelper.createCacheFromName("sampleCache2");
412         registeredListeners = sampleCache2.getCacheEventNotificationService().getCacheEventListeners();
413         assertEquals(1, registeredListeners.size());
414 
415         //Should have null and counting
416         Ehcache sampleCache3 = configurationHelper.createCacheFromName("sampleCache3");
417         registeredListeners = sampleCache3.getCacheEventNotificationService().getCacheEventListeners();
418         assertEquals(1, registeredListeners.size());
419 
420         //Should have none. None set.
421         Ehcache footerPageCache = configurationHelper.createCacheFromName("FooterPageCache");
422         registeredListeners = footerPageCache.getCacheEventNotificationService().getCacheEventListeners();
423         assertEquals(0, registeredListeners.size());
424 
425         //Should have one. null listener set.
426         Ehcache persistentLongExpiryIntervalCache = configurationHelper.createCacheFromName("persistentLongExpiryIntervalCache");
427         registeredListeners = persistentLongExpiryIntervalCache.getCacheEventNotificationService()
428                 .getCacheEventListeners();
429         assertEquals(1, registeredListeners.size());
430     }
431 
432     /***
433      * Tests for Distributed Cache config
434      */
435     @Test
436     public void testLoadConfigurationFromFileDistribution() throws Exception {
437         File file = new File(TEST_CONFIG_DIR + "distribution/ehcache-distributed1.xml");
438         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
439         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
440 
441         //Check CacheManagerPeerProvider
442         Map<String, CacheManagerPeerProvider> peerProviders = configurationHelper.createCachePeerProviders();
443         CacheManagerPeerProvider peerProvider = peerProviders.get("RMI");
444 
445 
446         //Check TTL
447         assertTrue(peerProvider instanceof MulticastRMICacheManagerPeerProvider);
448         assertEquals(Integer.valueOf(0), ((MulticastRMICacheManagerPeerProvider) peerProvider).getHeartBeatSender().getTimeToLive());
449 
450 
451         //check CacheManagerPeerListener
452         Map<String, CacheManagerPeerListener> peerListeners = configurationHelper.createCachePeerListeners();
453 
454         //should be one in this config
455         for (CacheManagerPeerListener peerListener : peerListeners.values()) {
456             assertTrue(peerListener instanceof RMICacheManagerPeerListener);
457         }
458 
459         //Check caches. Configuration should have completed
460         assertEquals(61, configurationHelper.createCaches().size());
461 
462         Ehcache sampleCache1 = configurationHelper.createCacheFromName("sampleCache1");
463         Set listeners = sampleCache1.getCacheEventNotificationService().getCacheEventListeners();
464         assertEquals(2, listeners.size());
465         for (Iterator iterator = listeners.iterator(); iterator.hasNext();) {
466             CacheEventListener cacheEventListener = (CacheEventListener) iterator.next();
467             assertTrue(cacheEventListener instanceof RMIAsynchronousCacheReplicator || cacheEventListener
468                     instanceof CountingCacheEventListener);
469         }
470 
471         BootstrapCacheLoader bootstrapCacheLoader = sampleCache1.getBootstrapCacheLoader();
472         assertNotNull(bootstrapCacheLoader);
473         assertEquals(RMIBootstrapCacheLoader.class, bootstrapCacheLoader.getClass());
474         assertEquals(true, bootstrapCacheLoader.isAsynchronous());
475         assertEquals(5000000, ((RMIBootstrapCacheLoader) bootstrapCacheLoader).getMaximumChunkSizeBytes());
476 
477     }
478 
479     /***
480      * The following should give defaults of true and 5000000
481      * <bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />
482      */
483     @Test
484     public void testLoadConfigurationFromFileNoBootstrapPropertiesSet() throws Exception {
485         File file = new File(TEST_CONFIG_DIR + "distribution/ehcache-distributed1.xml");
486         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
487         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
488         Ehcache sampleCache3 = configurationHelper.createCacheFromName("sampleCache3");
489 
490         BootstrapCacheLoader bootstrapCacheLoader = ((Cache) sampleCache3).getBootstrapCacheLoader();
491         assertEquals(true, bootstrapCacheLoader.isAsynchronous());
492         assertEquals(5000000, ((RMIBootstrapCacheLoader) bootstrapCacheLoader).getMaximumChunkSizeBytes());
493     }
494 
495     /***
496      * The following should give defaults of true and 5000000
497      * <bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"
498      * properties="bootstrapAsynchronously=false, maximumChunkSizeBytes=10000"/>
499      */
500     @Test
501     public void testLoadConfigurationFromFileWithSpecificPropertiesSet() throws Exception {
502         File file = new File(TEST_CONFIG_DIR + "distribution/ehcache-distributed1.xml");
503         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
504         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
505         Ehcache sampleCache4 = configurationHelper.createCacheFromName("sampleCache4");
506 
507         BootstrapCacheLoader bootstrapCacheLoader = ((Cache) sampleCache4).getBootstrapCacheLoader();
508         assertEquals(false, bootstrapCacheLoader.isAsynchronous());
509         assertEquals(10000, ((RMIBootstrapCacheLoader) bootstrapCacheLoader).getMaximumChunkSizeBytes());
510     }
511 
512     /***
513      * Tests that the loader successfully loads from ehcache-nodefault.xml
514      * given as a {@link File}
515      * <p/>
516      * <defaultCache
517      * maxElementsInMemory="10000"
518      * eternal="false"
519      * timeToIdleSeconds="120"
520      * timeToLiveSeconds="120"
521      * overflowToDisk="true"
522      * />
523      */
524     @Test
525     public void testLoadConfigurationFromFileNoDefault() throws Exception {
526         File file = new File(TEST_CONFIG_DIR + "ehcache-nodefault.xml");
527         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
528         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
529 
530         assertEquals(null, configurationHelper.getConfigurationBean().getName());
531         assertEquals(true, configurationHelper.getConfigurationBean().getUpdateCheck());
532         assertEquals(Configuration.Monitoring.AUTODETECT, configurationHelper.getConfigurationBean().getMonitoring());
533 
534         //Check disk path  <diskStore path="/tmp"/>
535         assertEquals(System.getProperty("java.io.tmpdir"), configurationHelper.getDiskStorePath());
536 
537         //Check default cache
538         try {
539             configurationHelper.createDefaultCache();
540             fail();
541         } catch (CacheException e) {
542             //noop
543         }
544 
545         //Check caches
546         assertEquals(4, configurationHelper.createCaches().size());
547 
548         //  <cache name="sampleCache1"
549         //  maxElementsInMemory="10000"
550         //  eternal="false"
551         //  timeToIdleSeconds="300"
552         //  timeToLiveSeconds="600"
553         //  overflowToDisk="true"
554         //  />
555         Ehcache sampleCache1 = configurationHelper.createCacheFromName("sampleCache1");
556         Ehcache sampleCache4 = configurationHelper.createCacheFromName("sampleCache4");
557         assertEquals("net.sf.ehcache.transaction.manager.DefaultTransactionManagerLookup", configuration.getTransactionManagerLookupConfiguration().getFullyQualifiedClassPath());
558         assertEquals("sampleCache1", sampleCache1.getName());
559         assertEquals(false, sampleCache1.getCacheConfiguration().isEternal());
560         assertEquals(300, sampleCache1.getCacheConfiguration().getTimeToIdleSeconds());
561         assertEquals(600, sampleCache1.getCacheConfiguration().getTimeToLiveSeconds());
562         assertEquals(true, sampleCache1.getCacheConfiguration().isOverflowToDisk());
563         assertEquals(CacheConfiguration.TransactionalMode.OFF, sampleCache1.getCacheConfiguration().getTransactionalMode());
564         assertEquals(false, sampleCache1.getCacheConfiguration().isTransactional());
565         assertEquals("sampleCache4", sampleCache4.getName());
566         assertEquals(CacheConfiguration.TransactionalMode.XA, sampleCache4.getCacheConfiguration().getTransactionalMode());
567         assertEquals(true, sampleCache4.getCacheConfiguration().isTransactional());
568     }
569 
570     /***
571      * Tests that the loader successfully loads from ehcache-nodefault.xml
572      * given as a {@link File}
573      * <p/>
574      * /**
575      * Tests that the loader successfully loads from ehcache-nodefault.xml
576      * given as a {@link File}
577      * <p/>
578      * <cache name="sampleCacheNoOptionalAttributes"
579      * maxElementsInMemory="1000"
580      * eternal="true"
581      * overflowToDisk="false"
582      * />
583      */
584     @Test
585     public void testDefaultValues() throws Exception {
586         File file = new File(TEST_CONFIG_DIR + "ehcache-nodefault.xml");
587         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
588         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
589 
590         Ehcache sampleCacheNoOptionalAttributes = configurationHelper.createCacheFromName("sampleCacheNoOptionalAttributes");
591         assertEquals("sampleCacheNoOptionalAttributes", sampleCacheNoOptionalAttributes.getName());
592         assertEquals(1000, sampleCacheNoOptionalAttributes.getCacheConfiguration().getMaxElementsInMemory());
593         assertEquals(true, sampleCacheNoOptionalAttributes.getCacheConfiguration().isEternal());
594         assertEquals(false, sampleCacheNoOptionalAttributes.getCacheConfiguration().isOverflowToDisk());
595         assertEquals(0, sampleCacheNoOptionalAttributes.getCacheConfiguration().getTimeToIdleSeconds());
596         assertEquals(0, sampleCacheNoOptionalAttributes.getCacheConfiguration().getTimeToLiveSeconds());
597         assertEquals(false, sampleCacheNoOptionalAttributes.getCacheConfiguration().isDiskPersistent());
598         assertEquals(120, sampleCacheNoOptionalAttributes.getCacheConfiguration().getDiskExpiryThreadIntervalSeconds());
599         assertEquals(1, sampleCacheNoOptionalAttributes.getCacheConfiguration().getDiskAccessStripes());
600     }
601 
602 
603     /***
604      * Tests that the loader successfully loads from ehcache-nodisk.xml
605      * given as a {@link File}
606      * <p/>
607      * <defaultCache
608      * maxElementsInMemory="10000"
609      * eternal="false"
610      * timeToIdleSeconds="120"
611      * timeToLiveSeconds="120"
612      * overflowToDisk="false"
613      * <p/>
614      */
615     @Test
616     public void testLoadConfigurationFromFileNoDisk() throws Exception {
617         File file = new File(TEST_CONFIG_DIR + "ehcache-nodisk.xml");
618         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
619         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
620 
621         assertEquals(null, configurationHelper.getConfigurationBean().getName());
622         assertEquals(true, configurationHelper.getConfigurationBean().getUpdateCheck());
623         assertEquals(Configuration.Monitoring.AUTODETECT, configurationHelper.getConfigurationBean().getMonitoring());
624 
625         //Check disk path  <diskStore path="/tmp"/>
626         assertEquals(null, configurationHelper.getDiskStorePath());
627 
628         //Check default cache
629         Ehcache defaultCache = configurationHelper.createDefaultCache();
630         assertEquals("default", defaultCache.getName());
631         assertEquals(false, defaultCache.getCacheConfiguration().isEternal());
632         assertEquals(5L, defaultCache.getCacheConfiguration().getTimeToIdleSeconds());
633         assertEquals(10, defaultCache.getCacheConfiguration().getTimeToLiveSeconds());
634         assertEquals(false, defaultCache.getCacheConfiguration().isOverflowToDisk());
635 
636         //Check caches
637         assertEquals(2, configurationHelper.createCaches().size());
638 
639         //  <cache name="sampleCache1"
640         //  maxElementsInMemory="10000"
641         //  eternal="false"
642         //  timeToIdleSeconds="300"
643         //  timeToLiveSeconds="600"
644         //  overflowToDisk="true"
645         //  />
646         Ehcache sampleCache1 = configurationHelper.createCacheFromName("sampleCache1");
647         assertEquals("sampleCache1", sampleCache1.getName());
648         assertEquals(false, sampleCache1.getCacheConfiguration().isEternal());
649         assertEquals(360, sampleCache1.getCacheConfiguration().getTimeToIdleSeconds());
650         assertEquals(1000, sampleCache1.getCacheConfiguration().getTimeToLiveSeconds());
651         assertEquals(false, sampleCache1.getCacheConfiguration().isOverflowToDisk());
652     }
653 
654     /***
655      * Tests the default values for optional attributes
656      * <p/>
657      * <!-- Sample cache. Optional attributes are removed -->
658      * <cache name="sampleRequiredAttributesOnly"
659      * maxElementsInMemory="1000"
660      * eternal="true"
661      * overflowToDisk="false"
662      * />
663      * <p/>
664      * No disk store path specified as disk store not being used
665      * />
666      */
667     @Test
668     public void testOptionalAttributeDefaultValues() throws Exception {
669         File file = new File(TEST_CONFIG_DIR + "ehcache-nodisk.xml");
670         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
671         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
672 
673         assertEquals(null, configurationHelper.getDiskStorePath());
674 
675 
676         //  <cache name="sampleCache1"
677         //  maxElementsInMemory="10000"
678         //  eternal="false"
679         //  timeToIdleSeconds="300"
680         //  timeToLiveSeconds="600"
681         //  overflowToDisk="true"
682         //  />
683         Ehcache sampleCache1 = configurationHelper.createCacheFromName("sampleCache1");
684         assertEquals("sampleCache1", sampleCache1.getName());
685         assertEquals(false, sampleCache1.getCacheConfiguration().isEternal());
686         assertEquals(360, sampleCache1.getCacheConfiguration().getTimeToIdleSeconds());
687         assertEquals(1000, sampleCache1.getCacheConfiguration().getTimeToLiveSeconds());
688         assertEquals(false, sampleCache1.getCacheConfiguration().isOverflowToDisk());
689     }
690 
691     /***
692      * Regression test for bug 1432074 - NullPointer on RMICacheManagerPeerProviderFactory
693      * If manual peer provider configuration is selected then an info message should be
694      * logged if there is no list.
695      */
696     @Test
697     public void testEmptyPeerListManualDistributedConfiguration() {
698         CacheManager cacheManager = new CacheManager(TEST_CONFIG_DIR + "distribution/ehcache-manual-distributed3.xml");
699         assertEquals(0, cacheManager.getCacheManagerPeerProvider("RMI")
700                 .listRemoteCachePeers(cacheManager.getCache("sampleCache1")).size());
701 
702     }
703 
704 
705     /***
706      * Tests that the loader successfully loads from ehcache.xml
707      * given as an {@link URL}.
708      * <p/>
709      * is found first
710      * <p/>
711      * <defaultCache
712      * maxElementsInMemory="10"
713      * eternal="false"
714      * timeToIdleSeconds="5"
715      * timeToLiveSeconds="10"
716      * overflowToDisk="true"
717      * />
718      */
719     @Test
720     public void testLoadConfigurationFromURL() throws Exception {
721         URL url = getClass().getResource("/ehcache.xml");
722         testDefaultConfiguration(url);
723     }
724 
725     /***
726      * Exposes a bug where the default configuration could not be loaded from a Jar URL
727      * (a common scenario when ehcache is deployed, and always used for failsafe config).
728      *
729      * @throws Exception When the test fails.
730      */
731     @Test
732     public void testLoadConfigurationFromJarURL() throws Exception {
733 
734         // first, create the jar
735         File tempJar = createTempConfigJar();
736 
737         // convert it to a URL
738         URL tempUrl = tempJar.toURI().toURL();
739 
740         // create a jar url that points to the configuration file
741         String entry = "jar:" + tempUrl + "!/ehcache.xml";
742 
743         // create a URL object from the string, going through the URI class so it's encoded
744         URL entryUrl = new URI(entry).toURL();
745 
746         testDefaultConfiguration(entryUrl);
747     }
748 
749     /***
750      * Given a URL, parse the configuration and test that the config read corresponds
751      * to that which exists in the ehcache.xml file.
752      *
753      * @param url The URL to load.
754      */
755     private void testDefaultConfiguration(URL url) {
756         Configuration configuration = ConfigurationFactory.parseConfiguration(url);
757         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
758 
759         //Check disk path missing in test ehcache.xml"/>
760         assertEquals(System.getProperty("java.io.tmpdir"), configurationHelper.getDiskStorePath());
761 
762         //Check default cache
763         Ehcache defaultCache = configurationHelper.createDefaultCache();
764         assertEquals("default", defaultCache.getName());
765         assertEquals(false, defaultCache.getCacheConfiguration().isEternal());
766         assertEquals(5L, defaultCache.getCacheConfiguration().getTimeToIdleSeconds());
767         assertEquals(10, defaultCache.getCacheConfiguration().getTimeToLiveSeconds());
768         assertEquals(true, defaultCache.getCacheConfiguration().isOverflowToDisk());
769 
770         //Check caches
771         assertEquals(CACHES_IN_TEST_EHCACHE, configurationHelper.createCaches().size());
772 
773         //  <cache name="sampleCache1"
774         //  maxElementsInMemory="10000"
775         //  eternal="false"
776         //  timeToIdleSeconds="300"
777         //  timeToLiveSeconds="600"
778         //  overflowToDisk="true"
779         //  />
780         Ehcache sampleCache1 = configurationHelper.createCacheFromName("sampleCache1");
781         assertEquals("sampleCache1", sampleCache1.getName());
782         assertEquals(false, sampleCache1.getCacheConfiguration().isEternal());
783         assertEquals(360, sampleCache1.getCacheConfiguration().getTimeToIdleSeconds());
784         assertEquals(1000, sampleCache1.getCacheConfiguration().getTimeToLiveSeconds());
785         assertEquals(true, sampleCache1.getCacheConfiguration().isOverflowToDisk());
786     }
787 
788     /***
789      * Creates a jar file that contains only ehcache.xml (a supplied configuration file).
790      *
791      * @return The jar file created with the configuration file as its only entry.
792      * @throws IOException If the jar could not be created.
793      */
794     private File createTempConfigJar() throws IOException, FileNotFoundException {
795         File tempJar = File.createTempFile("config_", ".jar");
796         tempJar.deleteOnExit();
797 
798         // write the default config to the jar
799         JarOutputStream jos = null;
800         try {
801             jos = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(tempJar)));
802 
803             jos.putNextEntry(new JarEntry("ehcache.xml"));
804 
805             InputStream defaultCfg = null;
806             try {
807                 defaultCfg = new BufferedInputStream(getClass().getResource("/ehcache.xml").openStream());
808                 byte[] buf = new byte[1024];
809                 int read = 0;
810                 while ((read = defaultCfg.read(buf)) > 0) {
811                     jos.write(buf, 0, read);
812                 }
813             } finally {
814                 try {
815                     if (defaultCfg != null) {
816                         defaultCfg.close();
817                     }
818                 } catch (IOException ioEx) {
819                     // swallow this exception
820                 }
821             }
822 
823         } finally {
824             try {
825                 if (jos != null) {
826                     jos.closeEntry();
827 
828                     jos.flush();
829                     jos.close();
830                 }
831             } catch (IOException ioEx) {
832                 // swallow this exception
833             }
834         }
835 
836         return tempJar;
837     }
838 
839     /***
840      * Tests that the loader successfully loads from ehcache.xml
841      * given as a {@link InputStream}
842      * <p/>
843      * <defaultCache
844      * maxElementsInMemory="10000"
845      * eternal="false"
846      * timeToIdleSeconds="120"
847      * timeToLiveSeconds="120"
848      * overflowToDisk="true"
849      * />
850      */
851     @Test
852     public void testLoadConfigurationFromInputStream() throws Exception {
853         InputStream fis = new FileInputStream(new File(SRC_CONFIG_DIR + "ehcache.xml").getAbsolutePath());
854         ConfigurationHelper configurationHelper;
855         try {
856             Configuration configuration = ConfigurationFactory.parseConfiguration(fis);
857             configurationHelper = new ConfigurationHelper(manager, configuration);
858         } finally {
859             fis.close();
860         }
861 
862         assertEquals(null, configurationHelper.getConfigurationBean().getName());
863         assertEquals(true, configurationHelper.getConfigurationBean().getUpdateCheck());
864         assertEquals(Configuration.Monitoring.AUTODETECT, configurationHelper.getConfigurationBean().getMonitoring());
865 
866         //Check disk path  <diskStore path="/tmp"/>
867         assertEquals(System.getProperty("java.io.tmpdir"), configurationHelper.getDiskStorePath());
868 
869         //Check default cache
870         Ehcache defaultCache = configurationHelper.createDefaultCache();
871         assertEquals("default", defaultCache.getName());
872         assertEquals(false, defaultCache.getCacheConfiguration().isEternal());
873         assertEquals(120, defaultCache.getCacheConfiguration().getTimeToIdleSeconds());
874         assertEquals(120, defaultCache.getCacheConfiguration().getTimeToLiveSeconds());
875         assertEquals(true, defaultCache.getCacheConfiguration().isOverflowToDisk());
876 
877         //Check caches
878         assertEquals(6, configurationHelper.createCaches().size());
879 
880         //  <cache name="sampleCache1"
881         //  maxElementsInMemory="10000"
882         //  eternal="false"
883         //  timeToIdleSeconds="300"
884         //  timeToLiveSeconds="600"
885         //  overflowToDisk="true"
886         //  />
887         Ehcache sampleCache1 = configurationHelper.createCacheFromName("sampleCache1");
888         assertEquals("sampleCache1", sampleCache1.getName());
889         assertEquals(false, sampleCache1.getCacheConfiguration().isEternal());
890         assertEquals(300, sampleCache1.getCacheConfiguration().getTimeToIdleSeconds());
891         assertEquals(600, sampleCache1.getCacheConfiguration().getTimeToLiveSeconds());
892         assertEquals(true, sampleCache1.getCacheConfiguration().isOverflowToDisk());
893     }
894 
895     /***
896      * Tests that the loader successfully loads from ehcache-failsafe.xml
897      * found in the classpath.
898      * ehcache.xml should be found in the classpath. In our ant configuration
899      * this should be from build/classes/ehcache-failsafe.xml
900      * <p/>
901      * We delete ehcache.xml from build/test-classes/ first, as failsafe only
902      * kicks in when ehcache.xml is not in the classpath.
903      * <p/>
904      * <defaultCache
905      * maxElementsInMemory="10000"
906      * eternal="false"
907      * timeToIdleSeconds="120"
908      * timeToLiveSeconds="120"
909      * overflowToDisk="true"
910      * />
911      */
912     @Test
913     public void testLoadConfigurationFromFailsafe() throws Exception {
914         try {
915             File file = new File(AbstractCacheTest.TEST_CLASSES_DIR + "ehcache.xml");
916             file.renameTo(new File(AbstractCacheTest.TEST_CLASSES_DIR + "hideehcache.xml"));
917             Configuration configuration = ConfigurationFactory.parseConfiguration();
918             ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
919 
920             assertEquals(null, configurationHelper.getConfigurationBean().getName());
921             assertEquals(true, configurationHelper.getConfigurationBean().getUpdateCheck());
922             assertEquals(Configuration.Monitoring.AUTODETECT, configurationHelper.getConfigurationBean().getMonitoring());
923 
924             //Check disk path  <diskStore path="/tmp"/>
925             assertEquals(System.getProperty("java.io.tmpdir"), configurationHelper.getDiskStorePath());
926 
927             //Check default cache
928             Ehcache defaultCache = configurationHelper.createDefaultCache();
929             assertEquals("default", defaultCache.getName());
930             assertEquals(false, defaultCache.getCacheConfiguration().isEternal());
931             assertEquals(120, defaultCache.getCacheConfiguration().getTimeToIdleSeconds());
932             assertEquals(120, defaultCache.getCacheConfiguration().getTimeToLiveSeconds());
933             assertEquals(true, defaultCache.getCacheConfiguration().isOverflowToDisk());
934 
935             //Check caches
936             assertEquals(0, configurationHelper.createCaches().size());
937         } finally {
938             //Put ehcache.xml back
939             File hiddenFile = new File(AbstractCacheTest.TEST_CLASSES_DIR + "hideehcache.xml");
940             hiddenFile.renameTo(new File(AbstractCacheTest.TEST_CLASSES_DIR + "ehcache.xml"));
941         }
942 
943     }
944 
945     /***
946      * Make sure that the empty Configuration constructor remains public for those wishing to create CacheManagers
947      * purely programmatically.
948      */
949     @Test
950     public void testCreateEmptyConfiguration() {
951         Configuration configuration = new Configuration();
952     }
953 
954 
955     /***
956      * Tests that you cannot use the name default for a cache.
957      */
958     @Test
959     public void testLoadConfigurationFromInvalidXMLFileWithDefaultCacheNameUsed() throws Exception {
960         File file = new File(TEST_CONFIG_DIR + "ehcache-withdefaultset.xml");
961         try {
962             Configuration configuration = ConfigurationFactory.parseConfiguration(file);
963         } catch (CacheException e) {
964             assertTrue(e.getMessage().contains("The Default Cache has already been configured"));
965         }
966 
967     }
968 
969 
970     /***
971      * Tests replacement in the config file.
972      */
973     @Test
974     public void testLoadConfigurationWithReplacement() throws Exception {
975         System.setProperty("multicastGroupPort", "4446");
976         File file = new File(TEST_CONFIG_DIR + "ehcache-replacement.xml");
977         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
978         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
979 
980 
981         //Check disk path  <diskStore path="/tmp"/>
982         assertNotSame(System.getProperty("java.io.tmpdir"), configurationHelper.getDiskStorePath());
983         assertTrue(configuration.getCacheManagerPeerProviderFactoryConfiguration().get(0)
984                 .getProperties().indexOf("multicastGroupPort=4446") != -1);
985 
986 
987     }
988 
989 
990     /***
991      * Fun with replaceAll which clobbers // by default!
992      */
993     @Test
994     public void testPathExpansionAndReplacement() throws Exception {
995 
996         String configuration = "This is my ${basedir}.";
997         String trimmedToken = "basedir";
998         String property = "D://sonatype//workspace//nexus-aggregator//nexus//nexus-app";
999         LOG.info("Property: " + property);
1000         LOG.info("configuration is: " + configuration);
1001         String propertyWithQuotesProtected = Matcher.quoteReplacement(property);
1002         configuration = configuration.replaceAll("//$//{" + trimmedToken + "//}", propertyWithQuotesProtected);
1003         assertTrue(configuration.contains(property));
1004         LOG.info("configuration is: " + configuration);
1005 
1006 
1007     }
1008 
1009 
1010     /***
1011      * Tests the property token extraction logic
1012      */
1013     @Test
1014     public void testMatchPropertyTokensProperlyFormed() {
1015         String example = "<cacheManagerPeerProviderFactory class=\"net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory\"" +
1016                 "properties=\"peerDiscovery=automatic, " +
1017                 "multicastGroupAddress=${multicastAddress}, " +
1018                 "multicastGroupPort=4446, timeToLive=1\"/>";
1019         Set propertyTokens = ConfigurationFactory.extractPropertyTokens(example);
1020         assertEquals(1, propertyTokens.size());
1021         String firstPropertyToken = (String) (propertyTokens.toArray())[0];
1022         assertEquals("${multicastAddress}", firstPropertyToken);
1023     }
1024 
1025     /***
1026      * Tests the property token extraction logic
1027      */
1028     @Test
1029     public void testMatchPropertyTokensProperlyFormedTwo() {
1030         String example = "<cacheManagerPeerProviderFactory class=\"net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory\"" +
1031                 "properties=\"peerDiscovery=automatic, " +
1032                 "multicastGroupAddress=${multicastAddress}\n, " +
1033                 "multicastGroupPort=4446, timeToLive=${multicastAddress}\"/>";
1034         Set propertyTokens = ConfigurationFactory.extractPropertyTokens(example);
1035         assertEquals(1, propertyTokens.size());
1036         String firstPropertyToken = (String) (propertyTokens.toArray())[0];
1037         assertEquals("${multicastAddress}", firstPropertyToken);
1038     }
1039 
1040 
1041     /***
1042      * Tests the property token extraction logic
1043      */
1044     @Test
1045     public void testMatchPropertyTokensProperlyFormedTwoUnique() {
1046         String example = "<cacheManagerPeerProviderFactory class=\"net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory\"" +
1047                 "properties=\"peerDiscovery=automatic, " +
1048                 "multicastGroupAddress=${multicastAddress}\n, " +
1049                 "multicastGroupPort=4446, timeToLive=${multicastAddress1}\"/>";
1050         Set propertyTokens = ConfigurationFactory.extractPropertyTokens(example);
1051         assertEquals(2, propertyTokens.size());
1052     }
1053 
1054     /***
1055      * If you leave off the } then no match.
1056      */
1057     @Test
1058     public void testMatchPropertyTokensNotClosed() {
1059         String example = "<cacheManagerPeerProviderFactory class=\"net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory\"" +
1060                 "properties=\"peerDiscovery=automatic, " +
1061                 "multicastGroupAddress=${multicastAddress\n, " +
1062                 "multicastGroupPort=4446, timeToLive=${multicastAddress\"/>";
1063         Set propertyTokens = ConfigurationFactory.extractPropertyTokens(example);
1064         assertEquals(0, propertyTokens.size());
1065     }
1066 
1067     @Test
1068     public void testCopyConfiguration() {
1069         File file = new File(TEST_CONFIG_DIR + "ehcache-copy.xml");
1070         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
1071         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
1072 
1073         Ehcache copyOnReadCache = configurationHelper.createCacheFromName("copyOnReadCache");
1074         assertTrue(copyOnReadCache.getCacheConfiguration().isCopyOnRead());
1075         assertFalse(copyOnReadCache.getCacheConfiguration().isCopyOnWrite());
1076         assertNotNull(copyOnReadCache.getCacheConfiguration().getCopyStrategy());
1077         assertTrue(copyOnReadCache.getCacheConfiguration().getCopyStrategy() instanceof SerializationCopyStrategy);
1078 
1079         Ehcache copyOnWriteCache = configurationHelper.createCacheFromName("copyOnWriteCache");
1080         assertFalse(copyOnWriteCache.getCacheConfiguration().isCopyOnRead());
1081         assertTrue(copyOnWriteCache.getCacheConfiguration().isCopyOnWrite());
1082         assertNotNull(copyOnWriteCache.getCacheConfiguration().getCopyStrategy());
1083         assertTrue(copyOnWriteCache.getCacheConfiguration().getCopyStrategy() instanceof SerializationCopyStrategy);
1084 
1085         Ehcache copyCache = configurationHelper.createCacheFromName("copyCache");
1086         assertTrue(copyCache.getCacheConfiguration().isCopyOnRead());
1087         assertTrue(copyCache.getCacheConfiguration().isCopyOnWrite());
1088         assertNotNull(copyCache.getCacheConfiguration().getCopyStrategy());
1089         assertTrue(copyCache.getCacheConfiguration().getCopyStrategy() instanceof FakeCopyStrategy);
1090 
1091         try {
1092             new CacheManager(TEST_CONFIG_DIR + "ehcache-copy.xml");
1093             fail("This should have thrown an Exception");
1094         } catch (Exception e) {
1095             if(!(e instanceof InvalidConfigurationException)) {
1096                 e.printStackTrace();
1097                 fail("Expected InvalidConfigurationException, but got "+ e.getClass().getSimpleName());
1098             }
1099         }
1100         
1101         file = new File(TEST_CONFIG_DIR + "ehcache-copy-tc.xml");
1102         configuration = ConfigurationFactory.parseConfiguration(file);
1103         configurationHelper = new ConfigurationHelper(manager, configuration);
1104 
1105         Ehcache nonCopyCache = configurationHelper.createCacheFromName("nonCopyOnReadCacheTcTrue");
1106         assertFalse(nonCopyCache.getCacheConfiguration().isCopyOnRead());
1107         assertTrue(nonCopyCache.getCacheConfiguration().getTerracottaConfiguration().isCopyOnRead());
1108 
1109         Ehcache nonCopyCacheTc = configurationHelper.createCacheFromName("copyOnReadCacheTcFalse");
1110         assertTrue(nonCopyCacheTc.getCacheConfiguration().isCopyOnRead());
1111         assertFalse(nonCopyCacheTc.getCacheConfiguration().getTerracottaConfiguration().isCopyOnRead());
1112 
1113         Ehcache copyOnReadCacheTc = configurationHelper.createCacheFromName("copyOnReadCacheTc");
1114         assertTrue(copyOnReadCacheTc.getCacheConfiguration().isCopyOnRead());
1115         assertTrue(copyOnReadCacheTc.getCacheConfiguration().getTerracottaConfiguration().isCopyOnRead());
1116     }
1117 
1118     /***
1119      * Test named cachemanager, terracotta config, clustered caches
1120      */
1121     @Test
1122     public void testTerracottaConfiguration() {
1123         File file = new File(TEST_CONFIG_DIR + "terracotta/ehcache-terracotta.xml");
1124         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
1125         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
1126 
1127         assertEquals("tc", configurationHelper.getConfigurationBean().getName());
1128         assertEquals(false, configurationHelper.getConfigurationBean().getUpdateCheck());
1129         assertEquals(Configuration.Monitoring.AUTODETECT, configurationHelper.getConfigurationBean().getMonitoring());
1130 
1131         //Check default cache
1132         Ehcache defaultCache = configurationHelper.createDefaultCache();
1133         assertEquals("default", defaultCache.getName());
1134         assertEquals(false, defaultCache.getCacheConfiguration().isEternal());
1135         assertEquals(5, defaultCache.getCacheConfiguration().getTimeToIdleSeconds());
1136         assertEquals(10, defaultCache.getCacheConfiguration().getTimeToLiveSeconds());
1137         assertEquals(false, defaultCache.getCacheConfiguration().isOverflowToDisk());
1138         assertEquals(10, defaultCache.getCacheConfiguration().getMaxElementsInMemory());
1139         assertEquals(0, defaultCache.getCacheConfiguration().getMaxElementsOnDisk());
1140         assertEquals(true, defaultCache.getCacheConfiguration().isTerracottaClustered());
1141         assertEquals(true, defaultCache.getCacheConfiguration().getTerracottaConfiguration().getCoherentReads());
1142 
1143         //Check caches
1144         assertEquals(16, configurationHelper.createCaches().size());
1145 
1146         //  <cache name="clustered-1"
1147         //   maxElementsInMemory="1000"
1148         //   memoryStoreEvictionPolicy="LFU">
1149         //   <terracotta/>
1150         //  </cache>
1151         Ehcache sampleCache1 = configurationHelper.createCacheFromName("clustered-1");
1152         assertEquals("clustered-1", sampleCache1.getName());
1153         assertEquals(true, sampleCache1.getCacheConfiguration().isTerracottaClustered());
1154         assertEquals(TerracottaConfiguration.ValueMode.SERIALIZATION,
1155                 sampleCache1.getCacheConfiguration().getTerracottaConfiguration().getValueMode());
1156 
1157         //  <cache name="clustered-2"
1158         //      maxElementsInMemory="1000"
1159         //            memoryStoreEvictionPolicy="LFU">
1160         //          <terracotta clustered="false"/>
1161         //   </cache>
1162         Ehcache sampleCache2 = configurationHelper.createCacheFromName("clustered-2");
1163         assertEquals("clustered-2", sampleCache2.getName());
1164         assertEquals(false, sampleCache2.getCacheConfiguration().isTerracottaClustered());
1165         assertEquals(TerracottaConfiguration.ValueMode.SERIALIZATION,
1166                 sampleCache2.getCacheConfiguration().getTerracottaConfiguration().getValueMode());
1167 
1168         //  <cache name="clustered-3"
1169         //   maxElementsInMemory="1000"
1170         //   memoryStoreEvictionPolicy="LFU">
1171         //   <terracotta valueMode="serialization"/>
1172         //  </cache>
1173         Ehcache sampleCache3 = configurationHelper.createCacheFromName("clustered-3");
1174         assertEquals("clustered-3", sampleCache3.getName());
1175         assertEquals(true, sampleCache3.getCacheConfiguration().isTerracottaClustered());
1176         assertEquals(TerracottaConfiguration.ValueMode.SERIALIZATION,
1177                 sampleCache3.getCacheConfiguration().getTerracottaConfiguration().getValueMode());
1178 
1179         //  <cache name="clustered-4"
1180         //   maxElementsInMemory="1000"
1181         //   memoryStoreEvictionPolicy="LFU">
1182         //   <terracotta valueMode="identity"/>
1183         //  </cache>
1184         Ehcache sampleCache4 = configurationHelper.createCacheFromName("clustered-4");
1185         assertEquals("clustered-4", sampleCache4.getName());
1186         assertEquals(true, sampleCache4.getCacheConfiguration().isTerracottaClustered());
1187         assertEquals(TerracottaConfiguration.ValueMode.IDENTITY,
1188                 sampleCache4.getCacheConfiguration().getTerracottaConfiguration().getValueMode());
1189 
1190         //  <cache name="clustered-5"
1191         //   maxElementsInMemory="1000"
1192         //   memoryStoreEvictionPolicy="LFU">
1193         //   <terracotta coherentReads="false"/>
1194         //  </cache>
1195         Ehcache sampleCache5 = configurationHelper.createCacheFromName("clustered-5");
1196         assertEquals("clustered-5", sampleCache5.getName());
1197         assertEquals(true, sampleCache5.getCacheConfiguration().isTerracottaClustered());
1198         assertEquals(false,
1199                 sampleCache5.getCacheConfiguration().getTerracottaConfiguration().getCoherentReads());
1200 
1201         //  <cache name="clustered-6"
1202         //   maxElementsInMemory="1000"
1203         //   memoryStoreEvictionPolicy="LFU">
1204         //   <terracotta orphanEviction="false"/>
1205         //  </cache>
1206         Ehcache sampleCache6 = configurationHelper.createCacheFromName("clustered-6");
1207         assertEquals("clustered-6", sampleCache6.getName());
1208         assertEquals(true, sampleCache6.getCacheConfiguration().isTerracottaClustered());
1209         assertEquals(false,
1210                 sampleCache6.getCacheConfiguration().getTerracottaConfiguration().getOrphanEviction());
1211 
1212         //  <cache name="clustered-7"
1213         //   maxElementsInMemory="1000"
1214         //   memoryStoreEvictionPolicy="LFU">
1215         //   <terracotta orphanEvictionPeriod="42"/>
1216         //  </cache>
1217         Ehcache sampleCache7 = configurationHelper.createCacheFromName("clustered-7");
1218         assertEquals("clustered-7", sampleCache7.getName());
1219         assertEquals(true, sampleCache7.getCacheConfiguration().isTerracottaClustered());
1220         assertEquals(42,
1221                 sampleCache7.getCacheConfiguration().getTerracottaConfiguration().getOrphanEvictionPeriod());
1222 
1223         //  <cache name="clustered-8"
1224         //   maxElementsInMemory="1000"
1225         //   memoryStoreEvictionPolicy="LFU">
1226         //   <terracotta localKeyCache="true"/>
1227         //  </cache>
1228         Ehcache sampleCache8 = configurationHelper.createCacheFromName("clustered-8");
1229         assertEquals("clustered-8", sampleCache8.getName());
1230         assertEquals(true, sampleCache8.getCacheConfiguration().isTerracottaClustered());
1231         assertEquals(true,
1232                 sampleCache8.getCacheConfiguration().getTerracottaConfiguration().getLocalKeyCache());
1233 
1234         //  <cache name="clustered-9"
1235         //   maxElementsInMemory="1000"
1236         //   memoryStoreEvictionPolicy="LFU">
1237         //   <terracotta localKeyCache="true"/>
1238         //  </cache>
1239         Ehcache sampleCache9 = configurationHelper.createCacheFromName("clustered-9");
1240         assertEquals("clustered-9", sampleCache9.getName());
1241         assertEquals(true, sampleCache9.getCacheConfiguration().isTerracottaClustered());
1242         assertEquals(42,
1243                 sampleCache9.getCacheConfiguration().getTerracottaConfiguration().getLocalKeyCacheSize());
1244       
1245       // assert default value is true always
1246       assertEquals(true, TerracottaConfiguration.DEFAULT_CACHE_COHERENT);
1247       
1248       Ehcache sampleCache10 = configurationHelper.createCacheFromName("clustered-10");
1249       assertEquals("clustered-10", sampleCache10.getName());
1250       assertEquals(true, sampleCache10.getCacheConfiguration().isTerracottaClustered());
1251       assertEquals(true,
1252               sampleCache10.getCacheConfiguration().getTerracottaConfiguration().isCoherent());
1253       
1254       Ehcache sampleCache11 = configurationHelper.createCacheFromName("clustered-11");
1255       assertEquals("clustered-11", sampleCache11.getName());
1256       assertEquals(true, sampleCache11.getCacheConfiguration().isTerracottaClustered());
1257       assertEquals(false,
1258               sampleCache11.getCacheConfiguration().getTerracottaConfiguration().isCoherent());
1259       
1260       Ehcache sampleCache12 = configurationHelper.createCacheFromName("clustered-12");
1261       assertEquals("clustered-12", sampleCache12.getName());
1262       assertEquals(true, sampleCache12.getCacheConfiguration().isTerracottaClustered());
1263       assertEquals(true,
1264               sampleCache12.getCacheConfiguration().getTerracottaConfiguration().isCoherent());
1265       
1266    // assert default value is false always
1267       assertEquals(false, TerracottaConfiguration.DEFAULT_SYNCHRONOUS_WRITES);
1268       
1269       Ehcache sampleCache13 = configurationHelper.createCacheFromName("clustered-13");
1270       assertEquals("clustered-13", sampleCache13.getName());
1271       assertEquals(true, sampleCache13.getCacheConfiguration().isTerracottaClustered());
1272       assertEquals(false,
1273               sampleCache13.getCacheConfiguration().getTerracottaConfiguration().isSynchronousWrites());
1274       
1275       Ehcache sampleCache14 = configurationHelper.createCacheFromName("clustered-14");
1276       assertEquals("clustered-14", sampleCache14.getName());
1277       assertEquals(true, sampleCache14.getCacheConfiguration().isTerracottaClustered());
1278       assertEquals(false,
1279               sampleCache14.getCacheConfiguration().getTerracottaConfiguration().isSynchronousWrites());
1280       
1281       Ehcache sampleCache15 = configurationHelper.createCacheFromName("clustered-15");
1282       assertEquals("clustered-15", sampleCache15.getName());
1283       assertEquals(true, sampleCache15.getCacheConfiguration().isTerracottaClustered());
1284       assertEquals(true,
1285               sampleCache15.getCacheConfiguration().getTerracottaConfiguration().isSynchronousWrites());
1286 
1287         // <terracottaConfig>
1288         //  <url>localhost:9510</url>
1289         // </terracottaConfig>
1290         TerracottaClientConfiguration tcConfig = configuration.getTerracottaConfiguration();
1291         assertNotNull(tcConfig);
1292         assertEquals("localhost:9510", tcConfig.getUrl());
1293     }
1294 
1295 
1296     /***
1297      * Test tc-config embedded in ehcache.xml
1298      */
1299     @Test
1300     public void testTerracottaEmbeddedConfig() {
1301         File file = new File(TEST_CONFIG_DIR + "terracotta/ehcache-tc-embedded.xml");
1302         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
1303         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
1304 
1305         assertEquals("tc", configurationHelper.getConfigurationBean().getName());
1306         assertEquals(false, configurationHelper.getConfigurationBean().getUpdateCheck());
1307         assertEquals(Configuration.Monitoring.AUTODETECT, configurationHelper.getConfigurationBean().getMonitoring());
1308 
1309         //Check default cache
1310         Ehcache defaultCache = configurationHelper.createDefaultCache();
1311         assertEquals("default", defaultCache.getName());
1312         assertEquals(false, defaultCache.getCacheConfiguration().isEternal());
1313         assertEquals(5, defaultCache.getCacheConfiguration().getTimeToIdleSeconds());
1314         assertEquals(10, defaultCache.getCacheConfiguration().getTimeToLiveSeconds());
1315         assertEquals(false, defaultCache.getCacheConfiguration().isOverflowToDisk());
1316         assertEquals(10, defaultCache.getCacheConfiguration().getMaxElementsInMemory());
1317         assertEquals(0, defaultCache.getCacheConfiguration().getMaxElementsOnDisk());
1318         assertEquals(true, defaultCache.getCacheConfiguration().isTerracottaClustered());
1319 
1320         //Check caches
1321         assertEquals(1, configurationHelper.createCaches().size());
1322 
1323         //  <cache name="clustered-1"
1324         //   maxElementsInMemory="1000"
1325         //   memoryStoreEvictionPolicy="LFU">
1326         //   <terracotta/>
1327         //  </cache>
1328         Ehcache sampleCache1 = configurationHelper.createCacheFromName("clustered-1");
1329         assertEquals("clustered-1", sampleCache1.getName());
1330         assertEquals(true, sampleCache1.getCacheConfiguration().isTerracottaClustered());
1331         assertEquals(TerracottaConfiguration.ValueMode.SERIALIZATION,
1332                 sampleCache1.getCacheConfiguration().getTerracottaConfiguration().getValueMode());
1333 
1334         // <terracottaConfig>
1335         //  <tc-config> ... </tc-config>
1336         // </terracottaConfig>
1337         TerracottaClientConfiguration tcConfig = configuration.getTerracottaConfiguration();
1338         assertNotNull(tcConfig);
1339         assertEquals(null, tcConfig.getUrl());
1340         String embeddedConfig = tcConfig.getEmbeddedConfig();
1341         assertEquals("<tc:tc-config xmlns:tc=\"http://www.terracotta.org/config\"> " +
1342                 "<servers> <server host=\"server1\" name=\"s1\"></server> " +
1343                 "<server host=\"server2\" name=\"s2\"></server> </servers> " +
1344                 "<clients> <logs>app/logs-%i</logs> </clients> </tc:tc-config>",
1345                 removeLotsOfWhitespace(tcConfig.getEmbeddedConfig()));
1346     }
1347 
1348     @Test
1349     public void testTerracottaEmbeddedXsdConfig() {
1350         File file = new File(TEST_CONFIG_DIR
1351                 + "terracotta/ehcache-tc-embedded-xsd.xml");
1352         Configuration configuration = ConfigurationFactory
1353                 .parseConfiguration(file);
1354         ConfigurationHelper configurationHelper = new ConfigurationHelper(
1355                 manager, configuration);
1356 
1357         assertEquals("tc", configurationHelper.getConfigurationBean().getName());
1358         assertEquals(false, configurationHelper.getConfigurationBean()
1359                 .getUpdateCheck());
1360         assertEquals(Configuration.Monitoring.AUTODETECT, configurationHelper
1361                 .getConfigurationBean().getMonitoring());
1362 
1363         // <terracottaConfig>
1364         // <tc-config> ... </tc-config>
1365         // </terracottaConfig>
1366         TerracottaClientConfiguration tcConfig = configuration
1367                 .getTerracottaConfiguration();
1368         assertNotNull(tcConfig);
1369         assertEquals(null, tcConfig.getUrl());
1370         String embeddedConfig = tcConfig.getEmbeddedConfig();
1371         assertEquals(
1372                 "<tc:tc-config xmlns:tc=\"http://www.terracotta.org/config\"> <servers> "
1373                         + "<server host=\"server1\" name=\"s1\"></server> "
1374                         + "<server host=\"server2\" name=\"s2\"></server> </servers> "
1375                         + "<clients> <logs>app/logs-%i</logs> </clients> </tc:tc-config>",
1376                 removeLotsOfWhitespace(tcConfig.getEmbeddedConfig()));
1377     }
1378 
1379     /***
1380      * Test invalid combination of overflow to disk and terracotta ehcache.xml
1381      */
1382     @Test
1383     public void testTerracottaInvalidConfig1() {
1384         File file = new File(TEST_CONFIG_DIR + "terracotta/ehcache-terracotta-invalid1.xml");
1385         try {
1386             Configuration configuration = ConfigurationFactory.parseConfiguration(file);
1387             fail("expecting exception to be thrown");
1388         } catch (CacheException e) {
1389             assertTrue(e.getMessage().contains("overflowToDisk isn't supported for a clustered Terracotta cache"));
1390         }
1391     }
1392 
1393     /***
1394      * Test invalid combination of disk persistent and terracotta ehcache.xml
1395      */
1396     @Test
1397     public void testTerracottaInvalidConfig2() {
1398         File file = new File(TEST_CONFIG_DIR + "terracotta/ehcache-terracotta-invalid2.xml");
1399         try {
1400             Configuration configuration = ConfigurationFactory.parseConfiguration(file);
1401             fail("expecting exception to be thrown");
1402         } catch (CacheException e) {
1403             assertTrue(e.getMessage().contains("diskPersistent isn't supported for a clustered Terracotta cache"));
1404         }
1405     }
1406 
1407     /***
1408      * Test valid combination of replicated and terracotta ehcache.xml
1409      */
1410     @Test
1411     public void testTerracottaConfigRMIReplication() {
1412         File file = new File(TEST_CONFIG_DIR + "terracotta/ehcache-terracotta-rmi.xml");
1413         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
1414         List configs = configuration.getCacheConfigurations().get("clustered").getCacheEventListenerConfigurations();
1415         assertEquals(1, configs.size());
1416         assertEquals(((CacheConfiguration.CacheEventListenerFactoryConfiguration)configs.get(0)).getFullyQualifiedClassPath(), RMICacheReplicatorFactory.class.getName());
1417     }
1418 
1419     /***
1420      * Test valid combination of replicated and terracotta ehcache.xml
1421      */
1422     @Test
1423     public void testTerracottaConfigJGroupsReplication() {
1424         File file = new File(TEST_CONFIG_DIR + "terracotta/ehcache-terracotta-jgroups.xml");
1425         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
1426         List configs = configuration.getCacheConfigurations().get("clustered").getCacheEventListenerConfigurations();
1427         assertEquals(1, configs.size());
1428         assertEquals(((CacheConfiguration.CacheEventListenerFactoryConfiguration)configs.get(0)).getFullyQualifiedClassPath(), "net.sf.ehcache.distribution.JGroupsCacheReplicatorFactory");
1429     }
1430 
1431     /***
1432      * Test valid combination of replicated and terracotta ehcache.xml
1433      */
1434     @Test
1435     public void testTerracottaInvalidConfig5() {
1436         File file = new File(TEST_CONFIG_DIR + "terracotta/ehcache-terracotta-jms.xml");
1437         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
1438         List configs = configuration.getCacheConfigurations().get("clustered").getCacheEventListenerConfigurations();
1439         assertEquals(1, configs.size());
1440         assertEquals(((CacheConfiguration.CacheEventListenerFactoryConfiguration)configs.get(0)).getFullyQualifiedClassPath(), "net.sf.ehcache.distribution.JMSCacheReplicatorFactory");
1441     }
1442 
1443     private String removeLotsOfWhitespace(String str) {
1444         return str.replace("\t", "").replace("\r", "").replace("\n", "").replaceAll("//s+", " ");
1445     }
1446 
1447     @Test
1448     public void testMonitoringOn() {
1449         File file = new File(TEST_CONFIG_DIR + "ehcache-monitoring-on.xml");
1450         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
1451         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
1452 
1453         assertEquals(Configuration.Monitoring.ON, configurationHelper.getConfigurationBean().getMonitoring());
1454     }
1455 
1456     @Test
1457     public void testMonitoringOff() {
1458         File file = new File(TEST_CONFIG_DIR + "ehcache-monitoring-off.xml");
1459         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
1460         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
1461 
1462         assertEquals(Configuration.Monitoring.OFF, configurationHelper.getConfigurationBean().getMonitoring());
1463     }
1464 
1465     @Test
1466     public void testMonitoringAutodetect() {
1467         File file = new File(TEST_CONFIG_DIR + "ehcache-monitoring-autodetect.xml");
1468         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
1469         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
1470 
1471         assertEquals(Configuration.Monitoring.AUTODETECT, configurationHelper.getConfigurationBean().getMonitoring());
1472     }
1473 
1474     /***
1475      * Test cache writer config
1476      */
1477     @Test
1478     public void testWriterConfig() {
1479         File file = new File(TEST_CONFIG_DIR + "ehcache-writer.xml");
1480         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
1481         ConfigurationHelper configurationHelper = new ConfigurationHelper(manager, configuration);
1482 
1483         CacheWriterConfiguration defaultCacheWriterConfig = new CacheWriterConfiguration();
1484 
1485         CacheConfiguration configDefault = configurationHelper.getConfigurationBean().getDefaultCacheConfiguration();
1486         assertEquals(false, configDefault.isEternal());
1487         assertEquals(5, configDefault.getTimeToIdleSeconds());
1488         assertEquals(10, configDefault.getTimeToLiveSeconds());
1489         assertEquals(false, configDefault.isOverflowToDisk());
1490         assertEquals(10, configDefault.getMaxElementsInMemory());
1491         assertNotNull(configDefault.getCacheWriterConfiguration());
1492         assertEquals(defaultCacheWriterConfig.getWriteMode(), configDefault.getCacheWriterConfiguration().getWriteMode());
1493         assertEquals(defaultCacheWriterConfig.getCacheWriterFactoryConfiguration(), configDefault.getCacheWriterConfiguration().getCacheWriterFactoryConfiguration());
1494         assertEquals(defaultCacheWriterConfig.getNotifyListenersOnException(), configDefault.getCacheWriterConfiguration().getNotifyListenersOnException());
1495         assertEquals(defaultCacheWriterConfig.getMaxWriteDelay(), configDefault.getCacheWriterConfiguration().getMaxWriteDelay());
1496         assertEquals(defaultCacheWriterConfig.getRateLimitPerSecond(), configDefault.getCacheWriterConfiguration().getRateLimitPerSecond());
1497         assertEquals(defaultCacheWriterConfig.getWriteCoalescing(), configDefault.getCacheWriterConfiguration().getWriteCoalescing());
1498         assertEquals(defaultCacheWriterConfig.getWriteBatching(), configDefault.getCacheWriterConfiguration().getWriteBatching());
1499         assertEquals(defaultCacheWriterConfig.getWriteBatchSize(), configDefault.getCacheWriterConfiguration().getWriteBatchSize());
1500         assertEquals(defaultCacheWriterConfig.getRetryAttempts(), configDefault.getCacheWriterConfiguration().getRetryAttempts());
1501         assertEquals(defaultCacheWriterConfig.getRetryAttemptDelaySeconds(), configDefault.getCacheWriterConfiguration().getRetryAttemptDelaySeconds());
1502 
1503         Ehcache defaultCache = configurationHelper.createDefaultCache();
1504         assertEquals("default", defaultCache.getName());
1505         assertNotNull(defaultCache.getCacheConfiguration().getCacheWriterConfiguration());
1506 
1507         Map<String, CacheConfiguration> configs = configurationHelper.getConfigurationBean().getCacheConfigurations();
1508         CacheConfiguration config1 = configs.get("writeThroughCache1");
1509         assertNotNull(config1.getCacheWriterConfiguration());
1510         assertEquals(defaultCacheWriterConfig.getWriteMode(), config1.getCacheWriterConfiguration().getWriteMode());
1511         assertEquals(defaultCacheWriterConfig.getCacheWriterFactoryConfiguration(), config1.getCacheWriterConfiguration().getCacheWriterFactoryConfiguration());
1512         assertEquals(defaultCacheWriterConfig.getNotifyListenersOnException(), config1.getCacheWriterConfiguration().getNotifyListenersOnException());
1513         assertEquals(defaultCacheWriterConfig.getMaxWriteDelay(), config1.getCacheWriterConfiguration().getMaxWriteDelay());
1514         assertEquals(defaultCacheWriterConfig.getRateLimitPerSecond(), config1.getCacheWriterConfiguration().getRateLimitPerSecond());
1515         assertEquals(defaultCacheWriterConfig.getWriteCoalescing(), config1.getCacheWriterConfiguration().getWriteCoalescing());
1516         assertEquals(defaultCacheWriterConfig.getWriteBatching(), config1.getCacheWriterConfiguration().getWriteBatching());
1517         assertEquals(defaultCacheWriterConfig.getWriteBatchSize(), config1.getCacheWriterConfiguration().getWriteBatchSize());
1518         assertEquals(defaultCacheWriterConfig.getRetryAttempts(), config1.getCacheWriterConfiguration().getRetryAttempts());
1519         assertEquals(defaultCacheWriterConfig.getRetryAttemptDelaySeconds(), config1.getCacheWriterConfiguration().getRetryAttemptDelaySeconds());
1520 
1521         CacheConfiguration config2 = configs.get("writeThroughCache2");
1522         assertNotNull(config2.getCacheWriterConfiguration());
1523         assertEquals(defaultCacheWriterConfig.getWriteMode(), config2.getCacheWriterConfiguration().getWriteMode());
1524         assertEquals(defaultCacheWriterConfig.getCacheWriterFactoryConfiguration(), config2.getCacheWriterConfiguration().getCacheWriterFactoryConfiguration());
1525         assertEquals(defaultCacheWriterConfig.getNotifyListenersOnException(), config2.getCacheWriterConfiguration().getNotifyListenersOnException());
1526         assertEquals(defaultCacheWriterConfig.getMaxWriteDelay(), config2.getCacheWriterConfiguration().getMaxWriteDelay());
1527         assertEquals(defaultCacheWriterConfig.getRateLimitPerSecond(), config2.getCacheWriterConfiguration().getRateLimitPerSecond());
1528         assertEquals(defaultCacheWriterConfig.getWriteCoalescing(), config2.getCacheWriterConfiguration().getWriteCoalescing());
1529         assertEquals(defaultCacheWriterConfig.getWriteBatching(), config2.getCacheWriterConfiguration().getWriteBatching());
1530         assertEquals(defaultCacheWriterConfig.getWriteBatchSize(), config2.getCacheWriterConfiguration().getWriteBatchSize());
1531         assertEquals(defaultCacheWriterConfig.getRetryAttempts(), config2.getCacheWriterConfiguration().getRetryAttempts());
1532         assertEquals(defaultCacheWriterConfig.getRetryAttemptDelaySeconds(), config2.getCacheWriterConfiguration().getRetryAttemptDelaySeconds());
1533 
1534         CacheConfiguration config3 = configs.get("writeThroughCache3");
1535         assertNotNull(config3.getCacheWriterConfiguration());
1536         assertEquals(CacheWriterConfiguration.WriteMode.WRITE_THROUGH, config3.getCacheWriterConfiguration().getWriteMode());
1537         assertEquals(defaultCacheWriterConfig.getCacheWriterFactoryConfiguration(), config3.getCacheWriterConfiguration().getCacheWriterFactoryConfiguration());
1538         assertEquals(true, config3.getCacheWriterConfiguration().getNotifyListenersOnException());
1539         assertEquals(30, config3.getCacheWriterConfiguration().getMaxWriteDelay());
1540         assertEquals(10, config3.getCacheWriterConfiguration().getRateLimitPerSecond());
1541         assertEquals(true, config3.getCacheWriterConfiguration().getWriteCoalescing());
1542         assertEquals(true, config3.getCacheWriterConfiguration().getWriteBatching());
1543         assertEquals(8, config3.getCacheWriterConfiguration().getWriteBatchSize());
1544         assertEquals(20, config3.getCacheWriterConfiguration().getRetryAttempts());
1545         assertEquals(60, config3.getCacheWriterConfiguration().getRetryAttemptDelaySeconds());
1546 
1547         CacheConfiguration config4 = configs.get("writeThroughCache4");
1548         assertNotNull(config4.getCacheWriterConfiguration());
1549         assertEquals(CacheWriterConfiguration.WriteMode.WRITE_THROUGH, config4.getCacheWriterConfiguration().getWriteMode());
1550         assertEquals("net.sf.ehcache.writer.TestCacheWriterFactory", config4.getCacheWriterConfiguration().getCacheWriterFactoryConfiguration().getFullyQualifiedClassPath());
1551         assertEquals(null, config4.getCacheWriterConfiguration().getCacheWriterFactoryConfiguration().getProperties());
1552         assertEquals(null, config4.getCacheWriterConfiguration().getCacheWriterFactoryConfiguration().getPropertySeparator());
1553         assertEquals(false, config4.getCacheWriterConfiguration().getNotifyListenersOnException());
1554         assertEquals(0, config4.getCacheWriterConfiguration().getMaxWriteDelay());
1555         assertEquals(0, config4.getCacheWriterConfiguration().getRateLimitPerSecond());
1556         assertEquals(false, config4.getCacheWriterConfiguration().getWriteCoalescing());
1557         assertEquals(false, config4.getCacheWriterConfiguration().getWriteBatching());
1558         assertEquals(1, config4.getCacheWriterConfiguration().getWriteBatchSize());
1559         assertEquals(0, config4.getCacheWriterConfiguration().getRetryAttempts());
1560         assertEquals(0, config4.getCacheWriterConfiguration().getRetryAttemptDelaySeconds());
1561 
1562         CacheConfiguration config5 = configs.get("writeBehindCache5");
1563         assertNotNull(config5.getCacheWriterConfiguration());
1564         assertEquals(CacheWriterConfiguration.WriteMode.WRITE_BEHIND, config5.getCacheWriterConfiguration().getWriteMode());
1565         assertEquals("net.sf.ehcache.writer.TestCacheWriterFactory", config5.getCacheWriterConfiguration().getCacheWriterFactoryConfiguration().getFullyQualifiedClassPath());
1566         assertEquals("just.some.property=test; another.property=test2", config5.getCacheWriterConfiguration().getCacheWriterFactoryConfiguration().getProperties());
1567         assertEquals(";", config5.getCacheWriterConfiguration().getCacheWriterFactoryConfiguration().getPropertySeparator());
1568         assertEquals(true, config5.getCacheWriterConfiguration().getNotifyListenersOnException());
1569         assertEquals(8, config5.getCacheWriterConfiguration().getMaxWriteDelay());
1570         assertEquals(5, config5.getCacheWriterConfiguration().getRateLimitPerSecond());
1571         assertEquals(true, config5.getCacheWriterConfiguration().getWriteCoalescing());
1572         assertEquals(false, config5.getCacheWriterConfiguration().getWriteBatching());
1573         assertEquals(20, config5.getCacheWriterConfiguration().getWriteBatchSize());
1574         assertEquals(2, config5.getCacheWriterConfiguration().getRetryAttempts());
1575         assertEquals(2, config5.getCacheWriterConfiguration().getRetryAttemptDelaySeconds());
1576         Ehcache cache5 = configurationHelper.createCacheFromName("writeBehindCache5");
1577         Properties properties5 = ((TestCacheWriter)cache5.getRegisteredCacheWriter()).getProperties();
1578         assertEquals(2, properties5.size());
1579         assertEquals("test", properties5.getProperty("just.some.property"));
1580         assertEquals("test2", properties5.getProperty("another.property"));
1581     }
1582 
1583 
1584     private void helpTestListenFor(Configuration configuration, String cacheName, NotificationScope expectedScope) {
1585         CacheConfiguration cache = configuration.getCacheConfigurations().get(cacheName);
1586         List<CacheConfiguration.CacheEventListenerFactoryConfiguration> listenerConfigs = cache.getCacheEventListenerConfigurations();
1587         assertEquals(1, listenerConfigs.size());
1588 
1589         CacheConfiguration.CacheEventListenerFactoryConfiguration listenerFactoryConfig = listenerConfigs.get(0);
1590         assertEquals(expectedScope, listenerFactoryConfig.getListenFor());
1591     }
1592 
1593     @Test
1594     public void testListenForAttributeParsing() {
1595         File file = new File(TEST_CONFIG_DIR + "ehcache-listener-scope.xml");
1596         Configuration configuration = ConfigurationFactory.parseConfiguration(file);
1597 
1598         helpTestListenFor(configuration, "listenDefault", NotificationScope.ALL);
1599         helpTestListenFor(configuration, "listenAll", NotificationScope.ALL);
1600         helpTestListenFor(configuration, "listenLocal", NotificationScope.LOCAL);
1601         helpTestListenFor(configuration, "listenRemote", NotificationScope.REMOTE);
1602     }
1603     
1604 }