|
1 package stx.libjava.tools.compiler.ecj.tests; |
|
2 |
|
3 import static org.junit.Assert.*; |
|
4 |
|
5 import java.lang.reflect.InvocationTargetException; |
|
6 |
|
7 import stx.libjava.tools.compiler.ecj.Compiler; |
|
8 |
|
9 import org.eclipse.jdt.internal.compiler.ClassFile; |
|
10 import org.junit.Test; |
|
11 |
|
12 public class CompilerTests { |
|
13 |
|
14 protected class ClassLoader extends java.lang.ClassLoader { |
|
15 |
|
16 @SuppressWarnings("deprecation") |
|
17 public Class<?> load(byte[] classfile) { |
|
18 return this.defineClass(classfile, 0, classfile.length); |
|
19 } |
|
20 } |
|
21 |
|
22 @Test |
|
23 public void test_01() { |
|
24 Compiler c = new Compiler(); |
|
25 ClassLoader l = new ClassLoader(); |
|
26 |
|
27 c.compile("test.pkg.Foo", "package test.pkg; public class Foo {}"); |
|
28 |
|
29 assertFalse(c.getResult().hasErrors()); |
|
30 |
|
31 ClassFile[] classfiles = c.getResult().getClassFiles(); |
|
32 |
|
33 assertEquals(1, classfiles.length); |
|
34 |
|
35 Class<?> clazz = l.load(classfiles[0].getBytes()); |
|
36 |
|
37 assertEquals("test.pkg.Foo" , clazz.getName()); |
|
38 } |
|
39 |
|
40 |
|
41 /** |
|
42 * Tests compilation of an incomplete class |
|
43 */ |
|
44 @Test |
|
45 public void test_02() { |
|
46 Compiler c = new Compiler(); |
|
47 ClassLoader l = new ClassLoader(); |
|
48 |
|
49 c.compile("test.pkg.Foo", "package test.pkg; public class Foo { public void f() { g(); } }"); |
|
50 ClassFile[] classfiles = c.getResult().getClassFiles(); |
|
51 |
|
52 assertEquals(1, classfiles.length); |
|
53 |
|
54 Class<?> fooClass = l.load(classfiles[0].getBytes()); |
|
55 |
|
56 assertEquals("test.pkg.Foo" , fooClass.getName()); |
|
57 assertEquals(1, fooClass.getDeclaredMethods().length); |
|
58 |
|
59 try { |
|
60 fooClass.getMethod("f").invoke(fooClass.newInstance()); |
|
61 assertTrue(false); |
|
62 } catch (InvocationTargetException ite) { |
|
63 assertTrue(ite.getCause() instanceof Error); |
|
64 } catch (IllegalAccessException | IllegalArgumentException |
|
65 | NoSuchMethodException | SecurityException | InstantiationException e) { |
|
66 e.printStackTrace(); |
|
67 assertTrue(false); |
|
68 } |
|
69 } |
|
70 |
|
71 |
|
72 } |