Source Code Cross Referenced for GlobalModelJUnitTest.java in  » IDE » DrJava » edu » rice » cs » drjava » model » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE » DrJava » edu.rice.cs.drjava.model 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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;
038:
039:        import junit.framework.*;
040:
041:        import java.io.File;
042:        import java.io.IOException;
043:        import java.util.LinkedList;
044:        import java.util.List;
045:        import java.util.Arrays;
046:        import javax.swing.SwingUtilities;
047:
048:        import edu.rice.cs.drjava.model.compiler.CompilerListener;
049:        import edu.rice.cs.drjava.model.junit.*;
050:        import edu.rice.cs.util.Log;
051:        import edu.rice.cs.util.UnexpectedException;
052:        import edu.rice.cs.util.swing.Utilities;
053:
054:        /** A test of Junit testing support in the GlobalModel.
055:         * @version $Id: GlobalModelJUnitTest.java 4255 2007-08-28 19:17:37Z mgricken $
056:         */
057:        public final class GlobalModelJUnitTest extends GlobalModelTestCase {
058:
059:            private static Log _log = new Log("MasterSlave.txt", false);
060:
061:            /** Whether or not to print debugging output. */
062:            static final boolean printMessages = false;
063:
064:            private static final String ELSPETH_ERROR_TEXT = "import junit.framework.TestCase;"
065:                    + "public class Elspeth extends TestCase {"
066:                    + "    public void testMe() {"
067:                    + "        String s = \"elspeth\";"
068:                    + "        assertEquals(\"they match\", s, \"elspeth4\");"
069:                    + "    }"
070:                    + "  public Elspeth() {"
071:                    + "    super();"
072:                    + "  }"
073:                    + "  public java.lang.String toString() {"
074:                    + "    return \"Elspeth(\" + \")\";"
075:                    + "  }"
076:                    + "  public boolean equals(java.lang.Object o) {"
077:                    + "    if ((o == null) || getClass() != o.getClass()) return false;"
078:                    + "    return true;"
079:                    + "  }"
080:                    + "  public int hashCode() {"
081:                    + "    return getClass().hashCode();" + "  }" + "}";
082:
083:            private static final String MONKEYTEST_PASS_TEXT = "import junit.framework.*; \n"
084:                    + "import java.io.*; \n"
085:                    + "public class MonkeyTestPass extends TestCase { \n"
086:                    + "  public MonkeyTestPass(String name) { super(name); } \n"
087:                    + "  public void testShouldPass() { \n"
088:                    + "    assertEquals(\"monkey\", \"monkey\"); \n"
089:                    + "  } \n"
090:                    + "}\n";
091:
092:            private static final String MONKEYTEST_PASS_ALT_TEXT = "import junit.framework.*; \n"
093:                    + "import java.io.*; \n"
094:                    + "public class MonkeyTestPass extends TestCase { \n"
095:                    + "  public MonkeyTestPass(String name) { super(name); } \n"
096:                    + "  public void testShouldPass() { \n"
097:                    + "    assertEquals(\"monkeys\", \"monkeys\"); \n"
098:                    + "  } \n" + "}\n";
099:
100:            private static final String MONKEYTEST_FAIL_TEXT = "import junit.framework.*; "
101:                    + "public class MonkeyTestFail extends TestCase { "
102:                    + "  public MonkeyTestFail(String name) { super(name); } "
103:                    + "  public void testShouldFail() { "
104:                    + "    assertEquals(\"monkey\", \"baboon\"); "
105:                    + "  } "
106:                    + "}";
107:
108:            private static final String MONKEYTEST_ERROR_TEXT = "import junit.framework.*; "
109:                    + "public class MonkeyTestError extends TestCase { "
110:                    + "  public MonkeyTestError(String name) { super(name); } "
111:                    + "  public void testThrowsError() { "
112:                    + "    throw new Error(\"This is an error.\"); "
113:                    + "  } "
114:                    + "}";
115:
116:            //  private static final String MONKEYTEST_COMPILEERROR_TEXT =
117:            //    "import junit.framework.*; " +
118:            //    "public class MonkeyTestCompileError extends TestCase { " +
119:            //    "  Object MonkeyTestFail(String name) { super(name); } " +
120:            //    "  public void testShouldFail() { " +
121:            //    "    assertEquals(\"monkey\", \"baboon\"); " +
122:            //    "  } " +
123:            //    "}";
124:
125:            private static final String NONPUBLIC_TEXT = "import junit.framework.*; "
126:                    + "class NonPublic extends TestCase { "
127:                    + "  NonPublic(String name) { super(name); } "
128:                    + "  void testShouldFail() { "
129:                    + "    assertEquals(\"monkey\", \"baboon\"); "
130:                    + "  } "
131:                    + "}";
132:
133:            private static final String NON_TESTCASE_TEXT = "public class NonTestCase {}";
134:
135:            private static final String MONKEYTEST_INFINITE_TEXT = "import junit.framework.*; "
136:                    + "public class MonkeyTestInfinite extends TestCase { "
137:                    + "  public MonkeyTestInfinite(String name) { super(name); } "
138:                    + "  public void testInfinite() { "
139:                    + "    while(true) {}"
140:                    + "  } " + "}";
141:
142:            private static final String HAS_MULTIPLE_TESTS_PASS_TEXT = "import junit.framework.*; "
143:                    + "public class HasMultipleTestsPass extends TestCase { "
144:                    + "  public HasMultipleTestsPass(String name) { super(name); } "
145:                    + "  public void testShouldPass() { "
146:                    + "    assertEquals(\"monkey\", \"monkey\"); "
147:                    + "  } "
148:                    + "  public void testShouldAlsoPass() { "
149:                    + "    assertTrue(true); " + "  } " + "}";
150:
151:            private static final String STATIC_INNER_TEST_TEXT = "import junit.framework.TestCase;"
152:                    + " public class StaticInnerTestCase{"
153:                    + "   public static class Sadf extends TestCase {"
154:                    + "     public Sadf() {"
155:                    + "       super();"
156:                    + "     }"
157:                    + "     public Sadf(String name) {"
158:                    + "       super(name);"
159:                    + "     }"
160:                    + "     public void testX() {"
161:                    + "       assertTrue(\"this is true\", true);"
162:                    + "     }"
163:                    + "     public void testY() {"
164:                    + "       assertFalse(\"this is false\", false);"
165:                    + "     }" + "   }" + "}";
166:
167:            private static final String MULTI_CLASSES_IN_FILE_TEXT = "import junit.framework.TestCase;"
168:                    + " class A { } "
169:                    + " class B /* with syntax error */ { public void foo(int x) { } } "
170:                    + " public class Test extends TestCase { "
171:                    + "   public void testAB() { assertTrue(\"this is true\", true); } "
172:                    + " }";
173:
174:            /** Creates a test suite for JUnit to run.
175:             * @return a test suite based on the methods in this class
176:             */
177:            public static Test suite() {
178:                return new TestSuite(GlobalModelJUnitTest.class);
179:            }
180:
181:            /** Tests that a JUnit file with no errors is reported to have no errors. */
182:            public void testNoJUnitErrors() throws Exception {
183:                if (printMessages)
184:                    System.out.println("----testNoJUnitErrors-----");
185:                //    Utilities.show("Running testNoJUnitErrors");
186:
187:                OpenDefinitionsDocument doc = setupDocument(MONKEYTEST_PASS_TEXT);
188:                final File file = new File(_tempDir, "MonkeyTestPass.java");
189:                doc.saveFile(new FileSelector(file));
190:                JUnitTestListener listener = new JUnitTestListener();
191:                _model.addListener(listener);
192:
193:                listener.compile(doc); // synchronously compiles doc
194:                listener.checkCompileOccurred();
195:
196:                listener.runJUnit(doc);
197:
198:                listener.assertJUnitStartCount(1);
199:
200:                if (printMessages)
201:                    System.out.println("errors: "
202:                            + _model.getJUnitModel().getJUnitErrorModel());
203:
204:                listener.assertNonTestCaseCount(0);
205:                assertEquals("test case should have no errors reported", 0,
206:                        _model.getJUnitModel().getJUnitErrorModel()
207:                                .getNumErrors());
208:
209:                _model.removeListener(listener);
210:                _log.log("testNoJUnitErrors completed");
211:            }
212:
213:            /** Tests that a JUnit file with an error is reported to have an error. */
214:            public void testOneJUnitError() throws Exception {
215:                if (printMessages)
216:                    System.out.println("----testOneJUnitError-----");
217:                //    Utilities.show("Running testOneJUnitError");
218:
219:                OpenDefinitionsDocument doc = setupDocument(MONKEYTEST_FAIL_TEXT);
220:                final File file = new File(_tempDir, "MonkeyTestFail.java");
221:                doc.saveFile(new FileSelector(file));
222:                JUnitTestListener listener = new JUnitTestListener();
223:                _model.addListener(listener);
224:
225:                listener.compile(doc);
226:                listener.checkCompileOccurred();
227:
228:                listener.runJUnit(_model.getJUnitModel());
229:
230:                assertEquals("test case has one error reported", 1, _model
231:                        .getJUnitModel().getJUnitErrorModel().getNumErrors());
232:                _model.removeListener(listener);
233:
234:                _log.log("testOneJUnitError completed");
235:            }
236:
237:            /** Tests that a JUnit file with an error is reported to have an error. */
238:            public void testElspethOneJUnitError() throws Exception {
239:                if (printMessages)
240:                    System.out.println("----testElspethOneJUnitError-----");
241:                //    Utilities.show("Running testElspethOneJunitError");
242:
243:                OpenDefinitionsDocument doc = setupDocument(ELSPETH_ERROR_TEXT);
244:                final File file = new File(_tempDir, "Elspeth.java");
245:                doc.saveFile(new FileSelector(file));
246:                JUnitTestListener listener = new JUnitTestListener();
247:                _model.addListener(listener);
248:
249:                listener.compile(doc);
250:                listener.checkCompileOccurred();
251:
252:                listener.runJUnit(doc);
253:
254:                JUnitErrorModel jem = _model.getJUnitModel()
255:                        .getJUnitErrorModel();
256:                assertEquals("test case has one error reported", 1, jem
257:                        .getNumErrors());
258:                assertTrue("first error should be an error not a warning", !jem
259:                        .getError(0).isWarning());
260:                _model.removeListener(listener);
261:
262:                _log.log("testElspethOneJUnitError completed");
263:            }
264:
265:            /** Tests that a test class which throws a *real* Error (not an Exception) is handled correctly. */
266:            public void testRealError() throws Exception {
267:                if (printMessages)
268:                    System.out.println("----testRealError-----");
269:                //    Utilities.show("Running testRealError");
270:
271:                OpenDefinitionsDocument doc = setupDocument(MONKEYTEST_ERROR_TEXT);
272:                final File file = new File(_tempDir, "MonkeyTestError.java");
273:                doc.saveFile(new FileSelector(file));
274:                JUnitTestListener listener = new JUnitTestListener();
275:                _model.addListener(listener);
276:
277:                listener.compile(doc);
278:                listener.checkCompileOccurred();
279:
280:                listener.runJUnit(doc);
281:
282:                assertEquals("test case has one error reported", 1, _model
283:                        .getJUnitModel().getJUnitErrorModel().getNumErrors());
284:                listener.assertJUnitEndCount(1);
285:                _model.removeListener(listener);
286:
287:                _log.log("testRealError completed");
288:            }
289:
290:            /** Tests that the ui is notified to put up an error dialog if JUnit is run on a non-TestCase. */
291:            public void testNonTestCaseError() throws Exception {
292:                if (printMessages)
293:                    System.out.println("----testNonTestCaseError-----");
294:                //    Utilities.show("Running testNonTestCaseError");
295:
296:                final OpenDefinitionsDocument doc = setupDocument(NON_TESTCASE_TEXT);
297:                final File file = new File(_tempDir, "NonTestCase.java");
298:                doc.saveFile(new FileSelector(file));
299:
300:                JUnitTestListener listener = new JUnitNonTestListener();
301:
302:                _model.addListener(listener);
303:
304:                listener.compile(doc);
305:                listener.checkCompileOccurred();
306:
307:                listener.runJUnit(doc);
308:
309:                if (printMessages)
310:                    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 {
326:                if (printMessages)
327:                    System.out
328:                            .println("----testResultOfNonPublicTestCase-----");
329:                //    Utilities.show("Running testResultOfNonPublicTestCase");
330:
331:                final OpenDefinitionsDocument doc = setupDocument(NONPUBLIC_TEXT);
332:                final File file = new File(_tempDir, "NonPublic.java");
333:                doc.saveFile(new FileSelector(file));
334:
335:                JUnitTestListener listener = new JUnitTestListener();
336:
337:                _model.addListener(listener);
338:
339:                listener.compile(doc);
340:                listener.checkCompileOccurred();
341:
342:                listener.runJUnit(doc);
343:
344:                if (printMessages)
345:                    System.out.println("after test");
346:
347:                //System.err.println(testResults.toString());
348:
349:                // Check events fired
350:                listener.assertJUnitStartCount(1);
351:                listener.assertJUnitEndCount(1);
352:
353:                assertEquals("test case has one error reported", 1, _model
354:                        .getJUnitModel().getJUnitErrorModel().getNumErrors());
355:                _model.removeListener(listener);
356:
357:                _log.log("testResultOfNonPublicTestCase completed");
358:            }
359:
360:            /* This test has become inconsistent with DrJava behavior.  If a document's file no longer exists and no class file
361:             * exists, DrJava will detect that there is no valid class file for the document and ask the user to compile the
362:             * file
363:             */
364:            //  public void testDoNotRunJUnitIfFileHasBeenMoved() throws Exception {
365:            //    if (printMessages) System.out.println("----testDoNotRunJUnitIfFileHasBeenMoved-----");
366:            ////    Utilities.show("Running testDoNotRunJUnitIfFileHasBeenMoved");
367:            //    
368:            //    final OpenDefinitionsDocument doc = setupDocument(MONKEYTEST_PASS_TEXT);
369:            //    final File file = new File(_tempDir, "MonkeyTestPass.java");
370:            //    doc.saveFile(new FileSelector(file));
371:            //
372:            //    JUnitTestListener listener = new JUnitTestListener();
373:            //
374:            //    _model.addListener(listener);
375:            //    file.delete();
376:            //
377:            //    listener.runJUnit(doc);
378:            //
379:            //    listener.assertJUnitStartCount(0);
380:            //    listener.assertJUnitTestStartedCount(0);
381:            //
382:            //    _model.removeListener(listener);
383:            //    _log.log("testDoNotRunJUnitIfFileHasBeenMoved completed");
384:            //  }
385:            /** Tests a document that has no corresponding class file. */
386:            public void testNoClassFile() throws Exception {
387:                if (printMessages)
388:                    System.out.println("----testNoClassFile-----");
389:                //    Utilities.show("Running testNoClassFile");
390:
391:                final OpenDefinitionsDocument doc = setupDocument(MONKEYTEST_PASS_TEXT);
392:                final File file = new File(_tempDir, "MonkeyTestPass.java");
393:                doc.saveFile(new FileSelector(file));
394:
395:                JUnitTestListener listener = new JUnitCompileBeforeTestListener();
396:
397:                _model.addListener(listener);
398:
399:                //    Utilities.show("calling _runJunit in testNoClassFile");
400:
401:                listener.runJUnit(doc);
402:                //    Utilities.showDebug("Junit run completed");
403:
404:                if (printMessages)
405:                    System.out.println("after test");
406:                listener.assertCompileBeforeJUnitCount(1);
407:                listener.assertNonTestCaseCount(0);
408:                listener.assertJUnitStartCount(1);
409:                listener.assertJUnitEndCount(1);
410:                listener.assertJUnitSuiteStartedCount(1);
411:                listener.assertJUnitTestStartedCount(1);
412:                listener.assertJUnitTestEndedCount(1);
413:                _model.removeListener(listener);
414:                _log.log("testNoClassFile completed");
415:            }
416:
417:            /** Tests that an infinite loop in a test case can be aborted by clicking the Reset button. */
418:            public void testInfiniteLoop() throws Exception {
419:                if (printMessages)
420:                    System.out.println("----testInfiniteLoop-----");
421:                //    Utilities.show("Running testInfiniteLoop");
422:
423:                final OpenDefinitionsDocument doc = setupDocument(MONKEYTEST_INFINITE_TEXT);
424:                final File file = new File(_tempDir, "MonkeyTestInfinite.java");
425:                doc.saveFile(new FileSelector(file));
426:
427:                JUnitTestListener listener = new JUnitTestListener(false) {
428:                    public void junitSuiteStarted(int numTests) {
429:                        assertEquals("should run 1 test", 1, numTests);
430:                        synchronized (this ) {
431:                            junitSuiteStartedCount++;
432:                        }
433:                        // kill the infinite test once testSuiteProcessing starts
434:                        _model.resetInteractions(new File(System
435:                                .getProperty("user.dir")));
436:                    }
437:                };
438:
439:                _model.addListener(listener);
440:                listener.compile(doc);
441:
442:                _log.log("Compilation of infinite loop completed");
443:
444:                if (_model.getCompilerModel().getNumErrors() > 0) {
445:                    fail("compile failed: " + getCompilerErrorString());
446:                }
447:                listener.checkCompileOccurred();
448:
449:                //    _model.removeListener(listener);
450:                //    
451:                //    _model.addListener(listener2);
452:
453:                listener.logJUnitStart();
454:                try {
455:                    _log.log("startJUnit being called");
456:                    doc.startJUnit();
457:                    listener.waitJUnitDone();
458:                    fail("slave JVM should throw an exception because testing is interrupted by resetting interactions");
459:                } catch (Exception e) { /* Expected behavior for this test */
460:                }
461:
462:                listener.waitResetDone(); // reset should occur when test suite is started
463:
464:                if (printMessages)
465:                    System.out.println("after test");
466:                listener.assertJUnitStartCount(1);
467:                _model.removeListener(listener);
468:                listener.assertJUnitEndCount(1);
469:                _log.log("testInfiniteLoop completed");
470:            }
471:
472:            /** Tests that when a JUnit file with no errors, after being saved and compiled,
473:             *  has it's contents replaced by a test that should fail, will pass all tests.
474:             */
475:            public void testUnsavedAndUnCompiledChanges() throws Exception {
476:                if (printMessages)
477:                    System.out
478:                            .println("-----testUnsavedAndUnCompiledChanges-----");
479:
480:                OpenDefinitionsDocument doc = setupDocument(MONKEYTEST_PASS_TEXT);
481:                final File file = new File(_tempDir, "MonkeyTestPass.java");
482:                doc.saveFile(new FileSelector(file));
483:
484:                List<OpenDefinitionsDocument> docs = _model
485:                        .getSortedOpenDefinitionsDocuments();
486:
487:                final OpenDefinitionsDocument untitled = docs.get(0);
488:
489:                //    System.out.println("Untitled file is named: " + untitled.getName());
490:
491:                Utilities.invokeAndWait(new Runnable() {
492:                    public void run() {
493:                        untitled.quitFile();
494:                        _model.closeFileWithoutPrompt(untitled);
495:                    }
496:                });
497:
498:                // set up test listener for compile command; automatically checks that compilation is performed
499:                JUnitTestListener listener = new JUnitCompileBeforeTestListener();
500:                _model.addListener(listener);
501:
502:                doc.startCompile();
503:
504:                //    System.err.println("Ordinary compile completed");
505:
506:                listener.resetCompileCounts();
507:
508:                changeDocumentText(MONKEYTEST_PASS_ALT_TEXT, doc);
509:                //    System.err.println("document changed; modifiedSinceSave = " + doc.isModifiedSinceSave());
510:
511:                listener.runJUnit(doc);
512:
513:                //    System.err.println("JUnit completed");
514:
515:                /* Unsaved document forces both saveBeforeCompile and compileBeforeTest */
516:
517:                listener.assertSaveBeforeCompileCount(1);
518:                listener.assertCompileBeforeJUnitCount(1);
519:                listener.assertNonTestCaseCount(0);
520:                listener.assertJUnitStartCount(1);
521:                listener.assertJUnitEndCount(1);
522:                listener.assertJUnitSuiteStartedCount(1);
523:                listener.assertJUnitTestStartedCount(1);
524:                listener.assertJUnitTestEndedCount(1);
525:
526:                if (printMessages)
527:                    System.out.println("after test");
528:                _model.removeListener(listener);
529:
530:                assertEquals(
531:                        "test case should have no errors reported after modifying",
532:                        0, _model.getJUnitModel().getJUnitErrorModel()
533:                                .getNumErrors());
534:
535:                doc.saveFile(new FileSelector(file));
536:
537:                listener = new JUnitTestListener();
538:                _model.addListener(listener);
539:
540:                assertEquals(
541:                        "test case should have no errors reported after saving",
542:                        0, _model.getJUnitModel().getJUnitErrorModel()
543:                                .getNumErrors());
544:                _model.removeListener(listener);
545:
546:                _log.log("testUnsavedAndUnCompiledChanges completed");
547:            }
548:
549:            /** Verifies that we get a nonTestCase event and that opening a single test file enables testing. */
550:            public void testJUnitAllWithNoValidTests() throws Exception {
551:
552:                //    if (printMessages) System.err.println("-----testJUnitAllWithNoValidTests-----");
553:
554:                JUnitNonTestListener listener = new JUnitNonTestListener(true);
555:                _model.addListener(listener);
556:
557:                listener.runJUnit(_model.getJUnitModel());
558:
559:                listener.assertNonTestCaseCount(1);
560:                listener.assertJUnitSuiteStartedCount(0);
561:                listener.assertJUnitTestStartedCount(0);
562:                listener.assertJUnitTestEndedCount(0);
563:                _model.removeListener(listener);
564:
565:                _log.log("First test of NoValidTests complete");
566:
567:                JUnitCompileBeforeTestListener listener2 = new JUnitCompileBeforeTestListener();
568:                _model.addListener(listener2);
569:                _log.log("Second listener added to model");
570:                OpenDefinitionsDocument doc = setupDocument(NON_TESTCASE_TEXT);
571:                File file = new File(_tempDir, "NonTestCase.java");
572:                doc.saveFile(new FileSelector(file));
573:
574:                listener2.compile(doc);
575:                listener2.checkCompileOccurred();
576:
577:                _log.log("Resetting compile counts");
578:                listener2.resetCompileCounts();
579:
580:                // Opending Test
581:                File file2 = new File(_tempDir, "MonkeyTestPass.java");
582:                OpenDefinitionsDocument doc2 = setupDocument(MONKEYTEST_PASS_TEXT);
583:                doc2.saveFile(new FileSelector(file2));
584:                listener2.runJUnit(_model.getJUnitModel());
585:
586:                listener2.assertNonTestCaseCount(0);
587:                listener2.assertJUnitSuiteStartedCount(1);
588:                listener2.assertJUnitTestStartedCount(1);
589:                listener2.assertJUnitTestEndedCount(1);
590:                _model.removeListener(listener2);
591:
592:                _log.log("testJUnitAllWithNoValidTests completed");
593:            }
594:
595:            /** Tests that junit all works with one or two test cases that should pass. */
596:            public void testJUnitAllWithNoErrors() throws Exception {
597:                if (printMessages)
598:                    System.out.println("-----testJUnitAllWithNoErrors-----");
599:
600:                OpenDefinitionsDocument doc = setupDocument(NON_TESTCASE_TEXT);
601:                JUnitNonTestListener listener = new JUnitNonTestListener(true);
602:                File file = new File(_tempDir, "NonTestCase.java");
603:                doc.saveFile(new FileSelector(file));
604:                doc.startCompile();
605:                doc = setupDocument(MONKEYTEST_PASS_TEXT);
606:                file = new File(_tempDir, "MonkeyTestPass.java");
607:                doc.saveFile(new FileSelector(file));
608:                doc.startCompile();
609:                _model.addListener(listener);
610:
611:                listener.runJUnit(_model.getJUnitModel());
612:
613:                listener.assertNonTestCaseCount(0);
614:                listener.assertJUnitSuiteStartedCount(1);
615:                listener.assertJUnitTestStartedCount(1);
616:                listener.assertJUnitTestEndedCount(1);
617:                _model.removeListener(listener);
618:
619:                listener = new JUnitNonTestListener(true);
620:                doc = setupDocument(HAS_MULTIPLE_TESTS_PASS_TEXT);
621:                file = new File(_tempDir, "HasMultipleTestsPass.java");
622:                doc.saveFile(new FileSelector(file));
623:                doc.startCompile();
624:                _model.addListener(listener);
625:
626:                listener.runJUnit(_model.getJUnitModel());
627:
628:                listener.assertNonTestCaseCount(0);
629:                listener.assertJUnitSuiteStartedCount(1);
630:                listener.assertJUnitTestStartedCount(3);
631:                listener.assertJUnitTestEndedCount(3);
632:                _model.removeListener(listener);
633:
634:                _log.log("testJUnitAllWithNoErrors completed");
635:            }
636:
637:            /** Tests that junit all works with test cases that do not pass. */
638:            public void testJUnitAllWithErrors() throws Exception {
639:
640:                if (printMessages)
641:                    System.out.println("-----testJUnitAllWithErrors-----");
642:
643:                OpenDefinitionsDocument doc = setupDocument(MONKEYTEST_ERROR_TEXT);
644:                OpenDefinitionsDocument doc2 = setupDocument(MONKEYTEST_FAIL_TEXT);
645:                File file = new File(_tempDir, "MonkeyTestError.java");
646:                File file2 = new File(_tempDir, "MonkeyTestFail.java");
647:                doc.saveFile(new FileSelector(file));
648:                doc2.saveFile(new FileSelector(file2));
649:                JUnitNonTestListener listener = new JUnitNonTestListener(true);
650:                _model.addListener(listener);
651:                listener.compile(doc);
652:                listener.checkCompileOccurred();
653:                listener.resetCompileCounts();
654:                listener.compile(doc2);
655:                listener.checkCompileOccurred();
656:
657:                listener.runJUnit(_model.getJUnitModel());
658:
659:                listener.assertNonTestCaseCount(0);
660:                listener.assertJUnitSuiteStartedCount(1);
661:                listener.assertJUnitTestStartedCount(2);
662:                listener.assertJUnitTestEndedCount(2);
663:                _model.removeListener(listener);
664:
665:                JUnitErrorModel jem = _model.getJUnitModel()
666:                        .getJUnitErrorModel();
667:                assertEquals("test case has one error reported", 2, jem
668:                        .getNumErrors());
669:
670:                assertTrue("first error should be an error", jem.getError(0)
671:                        .isWarning());
672:                assertFalse("second error should be a failure", jem.getError(1)
673:                        .isWarning());
674:
675:                _log.log("testJUnitAllWithErrors completed");
676:            }
677:
678:            /** Tests that junit all works with one or two test cases that should pass. */
679:            public void testJUnitStaticInnerClass() throws Exception {
680:                if (printMessages)
681:                    System.out
682:                            .println("-----testJUnitAllWithStaticInnerClass-----");
683:
684:                OpenDefinitionsDocument doc = setupDocument(NON_TESTCASE_TEXT);
685:                OpenDefinitionsDocument doc2 = setupDocument(STATIC_INNER_TEST_TEXT);
686:                File file = new File(_tempDir, "NonTestCase.java");
687:                File file2 = new File(_tempDir, "StaticInnerTestCase.java");
688:                doc.saveFile(new FileSelector(file));
689:                doc2.saveFile(new FileSelector(file2));
690:
691:                JUnitNonTestListener listener = new JUnitNonTestListener(true);
692:                _model.addListener(listener);
693:                listener.compile(doc);
694:                listener.checkCompileOccurred();
695:                listener.resetCompileCounts();
696:                listener.compile(doc2);
697:                listener.checkCompileOccurred();
698:
699:                listener.runJUnit(_model.getJUnitModel());
700:
701:                listener.assertNonTestCaseCount(0);
702:                listener.assertJUnitSuiteStartedCount(1);
703:                listener.assertJUnitTestStartedCount(2);
704:                listener.assertJUnitTestEndedCount(2);
705:                _model.removeListener(listener);
706:                if (printMessages)
707:                    System.out.println("----testJUnitAllWithNoErrors-----");
708:
709:                _log.log("testJUnitStaticInnerClass completed");
710:            }
711:
712:            /** Tests that testing an uncompiled but correct group of files will first compile and then run test. */
713:            public class JUnitCompileBeforeTestListener extends
714:                    JUnitTestListener {
715:
716:                /* Method copied by _mainListener in MainFrame. */
717:                public void compileBeforeJUnit(
718:                        final CompilerListener testAfterCompile) {
719:                    //      System.err.println("compileBeforeJUnit called in listener " + this);
720:                    synchronized (this ) {
721:                        compileBeforeJUnitCount++;
722:                    }
723:                    // Compile all open source files
724:                    _model.getCompilerModel().addListener(testAfterCompile); // listener removes itself
725:                    //        Utilities.show("Calling _compileAll()");
726:                    try {
727:                        _model.getCompilerModel().compileAll(); /* instead of invoking MainFrame._compileAll() */
728:                    } catch (IOException e) {
729:                        fail("Compile step generated IOException");
730:                    }
731:
732:                    //        Utilities.show("Compilation finished");
733:                }
734:
735:                public void saveBeforeCompile() {
736:                    //      System.err.println("saveBeforeCompile called in " + this);
737:                    synchronized (this ) {
738:                        saveBeforeCompileCount++;
739:                    }
740:                    /** Assumes that DrJava is in flat file mode! */
741:                    try {
742:                        _model.saveAllFiles(new FileSaveSelector() {
743:                            public File getFile() {
744:                                throw new UnexpectedException(
745:                                        "Test should not ask for save file name");
746:                            }
747:
748:                            public boolean warnFileOpen(File f) {
749:                                return false;
750:                            }
751:
752:                            public boolean verifyOverwrite() {
753:                                return true;
754:                            }
755:
756:                            public boolean shouldSaveAfterFileMoved(
757:                                    OpenDefinitionsDocument doc, File oldFile) {
758:                                return false;
759:                            }
760:                        });
761:                    } catch (IOException e) {
762:                        throw new UnexpectedException(e);
763:                    }
764:                }
765:
766:                public void fileSaved(OpenDefinitionsDocument doc) {
767:                }
768:            }
769:
770:            /** Tests that when a JUnit file with no errors is compiled and then modified to contain
771:             * an error does not pass unit testing (by running correct class files).
772:             */
773:            public void testCorrectFilesAfterIncorrectChanges()
774:                    throws Exception {
775:                if (printMessages)
776:                    System.out
777:                            .println("-----testCorrectFilesAfterIncorrectChanges-----");
778:
779:                OpenDefinitionsDocument doc = setupDocument(NON_TESTCASE_TEXT);
780:                JUnitNonTestListener listener = new JUnitNonTestListener(true);
781:                File file = new File(_tempDir, "NonTestCase.java");
782:                doc.saveFile(new FileSelector(file));
783:                doc.startCompile();
784:                doc = setupDocument(MULTI_CLASSES_IN_FILE_TEXT);
785:                file = new File(_tempDir, "Test.java");
786:                doc.saveFile(new FileSelector(file));
787:                doc.startCompile();
788:                _model.addListener(listener);
789:
790:                listener.runJUnit(_model.getJUnitModel());
791:
792:                listener.assertNonTestCaseCount(0);
793:                listener.assertJUnitSuiteStartedCount(1);
794:                listener.assertJUnitTestStartedCount(1);
795:                listener.assertJUnitTestEndedCount(1);
796:                _model.removeListener(listener);
797:
798:                doc.remove(87, 4);
799:
800:                JUnitTestListener listener2 = new JUnitCompileBeforeTestListener();
801:
802:                _model.addListener(listener2);
803:
804:                //    Utilities.show("calling _runJunit in testNoClassFile");
805:
806:                listener2.runJUnit(doc);
807:                //    Utilities.showDebug("Junit run completed");
808:
809:                if (printMessages)
810:                    System.out.println("after test");
811:                listener2.assertCompileBeforeJUnitCount(1);
812:                listener2.assertNonTestCaseCount(1);
813:                listener2.assertJUnitStartCount(0);
814:                listener2.assertJUnitEndCount(0);
815:                listener2.assertJUnitSuiteStartedCount(0);
816:                listener2.assertJUnitTestStartedCount(0);
817:                listener2.assertJUnitTestEndedCount(0);
818:                _model.removeListener(listener2);
819:                _log.log("testCorrectFilesAfterIncorrectChanges completed");
820:            }
821:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.