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 java.util.Arrays;
20
21 import junit.framework.Assert;
22 import junit.framework.TestCase;
23 import net.sf.ehcache.Cache;
24 import net.sf.ehcache.CacheException;
25 import net.sf.ehcache.CacheManager;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /***
30 * @author Abhishek Sanoujam
31 */
32 public class DefaultCacheOptionalTest extends TestCase {
33
34 private static final Logger LOG = LoggerFactory.getLogger(DefaultCacheOptionalTest.class);
35
36 private CacheManager cacheManager;
37
38 @Override
39 public void tearDown() {
40 if (cacheManager != null) {
41 cacheManager.shutdown();
42 }
43 }
44
45 public void testDefaultCacheIsOptional() {
46 cacheManager = new CacheManager(this.getClass().getResourceAsStream("/no-default-cache.xml"));
47 String[] cacheNames = cacheManager.getCacheNames();
48 LOG.info("Cache names: " + Arrays.asList(cacheNames));
49 Assert.assertEquals(1, cacheNames.length);
50 Assert.assertEquals("sampleCache", cacheNames[0]);
51
52
53 try {
54 cacheManager.addCache("someNewCache");
55 fail("Adding cache by name with no default config should fail");
56 } catch (CacheException e) {
57 LOG.info("Got expected exception - " + e.getMessage());
58 }
59
60 try {
61 cacheManager.addCacheIfAbsent("someNewCache");
62 fail("Adding cache by name with no default config should fail");
63 } catch (CacheException e) {
64 LOG.info("Got expected exception - " + e.getMessage());
65 }
66
67
68 CacheConfiguration config = new CacheConfiguration("some-name", 92843);
69 Cache cache = new Cache(config);
70 cacheManager.addCache(cache);
71 LOG.info("Added concrete cache successfully");
72
73 }
74
75 }