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.project;
038:
039: import edu.rice.cs.drjava.DrJavaTestCase;
040: import edu.rice.cs.plt.tuple.Pair;
041: import edu.rice.cs.plt.io.IOUtil;
042: import edu.rice.cs.plt.iter.IterUtil;
043:
044: import static edu.rice.cs.util.StringOps.convertToLiteral;
045:
046: import edu.rice.cs.util.sexp.SEList;
047: import edu.rice.cs.util.sexp.SExpParseException;
048: import edu.rice.cs.util.sexp.SExpParser;
049:
050: import java.io.File;
051: import java.io.FileReader;
052: import java.io.FileWriter;
053: import java.io.IOException;
054:
055: //import java.text.SimpleDateFormat;
056:
057: /** Test class for project files */
058: public class ProjectTest extends DrJavaTestCase {
059:
060: File base;
061: File parent;
062: File buildDir;
063: File srcDir;
064:
065: private String absp; // absolute path
066:
067: public void setUp() throws Exception {
068: super .setUp();
069: try {
070: base = new File(System.getProperty("java.io.tmpdir"))
071: .getCanonicalFile();
072: parent = IOUtil
073: .createAndMarkTempDirectory("proj", "", base);
074: buildDir = new File(parent, "built");
075: buildDir.mkdir(); // create the specified directory
076: srcDir = new File(parent, "src");
077: srcDir.mkdir(); // create the specified directory
078: absp = parent.getCanonicalPath() + File.separator;
079: IOUtil.deleteOnExitRecursively(parent);
080: } catch (IOException e) {
081: fail("could not initialize temp path string");
082: }
083: }
084:
085: /** Test to make sure all elements of the project are read correctly into the IR */
086: public void testLegacyParseProject() throws IOException,
087: MalformedProjectFileException, java.text.ParseException {
088: String proj1 = ";; DrJava project file. Written with build: 20040623-1933\n"
089: + "(source ;; comment\n"
090: + " (file (name \"src/sexp/Atom.java\")(select 32 32)(mod-date \"16-Jul-2004 03:45:23\"))\n"
091: + " (file (name \"src/sexp/BoolAtom.java\")(select 0 0)(mod-date \"16-Jul-2004 03:45:23\"))\n"
092: + " (file (name \"src/sexp/Cons.java\")(select 0 0)(mod-date \"16-Jul-2004 03:45:23\"))\n"
093: + " (file (name \"src/sexp/Empty.java\")(select 24 28)(mod-date \"16-Jul-2004 03:45:23\"))\n"
094: + " (file (name \"src/sexp/Lexer.java\")(select 0 0)(mod-date \"16-Jul-2004 03:45:23\"))\n"
095: + " (file (name \"src/sexp/NumberAtom.java\")(select 12 12)(mod-date \"16-Jul-2004 03:45:23\"))\n"
096: + " (file (name \"src/sexp/SEList.java\")(select 0 0)))\n"
097: + // doesn't have mod date
098: "(auxiliary ;; absolute file names\n"
099: + " (file (name "
100: + convertToLiteral(new File(parent,
101: "junk/sexp/Tokens.java").getCanonicalPath())
102: + ")(select 32 32)(mod-date \"16-Jul-2004 03:45:23\"))\n"
103: + " (file (name "
104: + convertToLiteral(new File(parent,
105: "jdk1.5.0/JScrollPane.java").getCanonicalPath())
106: + ")(select 9086 8516)(mod-date \"16-Jul-2004 03:45:23\")))\n"
107: + "(collapsed ;; relative paths\n"
108: + " (path \"./[ Source Files ]/sexp/\")\n"
109: + " (path \"./[ External ]/\"))\n"
110: + "(build-dir ;; absolute path\n"
111: + " (file (name "
112: + convertToLiteral(new File(parent, "built")
113: .getCanonicalPath())
114: + ")))\n"
115: + "(work-dir ;; absolute path\n"
116: + " (file (name "
117: + convertToLiteral(new File(parent, "src")
118: .getCanonicalPath())
119: + ")))\n"
120: + "(proj-root ;; absolute path\n"
121: + " (file (name "
122: + convertToLiteral(new File(parent, "src")
123: .getCanonicalPath())
124: + ")))\n"
125: + "(classpaths\n"
126: + " (file (name "
127: + convertToLiteral(new File(parent,
128: "src/edu/rice/cs/lib").getCanonicalPath())
129: + ")))\n"
130: + "(main-class\n"
131: + " (file (name \"src/sexp/SEList.java\")))";
132:
133: File f = new File(parent, "test1.pjt");
134:
135: IOUtil.writeStringToFile(f, proj1);
136: // System.err.println("Project directory is " + parent);
137: // System.err.println("Project file is " + f);
138: // System.err.println("projFile exists? " + f.exists());
139: ProjectFileIR pfir = ProjectFileParser.ONLY.parse(f);
140: // System.err.println("buildDir = " + pfir.getBuildDirectory().getCanonicalPath());
141: assertEquals("number of source files", 7,
142: pfir.getSourceFiles().length);
143: assertEquals("number of aux files", 2,
144: pfir.getAuxiliaryFiles().length);
145: assertEquals("number of collapsed", 2,
146: pfir.getCollapsedPaths().length);
147: assertEquals("number of classpaths", 1, IterUtil.sizeOf(pfir
148: .getClassPaths()));
149: File base = f.getParentFile();
150: assertEquals("first source filename", new File(base,
151: "src/sexp/Atom.java").getPath(),
152: pfir.getSourceFiles()[0].getPath());
153: assertEquals("mod-date value", ProjectProfile.MOD_DATE_FORMAT
154: .parse("16-Jul-2004 03:45:23").getTime(), pfir
155: .getSourceFiles()[0].getSavedModDate());
156: assertEquals("last source filename", new File(base,
157: "src/sexp/SEList.java").getPath(), pfir
158: .getSourceFiles()[6].getPath());
159: assertEquals("first aux filename", new File(base,
160: "junk/sexp/Tokens.java").getPath(), pfir
161: .getAuxiliaryFiles()[0].getCanonicalPath());
162: assertEquals("last collapsed path", "./[ External ]/", pfir
163: .getCollapsedPaths()[1]);
164: assertEquals("build-dir name", new File(base, "built")
165: .getCanonicalPath(), pfir.getBuildDirectory()
166: .getCanonicalPath());
167: assertEquals("work-dir name", new File(base, "src")
168: .getCanonicalPath(), pfir.getWorkingDirectory()
169: .getCanonicalPath());
170: assertEquals("classpath name", new File(base,
171: "src/edu/rice/cs/lib").getCanonicalPath(), IterUtil
172: .first(pfir.getClassPaths()).getCanonicalPath());
173: assertEquals("main-class name", new File(base,
174: "src/sexp/SEList.java").getCanonicalPath(), pfir
175: .getMainClass().getCanonicalPath());
176: }
177:
178: /** Test to make sure all elements of the project are read correctly into the IR */
179: public void testParseProject() throws IOException,
180: MalformedProjectFileException, java.text.ParseException {
181: String proj1 = ";; DrJava project file. Written with build: 2006??\n"
182: + "(proj-root-and-base (file (name \"src\")))\n"
183: + "(source-files ;; comment\n"
184: + " (file (name \"sexp/Atom.java\")(select 32 32)(mod-date \"16-Jul-2004 03:45:23\"))\n"
185: + " (file (name \"sexp/BoolAtom.java\")(select 0 0)(mod-date \"16-Jul-2004 03:45:23\"))\n"
186: + " (file (name \"sexp/Cons.java\")(select 0 0)(mod-date \"16-Jul-2004 03:45:23\"))\n"
187: + " (file (name \"sexp/Empty.java\")(select 24 28)(mod-date \"16-Jul-2004 03:45:23\"))\n"
188: + " (file (name \"sexp/Lexer.java\")(select 0 0)(mod-date \"16-Jul-2004 03:45:23\"))\n"
189: + " (file (name \"sexp/NumberAtom.java\")(select 12 12)(mod-date \"16-Jul-2004 03:45:23\"))\n"
190: + " (file (name \"sexp/SEList.java\")(select 0 0)))\n"
191: + // doesn't have mod date
192: "(auxiliary ;; absolute file names\n"
193: + " (file (name "
194: + convertToLiteral(new File(parent,
195: "junk/sexp/Tokens.java").getCanonicalPath())
196: + ")(select 32 32)(mod-date \"16-Jul-2004 03:45:23\"))\n"
197: + " (file (name "
198: + convertToLiteral(new File(parent,
199: "jdk1.5.0/JScrollPane.java").getCanonicalPath())
200: + ")(select 9086 8516)(mod-date \"16-Jul-2004 03:45:23\")))\n"
201: + "(collapsed ;; relative paths\n"
202: + " (path \"./[ Source Files ]/sexp/\")\n"
203: + " (path \"./[ External ]/\"))\n"
204: + "(build-dir ;; absolute path\n"
205: + " (file (name "
206: + convertToLiteral(new File(parent, "built")
207: .getCanonicalPath())
208: + ")))\n"
209: + "(work-dir (file (name \"src\")))\n"
210: + "(classpaths\n"
211: + " (file (name "
212: + convertToLiteral(new File(parent,
213: "src/edu/rice/cs/lib").getCanonicalPath())
214: + ")))\n"
215: + "(main-class\n"
216: + " (file (name \"src/sexp/SEList.java\")))";
217:
218: File f = new File(parent, "test1.pjt");
219:
220: IOUtil.writeStringToFile(f, proj1);
221: // System.err.println("Project directory is " + parent);
222: // System.err.println("Project file is " + f);
223: // System.err.println("projFile exists? " + f.exists());
224: ProjectFileIR pfir = ProjectFileParser.ONLY.parse(f);
225: // System.err.println("buildDir = " + pfir.getBuildDirectory().getCanonicalPath());
226: assertEquals("number of source files", 7,
227: pfir.getSourceFiles().length);
228: assertEquals("number of aux files", 2,
229: pfir.getAuxiliaryFiles().length);
230: assertEquals("number of collapsed", 2,
231: pfir.getCollapsedPaths().length);
232: assertEquals("number of classpaths", 1, IterUtil.sizeOf(pfir
233: .getClassPaths()));
234: File base = f.getParentFile();
235: File root = new File(base, "src");
236: assertEquals("proj-root-and-base", root.getPath(), pfir
237: .getProjectRoot().getPath());
238: assertEquals("first source filename", new File(base,
239: "src/sexp/Atom.java").getPath(),
240: pfir.getSourceFiles()[0].getPath());
241: assertEquals("mod-date value", ProjectProfile.MOD_DATE_FORMAT
242: .parse("16-Jul-2004 03:45:23").getTime(), pfir
243: .getSourceFiles()[0].getSavedModDate());
244: assertEquals("last source filename", new File(root,
245: "sexp/SEList.java").getPath(), pfir.getSourceFiles()[6]
246: .getPath());
247: assertEquals("first aux filename", new File(base,
248: "junk/sexp/Tokens.java").getPath(), pfir
249: .getAuxiliaryFiles()[0].getCanonicalPath());
250: assertEquals("last collapsed path", "./[ External ]/", pfir
251: .getCollapsedPaths()[1]);
252: assertEquals("build-dir name", new File(base, "built")
253: .getCanonicalPath(), pfir.getBuildDirectory()
254: .getCanonicalPath());
255: assertEquals("work-dir name", new File(base, "src")
256: .getCanonicalPath(), pfir.getWorkingDirectory()
257: .getCanonicalPath());
258: assertEquals("classpath name", new File(base,
259: "src/edu/rice/cs/lib").getCanonicalPath(), IterUtil
260: .first(pfir.getClassPaths()).getCanonicalPath());
261: assertEquals("main-class name", new File(root,
262: "sexp/SEList.java").getCanonicalPath(), pfir
263: .getMainClass().getCanonicalPath());
264: }
265:
266: public void testParseFile() throws SExpParseException {
267: SEList c = SExpParser.parse(
268: "(file (name \"file-name\") (select 1 2))").get(0);
269: DocFile df = ProjectFileParser.ONLY.parseFile(c, null);
270: Pair<Integer, Integer> p = df.getSelection();
271: assertEquals("First int should be a 1", 1, (int) p.first()); //need cast to prevent ambiguity
272: assertEquals("Second int should be a 2", 2, (int) p.second());//need cast to prevent ambiguity
273: assertEquals("Name should have been file-name", "file-name", df
274: .getPath());
275: }
276:
277: public void testWriteFile() throws IOException,
278: MalformedProjectFileException {
279: File pf = new File(parent, "test2.pjt");
280: IOUtil.writeStringToFile(pf, "");
281: ProjectProfile fb = new ProjectProfile(pf);
282: String sr = pf.getCanonicalFile().getParent();
283:
284: fb.addSourceFile(makeGetter(0, 0, 0, 0, "dir1/testfile1.java",
285: "dir1", false, false, pf));
286: fb.addSourceFile(makeGetter(1, 1, 0, 0, "dir1/testfile2.java",
287: "dir1", false, false, pf));
288: fb.addSourceFile(makeGetter(20, 22, 0, 0,
289: "dir2/testfile3.java", "dir2", false, false, pf));
290: fb.addSourceFile(makeGetter(1, 1, 0, 0, "dir2/testfile4.java",
291: "dir2", true, false, pf));
292: fb.addSourceFile(makeGetter(0, 0, 0, 0, "dir3/testfile5.java",
293: "", false, false, pf));
294: fb.addAuxiliaryFile(makeGetter(1, 1, 0, 0, absp
295: + "test/testfile6.java", "/home/javaplt", false, false,
296: null));
297: fb.addAuxiliaryFile(makeGetter(1, 1, 0, 0, absp
298: + "test/testfile7.java", "/home/javaplt", false, false,
299: null));
300: fb.addCollapsedPath("./[ Source Files ]/dir1/");
301: fb.addClassPathFile(new File(parent, "lib"));
302: fb.setBuildDirectory(new File(parent, "built"));
303: fb.setWorkingDirectory(new File(parent, "src"));
304: fb.setMainClass(new File(pf.getParentFile(),
305: "dir1/testfile1.java"));
306:
307: String expected = "";
308: String received = "";
309: fb.write();
310:
311: FileReader fr = new FileReader(pf);
312: int c = fr.read();
313: while (c >= 0) {
314: received += (char) c;
315: c = fr.read();
316: }
317: // assertEquals("Make relative", "dir1/test.java",
318: // fb.makeRelative(new File(pf.getParentFile(),"dir1/test.java")));
319: // assertEquals("The file written by the builder", expected, received);
320:
321: // parse in the file that was just written.
322: ProjectFileIR pfir = null;
323: try {
324: pfir = ProjectFileParser.ONLY.parse(pf);
325: } catch (MalformedProjectFileException e) {
326: throw new MalformedProjectFileException(e.getMessage()
327: + ", file: " + pf);
328: }
329: assertEquals("number of source files", 5,
330: pfir.getSourceFiles().length);
331: assertEquals("number of aux files", 2,
332: pfir.getAuxiliaryFiles().length);
333: assertEquals("number of collapsed", 1,
334: pfir.getCollapsedPaths().length);
335: assertEquals("number of classpaths", 1, IterUtil.sizeOf(pfir
336: .getClassPaths()));
337:
338: String base = pf.getParent();
339:
340: // assertEquals("first source filename", new File(parent,"/dir1/testfile1.java").getPath(),
341: // pfir.getSourceFiles()[0].getPath());
342: // assertEquals("last source filename", new File(parent,"/dir3/testfile5.java").getPath(),
343: // pfir.getSourceFiles()[4].getPath());
344: assertEquals("first aux filename", new File(parent,
345: "test/testfile6.java").getPath(), pfir
346: .getAuxiliaryFiles()[0].getPath());
347: assertEquals("last collapsed path", "./[ Source Files ]/dir1/",
348: pfir.getCollapsedPaths()[0]);
349: assertEquals("build-dir name", buildDir, pfir
350: .getBuildDirectory());
351: assertEquals("work-dir name", srcDir, pfir
352: .getWorkingDirectory());
353: assertEquals("classpath name", new File(parent, "lib"),
354: IterUtil.first(pfir.getClassPaths()));
355: assertEquals("main-class name", new File(parent,
356: "/dir1/testfile1.java"), pfir.getMainClass());
357: pf.delete();
358: }
359:
360: private DocumentInfoGetter makeGetter(final int sel1,
361: final int sel2, final int scrollv, final int scrollh,
362: final String fname, final String pack,
363: final boolean active, final boolean isUntitled,
364: final File pf) {
365: return new DocumentInfoGetter() {
366: public Pair<Integer, Integer> getSelection() {
367: return new Pair<Integer, Integer>(new Integer(sel1),
368: new Integer(sel2));
369: }
370:
371: public Pair<Integer, Integer> getScroll() {
372: return new Pair<Integer, Integer>(new Integer(scrollv),
373: new Integer(scrollh));
374: }
375:
376: public File getFile() {
377: if (pf == null)
378: return new File(fname);
379: else
380: return new File(pf.getParentFile(), fname);
381: }
382:
383: public String getPackage() {
384: return pack;
385: }
386:
387: public boolean isActive() {
388: return active;
389: }
390:
391: public boolean isUntitled() {
392: return isUntitled;
393: }
394: };
395:
396: }
397: }
|