KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > GlobalModelJUnitTest


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;
35
36 import junit.framework.*;
37
38 import java.io.File JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.util.LinkedList JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.Arrays JavaDoc;
43 import javax.swing.SwingUtilities JavaDoc;
44
45 import edu.rice.cs.drjava.model.compiler.CompilerListener;
46 import edu.rice.cs.drjava.model.junit.*;
47 import edu.rice.cs.util.Log;
48 import edu.rice.cs.util.UnexpectedException;
49 import edu.rice.cs.util.swing.Utilities;
50
51 /** A test of Junit testing support in the GlobalModel.
52  * @version $Id: GlobalModelJUnitTest.java 4044 2006-11-23 13:40:43Z dlsmith $
53  */

54 public final class GlobalModelJUnitTest extends GlobalModelTestCase {
55   
56   private static Log _log = new Log("MasterSlave.txt", false);
57   
58   /** Whether or not to print debugging output. */
59   static final boolean printMessages = false;
60   
61   private static final String JavaDoc ELSPETH_ERROR_TEXT =
62     "import junit.framework.TestCase;" +
63     "public class Elspeth extends TestCase {" +
64     " public void testMe() {" +
65     " String s = \"elspeth\";" +
66     " assertEquals(\"they match\", s, \"elspeth4\");" +
67     " }" +
68     " public Elspeth() {" +
69     " super();" +
70     " }" +
71     " public java.lang.String toString() {" +
72     " return \"Elspeth(\" + \")\";" +
73     " }" +
74     " public boolean equals(java.lang.Object o) {" +
75     " if ((o == null) || getClass() != o.getClass()) return false;" +
76     " return true;" +
77     " }" +
78     " public int hashCode() {" +
79     " return getClass().hashCode();" +
80     " }" +
81     "}";
82   
83   private static final String JavaDoc MONKEYTEST_PASS_TEXT =
84     "import junit.framework.*; \n" +
85     "import java.io.*; \n" +
86     "public class MonkeyTestPass extends TestCase { \n" +
87     " public MonkeyTestPass(String name) { super(name); } \n" +
88     " public void testShouldPass() { \n" +
89     " assertEquals(\"monkey\", \"monkey\"); \n" +
90     " } \n" +
91     "}\n";
92
93   private static final String JavaDoc MONKEYTEST_PASS_ALT_TEXT =
94     "import junit.framework.*; \n" +
95     "import java.io.*; \n" +
96     "public class MonkeyTestPass extends TestCase { \n" +
97     " public MonkeyTestPass(String name) { super(name); } \n" +
98     " public void testShouldPass() { \n" +
99     " assertEquals(\"monkeys\", \"monkeys\"); \n" +
100     " } \n" +
101     "}\n";
102   
103   private static final String JavaDoc MONKEYTEST_FAIL_TEXT =
104     "import junit.framework.*; " +
105     "public class MonkeyTestFail extends TestCase { " +
106     " public MonkeyTestFail(String name) { super(name); } " +
107     " public void testShouldFail() { " +
108     " assertEquals(\"monkey\", \"baboon\"); " +
109     " } " +
110     "}";
111
112   private static final String JavaDoc MONKEYTEST_ERROR_TEXT =
113     "import junit.framework.*; " +
114     "public class MonkeyTestError extends TestCase { " +
115     " public MonkeyTestError(String name) { super(name); } " +
116     " public void testThrowsError() { " +
117     " throw new Error(\"This is an error.\"); " +
118     " } " +
119     "}";
120
121 // private static final String MONKEYTEST_COMPILEERROR_TEXT =
122
// "import junit.framework.*; " +
123
// "public class MonkeyTestCompileError extends TestCase { " +
124
// " Object MonkeyTestFail(String name) { super(name); } " +
125
// " public void testShouldFail() { " +
126
// " assertEquals(\"monkey\", \"baboon\"); " +
127
// " } " +
128
// "}";
129

130   private static final String JavaDoc NONPUBLIC_TEXT =
131     "import junit.framework.*; " +
132     "class NonPublic extends TestCase { " +
133     " NonPublic(String name) { super(name); } " +
134     " void testShouldFail() { " +
135     " assertEquals(\"monkey\", \"baboon\"); " +
136     " } " +
137     "}";
138
139   private static final String JavaDoc NON_TESTCASE_TEXT =
140     "public class NonTestCase {}";
141
142   private static final String JavaDoc MONKEYTEST_INFINITE_TEXT =
143     "import junit.framework.*; " +
144     "public class MonkeyTestInfinite extends TestCase { " +
145     " public MonkeyTestInfinite(String name) { super(name); } " +
146     " public void testInfinite() { " +
147     " while(true) {}" +
148     " } " +
149     "}";
150
151   private static final String JavaDoc HAS_MULTIPLE_TESTS_PASS_TEXT =
152     "import junit.framework.*; " +
153     "public class HasMultipleTestsPass extends TestCase { " +
154     " public HasMultipleTestsPass(String name) { super(name); } " +
155     " public void testShouldPass() { " +
156     " assertEquals(\"monkey\", \"monkey\"); " +
157     " } " +
158     " public void testShouldAlsoPass() { " +
159     " assertTrue(true); " +
160     " } " +
161     "}";
162   
163   private static final String JavaDoc STATIC_INNER_TEST_TEXT =
164     "import junit.framework.TestCase;" +
165     " public class StaticInnerTestCase{" +
166     " public static class Sadf extends TestCase {" +
167     " public Sadf() {" +
168     " super();" +
169     " }" +
170     " public Sadf(String name) {" +
171     " super(name);" +
172     " }" +
173     " public void testX() {" +
174     " assertTrue(\"this is true\", true);" +
175     " }" +
176     " public void testY() {" +
177     " assertFalse(\"this is false\", false);" +
178     " }" +
179     " }" +
180     "}";
181
182    private static final String JavaDoc MULTI_CLASSES_IN_FILE_TEXT =
183     "import junit.framework.TestCase;" +
184     " class A { } " +
185     " class B /* with syntax error */ { public void foo(int x) { } } " +
186     " public class Test extends TestCase { " +
187     " public void testAB() { assertTrue(\"this is true\", true); } " +
188     " }";
189   
190   /** Creates a test suite for JUnit to run.
191     * @return a test suite based on the methods in this class
192     */

193   public static Test suite() { return new TestSuite(GlobalModelJUnitTest.class); }
194
195   /** Tests that a JUnit file with no errors is reported to have no errors. */
196   public void testNoJUnitErrors() throws Exception JavaDoc {
197     if (printMessages) System.out.println("----testNoJUnitErrors-----");
198 // Utilities.show("Running testNoJUnitErrors");
199

200     OpenDefinitionsDocument doc = setupDocument(MONKEYTEST_PASS_TEXT);
201     final File JavaDoc file = new File JavaDoc(_tempDir, "MonkeyTestPass.java");
202     doc.saveFile(new FileSelector(file));
203     JUnitTestListener listener = new JUnitTestListener();
204     _model.addListener(listener);
205     
206     listener.compile(doc); // synchronously compiles doc
207
listener.checkCompileOccurred();
208     
209     listener.runJUnit(doc);
210     
211     listener.assertJUnitStartCount(1);
212     
213     if (printMessages) System.out.println("errors: "+_model.getJUnitModel().getJUnitErrorModel());
214     
215     listener.assertNonTestCaseCount(0);
216     assertEquals("test case should have no errors reported", 0,
217                  _model.getJUnitModel().getJUnitErrorModel().getNumErrors());
218                  
219     _model.removeListener(listener);
220     _log.log("testNoJUnitErrors completed");
221   }
222   
223   /** Tests that a JUnit file with an error is reported to have an error. */
224   public void testOneJUnitError() throws Exception JavaDoc {
225     if (printMessages) System.out.println("----testOneJUnitError-----");
226 // Utilities.show("Running testOneJUnitError");
227

228     OpenDefinitionsDocument doc = setupDocument(MONKEYTEST_FAIL_TEXT);
229     final File JavaDoc file = new File JavaDoc(_tempDir, "MonkeyTestFail.java");
230     doc.saveFile(new FileSelector(file));
231     JUnitTestListener listener = new JUnitTestListener();
232     _model.addListener(listener);
233     
234     listener.compile(doc);
235     listener.checkCompileOccurred();
236     
237     listener.runJUnit(_model.getJUnitModel());
238
239     assertEquals("test case has one error reported", 1, _model.getJUnitModel().getJUnitErrorModel().getNumErrors());
240     _model.removeListener(listener);
241     
242     _log.log("testOneJUnitError completed");
243   }
244   
245   /** Tests that a JUnit file with an error is reported to have an error. */
246   public void testElspethOneJUnitError() throws Exception JavaDoc {
247     if (printMessages) System.out.println("----testElspethOneJUnitError-----");
248 // Utilities.show("Running testElspethOneJunitError");
249

250     OpenDefinitionsDocument doc = setupDocument(ELSPETH_ERROR_TEXT);
251     final File JavaDoc file = new File JavaDoc(_tempDir, "Elspeth.java");
252     doc.saveFile(new FileSelector(file));
253     JUnitTestListener listener = new JUnitTestListener();
254     _model.addListener(listener);
255
256     listener.compile(doc);
257     listener.checkCompileOccurred();
258     
259     listener.runJUnit(doc);
260
261     JUnitErrorModel jem = _model.getJUnitModel().getJUnitErrorModel();
262     assertEquals("test case has one error reported", 1, jem.getNumErrors());
263     assertTrue("first error should be an error not a warning", !jem.getError(0).isWarning());
264     _model.removeListener(listener);
265     
266     _log.log("testElspethOneJUnitError completed");
267   }
268
269   /** Tests that a test class which throws a *real* Error (not an Exception) is handled correctly. */
270   public void testRealError() throws Exception JavaDoc {
271     if (printMessages) System.out.println("----testRealError-----");
272 // Utilities.show("Running testRealError");
273

274     OpenDefinitionsDocument doc = setupDocument(MONKEYTEST_ERROR_TEXT);
275     final File JavaDoc file = new File JavaDoc(_tempDir, "MonkeyTestError.java");
276     doc.saveFile(new FileSelector(file));
277     JUnitTestListener listener = new JUnitTestListener();
278     _model.addListener(listener);
279     
280     listener.compile(doc);
281     listener.checkCompileOccurred();
282     
283     listener.runJUnit(doc);
284
285     assertEquals("test case has one error reported", 1, _model.getJUnitModel().getJUnitErrorModel().getNumErrors());
286     listener.assertJUnitEndCount(1);
287     _model.removeListener(listener);
288     
289     _log.log("testRealError completed");
290   }
291
292   /** Tests that the ui is notified to put up an error dialog if JUnit is run on a non-TestCase. */
293   public void testNonTestCaseError() throws Exception JavaDoc {
294     if (printMessages) System.out.println("----testNonTestCaseError-----");
295 // Utilities.show("Running testNonTestCaseError");
296

297     final OpenDefinitionsDocument doc = setupDocument(NON_TESTCASE_TEXT);
298     final File JavaDoc file = new File JavaDoc(_tempDir, "NonTestCase.java");
299     doc.saveFile(new FileSelector(file));
300
301     JUnitTestListener listener = new JUnitNonTestListener();
302
303     _model.addListener(listener);
304     
305     listener.compile(doc);
306     listener.checkCompileOccurred();
307     
308     listener.runJUnit(doc);
309
310     if (printMessages) System.out.println("after test");
311
312     // Check events fired
313
listener.assertJUnitStartCount(0); // JUnit is never started
314
listener.assertJUnitEndCount(0); // JUnit never started and hence never ended
315
listener.assertNonTestCaseCount(1);
316     listener.assertJUnitSuiteStartedCount(0);
317     listener.assertJUnitTestStartedCount(0);
318     listener.assertJUnitTestEndedCount(0);
319     _model.removeListener(listener);
320     
321     _log.log("testNonTestCaseError completed");
322   }
323   
324   /** Tests that the ui is notified to put up an error dialog if JUnit is run on a non-public TestCase. */
325   public void testResultOfNonPublicTestCase() throws Exception JavaDoc {
326     if (printMessages) System.out.println("----testResultOfNonPublicTestCase-----");
327 // Utilities.show("Running testResultOfNonPublicTestCase");
328

329     final OpenDefinitionsDocument doc = setupDocument(NONPUBLIC_TEXT);
330     final File JavaDoc file = new File JavaDoc(_tempDir, "NonPublic.java");
331     doc.saveFile(new FileSelector(file));
332
333     JUnitTestListener listener = new JUnitTestListener();
334
335     _model.addListener(listener);
336
337     listener.compile(doc);
338     listener.checkCompileOccurred();
339     
340     listener.runJUnit(doc);
341    
342     if (printMessages) System.out.println("after test");
343
344     //System.err.println(testResults.toString());
345

346     // Check events fired
347
listener.assertJUnitStartCount(1);
348     listener.assertJUnitEndCount(1);
349
350     assertEquals("test case has one error reported", 1, _model.getJUnitModel().getJUnitErrorModel().getNumErrors());
351     _model.removeListener(listener);
352     
353     _log.log("testResultOfNonPublicTestCase completed");
354   }
355
356   /* This test has become inconsistent with DrJava behavior. If a document's file no longer exists and no class file
357    * exists, DrJava will detect that there is no valid class file for the document and ask the user to compile the
358    * file
359    */

360 // public void testDoNotRunJUnitIfFileHasBeenMoved() throws Exception {
361
// if (printMessages) System.out.println("----testDoNotRunJUnitIfFileHasBeenMoved-----");
362
//// Utilities.show("Running testDoNotRunJUnitIfFileHasBeenMoved");
363
//
364
// final OpenDefinitionsDocument doc = setupDocument(MONKEYTEST_PASS_TEXT);
365
// final File file = new File(_tempDir, "MonkeyTestPass.java");
366
// doc.saveFile(new FileSelector(file));
367
//
368
// JUnitTestListener listener = new JUnitTestListener();
369
//
370
// _model.addListener(listener);
371
// file.delete();
372
//
373
// listener.runJUnit(doc);
374
//
375
// listener.assertJUnitStartCount(0);
376
// listener.assertJUnitTestStartedCount(0);
377
//
378
// _model.removeListener(listener);
379
// _log.log("testDoNotRunJUnitIfFileHasBeenMoved completed");
380
// }
381

382 /** Tests a document that has no corresponding class file. */
383   public void testNoClassFile() throws Exception JavaDoc {
384     if (printMessages) System.out.println("----testNoClassFile-----");
385 // Utilities.show("Running testNoClassFile");
386

387     final OpenDefinitionsDocument doc = setupDocument(MONKEYTEST_PASS_TEXT);
388     final File JavaDoc file = new File JavaDoc(_tempDir, "MonkeyTestPass.java");
389     doc.saveFile(new FileSelector(file));
390
391     JUnitTestListener listener = new JUnitCompileBeforeTestListener();
392       
393     _model.addListener(listener);
394     
395 // Utilities.show("calling _runJunit in testNoClassFile");
396

397     listener.runJUnit(doc);
398 // Utilities.showDebug("Junit run completed");
399

400     if (printMessages) System.out.println("after test");
401     listener.assertCompileBeforeJUnitCount(1);
402     listener.assertNonTestCaseCount(0);
403     listener.assertJUnitStartCount(1);
404     listener.assertJUnitEndCount(1);
405     listener.assertJUnitSuiteStartedCount(1);
406     listener.assertJUnitTestStartedCount(1);
407     listener.assertJUnitTestEndedCount(1);
408     _model.removeListener(listener);
409     _log.log("testNoClassFile completed");
410   }
411   
412   /** Tests that an infinite loop in a test case can be aborted by clicking the Reset button. */
413   public void testInfiniteLoop() throws Exception JavaDoc {
414     if (printMessages) System.out.println("----testInfiniteLoop-----");
415 // Utilities.show("Running testInfiniteLoop");
416

417     final OpenDefinitionsDocument doc = setupDocument(MONKEYTEST_INFINITE_TEXT);
418     final File JavaDoc file = new File JavaDoc(_tempDir, "MonkeyTestInfinite.java");
419     doc.saveFile(new FileSelector(file));
420     
421     JUnitTestListener listener = new JUnitTestListener(false) {
422       public void junitSuiteStarted(int numTests) {
423         assertEquals("should run 1 test", 1, numTests);
424         synchronized(this) { junitSuiteStartedCount++; }
425         // kill the infinite test once testSuiteProcessing starts
426
_model.resetInteractions(new File JavaDoc(System.getProperty("user.dir")));
427       }
428     };
429
430     _model.addListener(listener);
431     listener.compile(doc);
432     
433     _log.log("Compilation of infinite loop completed");
434     
435     if (_model.getCompilerModel().getNumErrors() > 0) {
436       fail("compile failed: " + getCompilerErrorString());
437     }
438     listener.checkCompileOccurred();
439
440 // _model.removeListener(listener);
441
//
442
// _model.addListener(listener2);
443

444     listener.logJUnitStart();
445     try {
446       _log.log("startJUnit being called");
447       doc.startJUnit();
448       listener.waitJUnitDone();
449       fail("slave JVM should throw an exception because testing is interrupted by resetting interactions");
450     }
451     catch (Exception JavaDoc e) { /* Expected behavior for this test */ }
452     
453     listener.waitResetDone(); // reset should occur when test suite is started
454

455     if (printMessages) System.out.println("after test");
456     listener.assertJUnitStartCount(1);
457     _model.removeListener(listener);
458     listener.assertJUnitEndCount(1);
459     _log.log("testInfiniteLoop completed");
460   }
461   
462   /** Tests that when a JUnit file with no errors, after being saved and compiled,
463    * has it's contents replaced by a test that should fail, will pass all tests.
464    */

465   public void testUnsavedAndUnCompiledChanges() throws Exception JavaDoc {
466     if (printMessages) System.out.println("-----testUnsavedAndUnCompiledChanges-----");
467     
468     OpenDefinitionsDocument doc = setupDocument(MONKEYTEST_PASS_TEXT);
469     final File JavaDoc file = new File JavaDoc(_tempDir, "MonkeyTestPass.java");
470     doc.saveFile(new FileSelector(file));
471     
472     List JavaDoc<OpenDefinitionsDocument> docs = _model.getSortedOpenDefinitionsDocuments();
473     
474     final OpenDefinitionsDocument untitled = docs.get(0);
475     
476 // System.out.println("Untitled file is named: " + untitled.getName());
477

478     Utilities.invokeAndWait(new Runnable JavaDoc() {
479       public void run() {
480         untitled.quitFile();
481         _model.closeFileWithoutPrompt(untitled);
482       }
483     });
484     
485     // set up test listener for compile command; automatically checks that compilation is performed
486
JUnitTestListener listener = new JUnitCompileBeforeTestListener();
487     _model.addListener(listener);
488     
489     doc.startCompile();
490     
491 // System.err.println("Ordinary compile completed");
492

493     listener.resetCompileCounts();
494     
495     changeDocumentText(MONKEYTEST_PASS_ALT_TEXT, doc);
496 // System.err.println("document changed; modifiedSinceSave = " + doc.isModifiedSinceSave());
497

498     listener.runJUnit(doc);
499     
500 // System.err.println("JUnit completed");
501

502     /* Unsaved document forces both saveBeforeCompile and compileBeforeTest */
503     
504     listener.assertSaveBeforeCompileCount(1);
505     listener.assertCompileBeforeJUnitCount(1);
506     listener.assertNonTestCaseCount(0);
507     listener.assertJUnitStartCount(1);
508     listener.assertJUnitEndCount(1);
509     listener.assertJUnitSuiteStartedCount(1);
510     listener.assertJUnitTestStartedCount(1);
511     listener.assertJUnitTestEndedCount(1);
512     
513     if (printMessages) System.out.println("after test");
514     _model.removeListener(listener);
515     
516     assertEquals("test case should have no errors reported after modifying", 0,
517                  _model.getJUnitModel().getJUnitErrorModel().getNumErrors());
518     
519     doc.saveFile(new FileSelector(file));
520     
521     listener = new JUnitTestListener();
522     _model.addListener(listener);
523     
524     
525     assertEquals("test case should have no errors reported after saving", 0,
526                  _model.getJUnitModel().getJUnitErrorModel().getNumErrors());
527     _model.removeListener(listener);
528     
529     _log.log("testUnsavedAndUnCompiledChanges completed");
530   }
531   
532   /** Verifies that we get a nonTestCase event and that opening a single test file enables testing. */
533   public void testJUnitAllWithNoValidTests() throws Exception JavaDoc {
534     
535 // if (printMessages) System.err.println("-----testJUnitAllWithNoValidTests-----");
536

537     JUnitNonTestListener listener = new JUnitNonTestListener(true);
538     _model.addListener(listener);
539     
540     listener.runJUnit(_model.getJUnitModel());
541     
542     listener.assertNonTestCaseCount(1);
543     listener.assertJUnitSuiteStartedCount(0);
544     listener.assertJUnitTestStartedCount(0);
545     listener.assertJUnitTestEndedCount(0);
546     _model.removeListener(listener);
547     
548     _log.log("First test of NoValidTests complete");
549     
550     JUnitCompileBeforeTestListener listener2 = new JUnitCompileBeforeTestListener();
551     _model.addListener(listener2);
552     _log.log("Second listener added to model");
553      OpenDefinitionsDocument doc = setupDocument(NON_TESTCASE_TEXT);
554     File JavaDoc file = new File JavaDoc(_tempDir, "NonTestCase.java");
555     doc.saveFile(new FileSelector(file));
556
557     listener2.compile(doc);
558     listener2.checkCompileOccurred();
559     
560     _log.log("Resetting compile counts");
561     listener2.resetCompileCounts();
562     
563     // Opending Test
564
File JavaDoc file2 = new File JavaDoc(_tempDir, "MonkeyTestPass.java");
565     OpenDefinitionsDocument doc2 = setupDocument(MONKEYTEST_PASS_TEXT);
566     doc2.saveFile(new FileSelector(file2));
567     listener2.runJUnit(_model.getJUnitModel());
568     
569     listener2.assertNonTestCaseCount(0);
570     listener2.assertJUnitSuiteStartedCount(1);
571     listener2.assertJUnitTestStartedCount(1);
572     listener2.assertJUnitTestEndedCount(1);
573     _model.removeListener(listener2);
574     
575     _log.log("testJUnitAllWithNoValidTests completed");
576   }
577   
578   /** Tests that junit all works with one or two test cases that should pass. */
579   public void testJUnitAllWithNoErrors() throws Exception JavaDoc {
580     if (printMessages) System.out.println("-----testJUnitAllWithNoErrors-----");
581     
582     OpenDefinitionsDocument doc = setupDocument(NON_TESTCASE_TEXT);
583     JUnitNonTestListener listener = new JUnitNonTestListener(true);
584     File JavaDoc file = new File JavaDoc(_tempDir, "NonTestCase.java");
585     doc.saveFile(new FileSelector(file));
586     doc.startCompile();
587     doc = setupDocument(MONKEYTEST_PASS_TEXT);
588     file = new File JavaDoc(_tempDir, "MonkeyTestPass.java");
589     doc.saveFile(new FileSelector(file));
590     doc.startCompile();
591     _model.addListener(listener);
592     
593     listener.runJUnit(_model.getJUnitModel());
594     
595     listener.assertNonTestCaseCount(0);
596     listener.assertJUnitSuiteStartedCount(1);
597     listener.assertJUnitTestStartedCount(1);
598     listener.assertJUnitTestEndedCount(1);
599     _model.removeListener(listener);
600     
601     listener = new JUnitNonTestListener(true);
602     doc = setupDocument(HAS_MULTIPLE_TESTS_PASS_TEXT);
603     file = new File JavaDoc(_tempDir, "HasMultipleTestsPass.java");
604     doc.saveFile(new FileSelector(file));
605     doc.startCompile();
606     _model.addListener(listener);
607     
608     listener.runJUnit(_model.getJUnitModel());
609     
610     listener.assertNonTestCaseCount(0);
611     listener.assertJUnitSuiteStartedCount(1);
612     listener.assertJUnitTestStartedCount(3);
613     listener.assertJUnitTestEndedCount(3);
614     _model.removeListener(listener);
615         
616     _log.log("testJUnitAllWithNoErrors completed");
617   }
618   
619   /** Tests that junit all works with test cases that do not pass. */
620   public void testJUnitAllWithErrors() throws Exception JavaDoc {
621     
622     if (printMessages) System.out.println("-----testJUnitAllWithErrors-----");
623     
624     OpenDefinitionsDocument doc = setupDocument(MONKEYTEST_ERROR_TEXT);
625     OpenDefinitionsDocument doc2 = setupDocument(MONKEYTEST_FAIL_TEXT);
626     File JavaDoc file = new File JavaDoc(_tempDir, "MonkeyTestError.java");
627     File JavaDoc file2 = new File JavaDoc(_tempDir, "MonkeyTestFail.java");
628     doc.saveFile(new FileSelector(file));
629     doc2.saveFile(new FileSelector(file2));
630     JUnitNonTestListener listener = new JUnitNonTestListener(true);
631     _model.addListener(listener);
632     listener.compile(doc);
633     listener.checkCompileOccurred();
634     listener.resetCompileCounts();
635     listener.compile(doc2);
636     listener.checkCompileOccurred();
637     
638     listener.runJUnit(_model.getJUnitModel());
639     
640     listener.assertNonTestCaseCount(0);
641     listener.assertJUnitSuiteStartedCount(1);
642     listener.assertJUnitTestStartedCount(2);
643     listener.assertJUnitTestEndedCount(2);
644     _model.removeListener(listener);
645     
646     JUnitErrorModel jem = _model.getJUnitModel().getJUnitErrorModel();
647     assertEquals("test case has one error reported", 2, jem.getNumErrors());
648     
649     assertTrue("first error should be an error", jem.getError(0).isWarning());
650     assertFalse("second error should be a failure", jem.getError(1).isWarning());
651     
652     _log.log("testJUnitAllWithErrors completed");
653   }
654   
655   /** Tests that junit all works with one or two test cases that should pass. */
656   public void testJUnitStaticInnerClass() throws Exception JavaDoc {
657     if (printMessages) System.out.println("-----testJUnitAllWithStaticInnerClass-----");
658         
659     OpenDefinitionsDocument doc = setupDocument(NON_TESTCASE_TEXT);
660     OpenDefinitionsDocument doc2 = setupDocument(STATIC_INNER_TEST_TEXT);
661     File JavaDoc file = new File JavaDoc(_tempDir, "NonTestCase.java");
662     File JavaDoc file2 = new File JavaDoc(_tempDir, "StaticInnerTestCase.java");
663     doc.saveFile(new FileSelector(file));
664     doc2.saveFile(new FileSelector(file2));
665     
666     JUnitNonTestListener listener = new JUnitNonTestListener(true);
667     _model.addListener(listener);
668     listener.compile(doc);
669     listener.checkCompileOccurred();
670     listener.resetCompileCounts();
671     listener.compile(doc2);
672     listener.checkCompileOccurred();
673     
674     listener.runJUnit(_model.getJUnitModel());
675     
676     listener.assertNonTestCaseCount(0);
677     listener.assertJUnitSuiteStartedCount(1);
678     listener.assertJUnitTestStartedCount(2);
679     listener.assertJUnitTestEndedCount(2);
680     _model.removeListener(listener);
681     if (printMessages) System.out.println("----testJUnitAllWithNoErrors-----");
682     
683     _log.log("testJUnitStaticInnerClass completed");
684   }
685  
686   /** Tests that testing an uncompiled but correct group of files will first compile and then run test. */
687   public class JUnitCompileBeforeTestListener extends JUnitTestListener {
688     
689     /* Method copied by _mainListener in MainFrame. */
690     public void compileBeforeJUnit(final CompilerListener testAfterCompile) {
691 // System.err.println("compileBeforeJUnit called in listener " + this);
692
synchronized(this) { compileBeforeJUnitCount++; }
693       // Compile all open source files
694
_model.getCompilerModel().addListener(testAfterCompile); // listener removes itself
695
// Utilities.show("Calling _compileAll()");
696
try { _model.getCompilerModel().compileAll(); /* instead of invoking MainFrame._compileAll() */ }
697       catch(IOException JavaDoc e) { fail("Compile step generated IOException"); }
698       
699 // Utilities.show("Compilation finished");
700
}
701     
702     public void saveBeforeCompile() {
703 // System.err.println("saveBeforeCompile called in " + this);
704
synchronized(this) { saveBeforeCompileCount++; }
705       /** Assumes that DrJava is in flat file mode! */
706       try {
707         _model.saveAllFiles(new FileSaveSelector() {
708           public File JavaDoc getFile() { throw new UnexpectedException ("Test should not ask for save file name"); }
709           public boolean warnFileOpen(File JavaDoc f) { return false; }
710           public boolean verifyOverwrite() { return true; }
711           public boolean shouldSaveAfterFileMoved(OpenDefinitionsDocument doc, File JavaDoc oldFile) { return false; }
712         });
713       }
714       catch(IOException JavaDoc e) { throw new UnexpectedException(e); }
715     }
716     public void fileSaved(OpenDefinitionsDocument doc) { }
717   }
718   
719   /** Tests that when a JUnit file with no errors is compiled and then modified to contain
720     * an error does not pass unit testing (by running correct class files).
721     */

722   public void testCorrectFilesAfterIncorrectChanges() throws Exception JavaDoc {
723     if (printMessages) System.out.println("-----testCorrectFilesAfterIncorrectChanges-----");
724     
725     OpenDefinitionsDocument doc = setupDocument(NON_TESTCASE_TEXT);
726     JUnitNonTestListener listener = new JUnitNonTestListener(true);
727     File JavaDoc file = new File JavaDoc(_tempDir, "NonTestCase.java");
728     doc.saveFile(new FileSelector(file));
729     doc.startCompile();
730     doc = setupDocument(MULTI_CLASSES_IN_FILE_TEXT);
731     file = new File JavaDoc(_tempDir, "Test.java");
732     doc.saveFile(new FileSelector(file));
733     doc.startCompile();
734     _model.addListener(listener);
735     
736     listener.runJUnit(_model.getJUnitModel());
737     
738     listener.assertNonTestCaseCount(0);
739     listener.assertJUnitSuiteStartedCount(1);
740     listener.assertJUnitTestStartedCount(1);
741     listener.assertJUnitTestEndedCount(1);
742     _model.removeListener(listener);
743     
744     doc.remove(87,4);
745     
746     JUnitTestListener listener2 = new JUnitCompileBeforeTestListener();
747       
748     _model.addListener(listener2);
749     
750 // Utilities.show("calling _runJunit in testNoClassFile");
751

752     listener2.runJUnit(doc);
753 // Utilities.showDebug("Junit run completed");
754

755     if (printMessages) System.out.println("after test");
756     listener2.assertCompileBeforeJUnitCount(1);
757     listener2.assertNonTestCaseCount(1);
758     listener2.assertJUnitStartCount(0);
759     listener2.assertJUnitEndCount(0);
760     listener2.assertJUnitSuiteStartedCount(0);
761     listener2.assertJUnitTestStartedCount(0);
762     listener2.assertJUnitTestEndedCount(0);
763     _model.removeListener(listener2);
764     _log.log("testCorrectFilesAfterIncorrectChanges completed");
765   }
766 }
767
Popular Tags