1 33 34 package edu.rice.cs.drjava; 35 36 import edu.rice.cs.drjava.model.OpenDefinitionsDocument; 37 import edu.rice.cs.drjava.model.definitions.InvalidPackageException; 38 import edu.rice.cs.drjava.ui.MainFrame; 39 import edu.rice.cs.plt.io.IOUtil; 40 import edu.rice.cs.util.Log; 41 import edu.rice.cs.util.StringOps; 42 import edu.rice.cs.util.swing.Utilities; 43 44 import javax.swing.text.BadLocationException ; 45 import java.io.File ; 46 import java.io.FileWriter ; 47 import java.io.IOException ; 48 import java.util.List ; 49 50 54 public final class CommandLineTest extends DrJavaTestCase { 55 56 private static final char FS = File.separatorChar; 57 58 59 private MainFrame _mf; 60 61 62 private volatile File f1; 63 private volatile String f1_name; 64 private volatile String f1_contents; 65 private volatile File f2; 66 private volatile String f2_name; 67 private volatile String f2_contents; 68 private volatile File f3; 69 private volatile String f3_name; 70 private volatile String f3_contents; 71 private volatile File f4; 72 private volatile String f4_name; 73 private volatile String f4_contents; 74 private volatile File f5; 75 private volatile String f5_name; 76 private volatile String f5_contents; 77 private volatile File f6; 78 private volatile String f6_name; 79 private volatile String f6_contents; 80 private volatile File f7; 81 private volatile String f7_name; 82 private volatile String f7_contents; 83 private volatile File f8; 84 private volatile String f8_name; 85 private volatile String f8_contents; 86 87 88 89 private volatile File nof1; 90 private volatile File nof2; 91 private volatile File nof3; 92 private volatile File nof4; 93 private volatile File nof5; 94 private volatile String nof1_name; 95 private volatile String nof2_name; 96 private volatile String nof3_name; 97 private volatile String nof4_name; 98 private volatile String nof5_name; 99 100 private Log _log = new Log("CommandLineTest.txt", false); 101 102 106 public CommandLineTest(String name) { super(name); } 107 108 public void setUp() throws Exception { 109 super.setUp(); 110 111 114 _log.log("CREATing a MainFrame for " + this); 115 _mf = new MainFrame(); 116 _log.log("created a MainFrame for " + this + "; stating file setup"); 117 118 try { 119 f1 = File.createTempFile("DrJava-test", ".java").getCanonicalFile(); 120 f1.deleteOnExit(); 121 f1_name = f1.getAbsolutePath(); 122 f1_contents = "abcde"; 123 FileWriter fw1 = new FileWriter (f1); 124 fw1.write(f1_contents,0,f1_contents.length()); 125 fw1.close(); 126 f2 = File.createTempFile("DrJava-test", ".java").getCanonicalFile(); 127 f2.deleteOnExit(); 128 f2_name = f2.getAbsolutePath(); 129 f2_contents = "fghijklm"; 130 FileWriter fw2 = new FileWriter (f2); 131 fw2.write(f2_contents,0,f2_contents.length()); 132 fw2.close(); 133 f3 = File.createTempFile("DrJava-test", ".java").getCanonicalFile(); 134 f3.deleteOnExit(); 135 f3_name = f3.getAbsolutePath(); 136 f3_contents = "nopqrstuvwxyz"; 137 FileWriter fw3 = new FileWriter (f3); 138 fw3.write(f3_contents,0,f3_contents.length()); 139 fw3.close(); 140 f4 = File.createTempFile("DrJava-test", ".java").getCanonicalFile(); 141 f4.deleteOnExit(); 142 f4_name = f4.getAbsolutePath(); 143 f4_contents = "abcde"; 144 FileWriter fw4 = new FileWriter (f4); 145 fw4.write(f4_contents,0,f4_contents.length()); 146 fw4.close(); 147 f5 = File.createTempFile("DrJava-test", ".java").getCanonicalFile(); 148 f5.deleteOnExit(); 149 f5_name = f5.getAbsolutePath(); 150 f5_contents = "fghijklm"; 151 FileWriter fw5 = new FileWriter (f5); 152 fw5.write(f5_contents,0,f5_contents.length()); 153 fw5.close(); 154 f6 = File.createTempFile("DrJava-test", ".java").getCanonicalFile(); 155 f6.deleteOnExit(); 156 f6_name = f6.getAbsolutePath(); 157 f6_contents = "nopqrstuvwxyz"; 158 FileWriter fw6 = new FileWriter (f6); 159 fw6.write(f6_contents,0,f6_contents.length()); 160 fw6.close(); 161 f7 = File.createTempFile("DrJava-test", ".java").getCanonicalFile(); 162 f7.deleteOnExit(); 163 f7_name = f7.getAbsolutePath(); 164 f7_contents = "abcde"; 165 FileWriter fw7 = new FileWriter (f7); 166 fw7.write(f7_contents,0,f7_contents.length()); 167 fw7.close(); 168 f8 = File.createTempFile("DrJava-test", ".java").getCanonicalFile(); 169 f8.deleteOnExit(); 170 f8_name = f8.getAbsolutePath(); 171 f8_contents = "fghijklm"; 172 FileWriter fw8 = new FileWriter (f8); 173 fw8.write(f8_contents,0,f8_contents.length()); 174 fw8.close(); 175 176 nof1 = File.createTempFile("DrJava-test", ".java").getCanonicalFile(); 177 nof1_name = nof1.getAbsolutePath(); 178 nof1.delete(); 179 nof2 = File.createTempFile("DrJava-test", ".java").getCanonicalFile(); 180 nof2_name = nof2.getAbsolutePath(); 181 nof2.delete(); 182 nof3 = File.createTempFile("DrJava-test", ".java").getCanonicalFile(); 183 nof3_name = nof3.getAbsolutePath(); 184 nof3.delete(); 185 nof4 = File.createTempFile("DrJava-test", ".java").getCanonicalFile(); 186 nof4_name = nof4.getAbsolutePath(); 187 nof4.delete(); 188 nof5 = File.createTempFile("DrJava-test", ".java").getCanonicalFile(); 189 nof5_name = nof5.getAbsolutePath(); 190 nof5.delete(); 191 192 _log.log("File initialization (setUp) is complete"); 193 } 194 catch (IOException e) { 195 System.out.print("createTempFile failed. This should not happen."); 196 throw new RuntimeException (e.toString()); 197 } 198 } 199 200 public void tearDown() throws Exception { 201 Utilities.invokeAndWait(new Runnable () { public void run() { _mf.dispose(); } }); 202 _mf = null; 203 super.tearDown(); 204 } 205 206 207 public void testNone() { 208 DrJavaRoot.openCommandLineFiles(_mf, new String [0]); 209 List <OpenDefinitionsDocument> docs = _mf.getModel().getOpenDefinitionsDocuments(); 212 assertEquals("Only one document?", 1, docs.size()); 213 OpenDefinitionsDocument doc = docs.get(0); 214 assertTrue("Is new document untitled?", doc.isUntitled()); 215 _log.log("testNone() completed"); 216 } 217 218 219 public void testOpenOne() throws BadLocationException { 220 String [] list = new String [1]; 221 list[0] = f1_name; 222 DrJavaRoot.openCommandLineFiles(_mf, list); 223 List <OpenDefinitionsDocument> docs = _mf.getModel().getOpenDefinitionsDocuments(); 225 assertEquals("Only one document opened?", 1, docs.size()); 228 OpenDefinitionsDocument doc = docs.get(0); 229 assertEquals("Correct length of file?", f1_contents.length(), doc.getLength()); 230 assertEquals("Do the contents match?", f1_contents, doc.getText(0,f1_contents.length())); 232 _log.log("testOpenOne completed"); 233 } 234 235 236 public void testNE() { 237 String [] list = new String [1]; 238 list[0] = nof1_name; 239 DrJavaRoot.openCommandLineFiles(_mf, list); 240 List <OpenDefinitionsDocument> docs = _mf.getModel().getOpenDefinitionsDocuments(); 242 assertEquals("Exactly one document?", 1, docs.size()); 243 OpenDefinitionsDocument doc = docs.get(0); 244 assertTrue("Is document untitled?", doc.isUntitled()); 245 _log.log("testNE completed"); 246 } 247 248 249 public void testOpenMany() throws BadLocationException { 250 String [] list = new String [3]; 251 list[0] = f1_name; 252 list[1] = f2_name; 253 list[2] = f3_name; 254 DrJavaRoot.openCommandLineFiles(_mf, list); 255 List <OpenDefinitionsDocument> docs = _mf.getModel().getOpenDefinitionsDocuments(); 257 assertEquals("Exactly three documents?", 3, docs.size()); 258 OpenDefinitionsDocument doc1 = docs.get(0); 259 assertEquals("Correct length of file 1?", f1_contents.length(), doc1.getLength()); 260 assertEquals("Do the contents of file 1 match?", f1_contents, doc1.getText(0,f1_contents.length())); 261 262 OpenDefinitionsDocument doc2 = docs.get(1); 263 assertEquals("Correct length of file 2?", f2_contents.length(), doc2.getLength()); 264 assertEquals("Do the contents of file 2 match?", f2_contents, doc2.getText(0,f2_contents.length())); 265 266 OpenDefinitionsDocument doc3 = docs.get(2); 267 assertEquals("Correct length of file 3?", f3_contents.length(), doc3.getLength()); 268 assertEquals("Do the contents of file 3 match?", f3_contents, doc3.getText(0,f3_contents.length())); 269 270 assertEquals("Is the last document the active one?", doc3, _mf.getModel().getActiveDocument()); 271 _log.log("testOpenMany completed"); 272 } 273 274 275 public void testMixed() throws BadLocationException { 276 String [] list = new String [6]; 277 list[0] = f4_name; 278 list[1] = nof1_name; 279 list[2] = nof2_name; 280 list[3] = f5_name; 281 list[4] = f6_name; 282 list[5] = nof3_name; 283 DrJavaRoot.openCommandLineFiles(_mf, list); 284 List <OpenDefinitionsDocument> docs = _mf.getModel().getOpenDefinitionsDocuments(); 286 assertEquals("Exactly three documents?", 3, docs.size()); 287 OpenDefinitionsDocument doc1 = docs.get(0); 288 assertEquals("Correct length of file 1?", f4_contents.length(), doc1.getLength()); 289 assertEquals("Do the contents of file 1 match?", f4_contents, doc1.getText(0,f4_contents.length())); 290 291 OpenDefinitionsDocument doc2 = docs.get(1); 292 assertEquals("Correct length of file 2?", f5_contents.length(), doc2.getLength()); 293 assertEquals("Do the contents of file 2 match?", f5_contents, doc2.getText(0,f5_contents.length())); 294 295 OpenDefinitionsDocument doc3 = docs.get(2); 296 assertEquals("Correct length of file 3?", f6_contents.length(), doc3.getLength()); 297 assertEquals("Do the contents of file 3 match?", f6_contents, doc3.getText(0,f6_contents.length())); 298 299 assertEquals("Is the last document the active one?", doc3, _mf.getModel().getActiveDocument()); 300 _log.log("testMixed completed"); 301 } 302 303 304 public void testDups() throws BadLocationException { 305 String [] list = new String [6]; 306 list[0] = f7_name; 307 list[1] = nof4_name; 308 list[2] = nof5_name; 309 list[3] = f8_name; 310 list[4] = f8_name; 311 list[5] = f7_name; 312 DrJavaRoot.openCommandLineFiles(_mf, list); 313 315 List <OpenDefinitionsDocument> docs = _mf.getModel().getOpenDefinitionsDocuments(); 316 Utilities.clearEventQueue(); 317 assertEquals("Exactly two documents?", 2, docs.size()); 318 OpenDefinitionsDocument doc1 = docs.get(0); 319 assertEquals("Correct length of file 1?", f7_contents.length(), doc1.getLength()); 320 assertEquals("Do the contents of file 1 match?", f7_contents, doc1.getText(0,f7_contents.length())); 321 Utilities.clearEventQueue(); 322 OpenDefinitionsDocument doc2 = docs.get(1); 323 assertEquals("Correct length of file 2?", f8_contents.length(), doc2.getLength()); 324 assertEquals("Do the contents of file 2 match?", f8_contents, doc2.getText(0,f8_contents.length())); 325 326 assertEquals("Is the last document the active one?", doc2, _mf.getModel().getActiveDocument()); 327 } 329 330 334 public void testRelativePath() throws IOException , InvalidPackageException { 335 String funnyName = "DrJava_automatically_deletes_this_1"; 336 File newDirectory = mkTempDir(funnyName); 337 File relativeFile = new File (newDirectory, "X.java"); 338 339 assertEquals(relativeFile + " is absolute?", false, relativeFile.isAbsolute()); 340 341 try { checkFile(relativeFile, funnyName); } 342 catch (Exception e) { fail("Exception thrown: " + StringOps.getStackTrace(e)); } 343 finally { IOUtil.deleteOnExitRecursively(newDirectory); } 344 _log.log("testRelativePath completed"); 345 } 346 347 350 public void testDotPaths() { 351 String funnyName = "DrJava_automatically_deletes_this_2"; 352 File newDirectory = mkTempDir(funnyName); 353 354 assertTrue("child directory created OK", new File (newDirectory, "childDir").mkdir()); 355 356 File relativeFile = new File (newDirectory, "."+FS+"X.java"); 357 File relativeFile2 = new File (newDirectory, "."+FS+"Y.java"); 358 File relativeFile3 = new File (newDirectory, "childDir"+FS+".."+FS+"Z.java"); 359 360 try { 361 checkFile(relativeFile, funnyName); 362 checkFile(relativeFile2, funnyName); 363 checkFile(relativeFile3, funnyName); 364 } 365 catch (Exception e) { fail("Exception thrown: " + StringOps.getStackTrace(e)); } 366 finally { IOUtil.deleteOnExitRecursively(newDirectory); } 367 _log.log("testDotPaths completed"); 368 } 369 370 371 private File mkTempDir(String funnyName) { 372 File newDirectory = new File (funnyName); 375 if (newDirectory.exists()) IOUtil.deleteOnExitRecursively(newDirectory); 376 377 assertTrue("directory created OK", newDirectory.mkdir()); 378 return newDirectory; 380 } 381 382 383 private void checkFile(File relativeFile, String funnyName) throws IOException , InvalidPackageException { 384 IOUtil.writeStringToFile(relativeFile, "package " + funnyName + "; class X { }"); 385 assertTrue("file exists", relativeFile.exists()); 386 387 String path = relativeFile.getCanonicalPath(); 388 DrJavaRoot.openCommandLineFiles(_mf, new String [] { path }); 389 390 List <OpenDefinitionsDocument> docs = _mf.getModel().getOpenDefinitionsDocuments(); 391 assertEquals("Number of open documents", 1, docs.size()); 392 393 OpenDefinitionsDocument doc = docs.get(0); 394 395 assertEquals("OpenDefDoc file is the right one and is canonical", relativeFile.getCanonicalFile(), doc.getFile()); 396 397 Utilities.clearEventQueue(); 399 File root = doc.getSourceRoot(); 400 Utilities.clearEventQueue(); 401 assertEquals("source root", new File ("").getCanonicalFile(), root); 404 405 _mf.getModel().closeFile(doc); 407 } 408 } 409 | Popular Tags |