001: /**
002: * Copyright 2003-2007 Luck Consulting Pty Ltd
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package net.sf.ehcache;
016:
017: import junit.framework.TestCase;
018:
019: import javax.management.MBeanServer;
020: import javax.management.MBeanServerFactory;
021: import java.io.File;
022: import java.io.IOException;
023: import java.util.List;
024: import java.lang.reflect.Method;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: /**
030: * Common fields and methods required by most test cases
031: *
032: * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
033: * @version $Id: AbstractCacheTest.java 572 2007-12-31 03:13:33Z gregluck $
034: */
035: public abstract class AbstractCacheTest extends TestCase {
036:
037: /**
038: * Where the config is
039: */
040: public static final String SRC_CONFIG_DIR = "src/main/config/";
041:
042: /**
043: * Where the test config is
044: */
045: public static final String TEST_CONFIG_DIR = "src/test/resources/";
046: /**
047: * Where the test classes are compiled.
048: */
049: public static final String TEST_CLASSES_DIR = "target/test-classes/";
050:
051: private static final Log LOG = LogFactory
052: .getLog(AbstractCacheTest.class.getName());
053:
054: /**
055: * name for sample cache 1
056: */
057: protected final String sampleCache1 = "sampleCache1";
058: /**
059: * name for sample cache 2
060: */
061: protected final String sampleCache2 = "sampleCache2";
062: /**
063: * the CacheManager instance
064: */
065: protected CacheManager manager;
066:
067: /**
068: * setup test
069: */
070: protected void setUp() throws Exception {
071: manager = CacheManager.create();
072: }
073:
074: /**
075: * teardown
076: */
077: protected void tearDown() throws Exception {
078: if (manager != null) {
079: manager.shutdown();
080: }
081: }
082:
083: /**
084: * Force the VM to grow to its full size. This stops SoftReferences from being reclaimed in favour of
085: * Heap growth. Only an issue when a VM is cold.
086: */
087: static public void forceVMGrowth() {
088: allocateFiftyMegabytes();
089: System.gc();
090: try {
091: Thread.sleep(200);
092: } catch (InterruptedException e) {
093: //
094: }
095: System.gc();
096: }
097:
098: private static void allocateFiftyMegabytes() {
099: byte[] forceVMGrowth = new byte[50000000];
100: }
101:
102: /**
103: * @param name
104: * @throws IOException
105: */
106: protected void deleteFile(String name) throws IOException {
107: String diskPath = System.getProperty("java.io.tmpdir");
108: final File diskDir = new File(diskPath);
109: File dataFile = new File(diskDir, name + ".data");
110: if (dataFile.exists()) {
111: dataFile.delete();
112: }
113: File indexFile = new File(diskDir, name + ".index");
114: if (indexFile.exists()) {
115: indexFile.delete();
116: }
117: }
118:
119: /**
120: * Measure memory used by the VM.
121: *
122: * @return
123: * @throws InterruptedException
124: */
125: protected long measureMemoryUse() throws InterruptedException {
126: System.gc();
127: Thread.sleep(2000);
128: System.gc();
129: return Runtime.getRuntime().totalMemory()
130: - Runtime.getRuntime().freeMemory();
131: }
132:
133: /**
134: * Runs a set of threads, for a fixed amount of time.
135: */
136: protected void runThreads(final List executables) throws Exception {
137:
138: final long endTime = System.currentTimeMillis() + 10000;
139: final Throwable[] errors = new Throwable[1];
140:
141: // Spin up the threads
142: final Thread[] threads = new Thread[executables.size()];
143: for (int i = 0; i < threads.length; i++) {
144: final Executable executable = (Executable) executables
145: .get(i);
146: threads[i] = new Thread() {
147: public void run() {
148: try {
149: // Run the thread until the given end time
150: while (System.currentTimeMillis() < endTime) {
151: executable.execute();
152: }
153: } catch (Throwable t) {
154: // Hang on to any errors
155: errors[0] = t;
156: LOG.info(t);
157: }
158: }
159: };
160: threads[i].start();
161: }
162:
163: // Wait for the threads to finish
164: for (int i = 0; i < threads.length; i++) {
165: threads[i].join();
166: }
167:
168: // Throw any error that happened
169: if (errors[0] != null) {
170: throw new Exception("Test thread failed.", errors[0]);
171: }
172: }
173:
174: /**
175: * Obtains an MBeanServer, which varies with Java version
176: * @return
177: */
178: public MBeanServer createMBeanServer() {
179: try {
180: Class managementFactoryClass = Class
181: .forName("java.lang.management.ManagementFactory");
182: Method method = managementFactoryClass.getMethod(
183: "getPlatformMBeanServer", null);
184: return (MBeanServer) method.invoke(null, null);
185: } catch (Exception e) {
186: LOG
187: .info(
188: "JDK1.5 ManagementFactory not found. Falling back to JMX1.2.1",
189: e);
190: return MBeanServerFactory.createMBeanServer("SimpleAgent");
191: }
192: }
193:
194: /**
195: * A runnable, that can throw an exception.
196: */
197: protected interface Executable {
198: /**
199: * Executes this object.
200: *
201: * @throws Exception
202: */
203: void execute() throws Exception;
204: }
205:
206: }
|