PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/groovy-eclipse/org.codehaus.groovy.eclipse.core.test/src/org/codehaus/groovy/eclipse/core/compiler/GroovyCompilerTests.java

http://groovy-eclipse.googlecode.com/
Java | 228 lines | 162 code | 47 blank | 19 comment | 16 complexity | 9ac2243922f6874f533b0950a3c131cb MD5 | raw file
Possible License(s): Apache-2.0
  1. package org.codehaus.groovy.eclipse.core.compiler;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.Iterator;
  6. import java.util.Set;
  7. import org.codehaus.groovy.eclipse.core.model.ChangeSet;
  8. import org.codehaus.groovy.eclipse.core.model.GroovyProject;
  9. import org.codehaus.groovy.eclipse.core.model.GroovyRuntime;
  10. import org.codehaus.groovy.eclipse.test.EclipseTestCase;
  11. import org.eclipse.core.runtime.CoreException;
  12. import org.eclipse.jdt.core.JavaCore;
  13. public class GroovyCompilerTests extends EclipseTestCase {
  14. // The number of source files added to the project. Change when adding/removing source files.
  15. static final int SOURCE_FILE_COUNT = 2;
  16. static long t0, t1;
  17. @Override
  18. protected void setUp() throws Exception {
  19. super.setUp();
  20. GroovyRuntime.addGroovyRuntime(testProject.getProject());
  21. testProject.createGroovyTypeAndPackage("core.tests", "GroovyClass.groovy", "class GroovyClass {}");
  22. testProject.createGroovyTypeAndPackage("core.tests", "GroovyClass2.groovy", "class GroovyClass2 {\n"
  23. + " def clos = { println 'yay'}\n" + " def method() { 10.times { println it } }\n" + "}\n"
  24. + "class GroovyClass3 {}");
  25. t0 = System.currentTimeMillis();
  26. }
  27. @Override
  28. protected void tearDown() throws Exception {
  29. t1 = System.currentTimeMillis();
  30. System.out.println("Time for test: " + (t1 - t0) / 1000000.0 + " seconds.");
  31. super.tearDown();
  32. }
  33. private String getClassPath() throws CoreException {
  34. Set setOfClassPath = model.getGroovyProject(testProject.getProject()).getClassPath();
  35. StringBuffer sb = new StringBuffer();
  36. for (Iterator iter = setOfClassPath.iterator(); iter.hasNext();) {
  37. sb.append(iter.next().toString());
  38. sb.append(File.pathSeparator);
  39. }
  40. return sb.length() > 0 ? sb.substring(0, sb.length() - 1) : sb.toString();
  41. }
  42. public void testReportingBeginEnd() throws CoreException {
  43. GroovyCompilerConfiguration config = new GroovyCompilerConfigurationBuilder().buildAST().done();
  44. ChangeSet changes = model.getGroovyProject(testProject.getProject()).filesForFullBuild();
  45. Reporter reporter = new Reporter();
  46. IGroovyCompiler compiler = new GroovyCompiler();
  47. compiler.compile(changes.fileNamesToBuild(), config, reporter);
  48. assertTrue(reporter.reportingStarted);
  49. assertTrue(reporter.reportingEnded);
  50. }
  51. public void testCompileASTs() throws CoreException {
  52. ChangeSet changes = model.getGroovyProject(testProject.getProject()).filesForFullBuild();
  53. Reporter reporter = new Reporter();
  54. IGroovyCompiler compiler = new GroovyCompiler();
  55. GroovyCompilerConfiguration config = new GroovyCompilerConfigurationBuilder()
  56. .buildAST()
  57. .classPath(getClassPath())
  58. .outputPath(GroovyProject.getOutputPath(JavaCore.create(testProject.getProject())))
  59. .done();
  60. compiler.compile(changes.fileNamesToBuild(), config, reporter);
  61. assertFalse(reporter.hasReportingErrors());
  62. assertTrue(reporter.mapFileNameToCST.size() == 0);
  63. // TODO: this doesn't seem correct. 3 files, not 2?
  64. assertEquals( SOURCE_FILE_COUNT, reporter.mapFileNameToAST.size() );
  65. assertEquals( 0, reporter.mapFileNameToClassNames.size() );
  66. assertEquals( 0, reporter.mapFileNameToClassFilePaths.size() );
  67. }
  68. public void testCompileCSTs() throws CoreException {
  69. ChangeSet changes = model.getGroovyProject(testProject.getProject()).filesForFullBuild();
  70. Reporter reporter = new Reporter();
  71. IGroovyCompiler compiler = new GroovyCompiler();
  72. GroovyCompilerConfiguration config = new GroovyCompilerConfigurationBuilder().buildCST().done();
  73. compiler.compile(changes.fileNamesToBuild(), config, reporter);
  74. assertFalse(reporter.hasReportingErrors());
  75. assertTrue(reporter.mapFileNameToCST.size() == 2);
  76. assertTrue(reporter.mapFileNameToAST.size() == 0);
  77. assertTrue(reporter.mapFileNameToClassNames.size() == 0);
  78. assertTrue(reporter.mapFileNameToClassFilePaths.size() == 0);
  79. }
  80. public void testCompileClassFiles() throws CoreException {
  81. ChangeSet changes = model.getGroovyProject(testProject.getProject()).filesForFullBuild();
  82. Reporter reporter = new Reporter();
  83. IGroovyCompiler compiler = new GroovyCompiler();
  84. GroovyCompilerConfiguration config = new GroovyCompilerConfigurationBuilder()
  85. .buildClasses()
  86. .classPath(getClassPath())
  87. .outputPath(GroovyProject.getOutputPath(JavaCore.create(testProject.getProject())))
  88. .done();
  89. // if (outputPath == "") {
  90. // // This is default behavious, however better for programming, e.g. outputPath + "/tmp" is intended to be
  91. // // "./tmp" and not "/tmp"
  92. // outputPath = ".";
  93. // }
  94. //
  95. compiler.compile(changes.fileNamesToBuild(), config, reporter);
  96. assertFalse(reporter.hasReportingErrors());
  97. assertTrue(reporter.mapFileNameToCST.size() == 0);
  98. assertTrue(reporter.mapFileNameToAST.size() == 0);
  99. assertTrue( "" + reporter.mapFileNameToClassNames.size(), reporter.mapFileNameToClassNames.size() > 0);
  100. assertTrue(reporter.mapFileNameToClassFilePaths.size() > 0);
  101. // System.out.println(outputPath + "/core/tests");
  102. // File file = new File("./core/tests");
  103. // System.out.println("isDir:" + file.isDirectory());
  104. // System.out.println("isFile:" + file.isFile());
  105. // System.out.println("exists:" + file.exists());
  106. // assertTrue(file.isDirectory());
  107. // File[] files = file.listFiles();
  108. // for (int i = 0; i < files.length; ++i) {
  109. // System.out.println(files[i].getName());
  110. // }
  111. }
  112. public void testCompileASTsAndClassFiles() throws CoreException {
  113. ChangeSet changes = model.getGroovyProject(testProject.getProject()).filesForFullBuild();
  114. Reporter reporter = new Reporter();
  115. IGroovyCompiler compiler = new GroovyCompiler();
  116. GroovyCompilerConfiguration config = new GroovyCompilerConfigurationBuilder()
  117. .buildAST()
  118. .buildClasses()
  119. .classPath(getClassPath())
  120. .outputPath(GroovyProject.getOutputPath(JavaCore.create(testProject.getProject())))
  121. .done();
  122. compiler.compile(changes.fileNamesToBuild(), config, reporter);
  123. assertFalse(reporter.hasReportingErrors());
  124. assertTrue(reporter.mapFileNameToCST.size() == 0);
  125. assertEquals( SOURCE_FILE_COUNT, reporter.mapFileNameToAST.size() );
  126. assertTrue( "" + reporter.mapFileNameToClassNames.size(), reporter.mapFileNameToClassNames.size() > 0);
  127. assertTrue( "" + reporter.mapFileNameToClassFilePaths.size(), reporter.mapFileNameToClassFilePaths.size() > 0 );
  128. }
  129. public void testCountGeneratedClassFiles() throws CoreException, IOException {
  130. ChangeSet changes = model.getGroovyProject(testProject.getProject()).filesForFullBuild();
  131. Reporter reporter = new Reporter();
  132. IGroovyCompiler compiler = new GroovyCompiler();
  133. GroovyCompilerConfiguration config = new GroovyCompilerConfigurationBuilder()
  134. .buildClasses()
  135. .classPath(getClassPath())
  136. .outputPath(GroovyProject.getOutputPath(JavaCore.create(testProject.getProject())))
  137. .done();
  138. String[] fileNamesToBuild = changes.fileNamesToBuild();
  139. compiler.compile(fileNamesToBuild, config, reporter);
  140. assertFalse(reporter.hasReportingErrors());
  141. assertTrue(reporter.mapFileNameToClassNames.size() == 2);
  142. assertTrue(reporter.mapFileNameToClassFilePaths.size() == 2);
  143. int count = countClassFilesBuilt(fileNamesToBuild, reporter);
  144. assertTrue(count == 5);
  145. }
  146. public void testBuildWithErrors() throws CoreException {
  147. Reporter reporter = new Reporter();
  148. IGroovyCompiler compiler = new GroovyCompiler();
  149. GroovyCompilerConfiguration config = new GroovyCompilerConfigurationBuilder()
  150. .buildAST()
  151. .classPath(getClassPath())
  152. .outputPath(GroovyProject.getOutputPath(JavaCore.create(testProject.getProject())))
  153. .done();
  154. // A class with errors.
  155. testProject.createGroovyTypeAndPackage("core.tests", "GroovyClass3.groovy", "class GroovyClass3 { def a = }");
  156. ChangeSet changes = model.getGroovyProject(testProject.getProject()).filesForFullBuild();
  157. String[] fileNamesToBuild = changes.fileNamesToBuild();
  158. compiler.compile(fileNamesToBuild, config, reporter);
  159. assertTrue(reporter.hasReportingErrors());
  160. assertTrue(reporter.mapFileNameToErrorMessages.size() == 1);
  161. }
  162. public void testBuildASTWithInputStream() {
  163. Reporter reporter = new Reporter();
  164. IGroovyCompiler compiler = new GroovyCompiler();
  165. GroovyCompilerConfiguration config = new GroovyCompilerConfigurationBuilder()
  166. .buildAST()
  167. .done();
  168. String fileName = "CreateAST";
  169. compiler.compile(fileName, new ByteArrayInputStream("x.y()[10].z".getBytes()), config, reporter);
  170. assertFalse(reporter.hasReportingErrors());
  171. assertTrue(reporter.mapFileNameToCST.size() == 0);
  172. assertTrue(reporter.mapFileNameToAST.size() == 1);
  173. }
  174. private int countClassFilesBuilt(String[] fileNamesToBuild, Reporter reporter) throws IOException {
  175. int count = 0;
  176. for (int i = 0; i < fileNamesToBuild.length; ++i) {
  177. count += ((String[]) reporter.mapFileNameToClassFilePaths.get(new File(fileNamesToBuild[i]).getCanonicalPath())).length;
  178. }
  179. return count;
  180. }
  181. }