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