001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model.junit;
038:
039: import edu.rice.cs.drjava.model.GlobalModelTestCase;
040: import edu.rice.cs.drjava.model.OpenDefinitionsDocument;
041: import edu.rice.cs.util.swing.Utilities;
042:
043: import java.io.File;
044:
045: import static edu.rice.cs.plt.debug.DebugUtil.debug;
046:
047: /**
048: * A test on the GlobalModel for JUnit testing.
049: *
050: * @version $Id: JUnitErrorModelTest.java 4255 2007-08-28 19:17:37Z mgricken $
051: */
052: public final class JUnitErrorModelTest extends GlobalModelTestCase {
053:
054: private JUnitErrorModel _m;
055:
056: private static final String MONKEYTEST_FAIL_TEXT = "import junit.framework.*; \n"
057: + "import java.io.*; \n"
058: + "public class MonkeyTestFail extends TestCase { \n"
059: + " public MonkeyTestFail(String name) { super(name); } \n"
060: + " public void testShouldFail() { \n"
061: + " assertEquals(\"monkey\", \"baboon\"); \n"
062: + " } \n"
063: + " public void testShouldErr() throws Exception { \n"
064: + " throw new IOException(\"Error\"); \n"
065: + " } \n"
066: + "}";
067:
068: private static final String TEST_ONE = "import junit.framework.TestCase;\n"
069: + "public class TestOne extends TestCase {\n"
070: + " public void testMyMethod() {\n"
071: + " assertTrue(false);\n"
072: + " }\n"
073: + " public TestOne() {\n"
074: + " super();\n"
075: + " }\n"
076: + " public java.lang.String toString() {\n"
077: + " return \"TestOne(\" + \")\";\n"
078: + " }\n"
079: + " public boolean equals(java.lang.Object o) {\n"
080: + " if ((o == null) || getClass() != o.getClass()) return false;\n"
081: + " return true;\n"
082: + " }\n"
083: + " public int hashCode() {\n"
084: + " return getClass().hashCode();\n"
085: + " }\n"
086: + " public void testThrowing() throws Exception{\n"
087: + " throw new Exception(\"here\");\n"
088: + " }\n"
089: + " public void testFail(){\n"
090: + " fail(\"i just failed the test\");\n" + " }\n" + "}";
091:
092: private static final String TEST_TWO = "import junit.framework.TestCase;\n"
093: + "public class TestTwo extends TestOne {\n"
094: + " public void testTwo() {\n"
095: + " assertTrue(true);\n"
096: + " }\n"
097: + " public TestTwo() {\n"
098: + " super();\n"
099: + " }\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" + " }\n" + "}";
109:
110: // private static final String NONPUBLIC_TEXT =
111: // "import junit.framework.*; " +
112: // "public class NonPublic extends TestCase { " +
113: // " public NonPublic(String name) { super(name); } " +
114: // " void testShouldFail() { " +
115: // " assertEquals(\"monkey\", \"baboon\"); " +
116: // " } " +
117: // "}";
118:
119: private static final String ABC_CLASS_ONE = "class ABC extends java.util.Vector {}\n";
120:
121: private static final String ABC_CLASS_TWO = "class ABC extends java.util.ArrayList {}\n";
122:
123: private static final String ABC_TEST = "public class ABCTest extends junit.framework.TestCase {\n"
124: + " public void testABC() {\n"
125: + " new ABC().get(0);\n"
126: + " }\n" + "}";
127:
128: private static final String LANGUAGE_LEVEL_TEST = "class MyTest extends junit.framework.TestCase {\n"
129: + " void testMyMethod() {\n"
130: + " assertEquals(\"OneString\", \"TwoStrings\");\n"
131: + " }\n" + "}\n";
132:
133: /** Tests that the errors array contains all encountered failures and error in the right order. */
134: public void testErrorsArrayInOrder() throws Exception {
135: debug.logStart();
136: _m = new JUnitErrorModel(new JUnitError[0], _model, false);
137: OpenDefinitionsDocument doc = setupDocument(MONKEYTEST_FAIL_TEXT);
138: final File file = new File(_tempDir, "MonkeyTestFail.java");
139: doc.saveFile(new FileSelector(file));
140:
141: JUnitTestListener listener = new JUnitTestListener();
142: _model.addListener(listener);
143:
144: doc.startCompile();
145: listener.waitCompileDone();
146: if (_model.getCompilerModel().getNumErrors() > 0)
147: fail("compile failed: " + getCompilerErrorString());
148: listener.checkCompileOccurred();
149:
150: listener.runJUnit(doc);
151:
152: listener.assertJUnitStartCount(1);
153: // Clear document so we can make sure it's written to after startJUnit
154: _model.getJUnitModel().getJUnitDocument()
155: .remove(
156: 0,
157: _model.getJUnitModel().getJUnitDocument()
158: .getLength() - 1);
159: //final TestResult testResults = doc.startJUnit();
160:
161: //_m = new JUnitErrorModel(doc.getDocument(), "MonkeyTestFail", testResults);
162: _m = _model.getJUnitModel().getJUnitErrorModel();
163:
164: //JUnitError[] errorsWithPositions = _m.getErrorsWithPositions();
165: //JUnitError[] errorsWithoutPositions = _m.getErrorsWithoutPositions();
166: //assertTrue("testResults should not be null", testResults != null);
167:
168: assertEquals(
169: "the test results should have one error and one failure "
170: + _m.getNumErrors(), 2, _m.getNumErrors());
171:
172: assertEquals("test case has one error reported"
173: + _m.getError(0).message(), _m.getError(0).isWarning(),
174: false);
175:
176: assertEquals("test case has one failure reported"
177: + _m.getError(1).message(), _m.getError(1).isWarning(),
178: true);
179: //_model.setResetAfterCompile(true);
180: debug.logEnd();
181: }
182:
183: /**
184: * Tests that a VerifyError is reported as an error, rather than
185: * simply causing JUnit to blow up. Note that this test will hang if
186: * the error is not reported correctly, because the JUnitTestManager will
187: * blow up in the other JVM and never notify us that it's finished.
188: */
189: public void testVerifyErrorHandledCorrectly() throws Exception {
190: OpenDefinitionsDocument doc = setupDocument(ABC_CLASS_ONE);
191: final File file = new File(_tempDir, "ABC1.java");
192: doc.saveFile(new FileSelector(file));
193:
194: OpenDefinitionsDocument doc2 = setupDocument(ABC_TEST);
195: final File file2 = new File(_tempDir, "ABCTest.java");
196: doc2.saveFile(new FileSelector(file2));
197:
198: // Compile the correct ABC and the test
199: // JUnitTestListener listener = new JUnitTestListener(false);
200: // System.out.println("compiling all");
201: _model.getCompilerModel().compileAll();
202:
203: OpenDefinitionsDocument doc3 = setupDocument(ABC_CLASS_TWO);
204: final File file3 = new File(_tempDir, "ABC2.java");
205: doc3.saveFile(new FileSelector(file3));
206:
207: JUnitTestListener listener = new JUnitNonTestListener();
208: // Compile the incorrect ABC
209: // System.out.println("compiling doc3");
210: doc3.startCompile();
211: if (_model.getCompilerModel().getNumErrors() > 0) {
212: fail("compile failed: " + getCompilerErrorString());
213: }
214: _model.addListener(listener);
215: // Run the test: a VerifyError will be thrown in Java 1.4
216: // JUnitTestListener listener2 = new JUnitTestListener();
217: // _model.addListener(listener2);
218:
219: listener.assertClassFileErrorCount(0);
220: listener.runJUnit(doc2);
221: listener.waitJUnitDone();
222:
223: double version = Double.valueOf(System
224: .getProperty("java.specification.version"));
225: if (version < 1.5)
226: listener.assertClassFileErrorCount(1);
227: else
228: assertEquals("Should report one error", 1, _model
229: .getJUnitModel().getJUnitErrorModel()
230: .getNumErrors());
231:
232: _model.removeListener(listener);
233: }
234:
235: /** Tests that an elementary level file has the previous line of the actual error reported as the line of its error.
236: * Necessitated by the added code in the .java file associated with the .dj0 file (the import statement added by the
237: * language level compiler)
238: */
239:
240: public void testLanguageLevelJUnitErrorLine() throws Exception {
241: debug.logStart();
242: _m = new JUnitErrorModel(new JUnitError[0], _model, false);
243: OpenDefinitionsDocument doc = setupDocument(LANGUAGE_LEVEL_TEST);
244: final File file = new File(_tempDir, "MyTest.dj0");
245: doc.saveFile(new FileSelector(file));
246:
247: JUnitTestListener listener = new JUnitTestListener();
248: _model.addListener(listener);
249:
250: doc.startCompile();
251: listener.waitCompileDone();
252: if (_model.getCompilerModel().getNumErrors() > 0) {
253: fail("compile failed: " + getCompilerErrorString());
254: }
255: listener.checkCompileOccurred();
256:
257: listener.runJUnit(doc);
258:
259: listener.assertJUnitStartCount(1);
260:
261: // Clear document so we can make sure it's written to after startJUnit
262: _model.getJUnitModel().getJUnitDocument()
263: .remove(
264: 0,
265: _model.getJUnitModel().getJUnitDocument()
266: .getLength() - 1);
267:
268: _m = _model.getJUnitModel().getJUnitErrorModel();
269:
270: assertEquals("the test results should have one failure "
271: + _m.getNumErrors(), 1, _m.getNumErrors());
272:
273: assertEquals("the error line should be line number 2", 2, _m
274: .getError(0).lineNumber());
275: debug.logEnd();
276: }
277:
278: /** Test errors that occur in superclass. */
279: public void testErrorInSuperClass() throws Exception {
280: debug.logStart();
281: OpenDefinitionsDocument doc1 = setupDocument(TEST_ONE);
282: OpenDefinitionsDocument doc2 = setupDocument(TEST_TWO);
283: final File file1 = new File(_tempDir, "TestOne.java");
284: final File file2 = new File(_tempDir, "TestTwo.java");
285: doc1.saveFile(new FileSelector(file1));
286: doc2.saveFile(new FileSelector(file2));
287:
288: JUnitTestListener listener = new JUnitTestListener();
289: _model.addListener(listener);
290: _model.getCompilerModel().compileAll();
291: // doc1.startCompile();
292: // doc2.startCompile();
293:
294: listener.waitCompileDone();
295: listener.runJUnit(doc1);
296:
297: listener.assertJUnitStartCount(1);
298:
299: _m = _model.getJUnitModel().getJUnitErrorModel();
300:
301: assertEquals("test case has one error reported", 3, _m
302: .getNumErrors());
303: assertTrue("first error should be an error not a warning", !_m
304: .getError(0).isWarning());
305:
306: assertTrue("it's a junit error",
307: _m.getError(0) instanceof JUnitError);
308:
309: assertEquals("The first error is on line 5", 3, _m.getError(0)
310: .lineNumber());
311: assertEquals("The first error is on line 5", 19, _m.getError(1)
312: .lineNumber());
313: assertEquals("The first error is on line 5", 22, _m.getError(2)
314: .lineNumber());
315:
316: listener.runJUnit(doc2);
317:
318: listener.assertJUnitStartCount(2);
319:
320: assertEquals("test case has one error reported", 3, _m
321: .getNumErrors());
322: assertTrue("first error should be an error not a warning", !_m
323: .getError(0).isWarning());
324: assertEquals("The first error is on line 5", 3, _m.getError(0)
325: .lineNumber());
326: assertEquals("The first error is on line 5", 19, _m.getError(1)
327: .lineNumber());
328: assertEquals("The first error is on line 5", 22, _m.getError(2)
329: .lineNumber());
330:
331: _model.removeListener(listener);
332: debug.logEnd();
333: }
334: }
|