KickJava   Java API By Example, From Geeks To Geeks.

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


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

52 public final class GlobalModelCompileSuccessOptionsTest extends GlobalModelCompileSuccessTestCase {
53
54   /**
55    * Tests a compile on a file that references a non-public class defined in
56    * another class with a name different than the non-public class.
57    * Doesn't reset interactions because no interpretations are performed.
58    */

59   public void testCompileReferenceToNonPublicClass()
60     throws BadLocationException JavaDoc, IOException, InterruptedException JavaDoc {
61 // System.out.println("testCompileReferenceToNonPublicClass()");
62
OpenDefinitionsDocument doc = setupDocument(FOO_NON_PUBLIC_CLASS_TEXT);
63     OpenDefinitionsDocument doc2 = setupDocument(FOO2_REFERENCES_NON_PUBLIC_CLASS_TEXT);
64     final File file = tempFile();
65     final File file2 = tempFile(1);
66     doc.saveFile(new FileSelector(file));
67     doc2.saveFile(new FileSelector(file2));
68     CompileShouldSucceedListener listener = new CompileShouldSucceedListener(false);
69     _model.addListener(listener);
70     listener.compile(doc);
71     if (_model.getCompilerModel().getNumErrors() > 0) {
72       fail("compile failed: " + getCompilerErrorString());
73     }
74     listener.checkCompileOccurred();
75     _model.removeListener(listener);
76     CompileShouldSucceedListener listener2 = new CompileShouldSucceedListener(false);
77     _model.addListener(listener2);
78     listener2.compile(doc2);
79     if (_model.getCompilerModel().getNumErrors() > 0) {
80       fail("compile failed: " + getCompilerErrorString());
81     }
82     
83     listener2.checkCompileOccurred();
84     _model.removeListener(listener2);
85     assertCompileErrorsPresent(_name(), false);
86     
87     // Make sure .class exists
88
File compiled = classForJava(file, "DrJavaTestFoo");
89     File compiled2 = classForJava(file, "DrJavaTestFoo2");
90     assertTrue(_name() + "Class file should exist after compile", compiled.exists());
91     assertTrue(_name() + "Class file should exist after compile", compiled2.exists());
92   }
93   
94   /**
95    * Test support for assert keyword if enabled.
96    * Note that this test only runs in Java 1.4 or higher.
97    * Doesn't reset interactions because no interpretations are performed.
98    */

99   public void testCompileWithJavaAssert()
100     throws BadLocationException JavaDoc, IOException, InterruptedException JavaDoc {
101 // System.out.println("testCompileWithJavaAssert()");
102
// No assert support by default (or in 1.3)
103
if (Float.valueOf(System.getProperty("java.specification.version")) < 1.5) {
104       OpenDefinitionsDocument doc = setupDocument(FOO_WITH_ASSERT);
105       final File file = tempFile();
106       doc.saveFile(new FileSelector(file));
107       CompileShouldFailListener listener = new CompileShouldFailListener();
108       _model.addListener(listener);
109       
110       // This is a CompileShouldFailListener, so we don't need to wait.
111
listener.compile(doc);
112       
113       assertCompileErrorsPresent(_name(), true);
114       listener.checkCompileOccurred();
115       File compiled = classForJava(file, "DrJavaTestFoo");
116       assertTrue(_name() + "Class file exists after compile?!", !compiled.exists());
117       _model.removeListener(listener);
118       
119       
120       // Only run assertions test in 1.4
121
String JavaDoc version = System.getProperty("java.version");
122       if ((version != null) && ("1.4.0".compareTo(version) <= 0)) {
123         // Turn on assert support
124
DrJava.getConfig().setSetting(OptionConstants.RUN_WITH_ASSERT,
125                                       Boolean.TRUE);
126         
127         CompileShouldSucceedListener listener2 = new CompileShouldSucceedListener(false);
128         _model.addListener(listener2);
129         listener2.compile(doc);
130         if (_model.getCompilerModel().getNumErrors() > 0) {
131           fail("compile failed: " + getCompilerErrorString());
132         }
133         _model.removeListener(listener2);
134         assertCompileErrorsPresent(_name(), false);
135         listener2.checkCompileOccurred();
136         
137         // Make sure .class exists
138
compiled = classForJava(file, "DrJavaTestFoo");
139         assertTrue(_name() + "Class file doesn't exist after compile",
140                    compiled.exists());
141       }
142     }
143   }
144
145   /**
146    * Tests compiling a file with generics works with generic compilers.
147    * (NOTE: this currently tests the GJ compiler, but not JSR-14...
148    * JSR-14 is only available if the config option is set, and we clear
149    * the config before running the tests. We have a guess where the jar
150    * is -- the lib directory -- but how can we get a URL for that?)
151    */

152   public void testCompileWithGenerics()
153     throws BadLocationException JavaDoc, IOException, InterruptedException JavaDoc
154   {
155 // System.out.println("testCompileWithGenerics()");
156
// Only run this test if using a compiler with generics
157
if (_isGenericCompiler()) {
158       
159       OpenDefinitionsDocument doc = setupDocument(FOO_WITH_GENERICS);
160       final File file = new File(_tempDir, "DrJavaTestFooGenerics.java");
161       doc.saveFile(new FileSelector(file));
162       
163       CompileShouldSucceedListener listener = new CompileShouldSucceedListener(false);
164       _model.addListener(listener);
165       _model.getCompilerModel().compileAll();
166       Utilities.clearEventQueue();
167       if (_model.getCompilerModel().getNumErrors() > 0) {
168         fail("compile failed: " + getCompilerErrorString());
169       }
170       assertCompileErrorsPresent(_name(), false);
171       listener.checkCompileOccurred();
172       _model.removeListener(listener);
173       
174       // Make sure .class exists
175
File compiled = classForJava(file, "DrJavaTestFooGenerics");
176       assertTrue(_name() + "FooGenerics Class file doesn't exist after compile", compiled.exists());
177     }
178   }
179 }
180
Popular Tags