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;
18  
19  import junit.framework.TestCase;
20  
21  import java.io.IOException;
22  import java.io.File;
23  
24  /**
25   * Common fields and methods required by most test cases
26   *
27   * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
28   * @version $Id: AbstractCacheTest.java 28 2006-04-15 05:12:32Z gregluck $
29   */
30  public abstract class AbstractCacheTest extends TestCase {
31  
32      /**
33       * Where the config is
34       */
35      public static final String SRC_CONFIG_DIR = "src/main/config/";
36      /**
37       * Where the test config is
38       */
39      public static final String TEST_CONFIG_DIR = "src/test/resources/";
40  
41  
42      /**
43       * Where the test classes are compiled.
44       */
45      public static final String TEST_CLASSES_DIR = "target/test-classes/";
46  
47      /**
48       * name for sample cache 1
49       */
50      protected final String sampleCache1 = "sampleCache1";
51      /**
52       * name for sample cache 2
53       */
54      protected final String sampleCache2 = "sampleCache2";
55      /**
56       * the CacheManager instance
57       */
58      protected CacheManager manager;
59  
60      /**
61       * setup test
62       */
63      protected void setUp() throws Exception {
64          manager = CacheManager.create();
65      }
66  
67      /**
68       * teardown
69       */
70      protected void tearDown() throws Exception {
71          manager.shutdown();
72      }
73  
74  
75      /**
76       * @param name
77       * @throws IOException
78       */
79      protected void deleteFile(String name) throws IOException {
80          String diskPath = System.getProperty("java.io.tmpdir");
81          final File diskDir = new File(diskPath);
82          File dataFile = new File(diskDir, name + ".data");
83          if (dataFile.exists()) {
84              dataFile.delete();
85          }
86          File indexFile = new File(diskDir, name + ".index");
87          if (indexFile.exists()) {
88              indexFile.delete();
89          }
90      }
91  
92      /**
93       * Measure memory used by the VM.
94       *
95       * @return
96       * @throws InterruptedException
97       */
98      protected long measureMemoryUse() throws InterruptedException {
99          System.gc();
100         Thread.sleep(3000);
101         System.gc();
102         return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
103     }
104 }