/groovy-eclipse/org.codehaus.groovy.eclipse.core.test/src/org/codehaus/groovy/eclipse/core/compiler/GroovyCompilerTests.java
Java | 228 lines | 162 code | 47 blank | 19 comment | 16 complexity | 9ac2243922f6874f533b0950a3c131cb MD5 | raw file
Possible License(s): Apache-2.0
- package org.codehaus.groovy.eclipse.core.compiler;
-
- import java.io.ByteArrayInputStream;
- import java.io.File;
- import java.io.IOException;
- import java.util.Iterator;
- import java.util.Set;
- import org.codehaus.groovy.eclipse.core.model.ChangeSet;
- import org.codehaus.groovy.eclipse.core.model.GroovyProject;
- import org.codehaus.groovy.eclipse.core.model.GroovyRuntime;
- import org.codehaus.groovy.eclipse.test.EclipseTestCase;
- import org.eclipse.core.runtime.CoreException;
- import org.eclipse.jdt.core.JavaCore;
-
- public class GroovyCompilerTests extends EclipseTestCase {
- // The number of source files added to the project. Change when adding/removing source files.
- static final int SOURCE_FILE_COUNT = 2;
-
- static long t0, t1;
-
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- GroovyRuntime.addGroovyRuntime(testProject.getProject());
-
- testProject.createGroovyTypeAndPackage("core.tests", "GroovyClass.groovy", "class GroovyClass {}");
-
- testProject.createGroovyTypeAndPackage("core.tests", "GroovyClass2.groovy", "class GroovyClass2 {\n"
- + " def clos = { println 'yay'}\n" + " def method() { 10.times { println it } }\n" + "}\n"
- + "class GroovyClass3 {}");
-
- t0 = System.currentTimeMillis();
- }
-
- @Override
- protected void tearDown() throws Exception {
- t1 = System.currentTimeMillis();
- System.out.println("Time for test: " + (t1 - t0) / 1000000.0 + " seconds.");
-
- super.tearDown();
- }
-
- private String getClassPath() throws CoreException {
- Set setOfClassPath = model.getGroovyProject(testProject.getProject()).getClassPath();
- StringBuffer sb = new StringBuffer();
- for (Iterator iter = setOfClassPath.iterator(); iter.hasNext();) {
- sb.append(iter.next().toString());
- sb.append(File.pathSeparator);
- }
- return sb.length() > 0 ? sb.substring(0, sb.length() - 1) : sb.toString();
- }
-
- public void testReportingBeginEnd() throws CoreException {
- GroovyCompilerConfiguration config = new GroovyCompilerConfigurationBuilder().buildAST().done();
- ChangeSet changes = model.getGroovyProject(testProject.getProject()).filesForFullBuild();
- Reporter reporter = new Reporter();
- IGroovyCompiler compiler = new GroovyCompiler();
-
- compiler.compile(changes.fileNamesToBuild(), config, reporter);
-
- assertTrue(reporter.reportingStarted);
- assertTrue(reporter.reportingEnded);
- }
-
- public void testCompileASTs() throws CoreException {
- ChangeSet changes = model.getGroovyProject(testProject.getProject()).filesForFullBuild();
- Reporter reporter = new Reporter();
- IGroovyCompiler compiler = new GroovyCompiler();
-
- GroovyCompilerConfiguration config = new GroovyCompilerConfigurationBuilder()
- .buildAST()
- .classPath(getClassPath())
- .outputPath(GroovyProject.getOutputPath(JavaCore.create(testProject.getProject())))
- .done();
-
- compiler.compile(changes.fileNamesToBuild(), config, reporter);
-
- assertFalse(reporter.hasReportingErrors());
- assertTrue(reporter.mapFileNameToCST.size() == 0);
- // TODO: this doesn't seem correct. 3 files, not 2?
- assertEquals( SOURCE_FILE_COUNT, reporter.mapFileNameToAST.size() );
- assertEquals( 0, reporter.mapFileNameToClassNames.size() );
- assertEquals( 0, reporter.mapFileNameToClassFilePaths.size() );
- }
-
- public void testCompileCSTs() throws CoreException {
- ChangeSet changes = model.getGroovyProject(testProject.getProject()).filesForFullBuild();
- Reporter reporter = new Reporter();
- IGroovyCompiler compiler = new GroovyCompiler();
-
- GroovyCompilerConfiguration config = new GroovyCompilerConfigurationBuilder().buildCST().done();
-
- compiler.compile(changes.fileNamesToBuild(), config, reporter);
-
- assertFalse(reporter.hasReportingErrors());
- assertTrue(reporter.mapFileNameToCST.size() == 2);
- assertTrue(reporter.mapFileNameToAST.size() == 0);
- assertTrue(reporter.mapFileNameToClassNames.size() == 0);
- assertTrue(reporter.mapFileNameToClassFilePaths.size() == 0);
- }
-
- public void testCompileClassFiles() throws CoreException {
- ChangeSet changes = model.getGroovyProject(testProject.getProject()).filesForFullBuild();
- Reporter reporter = new Reporter();
- IGroovyCompiler compiler = new GroovyCompiler();
-
- GroovyCompilerConfiguration config = new GroovyCompilerConfigurationBuilder()
- .buildClasses()
- .classPath(getClassPath())
- .outputPath(GroovyProject.getOutputPath(JavaCore.create(testProject.getProject())))
- .done();
-
-
- // if (outputPath == "") {
- // // This is default behavious, however better for programming, e.g. outputPath + "/tmp" is intended to be
- // // "./tmp" and not "/tmp"
- // outputPath = ".";
- // }
- //
- compiler.compile(changes.fileNamesToBuild(), config, reporter);
-
- assertFalse(reporter.hasReportingErrors());
- assertTrue(reporter.mapFileNameToCST.size() == 0);
- assertTrue(reporter.mapFileNameToAST.size() == 0);
- assertTrue( "" + reporter.mapFileNameToClassNames.size(), reporter.mapFileNameToClassNames.size() > 0);
- assertTrue(reporter.mapFileNameToClassFilePaths.size() > 0);
-
- // System.out.println(outputPath + "/core/tests");
- // File file = new File("./core/tests");
- // System.out.println("isDir:" + file.isDirectory());
- // System.out.println("isFile:" + file.isFile());
- // System.out.println("exists:" + file.exists());
-
- // assertTrue(file.isDirectory());
- // File[] files = file.listFiles();
- // for (int i = 0; i < files.length; ++i) {
- // System.out.println(files[i].getName());
- // }
- }
-
- public void testCompileASTsAndClassFiles() throws CoreException {
- ChangeSet changes = model.getGroovyProject(testProject.getProject()).filesForFullBuild();
- Reporter reporter = new Reporter();
- IGroovyCompiler compiler = new GroovyCompiler();
-
- GroovyCompilerConfiguration config = new GroovyCompilerConfigurationBuilder()
- .buildAST()
- .buildClasses()
- .classPath(getClassPath())
- .outputPath(GroovyProject.getOutputPath(JavaCore.create(testProject.getProject())))
- .done();
-
- compiler.compile(changes.fileNamesToBuild(), config, reporter);
-
- assertFalse(reporter.hasReportingErrors());
- assertTrue(reporter.mapFileNameToCST.size() == 0);
- assertEquals( SOURCE_FILE_COUNT, reporter.mapFileNameToAST.size() );
- assertTrue( "" + reporter.mapFileNameToClassNames.size(), reporter.mapFileNameToClassNames.size() > 0);
- assertTrue( "" + reporter.mapFileNameToClassFilePaths.size(), reporter.mapFileNameToClassFilePaths.size() > 0 );
- }
-
- public void testCountGeneratedClassFiles() throws CoreException, IOException {
- ChangeSet changes = model.getGroovyProject(testProject.getProject()).filesForFullBuild();
- Reporter reporter = new Reporter();
- IGroovyCompiler compiler = new GroovyCompiler();
-
- GroovyCompilerConfiguration config = new GroovyCompilerConfigurationBuilder()
- .buildClasses()
- .classPath(getClassPath())
- .outputPath(GroovyProject.getOutputPath(JavaCore.create(testProject.getProject())))
- .done();
-
- String[] fileNamesToBuild = changes.fileNamesToBuild();
-
- compiler.compile(fileNamesToBuild, config, reporter);
-
- assertFalse(reporter.hasReportingErrors());
- assertTrue(reporter.mapFileNameToClassNames.size() == 2);
- assertTrue(reporter.mapFileNameToClassFilePaths.size() == 2);
- int count = countClassFilesBuilt(fileNamesToBuild, reporter);
- assertTrue(count == 5);
- }
-
- public void testBuildWithErrors() throws CoreException {
- Reporter reporter = new Reporter();
- IGroovyCompiler compiler = new GroovyCompiler();
-
- GroovyCompilerConfiguration config = new GroovyCompilerConfigurationBuilder()
- .buildAST()
- .classPath(getClassPath())
- .outputPath(GroovyProject.getOutputPath(JavaCore.create(testProject.getProject())))
- .done();
-
- // A class with errors.
- testProject.createGroovyTypeAndPackage("core.tests", "GroovyClass3.groovy", "class GroovyClass3 { def a = }");
-
- ChangeSet changes = model.getGroovyProject(testProject.getProject()).filesForFullBuild();
- String[] fileNamesToBuild = changes.fileNamesToBuild();
-
- compiler.compile(fileNamesToBuild, config, reporter);
- assertTrue(reporter.hasReportingErrors());
- assertTrue(reporter.mapFileNameToErrorMessages.size() == 1);
- }
-
- public void testBuildASTWithInputStream() {
- Reporter reporter = new Reporter();
- IGroovyCompiler compiler = new GroovyCompiler();
-
- GroovyCompilerConfiguration config = new GroovyCompilerConfigurationBuilder()
- .buildAST()
- .done();
-
- String fileName = "CreateAST";
-
- compiler.compile(fileName, new ByteArrayInputStream("x.y()[10].z".getBytes()), config, reporter);
- assertFalse(reporter.hasReportingErrors());
- assertTrue(reporter.mapFileNameToCST.size() == 0);
- assertTrue(reporter.mapFileNameToAST.size() == 1);
- }
-
- private int countClassFilesBuilt(String[] fileNamesToBuild, Reporter reporter) throws IOException {
- int count = 0;
- for (int i = 0; i < fileNamesToBuild.length; ++i) {
- count += ((String[]) reporter.mapFileNameToClassFilePaths.get(new File(fileNamesToBuild[i]).getCanonicalPath())).length;
- }
- return count;
- }
- }