KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > CommandLineTest


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;
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 JavaDoc;
45 import java.io.File JavaDoc;
46 import java.io.FileWriter JavaDoc;
47 import java.io.IOException JavaDoc;
48 import java.util.List JavaDoc;
49
50 /**
51  * Tests opening/creating files specified as command line arguments.
52  * @version $Id: CommandLineTest.java 4075 2007-01-19 21:35:50Z dlsmith $
53  */

54 public final class CommandLineTest extends DrJavaTestCase {
55   /** File separator, i.e. '/' or '\\'. */
56   private static final char FS = File.separatorChar;
57
58   /** The MainFrame we're working with. */
59   private MainFrame _mf;
60
61   /** Files that exist, and the filenames that represent them. */
62   private volatile File JavaDoc f1;
63   private volatile String JavaDoc f1_name;
64   private volatile String JavaDoc f1_contents;
65   private volatile File JavaDoc f2;
66   private volatile String JavaDoc f2_name;
67   private volatile String JavaDoc f2_contents;
68   private volatile File JavaDoc f3;
69   private volatile String JavaDoc f3_name;
70   private volatile String JavaDoc f3_contents;
71   private volatile File JavaDoc f4;
72   private volatile String JavaDoc f4_name;
73   private volatile String JavaDoc f4_contents;
74   private volatile File JavaDoc f5;
75   private volatile String JavaDoc f5_name;
76   private volatile String JavaDoc f5_contents;
77   private volatile File JavaDoc f6;
78   private volatile String JavaDoc f6_name;
79   private volatile String JavaDoc f6_contents;
80   private volatile File JavaDoc f7;
81   private volatile String JavaDoc f7_name;
82   private volatile String JavaDoc f7_contents;
83   private volatile File JavaDoc f8;
84   private volatile String JavaDoc f8_name;
85   private volatile String JavaDoc f8_contents;
86
87   
88   /** Files that do not exist (constructor deletes them), and their filenames. */
89   private volatile File JavaDoc nof1;
90   private volatile File JavaDoc nof2;
91   private volatile File JavaDoc nof3;
92   private volatile File JavaDoc nof4;
93   private volatile File JavaDoc nof5;
94   private volatile String JavaDoc nof1_name;
95   private volatile String JavaDoc nof2_name;
96   private volatile String JavaDoc nof3_name;
97   private volatile String JavaDoc nof4_name;
98   private volatile String JavaDoc nof5_name;
99   
100   private Log _log = new Log("CommandLineTest.txt", false);
101
102   /** Constructor. Sets up test files for us to use: (i) three files that exist and can be opened; (ii) three
103    * files that don't exist
104    * @param name the name of the test case
105    */

106   public CommandLineTest(String JavaDoc name) { super(name); }
107     
108   public void setUp() throws Exception JavaDoc {
109     super.setUp();
110     
111 // _log.log("INVOKing DrJava._initConfig() for " + this);
112
// DrJava._initConfig();
113

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 JavaDoc fw1 = new FileWriter JavaDoc(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 JavaDoc fw2 = new FileWriter JavaDoc(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 JavaDoc fw3 = new FileWriter JavaDoc(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 JavaDoc fw4 = new FileWriter JavaDoc(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 JavaDoc fw5 = new FileWriter JavaDoc(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 JavaDoc fw6 = new FileWriter JavaDoc(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 JavaDoc fw7 = new FileWriter JavaDoc(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 JavaDoc fw8 = new FileWriter JavaDoc(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 JavaDoc e) {
195       System.out.print("createTempFile failed. This should not happen.");
196       throw new RuntimeException JavaDoc(e.toString());
197     }
198   }
199
200   public void tearDown() throws Exception JavaDoc {
201     Utilities.invokeAndWait(new Runnable JavaDoc() { public void run() { _mf.dispose(); } });
202     _mf = null;
203     super.tearDown();
204   }
205
206    /** Tests DrJava with no command line arguments. Should open a new, untitled document. */
207   public void testNone() {
208     DrJavaRoot.openCommandLineFiles(_mf, new String JavaDoc[0]);
209     // ListModel<DefinitionsDocument> docs =
210
// Wouldn't that be nice?
211
List JavaDoc<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   /** Open one file on the command line. Should (obviously) open that file. */
219   public void testOpenOne() throws BadLocationException JavaDoc {
220     String JavaDoc[] list = new String JavaDoc[1];
221     list[0] = f1_name;
222     DrJavaRoot.openCommandLineFiles(_mf, list);
223 // _log.log("openCommandLineFiles completed");
224
List JavaDoc<OpenDefinitionsDocument> docs = _mf.getModel().getOpenDefinitionsDocuments();
225 // Utilities.showDebug(docs.toString());
226
// _log.log("got OpenDefDocs");
227
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 // _log.log("Ready to perform getText operation");
231
assertEquals("Do the contents match?", f1_contents, doc.getText(0,f1_contents.length()));
232     _log.log("testOpenOne completed");
233   }
234
235   /** A nonexistent file. Should open a new, untitled document. */
236   public void testNE() {
237     String JavaDoc[] list = new String JavaDoc[1];
238     list[0] = nof1_name;
239     DrJavaRoot.openCommandLineFiles(_mf, list);
240 // _log.log("openCommandLineFiles completed");
241
List JavaDoc<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   /** Many files on the command line. Should open all of them, displaying the last one. */
249   public void testOpenMany() throws BadLocationException JavaDoc {
250     String JavaDoc[] list = new String JavaDoc[3];
251     list[0] = f1_name;
252     list[1] = f2_name;
253     list[2] = f3_name;
254     DrJavaRoot.openCommandLineFiles(_mf, list);
255 // _log.log("openCommandLineFiles completed");
256
List JavaDoc<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   /** Supplying both valid and invalid filenames on the command line. Should open only the valid ones. */
275   public void testMixed() throws BadLocationException JavaDoc {
276     String JavaDoc[] list = new String JavaDoc[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 // _log.log("openCommandLineFiles completed");
285
List JavaDoc<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   /** Test duplicate files. */
304   public void testDups() throws BadLocationException JavaDoc {
305     String JavaDoc[] list = new String JavaDoc[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 // _log.log("openCommandLineFiles in testDups completed");
314

315     List JavaDoc<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 // _log.log("testDups completed");
328
}
329
330   /** A regression test for bug #542747, which related to opening a file via the command line using a relative path.
331    * The problem was that getSourceRoot() would fail on the document, because the filename was not absolute. (The
332    * fix will be to absolutize file paths when opening files.)
333    */

334   public void testRelativePath() throws IOException JavaDoc, InvalidPackageException {
335     String JavaDoc funnyName = "DrJava_automatically_deletes_this_1";
336     File JavaDoc newDirectory = mkTempDir(funnyName);
337     File JavaDoc relativeFile = new File JavaDoc(newDirectory, "X.java");
338
339     assertEquals(relativeFile + " is absolute?", false, relativeFile.isAbsolute());
340
341     try { checkFile(relativeFile, funnyName); }
342     catch (Exception JavaDoc e) { fail("Exception thrown: " + StringOps.getStackTrace(e)); }
343     finally { IOUtil.deleteOnExitRecursively(newDirectory); }
344     _log.log("testRelativePath completed");
345   }
346
347   /** Tests paths with "." and ".." in them. Windows will blow up if you use one in a JFileChooser without
348    * converting it to a canonical filename.
349    */

350   public void testDotPaths() {
351     String JavaDoc funnyName = "DrJava_automatically_deletes_this_2";
352     File JavaDoc newDirectory = mkTempDir(funnyName);
353
354     assertTrue("child directory created OK", new File JavaDoc(newDirectory, "childDir").mkdir());
355
356     File JavaDoc relativeFile = new File JavaDoc(newDirectory, "."+FS+"X.java");
357     File JavaDoc relativeFile2 = new File JavaDoc(newDirectory, "."+FS+"Y.java");
358     File JavaDoc relativeFile3 = new File JavaDoc(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 JavaDoc e) { fail("Exception thrown: " + StringOps.getStackTrace(e)); }
366     finally { IOUtil.deleteOnExitRecursively(newDirectory); }
367     _log.log("testDotPaths completed");
368   }
369
370   /** Helper for testRelativeFile and testDotPaths. */
371   private File JavaDoc mkTempDir(String JavaDoc funnyName) {
372     // OK, we have to create a directory with a hard-coded name in the current working directory, so we'll make it
373
// strange. If this directory happens to exist, it'll be deleted.
374
File JavaDoc newDirectory = new File JavaDoc(funnyName);
375     if (newDirectory.exists()) IOUtil.deleteOnExitRecursively(newDirectory);
376
377     assertTrue("directory created OK", newDirectory.mkdir());
378 // _log.log("Temporary directory " + funnyName + " created");
379
return newDirectory;
380   }
381
382   /** Helper for testRelativeFile and testDotPaths. */
383   private void checkFile(File JavaDoc relativeFile, String JavaDoc funnyName) throws IOException JavaDoc, InvalidPackageException {
384     IOUtil.writeStringToFile(relativeFile, "package " + funnyName + "; class X { }");
385     assertTrue("file exists", relativeFile.exists());
386
387     String JavaDoc path = relativeFile.getCanonicalPath();
388     DrJavaRoot.openCommandLineFiles(_mf, new String JavaDoc[] { path });
389
390     List JavaDoc<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     // The source root should be the current directory (as a canonical path, of course).
398
Utilities.clearEventQueue();
399     File JavaDoc root = doc.getSourceRoot();
400     Utilities.clearEventQueue();
401 // System.err.println("Source root is: " + root);
402
// System.err.println("Package name is: " + doc.getPackageName());
403
assertEquals("source root", new File JavaDoc("").getCanonicalFile(), root);
404    
405     // Close this doc to clean up after ourselves for the next check.
406
_mf.getModel().closeFile(doc);
407   }
408 }
409
Popular Tags