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.terracotta;
18
19 import static org.mockito.Mockito.mock;
20 import static org.mockito.Mockito.when;
21
22 import java.util.regex.Pattern;
23
24 import junit.framework.Assert;
25 import junit.framework.TestCase;
26 import net.sf.ehcache.CacheManager;
27 import net.sf.ehcache.cluster.CacheCluster;
28 import net.sf.ehcache.config.InvalidConfigurationException;
29
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.runners.MockitoJUnitRunner;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 @RunWith(MockitoJUnitRunner.class)
37 public class StorageStrategyInvalidConfigurationTest extends TestCase {
38
39 private static final Logger LOG = LoggerFactory.getLogger(StorageStrategyInvalidConfigurationTest.class);
40
41 @Test
42 public void testInvalidRejoinWithoutNonstop() throws Exception {
43 ClusteredInstanceFactory mockFactory = mock(ClusteredInstanceFactory.class);
44 TerracottaUnitTesting.setupTerracottaTesting(mockFactory);
45
46 CacheCluster mockCacheCluster = new MockCacheCluster();
47 when(mockFactory.getTopology()).thenReturn(mockCacheCluster);
48
49 try {
50 new CacheManager(CacheManager.class.getResourceAsStream("/ehcache-classic-with-nonstop-invalid-configuration-test.xml"));
51 fail("Trying to run rejoin without nonstop terracotta caches should fail");
52 } catch (InvalidConfigurationException e) {
53 LOG.info("Caught expected exception: " + e);
54
55 Assert.assertTrue(e.getMessage().contains("NONSTOP can't be enabled with CLASSIC strategy. Invalid Cache: cache1"));
56 Assert.assertTrue(e.getMessage().contains("REJOIN can't be enabled with CLASSIC strategy. Invalid Cache: cache1"));
57 Assert.assertTrue(e.getMessage().contains("EVENTUAL consistency can't be enabled with CLASSIC strategy. Invalid Cache: cache1"));
58
59 Assert.assertTrue(e.getMessage().contains("NONSTOP can't be enabled with CLASSIC strategy. Invalid Cache: cache2"));
60 Assert.assertTrue(e.getMessage().contains("REJOIN can't be enabled with CLASSIC strategy. Invalid Cache: cache2"));
61 Assert.assertTrue(e.getMessage().contains("EVENTUAL consistency can't be enabled with CLASSIC strategy. Invalid Cache: cache2"));
62
63 Assert.assertTrue(e.getMessage().contains("REJOIN can't be enabled with CLASSIC strategy. Invalid Cache: cache3"));
64 Assert.assertTrue(e.getMessage().contains("EVENTUAL consistency can't be enabled with CLASSIC strategy. Invalid Cache: cache3"));
65 Assert.assertTrue(e.getMessage().contains("Terracotta clustered caches must be nonstop when rejoin is enabled. Invalid cache: cache3"));
66
67 Assert.assertEquals(10, Pattern.compile("$", Pattern.MULTILINE).split(e.getMessage()).length);
68 }
69 }
70 }