1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.microemu.app.classloader;
28
29 import java.io.InputStream;
30 import java.net.URL;
31 import java.net.URLClassLoader;
32
33 import junit.framework.TestCase;
34
35 import org.microemu.Injected;
36 import org.microemu.app.util.EventCatureLoggerAppender;
37 import org.microemu.app.util.IOUtils;
38 import org.microemu.app.util.MIDletResourceLoader;
39 import org.microemu.app.util.MIDletSystemProperties;
40 import org.microemu.app.util.MIDletThread;
41 import org.microemu.app.util.MIDletTimer;
42 import org.microemu.log.Logger;
43 import org.microemu.log.LoggingEvent;
44
45
46
47
48
49 public class MIDletClassLoaderTest extends TestCase {
50
51 public static final String TEST_APP_JAR = "bytecode-test-app.jar";
52
53 public static final String TEST_CLASS = "org.TestMain";
54
55 EventCatureLoggerAppender capture;
56
57 private boolean enhanceCatchBlockSave;
58
59 protected void setUp() throws Exception {
60 super.setUp();
61 capture = new EventCatureLoggerAppender();
62 Logger.addAppender(capture);
63 enhanceCatchBlockSave = MIDletClassLoader.enhanceCatchBlock;
64 }
65
66 protected void tearDown() throws Exception {
67 super.tearDown();
68 Logger.removeAppender(capture);
69 MIDletClassLoader.enhanceCatchBlock = enhanceCatchBlockSave;
70 }
71
72 public void testGetResourceAsStream() throws Exception {
73
74 ClassLoader parent = MIDletClassLoaderTest.class.getClassLoader();
75
76 URL jarURL = parent.getResource(TEST_APP_JAR);
77 assertNotNull("Can't find app jar", jarURL);
78
79 URLClassLoader ucl = new URLClassLoader(new URL[] { jarURL });
80
81 final String testFile = "META-INF/MANIFEST.MF";
82
83 InputStream is = null;
84 try {
85 is = ucl.getResourceAsStream(testFile);
86 assertNotNull("URLClassLoader", is);
87 } finally {
88 IOUtils.closeQuietly(is);
89 }
90
91 MIDletClassLoader mcl = new MIDletClassLoader(parent);
92 mcl.addURL(jarURL);
93 try {
94 is = ucl.getResourceAsStream(testFile);
95 assertNotNull("MIDletClassLoader", is);
96 } finally {
97 IOUtils.closeQuietly(is);
98 }
99
100 }
101
102 public void testApplication() throws Exception {
103 ClassLoader parent = MIDletClassLoaderTest.class.getClassLoader();
104 URL jarURL = parent.getResource(TEST_APP_JAR);
105 assertNotNull("Can't find app jar", jarURL);
106
107 System.setProperty("test.verbose", "1");
108
109 MIDletSystemProperties.setProperty("test.property1", "1");
110 MIDletSystemProperties.setProperty("microedition.platform", null);
111
112 MIDletResourceLoader.traceResourceLoading = true;
113 MIDletClassLoader.enhanceCatchBlock = false;
114 MIDletClassLoader mcl = new MIDletClassLoader(parent);
115
116 MIDletClassLoaderConfig clConfig = new MIDletClassLoaderConfig();
117 clConfig.setDelegationType("strict");
118 mcl.configure(clConfig, false);
119 mcl.disableClassPreporcessing(Injected.class);
120 MIDletResourceLoader.classLoader = mcl;
121 mcl.addURL(jarURL);
122
123 Class instrumentedClass = mcl.loadClass(TEST_CLASS);
124 Runnable instrumentedInstance = (Runnable) instrumentedClass.newInstance();
125 instrumentedInstance.run();
126
127 LoggingEvent lastEvent = capture.getLastEvent();
128 assertNotNull("got event", lastEvent);
129 assertEquals("All tests OK", lastEvent.getMessage());
130 StackTraceElement ste = lastEvent.getLocation();
131 assertEquals("MethodName", "run", ste.getMethodName());
132 assertEquals("ClassName", TEST_CLASS, ste.getClassName());
133
134 }
135
136 private void runEnhanceCatchBlock(MIDletClassLoader mcl, String name) throws Exception {
137 Class instrumentedClass = mcl.loadClass(name);
138 Runnable instrumentedInstance = (Runnable) instrumentedClass.newInstance();
139 instrumentedInstance.run();
140
141 LoggingEvent lastEvent = capture.getLastEvent();
142 assertNotNull("got event", lastEvent);
143 assertNotNull("got message", lastEvent.getMessage());
144 System.out.println("[" + lastEvent.getMessage() + "]");
145 assertTrue("error message", lastEvent.getMessage().indexOf("MIDlet caught") != -1);
146 }
147
148 public void x_testEnhanceCatchBlock() throws Exception {
149 ClassLoader parent = MIDletClassLoaderTest.class.getClassLoader();
150 URL jarURL = parent.getResource(TEST_APP_JAR);
151 assertNotNull("Can't find app jar", jarURL);
152
153 System.setProperty("test.verbose", "1");
154
155 MIDletClassLoader.enhanceCatchBlock = true;
156 MIDletClassLoader mcl = new MIDletClassLoader(parent);
157 mcl.disableClassPreporcessing(Injected.class);
158 mcl.addURL(jarURL);
159 runEnhanceCatchBlock(mcl, "org.catchBlock.CatchThrowable");
160 }
161
162 public void testTimer() throws Exception {
163 ClassLoader parent = MIDletClassLoaderTest.class.getClassLoader();
164 URL jarURL = parent.getResource(TEST_APP_JAR);
165 assertNotNull("Can't find app jar", jarURL);
166
167 System.setProperty("test.verbose", "1");
168
169 MIDletClassLoader mcl = new MIDletClassLoader(parent);
170 mcl.disableClassPreporcessing(Injected.class);
171 mcl.disableClassPreporcessing(MIDletThread.class);
172 mcl.disableClassPreporcessing(MIDletTimer.class);
173 mcl.addURL(jarURL);
174
175 Class instrumentedClass = mcl.loadClass("org.TimerCreationRunner");
176 Runnable instrumentedInstance = (Runnable) instrumentedClass.newInstance();
177 instrumentedInstance.run();
178 }
179
180 public void testTimerCancell() throws Exception {
181 ClassLoader parent = MIDletClassLoaderTest.class.getClassLoader();
182 URL jarURL = parent.getResource(TEST_APP_JAR);
183 assertNotNull("Can't find app jar", jarURL);
184
185 System.setProperty("test.verbose", "1");
186
187 MIDletClassLoader mcl = new MIDletClassLoader(parent);
188 mcl.disableClassPreporcessing(Injected.class);
189 mcl.disableClassPreporcessing(MIDletThread.class);
190 mcl.disableClassPreporcessing(MIDletTimer.class);
191 mcl.addURL(jarURL);
192
193 Class instrumentedClass = mcl.loadClass("org.TimerTaskCancelRunner");
194 Runnable instrumentedInstance = (Runnable) instrumentedClass.newInstance();
195 instrumentedInstance.run();
196 }
197 }