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.model;
038:
039: import java.io.*;
040:
041: import javax.swing.text.BadLocationException;
042:
043: import edu.rice.cs.drjava.DrJava;
044: import edu.rice.cs.drjava.config.OptionConstants;
045:
046: import edu.rice.cs.util.swing.Utilities;
047:
048: /**
049: * Tests to ensure that compilation succeeds when expected.
050: *
051: * Every test in this class is run for *each* of the compilers that is available.
052: *
053: * @version $Id: GlobalModelCompileSuccessOptionsTest.java 4255 2007-08-28 19:17:37Z mgricken $
054: */
055: public final class GlobalModelCompileSuccessOptionsTest extends
056: GlobalModelCompileSuccessTestCase {
057:
058: /**
059: * Tests a compile on a file that references a non-public class defined in
060: * another class with a name different than the non-public class.
061: * Doesn't reset interactions because no interpretations are performed.
062: */
063: public void testCompileReferenceToNonPublicClass()
064: throws BadLocationException, IOException,
065: InterruptedException {
066: // System.out.println("testCompileReferenceToNonPublicClass()");
067: OpenDefinitionsDocument doc = setupDocument(FOO_NON_PUBLIC_CLASS_TEXT);
068: OpenDefinitionsDocument doc2 = setupDocument(FOO2_REFERENCES_NON_PUBLIC_CLASS_TEXT);
069: final File file = tempFile();
070: final File file2 = tempFile(1);
071: doc.saveFile(new FileSelector(file));
072: doc2.saveFile(new FileSelector(file2));
073: CompileShouldSucceedListener listener = new CompileShouldSucceedListener(
074: false);
075: _model.addListener(listener);
076: listener.compile(doc);
077: if (_model.getCompilerModel().getNumErrors() > 0) {
078: fail("compile failed: " + getCompilerErrorString());
079: }
080: listener.checkCompileOccurred();
081: _model.removeListener(listener);
082: CompileShouldSucceedListener listener2 = new CompileShouldSucceedListener(
083: false);
084: _model.addListener(listener2);
085: listener2.compile(doc2);
086: if (_model.getCompilerModel().getNumErrors() > 0) {
087: fail("compile failed: " + getCompilerErrorString());
088: }
089:
090: listener2.checkCompileOccurred();
091: _model.removeListener(listener2);
092: assertCompileErrorsPresent(_name(), false);
093:
094: // Make sure .class exists
095: File compiled = classForJava(file, "DrJavaTestFoo");
096: File compiled2 = classForJava(file, "DrJavaTestFoo2");
097: assertTrue(_name() + "Class file should exist after compile",
098: compiled.exists());
099: assertTrue(_name() + "Class file should exist after compile",
100: compiled2.exists());
101: }
102:
103: /**
104: * Test support for assert keyword if enabled.
105: * Note that this test only runs in Java 1.4 or higher.
106: * Doesn't reset interactions because no interpretations are performed.
107: */
108: public void testCompileWithJavaAssert()
109: throws BadLocationException, IOException,
110: InterruptedException {
111: // System.out.println("testCompileWithJavaAssert()");
112: // No assert support by default (or in 1.3)
113: if (Float.valueOf(System
114: .getProperty("java.specification.version")) < 1.5) {
115: OpenDefinitionsDocument doc = setupDocument(FOO_WITH_ASSERT);
116: final File file = tempFile();
117: doc.saveFile(new FileSelector(file));
118: CompileShouldFailListener listener = new CompileShouldFailListener();
119: _model.addListener(listener);
120:
121: // This is a CompileShouldFailListener, so we don't need to wait.
122: listener.compile(doc);
123:
124: assertCompileErrorsPresent(_name(), true);
125: listener.checkCompileOccurred();
126: File compiled = classForJava(file, "DrJavaTestFoo");
127: assertTrue(_name() + "Class file exists after compile?!",
128: !compiled.exists());
129: _model.removeListener(listener);
130:
131: // Only run assertions test in 1.4
132: String version = System.getProperty("java.version");
133: if ((version != null) && ("1.4.0".compareTo(version) <= 0)) {
134: // Turn on assert support
135: DrJava.getConfig().setSetting(
136: OptionConstants.RUN_WITH_ASSERT, Boolean.TRUE);
137:
138: CompileShouldSucceedListener listener2 = new CompileShouldSucceedListener(
139: false);
140: _model.addListener(listener2);
141: listener2.compile(doc);
142: if (_model.getCompilerModel().getNumErrors() > 0) {
143: fail("compile failed: " + getCompilerErrorString());
144: }
145: _model.removeListener(listener2);
146: assertCompileErrorsPresent(_name(), false);
147: listener2.checkCompileOccurred();
148:
149: // Make sure .class exists
150: compiled = classForJava(file, "DrJavaTestFoo");
151: assertTrue(_name()
152: + "Class file doesn't exist after compile",
153: compiled.exists());
154: }
155: }
156: }
157:
158: /**
159: * Tests compiling a file with generics works with generic compilers.
160: * (NOTE: this currently tests the GJ compiler, but not JSR-14...
161: * JSR-14 is only available if the config option is set, and we clear
162: * the config before running the tests. We have a guess where the jar
163: * is -- the lib directory -- but how can we get a URL for that?)
164: */
165: public void testCompileWithGenerics() throws BadLocationException,
166: IOException, InterruptedException {
167: // System.out.println("testCompileWithGenerics()");
168: // Only run this test if using a compiler with generics
169: if (_isGenericCompiler()) {
170:
171: OpenDefinitionsDocument doc = setupDocument(FOO_WITH_GENERICS);
172: final File file = new File(_tempDir,
173: "DrJavaTestFooGenerics.java");
174: doc.saveFile(new FileSelector(file));
175:
176: CompileShouldSucceedListener listener = new CompileShouldSucceedListener(
177: false);
178: _model.addListener(listener);
179: _model.getCompilerModel().compileAll();
180: Utilities.clearEventQueue();
181: if (_model.getCompilerModel().getNumErrors() > 0) {
182: fail("compile failed: " + getCompilerErrorString());
183: }
184: assertCompileErrorsPresent(_name(), false);
185: listener.checkCompileOccurred();
186: _model.removeListener(listener);
187:
188: // Make sure .class exists
189: File compiled = classForJava(file, "DrJavaTestFooGenerics");
190: assertTrue(
191: _name()
192: + "FooGenerics Class file doesn't exist after compile",
193: compiled.exists());
194: }
195: }
196: }
|