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 package net.sf.ehcache.config;
18
19 import junit.framework.TestCase;
20 import net.sf.ehcache.Cache;
21 import net.sf.ehcache.CacheManager;
22 import net.sf.ehcache.config.TerracottaConfiguration.Consistency;
23
24 import org.junit.Test;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class CoherenceModeConfigTest extends TestCase {
29
30 private static final Logger LOG = LoggerFactory.getLogger(CoherenceModeConfigTest.class);
31
32 @Test
33 public void testCoherenceModeConfig() {
34 CacheManager cacheManager = new CacheManager(this.getClass().getResourceAsStream("/ehcache-coherence-mode-test.xml"));
35 Cache cache = cacheManager.getCache("defaultCoherenceMode");
36 boolean coherent;
37 Consistency consistency;
38 coherent = cache.getCacheConfiguration().getTerracottaConfiguration().isCoherent();
39 consistency = cache.getCacheConfiguration().getTerracottaConfiguration().getConsistency();
40 LOG.info("Default coherent: " + coherent);
41 LOG.info("Default Coherence mode: " + consistency);
42 final boolean expectedDefault = TerracottaConfiguration.DEFAULT_CONSISTENCY_TYPE == Consistency.STRONG ? true : false;
43 assertEquals(expectedDefault, coherent);
44 assertEquals(TerracottaConfiguration.DEFAULT_CONSISTENCY_TYPE, consistency);
45
46 cache = cacheManager.getCache("falseCoherenceMode");
47 coherent = cache.getCacheConfiguration().getTerracottaConfiguration().isCoherent();
48 consistency = cache.getCacheConfiguration().getTerracottaConfiguration().getConsistency();
49 LOG.info("False coherent: " + coherent);
50 LOG.info("False Coherence mode: " + consistency);
51 assertEquals(false, coherent);
52 assertEquals(Consistency.EVENTUAL, consistency);
53
54 cache = cacheManager.getCache("trueCoherenceMode");
55 coherent = cache.getCacheConfiguration().getTerracottaConfiguration().isCoherent();
56 consistency = cache.getCacheConfiguration().getTerracottaConfiguration().getConsistency();
57 LOG.info("True coherent: " + coherent);
58 LOG.info("True Coherence mode: " + consistency);
59 assertEquals(true, coherent);
60 assertEquals(Consistency.STRONG, consistency);
61
62 TerracottaConfiguration tcConfig = cache.getCacheConfiguration().getTerracottaConfiguration();
63 tcConfig.setCoherent(false);
64 assertEquals(Consistency.EVENTUAL, cache.getCacheConfiguration().getTerracottaConfiguration().getConsistency());
65
66 tcConfig.setCoherent(true);
67 assertEquals(Consistency.STRONG, cache.getCacheConfiguration().getTerracottaConfiguration().getConsistency());
68
69 tcConfig.setConsistency(Consistency.EVENTUAL);
70 assertEquals(Consistency.EVENTUAL, cache.getCacheConfiguration().getTerracottaConfiguration().getConsistency());
71
72 tcConfig.setConsistency(Consistency.STRONG);
73 assertEquals(Consistency.STRONG, cache.getCacheConfiguration().getTerracottaConfiguration().getConsistency());
74 }
75
76 }