KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > GlobalModelCompileIOTest


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.model;
35
36 import java.io.*;
37
38 import javax.swing.text.BadLocationException JavaDoc;
39
40 /** Tests to ensure that compilation interacts with files correctly.
41  *
42  * @version $Id: GlobalModelCompileIOTest.java 3839 2006-05-14 20:28:51Z rcartwright $
43  */

44 public final class GlobalModelCompileIOTest extends GlobalModelTestCase {
45   
46   /** After creating a new file, saving, and compiling it, this test checks that the new document is in sync after
47    * compiling and is out of sync after modifying and even saving it.
48    * Doesn't reset interactions because no interpretations are performed.
49    */

50   public void testClassFileSynchronization() throws BadLocationException JavaDoc, IOException, InterruptedException JavaDoc {
51     final OpenDefinitionsDocument doc = setupDocument(FOO_TEXT);
52     final File file = tempFile();
53
54     doc.saveFile(new FileSelector(file));
55
56     CompileShouldSucceedListener listener = new CompileShouldSucceedListener(false);
57     _model.addListener(listener);
58     assertTrue("Class file should not exist before compile", doc.getCachedClassFile() == null);
59     assertTrue("should not be in sync before compile", ! doc.checkIfClassFileInSync());
60     assertTrue("The state of all open documents should be out of sync", _model.hasOutOfSyncDocuments());
61     doc.startCompile();
62     listener.waitCompileDone();
63     if (_model.getCompilerModel().getNumErrors() > 0) {
64       fail("compile failed: " + getCompilerErrorString());
65     }
66     _model.removeListener(listener);
67     listener.checkCompileOccurred();
68     assertTrue("should be in sync after compile", doc.checkIfClassFileInSync());
69 // System.err.println(_model.getOpenDefinitionsDocuments());
70
assertTrue("The state of all open documents should be in sync", ! _model.hasOutOfSyncDocuments());
71     doc.insertString(0, "hi", null);
72     assertTrue("should not be in sync after modification", ! doc.checkIfClassFileInSync());
73
74     // Have to wait 2 seconds so file will have a different timestamp
75
Thread.sleep(2000);
76
77     doc.saveFile(new FileSelector(file));
78     assertTrue("should not be in sync after save", ! doc.checkIfClassFileInSync());
79
80     // Make sure .class exists
81
File compiled = classForJava(file, "DrJavaTestFoo");
82     assertTrue(" Class file should exist after compile", compiled.exists());
83   }
84
85   /** Ensure that renaming a file makes it out of sync with its class file.
86    * Doesn't reset interactions because no interpretations are performed.
87    */

88   public void testClassFileSynchronizationAfterRename() throws BadLocationException JavaDoc, IOException, IllegalStateException JavaDoc,
89     InterruptedException JavaDoc {
90     
91     final OpenDefinitionsDocument doc = setupDocument(FOO_TEXT);
92     final File file = tempFile();
93     final File file2 = tempFile(2);
94
95     doc.saveFile(new FileSelector(file));
96
97     CompileShouldSucceedListener listener = new CompileShouldSucceedListener(false);
98     _model.addListener(listener);
99     assertTrue("Class file should not exist before compile",
100                doc.getCachedClassFile() == null);
101     assertTrue("should not be in sync before compile",
102                !doc.checkIfClassFileInSync());
103     doc.startCompile();
104     listener.waitCompileDone();
105     if (_model.getCompilerModel().getNumErrors() > 0) {
106       fail("compile failed: " + getCompilerErrorString());
107     }
108     _model.removeListener(listener);
109     listener.checkCompileOccurred();
110     assertTrue("should be in sync after compile",
111                doc.checkIfClassFileInSync());
112
113     // Have to wait 1 second so file will have a different timestamp
114
Thread.sleep(2000);
115
116     // Rename to a different file
117
doc.saveFileAs(new FileSelector(file2));
118     assertTrue("should not be in sync after renaming", ! doc.checkIfClassFileInSync());
119   }
120
121   /** Tests a compile after a file has unexpectedly been moved or delete. */
122   public void testCompileAfterFileMoved() throws BadLocationException JavaDoc, IOException {
123     OpenDefinitionsDocument doc = setupDocument(FOO_TEXT);
124     final File file = tempFile();
125     doc.saveFile(new FileSelector(file));
126     TestListener listener = new TestListener();
127     _model.addListener(listener);
128     file.delete();
129     try {
130       doc.startCompile();
131       fail("Compile should not have begun.");
132     }
133     catch (FileMovedException fme) {
134       //compile should never have begun because the file was not where it was expected
135
// to be on disk.
136
}
137     assertCompileErrorsPresent("compile should succeed", false);
138
139     // Make sure .class exists
140
File compiled = classForJava(file, "DrJavaTestFoo");
141     assertTrue("Class file shouldn't exist after compile", !compiled.exists());
142     _model.removeListener(listener);
143   }
144
145 }
146
Popular Tags