|
Groovy example source code file (GroovycTest.java)
The Groovy GroovycTest.java source code
/*
* Copyright 2003-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codehaus.groovy.ant;
import java.io.*;
import java.util.regex.Pattern;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
import groovy.util.GroovyTestCase;
/**
* Unit tests for the {@link Groovyc} ant task.
* <p/>
* <p>NB the *.groovy files in this directory should not get compiled with the rest of the test classes
* since that would ruin the whole point of testing compilation by the Ant tasks. In fact it doesn't
* matter as the tests remove all class files that should not pre-exist from this directory at each
* step</p>
*
* @author Russel Winder
*/
public class GroovycTest extends GroovyTestCase {
private final String classDirectory = "target/test-classes/org/codehaus/groovy/ant/";
private final File antFile = new File("src/test/org/codehaus/groovy/ant/GroovycTest.xml");
private Project project;
private static boolean warned = false;
protected void setUp() throws Exception {
super.setUp(); // Potentially throws Exception.
project = new Project();
project.init();
ProjectHelper.getProjectHelper().parse(project, antFile);
project.executeTarget("clean");
String altJavaHome = System.getProperty("java.home");
if (altJavaHome.lastIndexOf("jre") >= 0) {
altJavaHome = altJavaHome.substring(0, altJavaHome.lastIndexOf("jre"));
} else {
altJavaHome = altJavaHome + File.separator + "jre";
}
try {
File altFile = new File(altJavaHome);
if (altFile.exists()) {
project.setProperty("alt.java.home", altJavaHome);
}
} catch (Exception e) {
// could be security, io, etc. Ignore it.
// End result is as if .exists() returned null
}
}
private void ensureNotPresent(final String classname) {
if (!(new File(classDirectory + "GroovycTest.class")).exists()) {
fail("Class file for GroovycTest does not exist and should.");
}
if ((new File(classDirectory + classname + ".class")).exists()) {
fail("Class file for " + classname + " already exists and shouldn't.");
}
}
private void ensurePresent(final String classname) {
if (!(new File(classDirectory + classname + ".class")).exists()) {
fail("Class file for " + classname + " does not exist and should.");
}
}
private void ensureResultOK(final String classname) {
if (!(new File(classDirectory + classname + ".class")).exists()) {
fail("Class file for " + classname + " does not exist and should.");
}
final File result = new File(classDirectory + classname + "_Result.txt");
final char[] buffer = new char[10];
try {
(new FileReader(result)).read(buffer);
assertEquals("OK.", new String(buffer).trim());
} catch (final FileNotFoundException fnfe) {
fail("File " + result.getName() + " should have been created but wasn't.");
} catch (final IOException ioe) {
fail("Error reading file " + result.getName() + ".");
}
}
public void testGroovycTest1_NoFork_NoClasspath() {
ensureExecutes("GroovycTest1_NoFork_NoClasspath");
}
public void testGroovycTest1_NoFork_WithGroovyClasspath() {
ensureExecutes("GroovycTest1_NoFork_WithGroovyClasspath");
}
public void testGroovycTest1_NoFork_WithJavaClasspath() {
ensureExecutes("GroovycTest1_NoFork_WithJavaClasspath");
}
public void testGroovycTest1_NoFork_WithBothClasspath() {
ensureExecutes("GroovycTest1_NoFork_WithBothClasspath");
}
public void testGroovycTest1_ForkGroovy_NoClasspath() {
ensureExecutes("GroovycTest1_ForkGroovy_NoClasspath");
}
public void testGroovycTest1_ForkGroovy_WithGroovyClasspath() {
ensureExecutes("GroovycTest1_ForkGroovy_WithGroovyClasspath");
}
public void testGroovycTest1_ForkGroovy_WithJavaClasspath() {
ensureExecutes("GroovycTest1_ForkGroovy_WithJavaClasspath");
}
public void testGroovycTest1_ForkGroovy_WithBothClasspath() {
ensureExecutes("GroovycTest1_ForkGroovy_WithBothClasspath");
}
public void testGroovycTest1_Joint_NoFork_NoClasspath() {
ensureExecutes("GroovycTest1_Joint_NoFork_NoClasspath");
}
public void testGroovycTest1_Joint_NoFork_WithGroovyClasspath() {
ensureExecutes("GroovycTest1_Joint_NoFork_WithGroovyClasspath");
}
public void testGroovyc_Joint_NoFork_NestedCompilerArg_WithGroovyClasspath() {
// capture ant's output so we can verify the effect of passing compilerarg to javac
ByteArrayOutputStream allOutput = new ByteArrayOutputStream();
PrintStream out = new PrintStream(allOutput);
PrintStream origOut = System.out;
System.setOut(out);
ensureNotPresent("IncorrectGenericsUsage");
project.executeTarget("Groovyc_Joint_NoFork_NestedCompilerArg_WithGroovyClasspath");
ensurePresent("IncorrectGenericsUsage");
String antOutput = allOutput.toString();
antOutput = adjustOutputToHandleOpenJDKJavacOutputDifference(antOutput);
System.setOut(origOut);
// verify if passing -Xlint in compilerarg had its effect
Pattern p = Pattern.compile(".*?found[ ]*:[ ]*java.util.ArrayList.*", Pattern.DOTALL);
assertTrue("Expected line 1 not found in ant output", p.matcher(antOutput).matches());
p = Pattern.compile(".*?required[ ]*:[ ]*java.util.ArrayList<java.lang.String>.*", Pattern.DOTALL);
assertTrue("Expected line 2 not found in ant output", p.matcher(antOutput).matches());
}
/**
* For the code:
* private ArrayList<String> x = new ArrayList
Other Groovy examples (source code examples)Here is a short list of links related to this Groovy GroovycTest.java source code file: |
Other websites by Alvin Alexander:
Life/living in Alaska (OneMansAlaska.com)
How I Sold My Business (HowISoldMyBusiness.com)
Copyright 1998-2011 Alvin Alexander, devdaily.com
All Rights Reserved.