KickJava   Java API By Example, From Geeks To Geeks.

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


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-2006 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 edu.rice.cs.drjava.model.definitions.InvalidPackageException;
37
38 import java.io.*;
39
40 import javax.swing.text.BadLocationException JavaDoc;
41
42 /**
43  * Tests to ensure that compilation succeeds when expected.
44  *
45  * Every test in this class is run for *each* of the compilers that is available.
46  *
47  * @version $Id: GlobalModelCompileSuccessTest.java 3901 2006-06-30 05:28:11Z rcartwright $
48  */

49 public final class GlobalModelCompileSuccessTest extends GlobalModelCompileSuccessTestCase {
50
51   /**
52    * Tests calling compileAll with different source roots works.
53    */

54   public void testCompileAllDifferentSourceRoots()
55     throws BadLocationException JavaDoc, IOException, InterruptedException JavaDoc
56   {
57 // System.out.println("testCompileAllDifferentSourceRoots()");
58
File aDir = new File(_tempDir, "a");
59     File bDir = new File(_tempDir, "b");
60     aDir.mkdir();
61     bDir.mkdir();
62     OpenDefinitionsDocument doc = setupDocument(FOO_TEXT);
63     final File file = new File(aDir, "DrJavaTestFoo.java");
64     doc.saveFile(new FileSelector(file));
65     OpenDefinitionsDocument doc2 = setupDocument(BAR_TEXT);
66     final File file2 = new File(bDir, "DrJavaTestBar.java");
67     doc2.saveFile(new FileSelector(file2));
68     
69     CompileShouldSucceedListener listener = new CompileShouldSucceedListener(false);
70     _model.addListener(listener);
71     _model.getCompilerModel().compileAll();
72     if (_model.getCompilerModel().getNumErrors() > 0) {
73       fail("compile failed: " + getCompilerErrorString());
74     }
75     assertCompileErrorsPresent(_name(), false);
76     listener.checkCompileOccurred();
77
78     // Make sure .class exists for both files
79
File compiled = classForJava(file, "DrJavaTestFoo");
80     assertTrue(_name() + "Foo Class file doesn't exist after compile",
81                compiled.exists());
82     File compiled2 = classForJava(file2, "DrJavaTestBar");
83     assertTrue(_name() + "Bar Class file doesn't exist after compile",
84                compiled2.exists());
85     _model.removeListener(listener);
86   }
87   
88
89   /**
90    * Test that one compiled file can depend on the other and that when a keyword
91    * is part of a field name, the file will compile.
92    * We compile DrJavaTestFoo and then DrJavaTestFoo2 (which extends
93    * DrJavaTestFoo). This shows that the compiler successfully found
94    * DrJavaTestFoo2 when compiling DrJavaTestFoo.
95    * Doesn't reset interactions because no interpretations are performed.
96    */

97   public void testCompileClassPathOKDefaultPackage()
98     throws BadLocationException JavaDoc, IOException, InterruptedException JavaDoc
99   {
100 // System.out.println("testCompileClasspathOKDefaultPackage()");
101
// Create/compile foo, assuming it works
102
OpenDefinitionsDocument doc1 = setupDocument(FOO_PACKAGE_AS_PART_OF_FIELD);
103     final File fooFile = new File(_tempDir, "DrJavaTestFoo.java");
104     
105     doc1.saveFile(new FileSelector(fooFile));
106     CompileShouldSucceedListener listener = new CompileShouldSucceedListener(false);
107     _model.addListener(listener);
108     doc1.startCompile();
109     if (_model.getCompilerModel().getNumErrors() > 0) {
110       fail("compile failed: " + getCompilerErrorString());
111     }
112     listener.checkCompileOccurred();
113     _model.removeListener(listener);
114
115     OpenDefinitionsDocument doc2 = setupDocument(FOO2_EXTENDS_FOO_TEXT);
116     final File foo2File = new File(_tempDir, "DrJavaTestFoo2.java");
117     doc2.saveFile(new FileSelector(foo2File));
118
119     CompileShouldSucceedListener listener2 = new CompileShouldSucceedListener(false);
120     _model.addListener(listener2);
121     doc2.startCompile();
122     if (_model.getCompilerModel().getNumErrors() > 0) {
123       fail("compile failed: " + getCompilerErrorString());
124     }
125     assertCompileErrorsPresent(_name(), false);
126     listener2.checkCompileOccurred();
127
128     // Make sure .class exists
129
File compiled = classForJava(foo2File, "DrJavaTestFoo2");
130     assertTrue(_name() + "Class file doesn't exist after compile",
131                compiled.exists());
132     _model.removeListener(listener2);
133   }
134
135   /**
136    * Test that one compiled file can depend on the other.
137    * We compile a.DrJavaTestFoo and then b.DrJavaTestFoo2 (which extends
138    * DrJavaTestFoo). This shows that the compiler successfully found
139    * DrJavaTestFoo2 when compiling DrJavaTestFoo.
140    * Doesn't reset interactions because no interpretations are performed.
141    */

142   public void testCompileClassPathOKDifferentPackages() throws BadLocationException JavaDoc, IOException, InterruptedException JavaDoc,
143     InvalidPackageException {
144 // System.out.println("testCompileClasspathOKDifferentPackages()");
145
File aDir = new File(_tempDir, "a");
146     File bDir = new File(_tempDir, "b");
147     aDir.mkdir();
148     bDir.mkdir();
149
150     // Create/compile foo, assuming it works
151
// foo must be public and in DrJavaTestFoo.java!
152
OpenDefinitionsDocument doc1 = setupDocument("package a;\n" + "public " + FOO_TEXT);
153     final File fooFile = new File(aDir, "DrJavaTestFoo.java");
154 // System.err.println("fooFile = " + fooFile.getCanonicalPath());
155
doc1.saveFile(new FileSelector(fooFile));
156     // _packageName must be updated on save
157
assertEquals("Check package name of doc1", "a", ((AbstractGlobalModel.ConcreteOpenDefDoc) doc1)._packageName);
158 // System.err.println("doc1 = " + doc1);
159
// System.err.println("doc1 has source root " + doc1.getSourceRoot());
160
CompileShouldSucceedListener listener = new CompileShouldSucceedListener(false);
161     _model.addListener(listener);
162     
163     doc1.startCompile();
164     if (_model.getCompilerModel().getNumErrors() > 0) {
165       fail("compile failed: " + getCompilerErrorString());
166     }
167     listener.checkCompileOccurred();
168     _model.removeListener(listener);
169     
170     OpenDefinitionsDocument doc2 =
171       setupDocument("package b;\nimport a.DrJavaTestFoo;\n" + FOO2_EXTENDS_FOO_TEXT);
172     final File foo2File = new File(bDir, "DrJavaTestFoo2.java");
173 // System.err.println("foo2File = " + foo2File.getCanonicalPath());
174
doc2.saveFile(new FileSelector(foo2File));
175     // _packageName must be updated on save
176
assertEquals("Check packangeName of doc2", "b", ((AbstractGlobalModel.ConcreteOpenDefDoc) doc2)._packageName);
177 // System.err.println("doc2 = " + doc2);
178

179     CompileShouldSucceedListener listener2 = new CompileShouldSucceedListener(false);
180     _model.addListener(listener2);
181     
182     doc2.startCompile();
183     if (_model.getCompilerModel().getNumErrors() > 0) {
184       fail("compile failed: " + getCompilerErrorString());
185     }
186     assertCompileErrorsPresent(_name(), false);
187     listener2.checkCompileOccurred();
188     _model.removeListener(listener2);
189
190     // Make sure .class exists
191
File compiled = classForJava(foo2File, "DrJavaTestFoo2");
192     assertTrue(_name() + "Class file doesn't exist after compile",
193                compiled.exists());
194   }
195 }
196
Popular Tags