KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > junit > JUnitErrorModelTest


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.model.junit;
35
36 import edu.rice.cs.drjava.model.GlobalModelTestCase;
37 import edu.rice.cs.drjava.model.OpenDefinitionsDocument;
38 import edu.rice.cs.util.swing.Utilities;
39
40 import java.io.File JavaDoc;
41
42 /**
43  * A test on the GlobalModel for JUnit testing.
44  *
45  * @version $Id: JUnitErrorModelTest.java 3839 2006-05-14 20:28:51Z rcartwright $
46  */

47 public final class JUnitErrorModelTest extends GlobalModelTestCase {
48
49   private JUnitErrorModel _m;
50   
51   private static final String JavaDoc MONKEYTEST_FAIL_TEXT =
52     "import junit.framework.*; \n" +
53     "import java.io.*; \n" +
54     "public class MonkeyTestFail extends TestCase { \n" +
55     " public MonkeyTestFail(String name) { super(name); } \n" +
56     " public void testShouldFail() { \n" +
57     " assertEquals(\"monkey\", \"baboon\"); \n" +
58     " } \n" +
59     " public void testShouldErr() throws Exception { \n" +
60     " throw new IOException(\"Error\"); \n" +
61     " } \n" +
62     "}";
63
64   private static final String JavaDoc TEST_ONE =
65     "import junit.framework.TestCase;\n" +
66     "public class TestOne extends TestCase {\n" +
67     " public void testMyMethod() {\n" +
68     " assertTrue(false);\n" +
69     " }\n" +
70     " public TestOne() {\n" +
71     " super();\n" +
72     " }\n" +
73     " public java.lang.String toString() {\n" +
74     " return \"TestOne(\" + \")\";\n" +
75     " }\n" +
76     " public boolean equals(java.lang.Object o) {\n" +
77     " if ((o == null) || getClass() != o.getClass()) return false;\n" +
78     " return true;\n" +
79     " }\n" +
80     " public int hashCode() {\n" +
81     " return getClass().hashCode();\n" +
82     " }\n" +
83     " public void testThrowing() throws Exception{\n" +
84     " throw new Exception(\"here\");\n" +
85     " }\n" +
86     " public void testFail(){\n" +
87     " fail(\"i just failed the test\");\n" +
88     " }\n" +
89     "}";
90
91   private static final String JavaDoc TEST_TWO =
92     "import junit.framework.TestCase;\n" +
93     "public class TestTwo extends TestOne {\n" +
94     " public void testTwo() {\n" +
95     " assertTrue(true);\n" +
96     " }\n" +
97     " public TestTwo() {\n" +
98     " super();\n" +
99     " }\n" +
100     " public java.lang.String toString() {\n" +
101     " return \"TestTwo(\" + \")\";\n" +
102     " }\n" +
103     " public boolean equals(java.lang.Object o) {\n" +
104     " if ((o == null) || getClass() != o.getClass()) return false;\n" +
105     " return true;\n" +
106     " }\n" +
107     " public int hashCode() {\n" +
108     " return getClass().hashCode();\n" +
109     " }\n" +
110     "}";
111
112 // private static final String NONPUBLIC_TEXT =
113
// "import junit.framework.*; " +
114
// "public class NonPublic extends TestCase { " +
115
// " public NonPublic(String name) { super(name); } " +
116
// " void testShouldFail() { " +
117
// " assertEquals(\"monkey\", \"baboon\"); " +
118
// " } " +
119
// "}";
120

121   private static final String JavaDoc ABC_CLASS_ONE =
122     "class ABC extends java.util.Vector {}\n";
123
124   private static final String JavaDoc ABC_CLASS_TWO =
125     "class ABC extends java.util.ArrayList {}\n";
126
127   private static final String JavaDoc ABC_TEST =
128     "public class ABCTest extends junit.framework.TestCase {\n" +
129     " public void testABC() {\n" +
130     " new ABC().get(0);\n" +
131     " }\n" +
132     "}";
133
134   private static final String JavaDoc LANGUAGE_LEVEL_TEST =
135     "class MyTest extends junit.framework.TestCase {\n"+
136     " void testMyMethod() {\n"+
137     " assertEquals(\"OneString\", \"TwoStrings\");\n"+
138     " }\n"+
139     "}\n";
140
141   /** Tests that the errors array contains all encountered failures and error in the right order. */
142   public void testErrorsArrayInOrder() throws Exception JavaDoc {
143     _m = new JUnitErrorModel(new JUnitError[0], _model, false);
144     OpenDefinitionsDocument doc = setupDocument(MONKEYTEST_FAIL_TEXT);
145     final File JavaDoc file = new File JavaDoc(_tempDir, "MonkeyTestFail.java");
146     doc.saveFile(new FileSelector(file));
147
148     JUnitTestListener listener = new JUnitTestListener();
149     _model.addListener(listener);
150     
151     doc.startCompile();
152     listener.waitCompileDone();
153     if (_model.getCompilerModel().getNumErrors() > 0) fail("compile failed: " + getCompilerErrorString());
154     listener.checkCompileOccurred();
155     
156     listener.runJUnit(doc);
157     
158     listener.assertJUnitStartCount(1);
159     // Clear document so we can make sure it's written to after startJUnit
160
_model.getJUnitModel().getJUnitDocument().remove
161       (0, _model.getJUnitModel().getJUnitDocument().getLength() - 1);
162     //final TestResult testResults = doc.startJUnit();
163

164     //_m = new JUnitErrorModel(doc.getDocument(), "MonkeyTestFail", testResults);
165
_m = _model.getJUnitModel().getJUnitErrorModel();
166
167     //JUnitError[] errorsWithPositions = _m.getErrorsWithPositions();
168
//JUnitError[] errorsWithoutPositions = _m.getErrorsWithoutPositions();
169
//assertTrue("testResults should not be null", testResults != null);
170

171     assertEquals("the test results should have one error and one failure "+_m.getNumErrors(), 2, _m.getNumErrors());
172
173     assertEquals("test case has one error reported" + _m.getError(0).message(), _m.getError(0).isWarning(), false);
174
175     assertEquals("test case has one failure reported" + _m.getError(1).message(), _m.getError(1).isWarning(), true);
176     //_model.setResetAfterCompile(true);
177
}
178
179   /**
180    * Tests that a VerifyError is reported as an error, rather than
181    * simply causing JUnit to blow up. Note that this test will hang if
182    * the error is not reported correctly, because the JUnitTestManager will
183    * blow up in the other JVM and never notify us that it's finished.
184    */

185   public void testVerifyErrorHandledCorrectly() throws Exception JavaDoc {
186     OpenDefinitionsDocument doc = setupDocument(ABC_CLASS_ONE);
187     final File JavaDoc file = new File JavaDoc(_tempDir, "ABC1.java");
188     doc.saveFile(new FileSelector(file));
189
190     OpenDefinitionsDocument doc2 = setupDocument(ABC_TEST);
191     final File JavaDoc file2 = new File JavaDoc(_tempDir, "ABCTest.java");
192     doc2.saveFile(new FileSelector(file2));
193
194     // Compile the correct ABC and the test
195
// JUnitTestListener listener = new JUnitTestListener(false);
196
// System.out.println("compiling all");
197
_model.getCompilerModel().compileAll();
198
199     OpenDefinitionsDocument doc3 = setupDocument(ABC_CLASS_TWO);
200     final File JavaDoc file3 = new File JavaDoc(_tempDir, "ABC2.java");
201     doc3.saveFile(new FileSelector(file3));
202
203     JUnitTestListener listener = new JUnitNonTestListener();
204     // Compile the incorrect ABC
205
// System.out.println("compiling doc3");
206
doc3.startCompile();
207     if (_model.getCompilerModel().getNumErrors() > 0) {
208       fail("compile failed: " + getCompilerErrorString());
209     }
210     _model.addListener(listener);
211     // Run the test: a VerifyError will be thrown in Java 1.4
212
// JUnitTestListener listener2 = new JUnitTestListener();
213
// _model.addListener(listener2);
214

215     listener.assertClassFileErrorCount(0);
216     listener.runJUnit(doc2);
217     listener.waitJUnitDone();
218     
219     double version = Double.valueOf(System.getProperty("java.specification.version"));
220     if (version < 1.5) listener.assertClassFileErrorCount(1);
221     else
222       assertEquals("Should report one error", 1, _model.getJUnitModel().getJUnitErrorModel().getNumErrors());
223     
224     _model.removeListener(listener);
225   }
226
227
228   /** Tests that an elementary level file has the previous line of the actual error reported as the line of its error.
229    * Necessitated by the added code in the .java file associated with the .dj0 file (the import statement added by the
230    * language level compiler)
231    */

232
233   public void testLanguageLevelJUnitErrorLine() throws Exception JavaDoc {
234     
235     _m = new JUnitErrorModel(new JUnitError[0], _model, false);
236     OpenDefinitionsDocument doc = setupDocument(LANGUAGE_LEVEL_TEST);
237     final File JavaDoc file = new File JavaDoc(_tempDir, "MyTest.dj0");
238     doc.saveFile(new FileSelector(file));
239
240     JUnitTestListener listener = new JUnitTestListener();
241     _model.addListener(listener);
242     
243
244     doc.startCompile();
245     listener.waitCompileDone();
246     if (_model.getCompilerModel().getNumErrors() > 0) {
247       fail("compile failed: " + getCompilerErrorString());
248     }
249     listener.checkCompileOccurred();
250     
251     listener.runJUnit(doc);
252     
253     listener.assertJUnitStartCount(1);
254
255     // Clear document so we can make sure it's written to after startJUnit
256
_model.getJUnitModel().getJUnitDocument().remove(0, _model.getJUnitModel().getJUnitDocument().getLength() - 1);
257
258     _m = _model.getJUnitModel().getJUnitErrorModel();
259
260     assertEquals("the test results should have one failure "+_m.getNumErrors(), 1, _m.getNumErrors());
261
262     assertEquals("the error line should be line number 2", 2, _m.getError(0).lineNumber());
263     
264   }
265
266
267   /** Test errors that occur in superclass. */
268   public void testErrorInSuperClass() throws Exception JavaDoc {
269     OpenDefinitionsDocument doc1 = setupDocument(TEST_ONE);
270     OpenDefinitionsDocument doc2 = setupDocument(TEST_TWO);
271     final File JavaDoc file1 = new File JavaDoc(_tempDir, "TestOne.java");
272     final File JavaDoc file2 = new File JavaDoc(_tempDir, "TestTwo.java");
273     doc1.saveFile(new FileSelector(file1));
274     doc2.saveFile(new FileSelector(file2));
275     
276     JUnitTestListener listener = new JUnitTestListener();
277     _model.addListener(listener);
278     _model.getCompilerModel().compileAll();
279 // doc1.startCompile();
280
// doc2.startCompile();
281

282     
283     listener.waitCompileDone();
284     listener.runJUnit(doc1);
285     
286     listener.assertJUnitStartCount(1);
287     
288     _m = _model.getJUnitModel().getJUnitErrorModel();
289     
290     assertEquals("test case has one error reported", 3, _m.getNumErrors());
291     assertTrue("first error should be an error not a warning", !_m.getError(0).isWarning());
292
293     assertTrue("it's a junit error", _m.getError(0) instanceof JUnitError);
294
295     assertEquals("The first error is on line 5", 3, _m.getError(0).lineNumber());
296     assertEquals("The first error is on line 5", 19, _m.getError(1).lineNumber());
297     assertEquals("The first error is on line 5", 22, _m.getError(2).lineNumber());
298     
299     listener.runJUnit(doc2);
300     
301     
302     listener.assertJUnitStartCount(2);
303     
304     assertEquals("test case has one error reported", 3, _m.getNumErrors());
305     assertTrue("first error should be an error not a warning", !_m.getError(0).isWarning());
306     assertEquals("The first error is on line 5", 3, _m.getError(0).lineNumber());
307     assertEquals("The first error is on line 5", 19, _m.getError(1).lineNumber());
308     assertEquals("The first error is on line 5", 22, _m.getError(2).lineNumber());
309
310     _model.removeListener(listener);
311     
312   }
313 }
314
315
Popular Tags