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;
038:
039: import javax.swing.text.BadLocationException;
040: import java.io.File;
041: import java.io.FileWriter;
042: import java.io.IOException;
043: import java.util.List;
044:
045: import edu.rice.cs.drjava.model.OpenDefinitionsDocument;
046: import edu.rice.cs.drjava.model.definitions.InvalidPackageException;
047: import edu.rice.cs.drjava.ui.MainFrame;
048: import edu.rice.cs.plt.io.IOUtil;
049: import edu.rice.cs.util.Log;
050: import edu.rice.cs.util.StringOps;
051: import edu.rice.cs.util.swing.Utilities;
052:
053: import edu.rice.cs.plt.debug.DebugUtil;
054:
055: /**
056: * Tests opening/creating files specified as command line arguments.
057: * @version $Id: CommandLineTest.java 4260 2007-10-10 20:28:34Z mgricken $
058: */
059: public final class CommandLineTest extends DrJavaTestCase {
060: /** File separator, i.e. '/' or '\\'. */
061: private static final char FS = File.separatorChar;
062:
063: /** The MainFrame we're working with. */
064: private MainFrame _mf;
065:
066: /** Files that exist, and the filenames that represent them. */
067: private volatile File f1;
068: private volatile String f1_name;
069: private volatile String f1_contents;
070: private volatile File f2;
071: private volatile String f2_name;
072: private volatile String f2_contents;
073: private volatile File f3;
074: private volatile String f3_name;
075: private volatile String f3_contents;
076: private volatile File f4;
077: private volatile String f4_name;
078: private volatile String f4_contents;
079: private volatile File f5;
080: private volatile String f5_name;
081: private volatile String f5_contents;
082: private volatile File f6;
083: private volatile String f6_name;
084: private volatile String f6_contents;
085: private volatile File f7;
086: private volatile String f7_name;
087: private volatile String f7_contents;
088: private volatile File f8;
089: private volatile String f8_name;
090: private volatile String f8_contents;
091:
092: /** Files that do not exist (constructor deletes them), and their filenames. */
093: private volatile File nof1;
094: private volatile File nof2;
095: private volatile File nof3;
096: private volatile File nof4;
097: private volatile File nof5;
098: private volatile String nof1_name;
099: private volatile String nof2_name;
100: private volatile String nof3_name;
101: private volatile String nof4_name;
102: private volatile String nof5_name;
103:
104: private Log _log = new Log("CommandLineTest.txt", false);
105:
106: /** Constructor. Sets up test files for us to use: (i) three files that exist and can be opened; (ii) three
107: * files that don't exist
108: * @param name the name of the test case
109: */
110: public CommandLineTest(String name) {
111: super (name);
112: }
113:
114: public void setUp() throws Exception {
115: super .setUp();
116:
117: // _log.log("INVOKing DrJava._initConfig() for " + this);
118: // DrJava._initConfig();
119:
120: _log.log("CREATing a MainFrame for " + this );
121: _mf = new MainFrame();
122: _log.log("created a MainFrame for " + this
123: + "; stating file setup");
124:
125: try {
126: f1 = File.createTempFile("DrJava-test", ".java")
127: .getCanonicalFile();
128: f1.deleteOnExit();
129: f1_name = f1.getAbsolutePath();
130: f1_contents = "abcde";
131: FileWriter fw1 = new FileWriter(f1);
132: fw1.write(f1_contents, 0, f1_contents.length());
133: fw1.close();
134: f2 = File.createTempFile("DrJava-test", ".java")
135: .getCanonicalFile();
136: f2.deleteOnExit();
137: f2_name = f2.getAbsolutePath();
138: f2_contents = "fghijklm";
139: FileWriter fw2 = new FileWriter(f2);
140: fw2.write(f2_contents, 0, f2_contents.length());
141: fw2.close();
142: f3 = File.createTempFile("DrJava-test", ".java")
143: .getCanonicalFile();
144: f3.deleteOnExit();
145: f3_name = f3.getAbsolutePath();
146: f3_contents = "nopqrstuvwxyz";
147: FileWriter fw3 = new FileWriter(f3);
148: fw3.write(f3_contents, 0, f3_contents.length());
149: fw3.close();
150: f4 = File.createTempFile("DrJava-test", ".java")
151: .getCanonicalFile();
152: f4.deleteOnExit();
153: f4_name = f4.getAbsolutePath();
154: f4_contents = "abcde";
155: FileWriter fw4 = new FileWriter(f4);
156: fw4.write(f4_contents, 0, f4_contents.length());
157: fw4.close();
158: f5 = File.createTempFile("DrJava-test", ".java")
159: .getCanonicalFile();
160: f5.deleteOnExit();
161: f5_name = f5.getAbsolutePath();
162: f5_contents = "fghijklm";
163: FileWriter fw5 = new FileWriter(f5);
164: fw5.write(f5_contents, 0, f5_contents.length());
165: fw5.close();
166: f6 = File.createTempFile("DrJava-test", ".java")
167: .getCanonicalFile();
168: f6.deleteOnExit();
169: f6_name = f6.getAbsolutePath();
170: f6_contents = "nopqrstuvwxyz";
171: FileWriter fw6 = new FileWriter(f6);
172: fw6.write(f6_contents, 0, f6_contents.length());
173: fw6.close();
174: f7 = File.createTempFile("DrJava-test", ".java")
175: .getCanonicalFile();
176: f7.deleteOnExit();
177: f7_name = f7.getAbsolutePath();
178: f7_contents = "abcde";
179: FileWriter fw7 = new FileWriter(f7);
180: fw7.write(f7_contents, 0, f7_contents.length());
181: fw7.close();
182: f8 = File.createTempFile("DrJava-test", ".java")
183: .getCanonicalFile();
184: f8.deleteOnExit();
185: f8_name = f8.getAbsolutePath();
186: f8_contents = "fghijklm";
187: FileWriter fw8 = new FileWriter(f8);
188: fw8.write(f8_contents, 0, f8_contents.length());
189: fw8.close();
190:
191: nof1 = File.createTempFile("DrJava-test", ".java")
192: .getCanonicalFile();
193: nof1_name = nof1.getAbsolutePath();
194: nof1.delete();
195: nof2 = File.createTempFile("DrJava-test", ".java")
196: .getCanonicalFile();
197: nof2_name = nof2.getAbsolutePath();
198: nof2.delete();
199: nof3 = File.createTempFile("DrJava-test", ".java")
200: .getCanonicalFile();
201: nof3_name = nof3.getAbsolutePath();
202: nof3.delete();
203: nof4 = File.createTempFile("DrJava-test", ".java")
204: .getCanonicalFile();
205: nof4_name = nof4.getAbsolutePath();
206: nof4.delete();
207: nof5 = File.createTempFile("DrJava-test", ".java")
208: .getCanonicalFile();
209: nof5_name = nof5.getAbsolutePath();
210: nof5.delete();
211:
212: _log.log("File initialization (setUp) is complete");
213: } catch (IOException e) {
214: System.out
215: .print("createTempFile failed. This should not happen.");
216: throw new RuntimeException(e.toString());
217: }
218: }
219:
220: public void tearDown() throws Exception {
221: Utilities.invokeAndWait(new Runnable() {
222: public void run() {
223: _mf.dispose();
224: }
225: });
226: _mf = null;
227: super .tearDown();
228: }
229:
230: /** Tests DrJava with no command line arguments. Should open a new, untitled document. */
231: public void testNone() {
232: DrJavaRoot.openCommandLineFiles(_mf, new String[0], false);
233: // ListModel<DefinitionsDocument> docs =
234: // Wouldn't that be nice?
235: List<OpenDefinitionsDocument> docs = _mf.getModel()
236: .getOpenDefinitionsDocuments();
237: assertEquals("Only one document?", 1, docs.size());
238: OpenDefinitionsDocument doc = docs.get(0);
239: assertTrue("Is new document untitled?", doc.isUntitled());
240: _log.log("testNone() completed");
241: }
242:
243: /** Open one file on the command line. Should (obviously) open that file. */
244: public void testOpenOne() throws BadLocationException {
245: String[] list = new String[1];
246: list[0] = f1_name;
247: DrJavaRoot.openCommandLineFiles(_mf, list, false);
248: // _log.log("openCommandLineFiles completed");
249: List<OpenDefinitionsDocument> docs = _mf.getModel()
250: .getOpenDefinitionsDocuments();
251: // Utilities.showDebug(docs.toString());
252: // _log.log("got OpenDefDocs");
253: assertEquals("Only one document opened?", 1, docs.size());
254: OpenDefinitionsDocument doc = docs.get(0);
255: // System.err.println("Doc text = " + doc.getText());
256: assertEquals("Correct length of file?", f1_contents.length(),
257: doc.getLength());
258: // _log.log("Ready to perform getText operation");
259: assertEquals("Do the contents match?", f1_contents, doc
260: .getText(0, f1_contents.length()));
261: _log.log("testOpenOne completed");
262: }
263:
264: /** A nonexistent file. Should open a new, untitled document. */
265: public void testNE() {
266: String[] list = new String[1];
267: list[0] = nof1_name;
268: DrJavaRoot.openCommandLineFiles(_mf, list, false);
269: // _log.log("openCommandLineFiles completed");
270: List<OpenDefinitionsDocument> docs = _mf.getModel()
271: .getOpenDefinitionsDocuments();
272: assertEquals("Exactly one document?", 1, docs.size());
273: OpenDefinitionsDocument doc = docs.get(0);
274: assertTrue("Is document untitled?", doc.isUntitled());
275: _log.log("testNE completed");
276: }
277:
278: /** Many files on the command line. Should open all of them, displaying the last one. */
279: public void testOpenMany() throws BadLocationException {
280: String[] list = new String[3];
281: list[0] = f1_name;
282: list[1] = f2_name;
283: list[2] = f3_name;
284: DrJavaRoot.openCommandLineFiles(_mf, list, false);
285: // _log.log("openCommandLineFiles completed");
286: List<OpenDefinitionsDocument> docs = _mf.getModel()
287: .getOpenDefinitionsDocuments();
288: assertEquals("Exactly three documents?", 3, docs.size());
289: OpenDefinitionsDocument doc1 = docs.get(0);
290: assertEquals("Correct length of file 1?", f1_contents.length(),
291: doc1.getLength());
292: assertEquals("Do the contents of file 1 match?", f1_contents,
293: doc1.getText(0, f1_contents.length()));
294:
295: OpenDefinitionsDocument doc2 = docs.get(1);
296: assertEquals("Correct length of file 2?", f2_contents.length(),
297: doc2.getLength());
298: assertEquals("Do the contents of file 2 match?", f2_contents,
299: doc2.getText(0, f2_contents.length()));
300:
301: OpenDefinitionsDocument doc3 = docs.get(2);
302: assertEquals("Correct length of file 3?", f3_contents.length(),
303: doc3.getLength());
304: assertEquals("Do the contents of file 3 match?", f3_contents,
305: doc3.getText(0, f3_contents.length()));
306:
307: assertEquals("Is the last document the active one?", doc3, _mf
308: .getModel().getActiveDocument());
309: _log.log("testOpenMany completed");
310: }
311:
312: /** Supplying both valid and invalid filenames on the command line. Should open only the valid ones. */
313: public void testMixed() throws BadLocationException {
314: String[] list = new String[6];
315: list[0] = f4_name;
316: list[1] = nof1_name;
317: list[2] = nof2_name;
318: list[3] = f5_name;
319: list[4] = f6_name;
320: list[5] = nof3_name;
321: DrJavaRoot.openCommandLineFiles(_mf, list, false);
322: // _log.log("openCommandLineFiles completed");
323: List<OpenDefinitionsDocument> docs = _mf.getModel()
324: .getOpenDefinitionsDocuments();
325: assertEquals("Exactly three documents?", 3, docs.size());
326: OpenDefinitionsDocument doc1 = docs.get(0);
327: assertEquals("Correct length of file 1?", f4_contents.length(),
328: doc1.getLength());
329: assertEquals("Do the contents of file 1 match?", f4_contents,
330: doc1.getText(0, f4_contents.length()));
331:
332: OpenDefinitionsDocument doc2 = docs.get(1);
333: assertEquals("Correct length of file 2?", f5_contents.length(),
334: doc2.getLength());
335: assertEquals("Do the contents of file 2 match?", f5_contents,
336: doc2.getText(0, f5_contents.length()));
337:
338: OpenDefinitionsDocument doc3 = docs.get(2);
339: assertEquals("Correct length of file 3?", f6_contents.length(),
340: doc3.getLength());
341: assertEquals("Do the contents of file 3 match?", f6_contents,
342: doc3.getText(0, f6_contents.length()));
343:
344: assertEquals("Is the last document the active one?", doc3, _mf
345: .getModel().getActiveDocument());
346: _log.log("testMixed completed");
347: }
348:
349: /** Test duplicate files. */
350: public void testDups() throws BadLocationException {
351: String[] list = new String[6];
352: list[0] = f7_name;
353: list[1] = nof4_name;
354: list[2] = nof5_name;
355: list[3] = f8_name;
356: list[4] = f8_name;
357: list[5] = f7_name;
358: DrJavaRoot.openCommandLineFiles(_mf, list, false);
359: // _log.log("openCommandLineFiles in testDups completed");
360:
361: List<OpenDefinitionsDocument> docs = _mf.getModel()
362: .getOpenDefinitionsDocuments();
363: Utilities.clearEventQueue();
364: assertEquals("Exactly two documents?", 2, docs.size());
365: OpenDefinitionsDocument doc1 = docs.get(0);
366: assertEquals("Correct length of file 1?", f7_contents.length(),
367: doc1.getLength());
368: assertEquals("Do the contents of file 1 match?", f7_contents,
369: doc1.getText(0, f7_contents.length()));
370: Utilities.clearEventQueue();
371: OpenDefinitionsDocument doc2 = docs.get(1);
372: assertEquals("Correct length of file 2?", f8_contents.length(),
373: doc2.getLength());
374: assertEquals("Do the contents of file 2 match?", f8_contents,
375: doc2.getText(0, f8_contents.length()));
376:
377: assertEquals("Is the last document the active one?", doc2, _mf
378: .getModel().getActiveDocument());
379: // _log.log("testDups completed");
380: }
381:
382: /** A regression test for bug #542747, which related to opening a file via the command line using a relative path.
383: * The problem was that getSourceRoot() would fail on the document, because the filename was not absolute. (The
384: * fix will be to absolutize file paths when opening files.)
385: */
386: public void testRelativePath() throws IOException,
387: InvalidPackageException {
388: String funnyName = "DrJava_automatically_deletes_this_1";
389: File newDirectory = mkTempDir(funnyName);
390: File relativeFile = new File(newDirectory, "X.java");
391:
392: assertEquals(relativeFile + " is absolute?", false,
393: relativeFile.isAbsolute());
394:
395: try {
396: checkFile(relativeFile, funnyName);
397: } catch (Exception e) {
398: fail("Exception thrown: " + StringOps.getStackTrace(e));
399: } finally {
400: IOUtil.deleteOnExitRecursively(newDirectory);
401: }
402: _log.log("testRelativePath completed");
403: }
404:
405: /** Tests paths with "." and ".." in them. Windows will blow up if you use one in a JFileChooser without
406: * converting it to a canonical filename.
407: */
408: public void testDotPaths() {
409: String funnyName = "DrJava_automatically_deletes_this_2";
410: File newDirectory = mkTempDir(funnyName);
411:
412: assertTrue("child directory created OK", new File(newDirectory,
413: "childDir").mkdir());
414:
415: File relativeFile = new File(newDirectory, "." + FS + "X.java");
416: File relativeFile2 = new File(newDirectory, "." + FS + "Y.java");
417: File relativeFile3 = new File(newDirectory, "childDir" + FS
418: + ".." + FS + "Z.java");
419:
420: try {
421: checkFile(relativeFile, funnyName);
422: checkFile(relativeFile2, funnyName);
423: checkFile(relativeFile3, funnyName);
424: } catch (Exception e) {
425: fail("Exception thrown: " + StringOps.getStackTrace(e));
426: } finally {
427: IOUtil.deleteOnExitRecursively(newDirectory);
428: }
429: _log.log("testDotPaths completed");
430: }
431:
432: /** Helper for testRelativeFile and testDotPaths. */
433: private File mkTempDir(String funnyName) {
434: // OK, we have to create a directory with a hard-coded name in the current working directory, so we'll make it
435: // strange. If this directory happens to exist, it'll be deleted.
436: File newDirectory = new File(funnyName);
437: if (newDirectory.exists())
438: IOUtil.deleteOnExitRecursively(newDirectory);
439: // System.err.println("newDirectory.exists() = " + newDirectory.exists());
440: assertTrue("directory created OK", newDirectory.mkdir());
441: // _log.log("Temporary directory " + funnyName + " created");
442: return newDirectory;
443: }
444:
445: /** Helper for testRelativeFile and testDotPaths. */
446: private void checkFile(File relativeFile, String funnyName)
447: throws IOException, InvalidPackageException {
448: IOUtil.writeStringToFile(relativeFile, "package " + funnyName
449: + "; class X { }");
450: assertTrue("file exists", relativeFile.exists());
451:
452: String path = relativeFile.getCanonicalPath();
453: DrJavaRoot.openCommandLineFiles(_mf, new String[] { path },
454: false);
455:
456: List<OpenDefinitionsDocument> docs = _mf.getModel()
457: .getOpenDefinitionsDocuments();
458: assertEquals("Number of open documents", 1, docs.size());
459:
460: OpenDefinitionsDocument doc = docs.get(0);
461:
462: assertEquals(
463: "OpenDefDoc file is the right one and is canonical",
464: relativeFile.getCanonicalFile(), doc.getFile());
465:
466: // The source root should be the current directory (as a canonical path, of course).
467: Utilities.clearEventQueue();
468: File root = doc.getSourceRoot();
469: Utilities.clearEventQueue();
470: // System.err.println("Source root is: " + root);
471: // System.err.println("Package name is: " + doc.getPackageName());
472: assertEquals("source root", new File("").getCanonicalFile(),
473: root);
474:
475: // Close this doc to clean up after ourselves for the next check.
476: _mf.getModel().closeFile(doc);
477: }
478: }
|