1 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 ; 41 42 47 public final class JUnitErrorModelTest extends GlobalModelTestCase { 48 49 private JUnitErrorModel _m; 50 51 private static final String 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 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 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 121 private static final String ABC_CLASS_ONE = 122 "class ABC extends java.util.Vector {}\n"; 123 124 private static final String ABC_CLASS_TWO = 125 "class ABC extends java.util.ArrayList {}\n"; 126 127 private static final String 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 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 142 public void testErrorsArrayInOrder() throws Exception { 143 _m = new JUnitErrorModel(new JUnitError[0], _model, false); 144 OpenDefinitionsDocument doc = setupDocument(MONKEYTEST_FAIL_TEXT); 145 final File file = new File (_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 _model.getJUnitModel().getJUnitDocument().remove 161 (0, _model.getJUnitModel().getJUnitDocument().getLength() - 1); 162 164 _m = _model.getJUnitModel().getJUnitErrorModel(); 166 167 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 } 178 179 185 public void testVerifyErrorHandledCorrectly() throws Exception { 186 OpenDefinitionsDocument doc = setupDocument(ABC_CLASS_ONE); 187 final File file = new File (_tempDir, "ABC1.java"); 188 doc.saveFile(new FileSelector(file)); 189 190 OpenDefinitionsDocument doc2 = setupDocument(ABC_TEST); 191 final File file2 = new File (_tempDir, "ABCTest.java"); 192 doc2.saveFile(new FileSelector(file2)); 193 194 _model.getCompilerModel().compileAll(); 198 199 OpenDefinitionsDocument doc3 = setupDocument(ABC_CLASS_TWO); 200 final File file3 = new File (_tempDir, "ABC2.java"); 201 doc3.saveFile(new FileSelector(file3)); 202 203 JUnitTestListener listener = new JUnitNonTestListener(); 204 doc3.startCompile(); 207 if (_model.getCompilerModel().getNumErrors() > 0) { 208 fail("compile failed: " + getCompilerErrorString()); 209 } 210 _model.addListener(listener); 211 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 232 233 public void testLanguageLevelJUnitErrorLine() throws Exception { 234 235 _m = new JUnitErrorModel(new JUnitError[0], _model, false); 236 OpenDefinitionsDocument doc = setupDocument(LANGUAGE_LEVEL_TEST); 237 final File file = new File (_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 _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 268 public void testErrorInSuperClass() throws Exception { 269 OpenDefinitionsDocument doc1 = setupDocument(TEST_ONE); 270 OpenDefinitionsDocument doc2 = setupDocument(TEST_TWO); 271 final File file1 = new File (_tempDir, "TestOne.java"); 272 final File file2 = new File (_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 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 |