From 4ac73161dfcd4cde7ede821900a313f5f2bf3536 Mon Sep 17 00:00:00 2001 From: Simon Schaefer Date: Tue, 9 Sep 2014 15:51:35 +0200 Subject: [PATCH] Add functionality to create Java files in `CompilerSupportTests` This adds functionality to create Java files in the test suite and changes the way how packages are created. Instead of moving all contents by default to the default package, it is now possible to move files to their own unique packages, which prevents name clashes. --- .../org/scalaide/CompilerSupportTests.scala | 82 +++++++++++++++++-- 1 file changed, 74 insertions(+), 8 deletions(-) diff --git a/org.scala-ide.sdt.core.tests/src/org/scalaide/CompilerSupportTests.scala b/org.scala-ide.sdt.core.tests/src/org/scalaide/CompilerSupportTests.scala index bbafd78046..9210372d49 100644 --- a/org.scala-ide.sdt.core.tests/src/org/scalaide/CompilerSupportTests.scala +++ b/org.scala-ide.sdt.core.tests/src/org/scalaide/CompilerSupportTests.scala @@ -1,6 +1,9 @@ package org.scalaide import org.eclipse.jdt.core.ICompilationUnit +import org.eclipse.jdt.core.IProblemRequestor +import org.eclipse.jdt.core.WorkingCopyOwner +import org.eclipse.jdt.core.compiler.IProblem import org.junit.AfterClass import org.scalaide.core.compiler.ScalaPresentationCompiler import org.scalaide.core.internal.jdt.model.ScalaCompilationUnit @@ -29,20 +32,83 @@ trait CompilerSupportTests { } /** - * Creates a compilation unit whose underlying source file physically exists - * in the test project of the test workspace. The file is placed in a unique - * package name to prevent name clashes between generated files. + * Generates a unique package name. + */ + final def uniquePkgName(): String = + s"testpackage${System.nanoTime()}" + + /** + * Creates a Scala compilation unit whose underlying source file physically + * exists in the test project of the test workspace. In order to prevent name + * clashes, `source` is scanned for a package declaration, which is mapped to + * the file system. + * + * If no package declaration is found, one is automatically generated. + * However, no package declaration is placed into `source` in such a case. + * This means that only file name clashes are avoided, but types can still + * hide each other. * * The newly generated file is made available to the Eclipse platform and the * Scala compiler to allow the usage of the full non GUI feature set of the IDE. + * + * Note, that `source` is not checked for compilation errors. This should be + * done immediately after the typechecking phase. */ - final def mkCompilationUnit(source: String): ICompilationUnit = { - val p = SDTTestUtils.createSourcePackage("testpackage" + System.nanoTime())(project) - SDTTestUtils.createCompilationUnit(p, "testfile.scala", source) + final def mkScalaCompilationUnit(source: String): ScalaCompilationUnit = { + val PkgFinder = """(?s).*?package ([\w\.]+).*?""".r + val pkgName = source match { + case PkgFinder(name) ⇒ name + case _ ⇒ uniquePkgName() + } + val p = SDTTestUtils.createSourcePackage(pkgName)(project) + SDTTestUtils.createCompilationUnit(p, "testfile.scala", source).asInstanceOf[ScalaCompilationUnit] } - final def mkScalaCompilationUnit(source: String): ScalaCompilationUnit = - mkCompilationUnit(source).asInstanceOf[ScalaCompilationUnit] + /** + * Creates a Java compilation unit whose underlying source file physically + * exists in the test project of the test workspace. In order to prevent name + * clashes, `source` is scanned for a package declaration, which is mapped to + * the file system. Furthermore, `source` is scanned for a type + * name, which is used as the filename. + * + * If either no package declaration or no type name is found, an exception is + * thrown. + * + * The newly generated file is made available to the Eclipse platform and the + * Scala compiler to allow the usage of the full non GUI feature set of the IDE. + * + * If any compilation errors are found in `source`, an exception is thrown + * too. + */ + final def mkJavaCompilationUnit(source: String): ICompilationUnit = { + val PkgFinder = """(?s).*?package ([\w\.]+).*?""".r + val ClassFinder = """(?s).*?public (?:(?:abstract )?class|interface) ([\w]+).*?""".r + val pkgName = source match { + case PkgFinder(name) ⇒ name + case _ ⇒ throw new IllegalArgumentException("No valid package declaration found.") + } + val fileName = source match { + case ClassFinder(name) ⇒ name + case _ ⇒ throw new IllegalArgumentException("No valid class declaration found.") + } + + val r = new IProblemRequestor { + override def acceptProblem(p: IProblem) = + throw new IllegalArgumentException(s"Got compilation error: $p") + override def beginReporting() = () + override def endReporting() = () + override def isActive() = true + } + import org.mockito.Mockito._ + import org.mockito.Matchers._ + + val o = mock(classOf[WorkingCopyOwner]) + when(o.getProblemRequestor(any())).thenReturn(r) + + val p = SDTTestUtils.createSourcePackage(pkgName)(project) + val u = SDTTestUtils.createCompilationUnit(p, s"$fileName.java", source) + u.getWorkingCopy(o, null) + } @AfterClass final def deleteProject(): Unit =