1   /*
2    *
3    *  Copyright (c) 1998-2001, The University of Sheffield.
4    *
5    *  This file is part of GATE (see http://gate.ac.uk/), and is free
6    *  software, licenced under the GNU Library General Public License,
7    *  Version 2, June 1991 (in the distribution as file licence.html,
8    *  and also available at http://gate.ac.uk/gate/licence.html).
9    *
10   *  Valentin Tablan, 26/Feb/2002
11   *
12   *  $Id: TestJavac.java,v 1.10 2003/05/21 14:30:17 valyt Exp $
13   */
14  
15  package gate.util;
16  
17  import java.util.*;
18  import java.io.*;
19  import junit.framework.*;
20  import java.lang.reflect.*;
21  
22  import gate.*;
23  import gate.util.*;
24  
25  public class TestJavac extends TestCase{
26    /** Construction */
27    public TestJavac(String name) { super(name); }
28  
29   /** Fixture set up */
30    public void setUp() {
31    } // setUp
32  
33    /** Test suite routine for the test runner */
34    public static Test suite() {
35      return new TestSuite(TestJavac.class);
36    } // suite
37  
38   /** Jdk compiler */
39    public void testCompiler() throws Exception {
40  
41      String nl = Strings.getNl();
42      String javaSource =
43        "package foo.bar;" + nl +
44        "public class Outer {" + nl +
45        "//let's make an inner class " + nl +
46        " class Adder{" + nl +
47        " public int inc(int i){" + nl +
48        "   return i + 1;" + nl +
49        " }//inc(int)" + nl +
50        " }//class Adder" + nl +
51        " //let's make another inner class" + nl +
52        " class Deccer{" + nl +
53        " public int dec(int i){" + nl +
54        "   return i - 1;" + nl +
55        " }//dec(int)" + nl +
56        " }//clas Deccer" + nl +
57        " //some public methods" + nl +
58        " public int inc(int i){" + nl +
59        "   return new Adder().inc(i);" + nl +
60        " }" + nl +
61        " public int dec(int i){" + nl +
62        "   return new Deccer().dec(i);" + nl +
63        " }" + nl +
64        " }//class Outer" + nl;
65  
66        //load the class
67        Map sources = new HashMap();
68        sources.put("foo.bar.Outer", javaSource);
69        Javac.loadClasses(sources);
70        //try to access the class
71        Class testClass = Gate.getClassLoader().loadClass("foo.bar.Outer");
72        assertNotNull("Could not find decalred class", testClass);
73        Object testInstance = testClass.newInstance();
74        assertNotNull("Could not instantiate declared class", testInstance);
75        Method testMethod =  testClass.getDeclaredMethod(
76                                            "inc",
77                                            new Class[]{int.class});
78        assertNotNull("Could not find declared method", testMethod);
79        Object result = testMethod.invoke(testInstance,
80                                          new Object[]{new Integer(1)});
81        assertEquals("Invalid result", result, new Integer(2));
82  
83        testMethod =  testClass.getDeclaredMethod(
84                                            "dec",
85                                            new Class[]{int.class});
86        assertNotNull("Could not find declared method", testMethod);
87        result = testMethod.invoke(testInstance, new Object[]{new Integer(2)});
88        assertEquals("Invalid result", result, new Integer(1));
89    }
90  
91    public void testCompileError() throws Exception {
92  //    disable System.out so that the compiler can't splash its error on screen
93  //    PrintStream syserr = System.err;
94  //    PrintStream newSyserr = new PrintStream(new ByteArrayOutputStream());
95  //    System.setErr(newSyserr);
96  
97      String nl = Strings.getNl();
98      String javaSource =
99          "package foo.bar;" + nl +
100         "public class X {" + nl +
101         " //some public methods" + nl +
102         " public void foo(){" + nl +
103         " String nullStr = null;" + nl +
104         " nullStr = 123;" + nl +
105         "} " + nl +
106         " " + nl +
107         " " + nl +
108         " }//class Outer" + nl;
109 
110     //load the class
111     Map sources = new HashMap();
112     sources.put("foo.bar.X", javaSource);
113     boolean gotException = false;
114     try {
115       Javac.loadClasses(sources);
116     }
117     catch (GateException ge) {
118       gotException = true;
119     }
120     finally {
121 //      newSyserr.flush();
122 //      // re-enable System.out
123 //      System.setErr(syserr);
124 //      newSyserr.close();
125     }
126     assertTrue("Garbage java code did not raise an exception!",
127                gotException);
128   }
129 
130 
131   /** Debug flag */
132   private static final boolean DEBUG = false;
133 }