1   /*
2    * $Id: CompilerErrorTest.java,v 1.6 2004/09/03 04:04:07 cpoirier Exp $
3    * 
4    * Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
5    * 
6    * Redistribution and use of this software and associated documentation
7    * ("Software"), with or without modification, are permitted provided that the
8    * following conditions are met:
9    *  1. Redistributions of source code must retain copyright statements and
10   * notices. Redistributions must also contain a copy of this document.
11   *  2. Redistributions in binary form must reproduce the above copyright
12   * notice, this list of conditions and the following disclaimer in the
13   * documentation and/or other materials provided with the distribution.
14   *  3. The name "groovy" must not be used to endorse or promote products
15   * derived from this Software without prior written permission of The Codehaus.
16   * For written permission, please contact info@codehaus.org.
17   *  4. Products derived from this Software may not be called "groovy" nor may
18   * "groovy" appear in their names without prior written permission of The
19   * Codehaus. "groovy" is a registered trademark of The Codehaus.
20   *  5. Due credit should be given to The Codehaus - http://groovy.codehaus.org/
21   * 
22   * THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS ``AS IS'' AND ANY
23   * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25   * DISCLAIMED. IN NO EVENT SHALL THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR
26   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30   * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32   * DAMAGE.
33   *  
34   */
35  
36  package org.codehaus.groovy.syntax.parser;
37  
38  import groovy.lang.GroovyObject;
39  import groovy.lang.MissingClassException;
40  import groovy.lang.MissingPropertyException;
41  
42  import java.io.ByteArrayInputStream;
43  
44  import org.codehaus.groovy.classgen.TestSupport;
45  import org.codehaus.groovy.control.CompilationFailedException;
46  
47  /***
48   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
49   * @version $Revision: 1.6 $
50   */
51  public class CompilerErrorTest extends TestSupport {
52  
53      public void testUnknownClassCatch() throws Exception {
54          MissingClassException e =
55              assertCompileFailed_WithMCE(
56                  "class UnknownClass {\n"
57                      + "    main() {\n"
58                      + "        try {\n"
59                      + "            println('Hello World!')\n"
60                      + "        }\n"
61                      + "        catch (UnknownException e) {\n"
62                      + "            println('This will never happen')\n"
63                      + "        }\n"
64                      + "    }\n"
65                      + "}\n");
66  
67          assertEquals("UnknownException", e.getType());
68      }
69  
70      public void testUnknownClassInNew() throws Exception {
71          MissingClassException e =
72              assertCompileFailed_WithMCE(
73                  "class UnknownClass {\n" + "    main() {\n" + "        x = new UnknownThingy()\n" + "    }\n" + "}\n");
74          assertEquals("UnknownThingy", e.getType());
75      }
76  
77      public void testUnknownClassInAssignment() throws Exception {
78          GroovyObject object =
79              assertCompileWorks(
80                  "class UnknownClass {\n" + "    main() {\n" + "        x = UnknownThingy\n" + "    }\n" + "}\n");
81  
82          try {
83              object.invokeMethod("main", null);
84              fail("Should have thrown exception due to unknown property");
85          }
86          catch (MissingPropertyException e) {
87              assertEquals("UnknownThingy", e.getProperty());
88          }
89          /*
90          catch (NoClassDefFoundError e) {
91          }
92          */
93      }
94  
95  
96  
97      public void testUnterminatedConstantGString() throws Exception {
98          assertCompileFailed( "println \"d" );
99      }
100 
101     public void testUnterminatedGString() throws Exception {
102         assertCompileFailed( "println \"${1+2\"\nprintln \"c\"" );
103     }
104 
105 
106 
107 
108 
109     protected GroovyObject assertCompileWorks(String code) throws Exception {
110         Class type =
111             loader.parseClass(new ByteArrayInputStream(code.getBytes()), "ValidClass_" + getMethodName() + ".groovy");
112         return (GroovyObject) type.newInstance();
113     }
114 
115     protected MissingClassException assertCompileFailed_WithMCE(String code) throws Exception {
116         try {
117             assertCompileWorks(code);
118 
119             fail("Should have thrown an exception");
120         }
121         catch( CompilationFailedException e ) {
122             Exception cause = e.getUnit().getException(0);
123             if( cause instanceof MissingClassException ) {
124                 System.out.println("Worked, threw: " + cause);
125                 //e.printStackTrace();
126                 return (MissingClassException)cause;
127             }
128             throw e;
129         }
130         return null;
131     }
132 
133     protected CompilationFailedException assertCompileFailed(String code) throws Exception {
134         try {
135             assertCompileWorks(code);
136  
137             fail("Should have thrown an exception");
138         }
139         catch( CompilationFailedException e ) {
140             return e;
141         }
142 
143         return null;
144     }
145 
146 }