1 33 34 package edu.rice.cs.drjava.ui; 35 36 import edu.rice.cs.drjava.model.MultiThreadedTestCase; 37 import edu.rice.cs.drjava.model.OpenDefinitionsDocument; 38 import edu.rice.cs.drjava.model.SingleDisplayModel; 39 import edu.rice.cs.drjava.project.DocFile; 40 import edu.rice.cs.drjava.project.MalformedProjectFileException; 41 import edu.rice.cs.drjava.project.ProjectFileIR; 42 import edu.rice.cs.drjava.project.ProjectFileParser; 43 44 import edu.rice.cs.plt.io.IOUtil; 45 import edu.rice.cs.util.FileOpenSelector; 46 import edu.rice.cs.util.OperationCanceledException; 47 import edu.rice.cs.util.UnexpectedException; 48 import edu.rice.cs.util.swing.Utilities; 49 50 import java.io.*; 51 import java.util.Arrays ; 52 import java.util.List ; 53 54 55 public final class ProjectMenuTest extends MultiThreadedTestCase { 56 57 private volatile MainFrame _frame; 58 59 private volatile SingleDisplayModel _model; 60 61 62 private File _base; 63 private File _parent; 64 private File _srcDir; 65 private File _projDir; 66 private File _auxFile; 67 private File _projFile; 68 private File _file1; 69 private File _file2; 70 71 private String _file1RelName; 72 private String _file2RelName; 73 74 75 BufferedReader reader = null; 76 77 private String _projFileText = null; 78 79 80 public void setUp() throws Exception { 81 super.setUp(); 82 83 _base = new File(System.getProperty("java.io.tmpdir")).getCanonicalFile(); 85 _parent = IOUtil.createAndMarkTempDirectory("proj", "", _base); 86 _srcDir = new File(_parent, "src"); 87 _srcDir.mkdir(); 89 _auxFile = File.createTempFile("aux", ".java").getCanonicalFile(); 91 File auxFileParent = _auxFile.getParentFile(); 92 _projFile = new File(_parent, "test.pjt"); 93 94 _file1 = new File(_srcDir, "test1.java"); 95 IOUtil.writeStringToFile(_file1, ""); _file2 = new File(_srcDir, "test2.java"); 97 IOUtil.writeStringToFile(_file2, ""); 99 101 107 _projFileText = 108 ";; DrJava project file. Written with build: 20040623-1933\n" + 109 "(source ;; comment\n" + 110 " (file (name \"src/test1.java\")(select 32 32))" + 111 " (file (name \"src/test2.java\")(select 32 32)))"; 112 113 IOUtil.writeStringToFile(_projFile, _projFileText); 114 115 _frame = new MainFrame(); 118 _frame.pack(); 119 122 _model = _frame.getModel(); 123 } 124 125 public void tearDown() throws Exception { 126 IOUtil.deleteOnExitRecursively(_parent); 127 _auxFile.delete(); 128 _frame.dispose(); 129 _projFile = null; 130 _model = null; 131 _frame = null; 132 super.tearDown(); 133 } 134 135 public void testSetBuildDirectory() throws MalformedProjectFileException, IOException { 136 137 139 File f = new File(""); 141 _model.setBuildDirectory(f); 142 assertEquals("Build directory should not have been set", null, _model.getBuildDirectory()); 143 144 Utilities.invokeAndWait(new Runnable () { 146 public void run() { 147 try { _model.openProject(_projFile); } 148 catch(Exception e) { throw new UnexpectedException(e); } 149 } 150 }); 151 154 assertEquals("Build directory should not have been set", null, _model.getBuildDirectory()); 155 156 _model.setBuildDirectory(f); 157 assertEquals("Build directory should have been set", f, _model.getBuildDirectory()); 158 159 } 160 161 public void testCloseAllClosesProject() throws MalformedProjectFileException, IOException { 162 163 Utilities.invokeAndWait(new Runnable () { 165 public void run() { 166 try { _model.openProject(_projFile); } 167 catch(Exception e) { throw new UnexpectedException(e); } 168 } 169 }); 170 assertTrue("Project should have been opened", _model.isProjectActive()); 171 172 Utilities.invokeAndWait(new Runnable () { 173 public void run() { 174 try { _frame.closeAll(); } 175 catch(Exception e) { throw new UnexpectedException(e); } 176 } 177 }); 178 179 assertFalse("Project should have been closed", _model.isProjectActive()); 180 } 181 182 public void testSaveProject() throws IOException, MalformedProjectFileException { 183 184 186 Utilities.invokeAndWait(new Runnable () { 187 public void run() { 188 _frame.openProject(new FileOpenSelector() { 189 public File[] getFiles() throws OperationCanceledException { return new File[] {_projFile}; } 190 }); 191 192 _frame.open(new FileOpenSelector() { 194 public File[] getFiles() throws OperationCanceledException { 195 return new File[] {_auxFile}; 196 } 197 }); 198 _frame._moveToAuxiliary(); 199 200 _frame.saveProject(); 201 _frame._closeProject(); 202 } 203 }); 204 List <OpenDefinitionsDocument> docs = _model.getOpenDefinitionsDocuments(); 205 assertEquals("One empty document remaining", 1, docs.size()); 206 assertEquals("Name is (Untitled)", "(Untitled)", _model.getActiveDocument().toString()); 207 208 ProjectFileIR pfir = ProjectFileParser.ONLY.parse(_projFile); 209 DocFile[] src = pfir.getSourceFiles(); 210 DocFile[] aux = pfir.getAuxiliaryFiles(); 212 assertEquals("Number of saved src files", 2, src.length); 214 assertEquals("Number of saved aux files", 1, aux.length); 215 assertEquals("wrong name for _file2", _file2.getCanonicalPath(), src[1].getCanonicalPath()); assertEquals("Wrong name for _file1", _file1.getCanonicalPath(), src[0].getCanonicalPath()); 217 assertEquals("Wrong aux file", _auxFile.getCanonicalPath(), aux[0].getCanonicalPath()); 218 } 219 220 } | Popular Tags |