1 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 ; 47 import java.io.FileReader ; 48 import java.io.FileWriter ; 49 import java.io.IOException ; 50 import java.text.SimpleDateFormat ; 51 52 53 public class ProjectTest extends DrJavaTestCase { 54 55 File base; 56 File parent; 57 File buildDir; 58 File srcDir; 59 60 private String absp; public void setUp() throws Exception { 62 super.setUp(); 63 try { 64 base = new File (System.getProperty("java.io.tmpdir")).getCanonicalFile(); 65 parent = IOUtil.createAndMarkTempDirectory("proj", "", base); 66 buildDir = new File (parent, "built"); 67 buildDir.mkdir(); srcDir = new File (parent, "src"); 69 srcDir.mkdir(); absp = parent.getCanonicalPath() + File.separator; 71 IOUtil.deleteOnExitRecursively(parent); 72 } 73 catch(IOException e) { fail("could not initialize temp path string"); } 74 } 75 76 77 public void testLegacyParseProject() throws IOException , MalformedProjectFileException, java.text.ParseException { 78 String 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" + "(auxiliary ;; absolute file names\n" + 89 " (file (name " + convertToLiteral(new File (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 (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 (parent,"built").getCanonicalPath()) + ")))\n" + 98 "(work-dir ;; absolute path\n" + 99 " (file (name "+ convertToLiteral(new File (parent,"src").getCanonicalPath()) + ")))\n" + 100 "(proj-root ;; absolute path\n" + 101 " (file (name "+ convertToLiteral(new File (parent,"src").getCanonicalPath()) + ")))\n" + 102 "(classpaths\n" + 103 " (file (name "+ convertToLiteral(new File (parent,"src/edu/rice/cs/lib").getCanonicalPath()) + ")))\n" + 104 "(main-class\n" + 105 " (file (name \"src/sexp/SEList.java\")))"; 106 107 File f = new File (parent, "test1.pjt"); 108 109 IOUtil.writeStringToFile(f, proj1); 110 ProjectFileIR pfir = ProjectFileParser.ONLY.parse(f); 114 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 base = f.getParentFile(); 120 assertEquals("first source filename", new File (base,"src/sexp/Atom.java").getPath(), pfir.getSourceFiles()[0].getPath()); 121 assertEquals("mod-date value", 122 new SimpleDateFormat ("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 (base,"src/sexp/SEList.java").getPath(), 125 pfir.getSourceFiles()[6].getPath()); 126 assertEquals("first aux filename", new File (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 (base, "built").getCanonicalPath(), 130 pfir.getBuildDirectory().getCanonicalPath()); 131 assertEquals("work-dir name", new File (base, "src").getCanonicalPath(), 132 pfir.getWorkingDirectory().getCanonicalPath()); 133 assertEquals("classpath name", new File (base, "src/edu/rice/cs/lib").getCanonicalPath(), 134 pfir.getClassPaths()[0].getCanonicalPath()); 135 assertEquals("main-class name", new File (base, "src/sexp/SEList.java").getCanonicalPath(), 136 pfir.getMainClass().getCanonicalPath()); 137 } 138 139 140 public void testParseProject() throws IOException , MalformedProjectFileException, java.text.ParseException { 141 String 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" + "(auxiliary ;; absolute file names\n" + 153 " (file (name " + convertToLiteral(new File (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 (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 (parent, "built").getCanonicalPath()) + ")))\n" + 162 "(work-dir (file (name \"src\")))\n" + 163 "(classpaths\n" + 164 " (file (name "+ convertToLiteral(new File (parent, "src/edu/rice/cs/lib").getCanonicalPath()) + ")))\n" + 165 "(main-class\n" + 166 " (file (name \"src/sexp/SEList.java\")))"; 167 168 File f = new File (parent, "test1.pjt"); 169 170 IOUtil.writeStringToFile(f, proj1); 171 ProjectFileIR pfir = ProjectFileParser.ONLY.parse(f); 175 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 base = f.getParentFile(); 181 File root = new File (base, "src"); 182 assertEquals("proj-root-and-base", root.getPath(), pfir.getProjectRoot().getPath()); 183 assertEquals("first source filename", new File (base,"src/sexp/Atom.java").getPath(), pfir.getSourceFiles()[0].getPath()); 184 assertEquals("mod-date value", 185 new SimpleDateFormat ("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 (root, "sexp/SEList.java").getPath(), 188 pfir.getSourceFiles()[6].getPath()); 189 assertEquals("first aux filename", new File (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 (base, "built").getCanonicalPath(), 193 pfir.getBuildDirectory().getCanonicalPath()); 194 assertEquals("work-dir name", new File (base, "src").getCanonicalPath(), 195 pfir.getWorkingDirectory().getCanonicalPath()); 196 assertEquals("classpath name", new File (base, "src/edu/rice/cs/lib").getCanonicalPath(), 197 pfir.getClassPaths()[0].getCanonicalPath()); 198 assertEquals("main-class name", new File (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 ,Integer > p = df.getSelection(); 206 assertEquals("First int should be a 1", 1, (int)p.first()); assertEquals("Second int should be a 2", 2, (int)p.second()); assertEquals("Name should have been file-name", "file-name", df.getPath()); 209 } 210 211 public void testWriteFile() throws IOException , MalformedProjectFileException { 212 File pf = new File (parent, "test2.pjt"); 213 IOUtil.writeStringToFile(pf, ""); 214 ProjectProfile fb = new ProjectProfile(pf); 215 String 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 (parent, "lib")); 226 fb.setBuildDirectory(new File (parent, "built")); 227 fb.setWorkingDirectory(new File (parent, "src")); 228 fb.setMainClass(new File (pf.getParentFile(), "dir1/testfile1.java")); 229 230 String expected = ""; 231 String received = ""; 232 fb.write(); 233 234 FileReader fr = new FileReader (pf); 235 int c = fr.read(); 236 while (c >= 0) { 237 received += (char) c; 238 c = fr.read(); 239 } 240 244 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 base = pf.getParent(); 256 257 assertEquals("first aux filename", new File (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 (parent,"lib"), pfir.getClassPaths()[0]); 267 assertEquals("main-class name", new File (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 fname, final String pack, 273 final boolean active, final boolean isUntitled, final File pf) { 274 return new DocumentInfoGetter() { 275 public Pair<Integer ,Integer > getSelection() { 276 return new Pair<Integer ,Integer >(new Integer (sel1),new Integer (sel2)); 277 } 278 public Pair<Integer ,Integer > getScroll() { 279 return new Pair<Integer ,Integer >(new Integer (scrollv),new Integer (scrollh)); 280 } 281 public File getFile() { 282 if (pf == null) return new File (fname); 283 else return new File (pf.getParentFile(),fname); 284 } 285 public String getPackage() { return pack; } 286 public boolean isActive() { return active; } 287 public boolean isUntitled() { return isUntitled; } 288 }; 289 290 } 291 } 292 | Popular Tags |