View Javadoc

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 net.sf.ehcache.AbstractCacheTest;
20  import net.sf.ehcache.CacheManager;
21  
22  import static org.hamcrest.CoreMatchers.is;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertThat;
25  import static org.junit.Assert.assertFalse;
26  import static org.junit.Assert.assertTrue;
27  import static org.junit.Assert.fail;
28  
29  import org.junit.Test;
30  
31  import java.io.File;
32  import java.io.IOException;
33  
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  /***
38   * Tests programmatically constructed Configuration instances
39   *
40   * @author Greg Luck
41   * @version $Id: ConfigurationHelperTest.html 13146 2011-08-01 17:12:39Z oletizi $
42   */
43  public class ConfigurationHelperTest extends AbstractCacheTest {
44  
45      private static final Logger LOG = LoggerFactory.getLogger(ConfigurationHelperTest.class.getName());
46  
47      @Test
48      public void testMaxBytesOnConfiguration() {
49          Configuration configuration = new Configuration()
50              .maxBytesLocalHeap(20, MemoryUnit.MEGABYTES)
51              .maxBytesLocalOffHeap(2, MemoryUnit.GIGABYTES)
52              .maxBytesLocalDisk(2000, MemoryUnit.GIGABYTES);
53  
54          assertThat(configuration.getMaxBytesLocalHeap(), is(MemoryUnit.MEGABYTES.toBytes(20)));
55          assertThat(configuration.getMaxBytesLocalOffHeap(), is((long) 2 * 1024 * 1024 * 1024));
56          assertThat(configuration.getMaxBytesLocalDisk(), is((long) 2000 * 1024 * 1024 * 1024));
57  
58          configuration.setMaxBytesLocalHeap((Long) null);
59          assertThat(configuration.getMaxBytesLocalHeap(), is(0L));
60          assertThat(configuration.getMaxBytesLocalOffHeap(), is((long) 2 * 1024 * 1024 * 1024));
61          assertThat(configuration.getMaxBytesLocalDisk(), is((long)2000 * 1024 * 1024 * 1024));
62  
63          configuration.setMaxBytesLocalOffHeap((Long)null);
64          assertThat(configuration.getMaxBytesLocalHeap(), is(0L));
65          assertThat(configuration.getMaxBytesLocalOffHeap(), is(0L));
66          assertThat(configuration.getMaxBytesLocalDisk(), is((long)2000 * 1024 * 1024 * 1024));
67  
68          configuration.setMaxBytesLocalDisk((Long)null);
69          assertThat(configuration.getMaxBytesLocalHeap(), is(0L));
70          assertThat(configuration.getMaxBytesLocalOffHeap(), is(0L));
71          assertThat(configuration.getMaxBytesLocalDisk(), is(0L));
72  
73          configuration = new Configuration();
74          configuration.setMaxBytesLocalHeap("1g");
75          configuration.setMaxBytesLocalOffHeap("12G");
76          configuration.setMaxBytesLocalDisk("200G");
77  
78          assertThat(configuration.getMaxBytesLocalHeap(), is(MemoryUnit.GIGABYTES.toBytes(1)));
79          assertThat(configuration.getMaxBytesLocalOffHeap(), is(MemoryUnit.GIGABYTES.toBytes(12)));
80          assertThat(configuration.getMaxBytesLocalDisk(), is(MemoryUnit.GIGABYTES.toBytes(200)));
81      }
82  
83      /***
84       * Should not give exceptions
85       */
86      @Test
87      public void testValidParameters() {
88          Configuration configuration = new Configuration();
89          CacheConfiguration defaultCache = new CacheConfiguration()
90                  .eternal(false);
91  
92          ConfigurationHelper configurationHelper =
93                  new ConfigurationHelper(manager, configuration);
94          assertNotNull(configurationHelper);
95      }
96  
97      /***
98       * Will fail if all params null
99       */
100     @Test
101     public void testNullParameters() {
102         try {
103             new ConfigurationHelper((CacheManager) null, null);
104             fail();
105         } catch (Exception e) {
106             //expected
107             LOG.debug("Expected exception " + e.getMessage() + ". Initial cause was " + e.getMessage(), e);
108         }
109     }
110 
111     /***
112      * Test the expansion of Java system properties.
113      * These can be mixed in with other path information, in which case they should be expanded and the other
114      * path information catenatated.
115      *
116      * @throws IOException
117      */
118     @Test
119     public void testDiskStorePathExpansion() throws IOException {
120         DiskStoreConfiguration diskStore = new DiskStoreConfiguration();
121 
122         specificPathTest(diskStore, "java.io.tmpdir", "java.io.tmpdir");
123         specificPathTest(diskStore, "java.io.tmpdir/cacheManager1", "java.io.tmpdir");
124         specificPathTest(diskStore, "java.io.tmpdir/cacheManager1/", "java.io.tmpdir");
125         specificPathTest(diskStore, "user.dir", "user.dir");
126         specificPathTest(diskStore, "user.dir/cacheManager1", "user.dir");
127         specificPathTest(diskStore, "user.dir/cacheManager1/", "user.dir");
128         specificPathTest(diskStore, "user.home", "user.home");
129         specificPathTest(diskStore, "user.home/cacheManager1", "user.home");
130         specificPathTest(diskStore, "user.home/cacheManager1/", "user.home");
131         specificPathTest(diskStore, "user.home/cacheManager1/dir1", "user.home");
132 
133         specificPathTest(diskStore, "${java.io.tmpdir}", "java.io.tmpdir");
134         specificPathTest(diskStore, "${java.io.tmpdir}/cacheManager1", "java.io.tmpdir");
135         specificPathTest(diskStore, "${java.io.tmpdir}/cacheManager1/", "java.io.tmpdir");
136         specificPathTest(diskStore, "${user.dir}", "user.dir");
137         specificPathTest(diskStore, "${user.dir}/cacheManager1", "user.dir");
138         specificPathTest(diskStore, "${user.dir}/cacheManager1/", "user.dir");
139         specificPathTest(diskStore, "${user.home}", "user.home");
140         specificPathTest(diskStore, "${user.home}/cacheManager1", "user.home");
141         specificPathTest(diskStore, "${user.home}/cacheManager1/", "user.home");
142         specificPathTest(diskStore, "${user.home}/cacheManager1/dir1", "user.home");
143 
144         System.setProperty("my-special-property", "hello");
145         specificPathTest(diskStore, "${user.home}/cacheManager1/${my-special-property}/world", "user.home", "my-special-property");
146         specificPathTest(diskStore, "user.home/cacheManager1/${my-special-property}/world", "user.home", "my-special-property");
147 
148         System.setProperty("ehcache.disk.store.dir", "/tmp");
149         specificPathTest(diskStore, "ehcache.disk.store.dir/cacheManager1/dir1", "ehcache.disk.store.dir");
150         specificPathTest(diskStore, "${ehcache.disk.store.dir}/cacheManager1/dir1", "ehcache.disk.store.dir");
151     }
152 
153     private void specificPathTest(DiskStoreConfiguration diskStoreConfiguration, String specifiedPath, String ... properties) {
154         diskStoreConfiguration.setPath(specifiedPath);
155         String expandedPath = diskStoreConfiguration.getPath();
156         for (String prop : properties) {
157             assertFalse(expandedPath.contains(prop));
158             assertTrue(expandedPath.contains(System.getProperty(prop)));
159         }
160 
161         File diskDir = null;
162         try {
163             diskDir = new File(expandedPath);
164             diskDir.mkdirs();
165             assertTrue(diskDir.exists());
166             assertTrue(diskDir.isDirectory());
167         } finally {
168             //delete only paths we created, not existing system paths, for repeatability
169             while (diskDir.getPath().indexOf("cacheManager1") != -1) {
170                 diskDir.delete();
171                 diskDir = diskDir.getParentFile();
172             }
173         }
174     }
175 }