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.6 2002/02/28 16:37:33 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      Gate.init();
41  
42      String nl = Strings.getNl();
43      String javaSource =
44        "package foo.bar;" + nl +
45        "public class Outer {" + nl +
46        "//let's make an inner class " + nl +
47        " class Adder{" + nl +
48        " public int inc(int i){" + nl +
49        "   return i + 1;" + nl +
50        " }//inc(int)" + nl +
51        " }//class Adder" + nl +
52        " //let's make another inner class" + nl +
53        " class Deccer{" + nl +
54        " public int dec(int i){" + nl +
55        "   return i - 1;" + nl +
56        " }//dec(int)" + nl +
57        " }//clas Deccer" + nl +
58        " //some public methods" + nl +
59        " public int inc(int i){" + nl +
60        "   return new Adder().inc(i);" + nl +
61        " }" + nl +
62        " public int dec(int i){" + nl +
63        "   return new Deccer().dec(i);" + nl +
64        " }" + nl +
65        " }//class Outer" + nl;
66  
67        //load the class
68        Map sources = new HashMap();
69        sources.put("foo.bar.Outer", javaSource);
70        Javac.loadClasses(sources);
71        //try to access the class
72        Class testClass = Gate.getClassLoader().loadClass("foo.bar.Outer");
73        assertNotNull("Could not find decalred class", testClass);
74        Object testInstance = testClass.newInstance();
75        assertNotNull("Could not instantiate declared class", testInstance);
76        Method testMethod =  testClass.getDeclaredMethod(
77                                            "inc",
78                                            new Class[]{int.class});
79        assertNotNull("Could not find declared method", testMethod);
80        Object result = testMethod.invoke(testInstance,
81                                          new Object[]{new Integer(1)});
82        assertEquals("Invalid result", result, new Integer(2));
83  
84        testMethod =  testClass.getDeclaredMethod(
85                                            "dec",
86                                            new Class[]{int.class});
87        assertNotNull("Could not find declared method", testMethod);
88        result = testMethod.invoke(testInstance, new Object[]{new Integer(2)});
89        assertEquals("Invalid result", result, new Integer(1));
90    }
91  
92    public void testCompileError() throws Exception {
93      // disable System.out so that the compiler can't splash its error on screen
94      PrintStream syserr = System.err;
95      System.setErr(new PrintStream(new ByteArrayOutputStream()));
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     }catch(GateException ge){
117       gotException = true;
118     }finally{
119       System.err.flush();
120       // re-enable System.out
121       System.setErr(syserr);
122     }
123     assertTrue("Garbage java code did not raise an exception!",
124                gotException);
125   }
126 
127   /** Debug flag */
128   private static final boolean DEBUG = false;
129 }