KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > ui > ProjectMenuTest


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.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 JavaDoc;
52 import java.util.List JavaDoc;
53
54 /** Test functions of Project Facility working through the main frame and model. */
55 public final class ProjectMenuTest extends MultiThreadedTestCase {
56
57   private volatile MainFrame _frame;
58   
59   private volatile SingleDisplayModel _model;
60   
61   /** Temporary files */
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 JavaDoc _file1RelName;
72   private String JavaDoc _file2RelName;
73   
74   /* The reader which reads the test project file */
75   BufferedReader reader = null;
76   
77   private String JavaDoc _projFileText = null;
78   
79   /** Setup method for each JUnit test case. */
80   public void setUp() throws Exception JavaDoc {
81     super.setUp();
82     
83     // create temp directory for this test
84
_base = new File(System.getProperty("java.io.tmpdir")).getCanonicalFile();
85     _parent = IOUtil.createAndMarkTempDirectory("proj", "", _base);
86     _srcDir = new File(_parent, "src");
87     _srcDir.mkdir(); // create the src directory
88

89     // create project in a directory with an auxiliary file outside of it
90
_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, ""); // create dummy file
96
_file2 = new File(_srcDir, "test2.java");
97     IOUtil.writeStringToFile(_file2, "");// create dummy file
98

99 // System.err.println("test1.java and test1.java created");
100

101 // // generate the relative path names for the files in the project file
102
// String temp = _file1.getParentFile().getCanonicalPath();
103
// _file1RelName = _file1.getCanonicalPath().substring(temp.length() + 1);
104
// temp = _file2.getParentFile().getCanonicalPath();
105
// _file2RelName = _file2.getCanonicalPath().substring(temp.length() + 1);
106

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 // Utilities.invokeAndWait(new Runnable() {
116
// public void run() {
117
_frame = new MainFrame();
118         _frame.pack();
119 // }
120
// });
121

122     _model = _frame.getModel();
123   }
124
125   public void tearDown() throws Exception JavaDoc {
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 // Utilities.showDebug("executing testSetBuildDirectory");
138

139     //test set build directory when not in project mode
140
File f = new File("");
141     _model.setBuildDirectory(f);
142     assertEquals("Build directory should not have been set", null, _model.getBuildDirectory());
143     
144 // System.err.println("Opening Project File");
145
Utilities.invokeAndWait(new Runnable JavaDoc() {
146       public void run() {
147         try { _model.openProject(_projFile); }
148         catch(Exception JavaDoc e) { throw new UnexpectedException(e); }
149       }
150     });
151 // System.err.println("Completed Opening Project File");
152
// System.err.println("Project documents are: " + _model.getProjectDocuments());
153

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.showDebug("executing testCloseAllClosesProject");
164
Utilities.invokeAndWait(new Runnable JavaDoc() {
165       public void run() {
166         try { _model.openProject(_projFile); }
167         catch(Exception JavaDoc e) { throw new UnexpectedException(e); }
168       }
169     });
170     assertTrue("Project should have been opened", _model.isProjectActive());
171     
172     Utilities.invokeAndWait(new Runnable JavaDoc() {
173       public void run() {
174         try { _frame.closeAll(); }
175         catch(Exception JavaDoc 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 // Utilities.showDebug("executing testSaveProject");
185

186     Utilities.invokeAndWait(new Runnable JavaDoc() {
187       public void run() {
188         _frame.openProject(new FileOpenSelector() {
189           public File[] getFiles() throws OperationCanceledException { return new File[] {_projFile}; }
190         });
191         
192         // open a new file and make it an auxiliary file
193
_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 JavaDoc<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 // System.err.println(Arrays.toString(src));
211
DocFile[] aux = pfir.getAuxiliaryFiles();
212 // System.err.println(Arrays.toString(aux));
213
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()); // assumes same (not reverse) order
216
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