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.ui;
038:
039: import edu.rice.cs.drjava.model.MultiThreadedTestCase;
040: import edu.rice.cs.drjava.model.OpenDefinitionsDocument;
041: import edu.rice.cs.drjava.model.SingleDisplayModel;
042: import edu.rice.cs.drjava.project.DocFile;
043: import edu.rice.cs.drjava.project.MalformedProjectFileException;
044: import edu.rice.cs.drjava.project.ProjectFileIR;
045: import edu.rice.cs.drjava.project.ProjectFileParser;
046:
047: import edu.rice.cs.plt.io.IOUtil;
048: import edu.rice.cs.util.FileOpenSelector;
049: import edu.rice.cs.util.OperationCanceledException;
050: import edu.rice.cs.util.UnexpectedException;
051: import edu.rice.cs.util.swing.Utilities;
052:
053: import java.io.*;
054: import java.util.Arrays;
055: import java.util.List;
056:
057: /** Test functions of Project Facility working through the main frame and model. */
058: public final class ProjectMenuTest extends MultiThreadedTestCase {
059:
060: private volatile MainFrame _frame;
061:
062: private volatile SingleDisplayModel _model;
063:
064: /** Temporary files */
065: private File _base;
066: private File _parent;
067: private File _srcDir;
068: private File _projDir;
069: private File _auxFile;
070: private File _projFile;
071: private File _file1;
072: private File _file2;
073:
074: private String _file1RelName;
075: private String _file2RelName;
076:
077: /* The reader which reads the test project file */
078: BufferedReader reader = null;
079:
080: private String _projFileText = null;
081:
082: /** Setup method for each JUnit test case in this Test class. */
083: public void setUp() throws Exception {
084: super .setUp();
085:
086: // create temp directory for this test
087: _base = new File(System.getProperty("java.io.tmpdir"))
088: .getCanonicalFile();
089: _parent = IOUtil.createAndMarkTempDirectory("proj", "", _base);
090: _srcDir = new File(_parent, "src");
091: _srcDir.mkdir(); // create the src directory
092:
093: // create project in a directory with an auxiliary file outside of it
094: _auxFile = File.createTempFile("aux", ".java")
095: .getCanonicalFile();
096: File auxFileParent = _auxFile.getParentFile();
097: _projFile = new File(_parent, "test.pjt");
098:
099: _file1 = new File(_srcDir, "test1.java");
100: IOUtil.writeStringToFile(_file1, ""); // create dummy file
101: _file2 = new File(_srcDir, "test2.java");
102: IOUtil.writeStringToFile(_file2, "");// create dummy file
103:
104: // System.err.println("test1.java and test1.java created");
105:
106: // // generate the relative path names for the files in the project file
107: // String temp = _file1.getParentFile().getCanonicalPath();
108: // _file1RelName = _file1.getCanonicalPath().substring(temp.length() + 1);
109: // temp = _file2.getParentFile().getCanonicalPath();
110: // _file2RelName = _file2.getCanonicalPath().substring(temp.length() + 1);
111:
112: _projFileText = ";; DrJava project file. Written with build: 20040623-1933\n"
113: + "(source ;; comment\n"
114: + " (file (name \"src/test1.java\")(select 32 32))"
115: + " (file (name \"src/test2.java\")(select 32 32)))";
116:
117: IOUtil.writeStringToFile(_projFile, _projFileText);
118:
119: // Utilities.invokeAndWait(new Runnable() {
120: // public void run() {
121: _frame = new MainFrame();
122: _frame.pack();
123: // }
124: // });
125:
126: _model = _frame.getModel();
127: }
128:
129: public void tearDown() throws Exception {
130: IOUtil.deleteOnExitRecursively(_parent);
131: _auxFile.delete();
132: _frame.dispose();
133: _projFile = null;
134: _model = null;
135: _frame = null;
136: super .tearDown();
137: }
138:
139: public void testSetBuildDirectory()
140: throws MalformedProjectFileException, IOException {
141:
142: // Utilities.showDebug("executing testSetBuildDirectory");
143:
144: //test set build directory when not in project mode
145: File f = new File("");
146: _model.setBuildDirectory(f);
147: assertEquals("Build directory should not have been set", null,
148: _model.getBuildDirectory());
149:
150: // System.err.println("Opening Project File");
151: Utilities.invokeAndWait(new Runnable() {
152: public void run() {
153: try {
154: _model.openProject(_projFile);
155: } catch (Exception e) {
156: throw new UnexpectedException(e);
157: }
158: }
159: });
160: // System.err.println("Completed Opening Project File");
161: // System.err.println("Project documents are: " + _model.getProjectDocuments());
162:
163: assertEquals("Build directory should not have been set", null,
164: _model.getBuildDirectory());
165:
166: _model.setBuildDirectory(f);
167: assertEquals("Build directory should have been set", f, _model
168: .getBuildDirectory());
169:
170: }
171:
172: public void testCloseAllClosesProject()
173: throws MalformedProjectFileException, IOException {
174:
175: // Utilities.showDebug("executing testCloseAllClosesProject");
176: Utilities.invokeAndWait(new Runnable() {
177: public void run() {
178: try {
179: _model.openProject(_projFile);
180: } catch (Exception e) {
181: throw new UnexpectedException(e);
182: }
183: }
184: });
185: assertTrue("Project should have been opened", _model
186: .isProjectActive());
187:
188: Utilities.invokeAndWait(new Runnable() {
189: public void run() {
190: try {
191: _frame.closeAll();
192: } catch (Exception e) {
193: throw new UnexpectedException(e);
194: }
195: }
196: });
197:
198: assertFalse("Project should have been closed", _model
199: .isProjectActive());
200: }
201:
202: public void testSaveProject() throws IOException,
203: MalformedProjectFileException {
204:
205: // Utilities.showDebug("executing testSaveProject");
206:
207: Utilities.invokeAndWait(new Runnable() {
208: public void run() {
209: _frame.openProject(new FileOpenSelector() {
210: public File[] getFiles()
211: throws OperationCanceledException {
212: return new File[] { _projFile };
213: }
214: });
215:
216: // open a new file and make it an auxiliary file
217: _frame.open(new FileOpenSelector() {
218: public File[] getFiles()
219: throws OperationCanceledException {
220: return new File[] { _auxFile };
221: }
222: });
223: _frame._moveToAuxiliary();
224:
225: _frame.saveProject();
226: _frame._closeProject();
227: }
228: });
229: List<OpenDefinitionsDocument> docs = _model
230: .getOpenDefinitionsDocuments();
231: assertEquals("One empty document remaining", 1, docs.size());
232: assertEquals("Name is (Untitled)", "(Untitled)", _model
233: .getActiveDocument().toString());
234:
235: ProjectFileIR pfir = ProjectFileParser.ONLY.parse(_projFile);
236: DocFile[] src = pfir.getSourceFiles();
237: // System.err.println(Arrays.toString(src));
238: DocFile[] aux = pfir.getAuxiliaryFiles();
239: // System.err.println(Arrays.toString(aux));
240: assertEquals("Number of saved src files", 2, src.length);
241: assertEquals("Number of saved aux files", 1, aux.length);
242: assertEquals("wrong name for _file2",
243: _file2.getCanonicalPath(), src[1].getCanonicalPath()); // assumes same (not reverse) order
244: assertEquals("Wrong name for _file1",
245: _file1.getCanonicalPath(), src[0].getCanonicalPath());
246: assertEquals("Wrong aux file", _auxFile.getCanonicalPath(),
247: aux[0].getCanonicalPath());
248: }
249:
250: }
|