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.nonstop;
18
19 import junit.framework.TestCase;
20 import net.sf.ehcache.Cache;
21 import net.sf.ehcache.CacheException;
22 import net.sf.ehcache.CacheManager;
23 import net.sf.ehcache.config.CacheConfiguration;
24 import net.sf.ehcache.config.NonstopConfiguration;
25 import net.sf.ehcache.config.TerracottaConfiguration;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class NonStopConfigTest extends TestCase {
30
31 private static final Logger LOG = LoggerFactory.getLogger(NonStopConfigTest.class);
32
33 public void testInvalidConfig() {
34 try {
35 CacheManager cacheManager = new CacheManager(getClass().getResourceAsStream("/nonstop/nonstop-invalid-config-test.xml"));
36 fail("Invalid config should have failed");
37 } catch (CacheException e) {
38
39 assertTrue(e.getMessage().contains("does not allow attribute \"one\""));
40 }
41 }
42
43 public void testNonStopCacheConfig() {
44 CacheManager cacheManager = new CacheManager(getClass().getResourceAsStream("/nonstop/nonstop-config-test.xml"));
45 assertNonstopConfig(cacheManager.getCache("defaultConfig"), NonstopConfiguration.DEFAULT_ENABLED,
46 NonstopConfiguration.DEFAULT_IMMEDIATE_TIMEOUT, NonstopConfiguration.DEFAULT_TIMEOUT_MILLIS,
47 NonstopConfiguration.DEFAULT_TIMEOUT_BEHAVIOR.getType());
48
49 assertNonstopConfig(cacheManager.getCache("one"), false, NonstopConfiguration.DEFAULT_IMMEDIATE_TIMEOUT,
50 NonstopConfiguration.DEFAULT_TIMEOUT_MILLIS, NonstopConfiguration.DEFAULT_TIMEOUT_BEHAVIOR.getType());
51
52 assertNonstopConfig(cacheManager.getCache("two"), false, false, NonstopConfiguration.DEFAULT_TIMEOUT_MILLIS,
53 NonstopConfiguration.DEFAULT_TIMEOUT_BEHAVIOR.getType());
54 assertNonstopConfig(cacheManager.getCache("three"), false, false, 12345, NonstopConfiguration.DEFAULT_TIMEOUT_BEHAVIOR
55 .getType());
56 assertNonstopConfig(cacheManager.getCache("four"), false, false, 12345, "localReads");
57 cacheManager.shutdown();
58 }
59
60 private void assertNonstopConfig(Cache cache, boolean nonstop, boolean immediateTimeout, int timeoutMillis, String timeoutBehavior) {
61 LOG.info("Checking for cache: " + cache.getName());
62 CacheConfiguration cacheConfiguration = cache.getCacheConfiguration();
63 assertNotNull(cacheConfiguration);
64 TerracottaConfiguration terracottaConfiguration = cacheConfiguration.getTerracottaConfiguration();
65 assertNotNull(terracottaConfiguration);
66 NonstopConfiguration nonstopConfiguration = terracottaConfiguration.getNonstopConfiguration();
67 assertNotNull(nonstopConfiguration);
68
69 assertEquals(nonstop, nonstopConfiguration.isEnabled());
70 assertEquals(immediateTimeout, nonstopConfiguration.isImmediateTimeout());
71 assertEquals(timeoutMillis, nonstopConfiguration.getTimeoutMillis());
72 assertEquals(timeoutBehavior, nonstopConfiguration.getTimeoutBehavior().getType());
73 }
74
75 }