1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.ehcache;
18
19 import junit.framework.TestCase;
20
21 import javax.management.MBeanServer;
22 import javax.management.MBeanServerFactory;
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.List;
26 import java.lang.reflect.Method;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31
32
33
34
35
36
37 public abstract class AbstractCacheTest extends TestCase {
38
39
40
41
42 public static final String SRC_CONFIG_DIR = "src/main/config/";
43
44
45
46
47 public static final String TEST_CONFIG_DIR = "src/test/resources/";
48
49
50
51 public static final String TEST_CLASSES_DIR = "target/test-classes/";
52
53
54 private static final Log LOG = LogFactory.getLog(AbstractCacheTest.class.getName());
55
56
57
58
59 protected final String sampleCache1 = "sampleCache1";
60
61
62
63 protected final String sampleCache2 = "sampleCache2";
64
65
66
67 protected CacheManager manager;
68
69
70
71
72 protected void setUp() throws Exception {
73 manager = CacheManager.create();
74 }
75
76
77
78
79 protected void tearDown() throws Exception {
80 if (manager != null) {
81 manager.shutdown();
82 }
83 }
84
85
86
87
88
89
90 static public void forceVMGrowth() {
91 allocateFiftyMegabytes();
92 System.gc();
93 try {
94 Thread.sleep(200);
95 } catch (InterruptedException e) {
96
97 }
98 System.gc();
99 }
100
101 private static void allocateFiftyMegabytes() {
102 byte[] forceVMGrowth = new byte[50000000];
103 }
104
105
106
107
108
109
110 protected void deleteFile(String name) throws IOException {
111 String diskPath = System.getProperty("java.io.tmpdir");
112 final File diskDir = new File(diskPath);
113 File dataFile = new File(diskDir, name + ".data");
114 if (dataFile.exists()) {
115 dataFile.delete();
116 }
117 File indexFile = new File(diskDir, name + ".index");
118 if (indexFile.exists()) {
119 indexFile.delete();
120 }
121 }
122
123
124
125
126
127
128
129 protected long measureMemoryUse() throws InterruptedException {
130 System.gc();
131 Thread.sleep(2000);
132 System.gc();
133 return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
134 }
135
136
137
138
139 protected void runThreads(final List executables) throws Exception {
140
141 final long endTime = System.currentTimeMillis() + 10000;
142 final Throwable[] errors = new Throwable[1];
143
144
145 final Thread[] threads = new Thread[executables.size()];
146 for (int i = 0; i < threads.length; i++) {
147 final Executable executable = (Executable) executables.get(i);
148 threads[i] = new Thread() {
149 public void run() {
150 try {
151
152 while (System.currentTimeMillis() < endTime) {
153 executable.execute();
154 }
155 } catch (Throwable t) {
156
157 errors[0] = t;
158 }
159 }
160 };
161 threads[i].start();
162 }
163
164
165 for (int i = 0; i < threads.length; i++) {
166 threads[i].join();
167 }
168
169
170 if (errors[0] != null) {
171 throw new Exception("Test thread failed.", errors[0]);
172 }
173 }
174
175
176
177
178
179 public MBeanServer createMBeanServer() {
180 try {
181 Class managementFactoryClass = Class.forName("java.lang.management.ManagementFactory");
182 Method method = managementFactoryClass.getMethod("getPlatformMBeanServer", null);
183 return (MBeanServer) method.invoke(null, null);
184 } catch (Exception e) {
185 LOG.info("JDK1.5 ManagementFactory not found. Falling back to JMX1.2.1", e);
186 return MBeanServerFactory.createMBeanServer("SimpleAgent");
187 }
188 }
189
190
191
192
193
194 protected interface Executable {
195
196
197
198
199
200 void execute() throws Exception;
201 }
202
203 }