View Javadoc

1   /**
2    *  Copyright 2003-2006 Greg Luck
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  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  import java.io.File;
25  import java.io.IOException;
26  
27  /**
28   * Tests programmatically constructed Configuration instances
29   *
30   * @author Greg Luck
31   * @version $Id: ConfigurationHelperTest.java 30 2006-04-16 09:23:03Z gregluck $
32   */
33  public class ConfigurationHelperTest extends AbstractCacheTest {
34  
35      private static final Log LOG = LogFactory.getLog(ConfigurationHelperTest.class.getName());
36  
37  
38      /**
39       * Should not give exceptions
40       */
41      public void testValidParameters() {
42          Configuration configuration = new Configuration();
43          CacheConfiguration defaultCache = new CacheConfiguration();
44          defaultCache.setEternal(false);
45  
46          ConfigurationHelper configurationHelper =
47                  new ConfigurationHelper(manager, configuration);
48          assertNotNull(configurationHelper);
49      }
50  
51      /**
52       * Will fail if all params null
53       */
54      public void testNullParameters() {
55          try {
56              new ConfigurationHelper((CacheManager) null, null);
57              fail();
58          } catch (Exception e) {
59              //expected
60              LOG.debug("Expected exception " + e.getMessage() + ". Initial cause was " + e.getMessage(), e);
61          }
62      }
63  
64      /**
65       * Test the expansion of Java system properties.
66       * These can be mixed in with other path information, in which case they should be expanded and the other
67       * path information catenatated.
68       * @throws IOException
69       */
70      public void testDiskStorePathExpansion() throws IOException {
71          DiskStoreConfiguration diskStore = new DiskStoreConfiguration();
72  
73          specificPathTest(diskStore, "java.io.tmpdir", "java.io.tmpdir");
74          specificPathTest(diskStore, "java.io.tmpdir/cacheManager1", "java.io.tmpdir");
75          specificPathTest(diskStore, "java.io.tmpdir/cacheManager1/", "java.io.tmpdir");
76          specificPathTest(diskStore, "user.dir", "user.dir");
77          specificPathTest(diskStore, "user.dir/cacheManager1", "user.dir");
78          specificPathTest(diskStore, "user.dir/cacheManager1/", "user.dir");
79          specificPathTest(diskStore, "user.home", "user.home");
80          specificPathTest(diskStore, "user.home/cacheManager1", "user.home");
81          specificPathTest(diskStore, "user.home/cacheManager1/", "user.home");
82          specificPathTest(diskStore, "user.home/cacheManager1/dir1", "user.home");
83  
84  
85      }
86  
87      private void specificPathTest(DiskStoreConfiguration diskStore, String specifiedPath, String systemProperty) {
88          diskStore.setPath(specifiedPath);
89          String expandedPath = diskStore.getPath();
90          assertTrue(expandedPath.indexOf(systemProperty) == -1);
91  
92          File diskDir = null;
93          try {
94              diskDir = new File(expandedPath);
95              diskDir.mkdirs();
96              assertTrue(diskDir.exists());
97              assertTrue(diskDir.isDirectory());
98          } finally {
99              //delete only paths we created, not existing system paths, for repeatability
100             if (diskDir.getPath().indexOf("cacheManager1") != -1) {
101                 diskDir.delete();
102             }
103         }
104     }
105 }