KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > project > ProjectTest


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.project;
35
36 import edu.rice.cs.drjava.DrJavaTestCase;
37 import edu.rice.cs.plt.tuple.Pair;
38 import edu.rice.cs.plt.io.IOUtil;
39
40 import static edu.rice.cs.util.StringOps.convertToLiteral;
41
42 import edu.rice.cs.util.sexp.SEList;
43 import edu.rice.cs.util.sexp.SExpParseException;
44 import edu.rice.cs.util.sexp.SExpParser;
45
46 import java.io.File JavaDoc;
47 import java.io.FileReader JavaDoc;
48 import java.io.FileWriter JavaDoc;
49 import java.io.IOException JavaDoc;
50 import java.text.SimpleDateFormat JavaDoc;
51
52 /** Test class for project files */
53 public class ProjectTest extends DrJavaTestCase {
54   
55   File JavaDoc base;
56   File JavaDoc parent;
57   File JavaDoc buildDir;
58   File JavaDoc srcDir;
59
60   private String JavaDoc absp; // absolute path
61
public void setUp() throws Exception JavaDoc {
62     super.setUp();
63     try {
64       base = new File JavaDoc(System.getProperty("java.io.tmpdir")).getCanonicalFile();
65       parent = IOUtil.createAndMarkTempDirectory("proj", "", base);
66       buildDir = new File JavaDoc(parent, "built");
67       buildDir.mkdir(); // create the specified directory
68
srcDir = new File JavaDoc(parent, "src");
69       srcDir.mkdir(); // create the specified directory
70
absp = parent.getCanonicalPath() + File.separator;
71       IOUtil.deleteOnExitRecursively(parent);
72     }
73     catch(IOException JavaDoc e) { fail("could not initialize temp path string"); }
74   }
75
76   /** Test to make sure all elements of the project are read correctly into the IR */
77   public void testLegacyParseProject() throws IOException JavaDoc, MalformedProjectFileException, java.text.ParseException JavaDoc {
78     String JavaDoc proj1 =
79       ";; DrJava project file. Written with build: 20040623-1933\n" +
80       "(source ;; comment\n" +
81       " (file (name \"src/sexp/Atom.java\")(select 32 32)(mod-date \"16-Jul-2004 03:45:23\"))\n" +
82       " (file (name \"src/sexp/BoolAtom.java\")(select 0 0)(mod-date \"16-Jul-2004 03:45:23\"))\n" +
83       " (file (name \"src/sexp/Cons.java\")(select 0 0)(mod-date \"16-Jul-2004 03:45:23\"))\n" +
84       " (file (name \"src/sexp/Empty.java\")(select 24 28)(mod-date \"16-Jul-2004 03:45:23\"))\n" +
85       " (file (name \"src/sexp/Lexer.java\")(select 0 0)(mod-date \"16-Jul-2004 03:45:23\"))\n" +
86       " (file (name \"src/sexp/NumberAtom.java\")(select 12 12)(mod-date \"16-Jul-2004 03:45:23\"))\n" +
87       " (file (name \"src/sexp/SEList.java\")(select 0 0)))\n" + // doesn't have mod date
88
"(auxiliary ;; absolute file names\n" +
89       " (file (name " + convertToLiteral(new File JavaDoc(parent, "junk/sexp/Tokens.java").getCanonicalPath()) +
90          ")(select 32 32)(mod-date \"16-Jul-2004 03:45:23\"))\n" +
91       " (file (name " + convertToLiteral(new File JavaDoc(parent, "jdk1.5.0/JScrollPane.java").getCanonicalPath()) +
92          ")(select 9086 8516)(mod-date \"16-Jul-2004 03:45:23\")))\n" +
93       "(collapsed ;; relative paths\n" +
94       " (path \"./[ Source Files ]/sexp/\")\n" +
95       " (path \"./[ External ]/\"))\n" +
96       "(build-dir ;; absolute path\n" +
97       " (file (name "+ convertToLiteral(new File JavaDoc(parent,"built").getCanonicalPath()) + ")))\n" +
98       "(work-dir ;; absolute path\n" +
99       " (file (name "+ convertToLiteral(new File JavaDoc(parent,"src").getCanonicalPath()) + ")))\n" +
100       "(proj-root ;; absolute path\n" +
101       " (file (name "+ convertToLiteral(new File JavaDoc(parent,"src").getCanonicalPath()) + ")))\n" +
102       "(classpaths\n" +
103       " (file (name "+ convertToLiteral(new File JavaDoc(parent,"src/edu/rice/cs/lib").getCanonicalPath()) + ")))\n" +
104       "(main-class\n" +
105       " (file (name \"src/sexp/SEList.java\")))";
106     
107     File JavaDoc f = new File JavaDoc(parent, "test1.pjt");
108
109     IOUtil.writeStringToFile(f, proj1);
110 // System.err.println("Project directory is " + parent);
111
// System.err.println("Project file is " + f);
112
// System.err.println("projFile exists? " + f.exists());
113
ProjectFileIR pfir = ProjectFileParser.ONLY.parse(f);
114 // System.err.println("buildDir = " + pfir.getBuildDirectory().getCanonicalPath());
115
assertEquals("number of source files", 7, pfir.getSourceFiles().length);
116     assertEquals("number of aux files", 2, pfir.getAuxiliaryFiles().length);
117     assertEquals("number of collapsed", 2, pfir.getCollapsedPaths().length);
118     assertEquals("number of classpaths", 1, pfir.getClassPaths().length);
119     File JavaDoc base = f.getParentFile();
120     assertEquals("first source filename", new File JavaDoc(base,"src/sexp/Atom.java").getPath(), pfir.getSourceFiles()[0].getPath());
121     assertEquals("mod-date value",
122                  new SimpleDateFormat JavaDoc("dd-MMM-yyyy HH:mm:ss").parse("16-Jul-2004 03:45:23").getTime(),
123                  pfir.getSourceFiles()[0].getSavedModDate());
124     assertEquals("last source filename", new File JavaDoc(base,"src/sexp/SEList.java").getPath(),
125                  pfir.getSourceFiles()[6].getPath());
126     assertEquals("first aux filename", new File JavaDoc(base,"junk/sexp/Tokens.java").getPath(),
127                  pfir.getAuxiliaryFiles()[0].getCanonicalPath());
128     assertEquals("last collapsed path", "./[ External ]/", pfir.getCollapsedPaths()[1]);
129     assertEquals("build-dir name", new File JavaDoc(base, "built").getCanonicalPath(),
130                  pfir.getBuildDirectory().getCanonicalPath());
131     assertEquals("work-dir name", new File JavaDoc(base, "src").getCanonicalPath(),
132                  pfir.getWorkingDirectory().getCanonicalPath());
133     assertEquals("classpath name", new File JavaDoc(base, "src/edu/rice/cs/lib").getCanonicalPath(),
134                  pfir.getClassPaths()[0].getCanonicalPath());
135     assertEquals("main-class name", new File JavaDoc(base, "src/sexp/SEList.java").getCanonicalPath(),
136                  pfir.getMainClass().getCanonicalPath());
137   }
138
139    /** Test to make sure all elements of the project are read correctly into the IR */
140   public void testParseProject() throws IOException JavaDoc, MalformedProjectFileException, java.text.ParseException JavaDoc {
141     String JavaDoc proj1 =
142       ";; DrJava project file. Written with build: 2006??\n" +
143       "(proj-root-and-base (file (name \"src\")))\n" +
144       "(source-files ;; comment\n" +
145       " (file (name \"sexp/Atom.java\")(select 32 32)(mod-date \"16-Jul-2004 03:45:23\"))\n" +
146       " (file (name \"sexp/BoolAtom.java\")(select 0 0)(mod-date \"16-Jul-2004 03:45:23\"))\n" +
147       " (file (name \"sexp/Cons.java\")(select 0 0)(mod-date \"16-Jul-2004 03:45:23\"))\n" +
148       " (file (name \"sexp/Empty.java\")(select 24 28)(mod-date \"16-Jul-2004 03:45:23\"))\n" +
149       " (file (name \"sexp/Lexer.java\")(select 0 0)(mod-date \"16-Jul-2004 03:45:23\"))\n" +
150       " (file (name \"sexp/NumberAtom.java\")(select 12 12)(mod-date \"16-Jul-2004 03:45:23\"))\n" +
151       " (file (name \"sexp/SEList.java\")(select 0 0)))\n" + // doesn't have mod date
152
"(auxiliary ;; absolute file names\n" +
153       " (file (name " + convertToLiteral(new File JavaDoc(parent,"junk/sexp/Tokens.java").getCanonicalPath()) +
154           ")(select 32 32)(mod-date \"16-Jul-2004 03:45:23\"))\n" +
155       " (file (name " + convertToLiteral(new File JavaDoc(parent,"jdk1.5.0/JScrollPane.java").getCanonicalPath()) +
156           ")(select 9086 8516)(mod-date \"16-Jul-2004 03:45:23\")))\n" +
157       "(collapsed ;; relative paths\n" +
158       " (path \"./[ Source Files ]/sexp/\")\n" +
159       " (path \"./[ External ]/\"))\n" +
160       "(build-dir ;; absolute path\n" +
161       " (file (name "+ convertToLiteral(new File JavaDoc(parent, "built").getCanonicalPath()) + ")))\n" +
162       "(work-dir (file (name \"src\")))\n" +
163       "(classpaths\n" +
164       " (file (name "+ convertToLiteral(new File JavaDoc(parent, "src/edu/rice/cs/lib").getCanonicalPath()) + ")))\n" +
165       "(main-class\n" +
166       " (file (name \"src/sexp/SEList.java\")))";
167     
168     File JavaDoc f = new File JavaDoc(parent, "test1.pjt");
169
170     IOUtil.writeStringToFile(f, proj1);
171 // System.err.println("Project directory is " + parent);
172
// System.err.println("Project file is " + f);
173
// System.err.println("projFile exists? " + f.exists());
174
ProjectFileIR pfir = ProjectFileParser.ONLY.parse(f);
175 // System.err.println("buildDir = " + pfir.getBuildDirectory().getCanonicalPath());
176
assertEquals("number of source files", 7, pfir.getSourceFiles().length);
177     assertEquals("number of aux files", 2, pfir.getAuxiliaryFiles().length);
178     assertEquals("number of collapsed", 2, pfir.getCollapsedPaths().length);
179     assertEquals("number of classpaths", 1, pfir.getClassPaths().length);
180     File JavaDoc base = f.getParentFile();
181     File JavaDoc root = new File JavaDoc(base, "src");
182     assertEquals("proj-root-and-base", root.getPath(), pfir.getProjectRoot().getPath());
183     assertEquals("first source filename", new File JavaDoc(base,"src/sexp/Atom.java").getPath(), pfir.getSourceFiles()[0].getPath());
184     assertEquals("mod-date value",
185                  new SimpleDateFormat JavaDoc("dd-MMM-yyyy HH:mm:ss").parse("16-Jul-2004 03:45:23").getTime(),
186                  pfir.getSourceFiles()[0].getSavedModDate());
187     assertEquals("last source filename", new File JavaDoc(root, "sexp/SEList.java").getPath(),
188                  pfir.getSourceFiles()[6].getPath());
189     assertEquals("first aux filename", new File JavaDoc(base,"junk/sexp/Tokens.java").getPath(),
190                  pfir.getAuxiliaryFiles()[0].getCanonicalPath());
191     assertEquals("last collapsed path", "./[ External ]/", pfir.getCollapsedPaths()[1]);
192     assertEquals("build-dir name", new File JavaDoc(base, "built").getCanonicalPath(),
193                  pfir.getBuildDirectory().getCanonicalPath());
194     assertEquals("work-dir name", new File JavaDoc(base, "src").getCanonicalPath(),
195                  pfir.getWorkingDirectory().getCanonicalPath());
196     assertEquals("classpath name", new File JavaDoc(base, "src/edu/rice/cs/lib").getCanonicalPath(),
197                  pfir.getClassPaths()[0].getCanonicalPath());
198     assertEquals("main-class name", new File JavaDoc(root, "sexp/SEList.java").getCanonicalPath(),
199                  pfir.getMainClass().getCanonicalPath());
200   }
201   
202   public void testParseFile() throws SExpParseException {
203     SEList c = SExpParser.parse("(file (name \"file-name\") (select 1 2))").get(0);
204     DocFile df = ProjectFileParser.ONLY.parseFile(c,null);
205     Pair<Integer JavaDoc,Integer JavaDoc> p = df.getSelection();
206     assertEquals("First int should be a 1", 1, (int)p.first()); //need cast to prevent ambiguity
207
assertEquals("Second int should be a 2", 2, (int)p.second());//need cast to prevent ambiguity
208
assertEquals("Name should have been file-name", "file-name", df.getPath());
209   }
210
211   public void testWriteFile() throws IOException JavaDoc, MalformedProjectFileException {
212     File JavaDoc pf = new File JavaDoc(parent, "test2.pjt");
213     IOUtil.writeStringToFile(pf, "");
214     ProjectProfile fb = new ProjectProfile(pf);
215     String JavaDoc sr = pf.getCanonicalFile().getParent();
216
217     fb.addSourceFile(makeGetter(0, 0, 0, 0, "dir1/testfile1.java", "dir1", false, false, pf));
218     fb.addSourceFile(makeGetter(1, 1, 0, 0, "dir1/testfile2.java", "dir1", false, false, pf));
219     fb.addSourceFile(makeGetter(20, 22, 0, 0, "dir2/testfile3.java", "dir2", false, false, pf));
220     fb.addSourceFile(makeGetter(1, 1, 0, 0, "dir2/testfile4.java", "dir2", true, false, pf));
221     fb.addSourceFile(makeGetter(0, 0, 0, 0, "dir3/testfile5.java", "", false, false, pf));
222     fb.addAuxiliaryFile(makeGetter(1, 1, 0, 0, absp + "test/testfile6.java", "/home/javaplt", false, false, null));
223     fb.addAuxiliaryFile(makeGetter(1, 1, 0, 0, absp + "test/testfile7.java", "/home/javaplt", false, false, null));
224     fb.addCollapsedPath("./[ Source Files ]/dir1/");
225     fb.addClassPathFile(new File JavaDoc(parent, "lib"));
226     fb.setBuildDirectory(new File JavaDoc(parent, "built"));
227     fb.setWorkingDirectory(new File JavaDoc(parent, "src"));
228     fb.setMainClass(new File JavaDoc(pf.getParentFile(), "dir1/testfile1.java"));
229
230     String JavaDoc expected = "";
231     String JavaDoc received = "";
232     fb.write();
233
234     FileReader JavaDoc fr = new FileReader JavaDoc(pf);
235     int c = fr.read();
236     while (c >= 0) {
237       received += (char) c;
238       c = fr.read();
239     }
240 // assertEquals("Make relative", "dir1/test.java",
241
// fb.makeRelative(new File(pf.getParentFile(),"dir1/test.java")));
242
// assertEquals("The file written by the builder", expected, received);
243

244     // parse in the file that was just written.
245
ProjectFileIR pfir = null;
246     try { pfir = ProjectFileParser.ONLY.parse(pf); }
247     catch(MalformedProjectFileException e) {
248       throw new MalformedProjectFileException(e.getMessage() + ", file: " + pf);
249     }
250     assertEquals("number of source files", 5, pfir.getSourceFiles().length);
251     assertEquals("number of aux files", 2, pfir.getAuxiliaryFiles().length);
252     assertEquals("number of collapsed", 1, pfir.getCollapsedPaths().length);
253     assertEquals("number of classpaths", 1, pfir.getClassPaths().length);
254
255     String JavaDoc base = pf.getParent();
256     
257 // assertEquals("first source filename", new File(parent,"/dir1/testfile1.java").getPath(),
258
// pfir.getSourceFiles()[0].getPath());
259
// assertEquals("last source filename", new File(parent,"/dir3/testfile5.java").getPath(),
260
// pfir.getSourceFiles()[4].getPath());
261
assertEquals("first aux filename", new File JavaDoc(parent,"test/testfile6.java").getPath(),
262                  pfir.getAuxiliaryFiles()[0].getPath());
263     assertEquals("last collapsed path", "./[ Source Files ]/dir1/", pfir.getCollapsedPaths()[0]);
264     assertEquals("build-dir name", buildDir, pfir.getBuildDirectory());
265     assertEquals("work-dir name", srcDir, pfir.getWorkingDirectory());
266     assertEquals("classpath name", new File JavaDoc(parent,"lib"), pfir.getClassPaths()[0]);
267     assertEquals("main-class name", new File JavaDoc(parent,"/dir1/testfile1.java"), pfir.getMainClass());
268     pf.delete();
269   }
270
271   private DocumentInfoGetter makeGetter(final int sel1, final int sel2, final int scrollv,
272                                         final int scrollh, final String JavaDoc fname, final String JavaDoc pack,
273                                         final boolean active, final boolean isUntitled, final File JavaDoc pf) {
274     return new DocumentInfoGetter() {
275       public Pair<Integer JavaDoc,Integer JavaDoc> getSelection() {
276         return new Pair<Integer JavaDoc,Integer JavaDoc>(new Integer JavaDoc(sel1),new Integer JavaDoc(sel2));
277       }
278       public Pair<Integer JavaDoc,Integer JavaDoc> getScroll() {
279         return new Pair<Integer JavaDoc,Integer JavaDoc>(new Integer JavaDoc(scrollv),new Integer JavaDoc(scrollh));
280       }
281       public File JavaDoc getFile() {
282         if (pf == null) return new File JavaDoc(fname);
283         else return new File JavaDoc(pf.getParentFile(),fname);
284       }
285       public String JavaDoc getPackage() { return pack; }
286       public boolean isActive() { return active; }
287       public boolean isUntitled() { return isUntitled; }
288     };
289
290   }
291 }
292
Popular Tags