Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added project dependencies test, and refactored some other tests to r…

…euse more code. Fixed #1000894.

(cherry picked from commit 390a1cf)
  • Loading branch information...
commit 641c027109d72d2cf2d652fef700a365fac0e5ba 1 parent de3d140
@dragos dragos authored
View
2  org.scala-ide.sdt.core.tests/src/scala/tools/eclipse/EclipseUserSimulator.scala
@@ -52,7 +52,7 @@ class EclipseUserSimulator {
ScalaPlugin.plugin.getScalaProject(project);
}
- def createPackage(packageName: String) =
+ def createPackage(packageName: String): IPackageFragment =
root.createPackageFragment(packageName, false, null);
def createCompilationUnit(pack: IPackageFragment, name: String, sourceCode: String) = {
View
2  org.scala-ide.sdt.core.tests/src/scala/tools/eclipse/TestsSuite.java
@@ -11,6 +11,7 @@
import scala.tools.eclipse.occurrences.OccurrencesFinderTest;
import scala.tools.eclipse.sbtbuilder.NestedProjectsTest;
import scala.tools.eclipse.sbtbuilder.OutputFoldersTest;
+import scala.tools.eclipse.sbtbuilder.ProjectDependenciesTest;
import scala.tools.eclipse.sbtbuilder.SbtBuilderTest;
import scala.tools.eclipse.sbtbuilder.TodoBuilderTest;
import scala.tools.eclipse.sbtbuilder.ScalaCompilerClasspathTest;
@@ -48,5 +49,6 @@
ScalaJavaDepTest.class,
NestedProjectsTest.class,
CompilerSettingsTest.class,
+ ProjectDependenciesTest.class,
})
class TestsSuite { }
View
2  org.scala-ide.sdt.core.tests/src/scala/tools/eclipse/pc/PresentationCompilerRefreshTest.scala
@@ -25,7 +25,7 @@ class PresentationCompilerRefreshTest {
EclipseUtils.workspaceRunnableIn(SDTTestUtils.workspace) { monitor =>
SDTTestUtils.addFileToProject(project.underlying, "src/b/C.scala", C_scala)
- SDTTestUtils.changeContentOfFile(project.underlying, unitA.getResource().getAdapter(classOf[IFile]).asInstanceOf[IFile], new_A_scala)
+ SDTTestUtils.changeContentOfFile(unitA.getResource().getAdapter(classOf[IFile]).asInstanceOf[IFile], new_A_scala)
}
unitA.doWithSourceFile { (sf, comp) =>
View
2  org.scala-ide.sdt.core.tests/src/scala/tools/eclipse/sbtbuilder/NestedProjectsTest.scala
@@ -58,7 +58,7 @@ class NestedProjectsTest {
// update and recompile Java_01.java
val compilationUnit= scalaSrcPackageRoot.getPackageFragment("test").getCompilationUnit("Java_01.java")
- SDTTestUtils.changeContentOfFile(project.underlying, compilationUnit.getResource().asInstanceOf[IFile], changed_test_Java_01)
+ SDTTestUtils.changeContentOfFile(compilationUnit.getResource().asInstanceOf[IFile], changed_test_Java_01)
scalaProject.underlying.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, new NullProgressMonitor)
View
109 org.scala-ide.sdt.core.tests/src/scala/tools/eclipse/sbtbuilder/ProjectDependenciesTest.scala
@@ -0,0 +1,109 @@
+package scala.tools.eclipse
+package sbtbuilder
+
+import org.junit.Test
+import org.eclipse.jdt.core.IJavaProject
+import org.eclipse.jdt.core.IClasspathEntry
+import org.eclipse.jdt.core.JavaCore
+import org.junit.Assert
+import util.EclipseUtils._
+import scala.tools.eclipse.testsetup.SDTTestUtils
+import org.eclipse.core.resources.IncrementalProjectBuilder
+import properties.IDESettings
+import properties.CompilerSettings
+import org.eclipse.jdt.core.IPackageFragment
+
+class ProjectDependenciesTest {
+
+ val simulator = new EclipseUserSimulator
+
+ @Test def transitive_dependencies_no_export() {
+ val Seq(prjA, prjB, prjC) = createProjects("A", "B", "C")
+
+ // A -> B -> C
+ addToClasspath(prjB, JavaCore.newProjectEntry(prjA.underlying.getFullPath, false))
+ addToClasspath(prjC, JavaCore.newProjectEntry(prjB.underlying.getFullPath, false))
+
+ Assert.assertEquals("No dependencies for base project", Seq(), prjA.transitiveDependencies)
+ Assert.assertEquals("One direct dependency for B", Seq(prjA.underlying), prjB.transitiveDependencies)
+ Assert.assertEquals("One transitive dependency for C", Seq(prjB.underlying), prjC.transitiveDependencies)
+
+ deleteProjects(prjA, prjB, prjC)
+ }
+
+ @Test def transitive_dependencies_with_export() {
+ val Seq(prjA, prjB, prjC) = createProjects("A", "B", "C")
+
+ // A -> B -> C
+ addToClasspath(prjB, JavaCore.newProjectEntry(prjA.underlying.getFullPath, true))
+ addToClasspath(prjC, JavaCore.newProjectEntry(prjB.underlying.getFullPath, false))
+
+ Assert.assertEquals("No dependencies for base project", Seq(), prjA.transitiveDependencies)
+ Assert.assertEquals("One direct dependency for B", Seq(prjA.underlying), prjB.transitiveDependencies)
+ Assert.assertEquals("Two transitive dependencies for C", Seq(prjB.underlying, prjA.underlying), prjC.transitiveDependencies)
+
+ deleteProjects(prjA, prjB, prjC)
+ }
+
+ @Test def transitive_dep_with_error_stops_build() {
+ val Seq(prjA, prjB, prjC) = createProjects("A", "B", "C")
+
+ // A -> B -> C
+ addToClasspath(prjB, JavaCore.newProjectEntry(prjA.underlying.getFullPath, true))
+ addToClasspath(prjC, JavaCore.newProjectEntry(prjB.underlying.getFullPath, false))
+
+ val Seq(packA, packB, packC) = Seq(prjA, prjB, prjC).map(createSourcePackage("test"))
+
+ val unitA = packA.createCompilationUnit("A.scala", "class A", true, null)
+ val unitB = packB.createCompilationUnit("B.scala", "class B extends A", true, null)
+ val unitC = packC.createCompilationUnit("C.scala", "class C(a: A, b: B)", true, null)
+
+ // set stopOnBuild to true
+ val stopBuildOnErrors = SettingConverterUtil.convertNameToProperty(properties.ScalaPluginSettings.stopBuildOnErrors.name)
+ ScalaPlugin.plugin.getPreferenceStore.setValue(stopBuildOnErrors, true)
+
+ // no errors
+ SDTTestUtils.workspace.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, null)
+
+ val unitsToWatch = Seq(unitA, unitB, unitC)
+
+ val errors = SDTTestUtils.getErrorMessages(unitsToWatch: _*)
+ Assert.assertEquals("No build errors", Seq(), errors)
+
+ // one error in A, no cascading errors
+ val errors1 = SDTTestUtils.buildWith(unitA.getResource, "klass A", unitsToWatch)
+ Assert.assertEquals("One build error in A", Seq("expected class or object definition"), errors1)
+
+ val errorsInBAndC = SDTTestUtils.getErrorMessages(unitB, unitC)
+ Assert.assertEquals("No errors in dependent projects", Seq(), errorsInBAndC)
+
+ // fix project A, error in B, no cascading errors in C
+ val errors2 = SDTTestUtils.buildWith(unitA.getResource, "class A(x: Int)", unitsToWatch)
+ Assert.assertEquals("No errors in A", Seq(), SDTTestUtils.getErrorMessages(unitA))
+ Assert.assertEquals("No errors in C", Seq(), SDTTestUtils.getErrorMessages(unitC))
+ Assert.assertEquals("One error in B", 1, SDTTestUtils.getErrorMessages(unitB).size)
+
+ // fix all errors, everything built
+ val errors3 = SDTTestUtils.buildWith(unitA.getResource, "class A", unitsToWatch)
+ Assert.assertEquals("No errors in A, B or C", Seq(), errors3)
+
+ deleteProjects(prjA, prjB, prjC)
+ }
+
+ private def createSourcePackage(name: String)(project: ScalaProject): IPackageFragment =
+ project.javaProject.getPackageFragmentRoot(project.underlying.getFolder("/src")).createPackageFragment(name, true, null)
+
+ private def addToClasspath(prj: ScalaProject, entries: IClasspathEntry*) {
+ val existing = prj.javaProject.getRawClasspath
+ prj.javaProject.setRawClasspath(existing ++ entries, null)
+ }
+
+ private def createProjects(names: String*): Seq[ScalaProject] =
+ names map (n => simulator.createProjectInWorkspace(n, true))
+
+ private def deleteProjects(projects: ScalaProject*) {
+ workspaceRunnableIn(ScalaPlugin.plugin.workspaceRoot.getWorkspace) { _ =>
+ projects foreach (_.underlying.delete(true, null))
+ }
+ }
+}
View
24 org.scala-ide.sdt.core.tests/src/scala/tools/eclipse/sbtbuilder/SbtBuilderTest.scala
@@ -36,6 +36,7 @@ class SbtBuilderTest {
@Test def testSimpleBuild() {
println("building " + project)
project.clean(new NullProgressMonitor())
+ depProject // initialize
project.underlying.build(IncrementalProjectBuilder.FULL_BUILD, new NullProgressMonitor)
val units = compilationUnits("test/ja/JClassA.java", "test/sc/ClassA.scala")
@@ -80,7 +81,7 @@ class SbtBuilderTest {
val fooCU = depProject.compilationUnit("subpack/Foo.scala")
println("IFile: " + fooCU.getResource().getAdapter(classOf[IFile]).asInstanceOf[IFile])
- SDTTestUtils.changeContentOfFile(depProject.project.underlying, fooCU.getResource().getAdapter(classOf[IFile]).asInstanceOf[IFile], changedFooScala)
+ SDTTestUtils.changeContentOfFile(fooCU.getResource().getAdapter(classOf[IFile]).asInstanceOf[IFile], changedFooScala)
val fooClientCU = scalaCompilationUnit("test/dependency/FooClient.scala")
@@ -106,29 +107,20 @@ class SbtBuilderTest {
Assert.assertEquals("Presentation compiler errors.", 2, pcProblems.size)
}
+ /** Where we look for errors. */
+ val unitsToWatch = compilationUnits("test/ja/JClassA.java", "test/sc/ClassA.scala", "test/dependency/FooClient.scala").toList
+
private def getProblemMarkers(): List[IMarker] = {
- val units = compilationUnits("test/ja/JClassA.java", "test/sc/ClassA.scala", "test/dependency/FooClient.scala").toList
- units.flatMap(SDTTestUtils.findProblemMarkers)
- }
-
- private def buildWith(resource: IResource, contents: String): List[String] = {
- SDTTestUtils.changeContentOfFile(depProject.project.underlying, resource.asInstanceOf[IFile], contents)
-
- println("=== Rebuilding workspace === ")
- SDTTestUtils.workspace.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, null)
-
- val problems = getProblemMarkers()
-
- for (p <- problems) yield p.getAttribute(IMarker.MESSAGE).toString
+ unitsToWatch.flatMap(SDTTestUtils.findProblemMarkers)
}
@Test def dependentProject_should_restart_PC_after_build() {
val fooCU = depProject.compilationUnit("subpack/Foo.scala")
- val changedErrors = buildWith(fooCU.getResource, changedFooScala)
+ val changedErrors = SDTTestUtils.buildWith(fooCU.getResource, changedFooScala, unitsToWatch)
Assert.assertEquals("Build problems " + changedErrors, 2, changedErrors.size)
- val errorMessages = buildWith(fooCU.getResource, originalFooScala)
+ val errorMessages = SDTTestUtils.buildWith(fooCU.getResource, originalFooScala, unitsToWatch)
Assert.assertEquals("No build problems: " + errorMessages, 0, errorMessages.size)
val fooClientCU = scalaCompilationUnit("test/dependency/FooClient.scala")
View
10 org.scala-ide.sdt.core.tests/src/scala/tools/eclipse/sbtbuilder/ScalaJavaDepTest.scala
@@ -39,12 +39,12 @@ class ScalaJavaDepTest {
assertTrue("Build errors found: " + userFriendlyMarkers(problems0), problems0.isEmpty)
val originalJJava = SDTTestUtils.slurpAndClose(project.underlying.getFile("src/test/J.java").getContents)
- SDTTestUtils.changeContentOfFile(project.underlying, JJavaCU.getResource().getAdapter(classOf[IFile]).asInstanceOf[IFile], changedJJava)
+ SDTTestUtils.changeContentOfFile(JJavaCU.getResource().getAdapter(classOf[IFile]).asInstanceOf[IFile], changedJJava)
rebuild(project)
val problems1 = getProblemMarkers
assertTrue("One build error expected, got: " + userFriendlyMarkers(problems1), problems1.length == 1) // do more precise matching later
- SDTTestUtils.changeContentOfFile(project.underlying, JJavaCU.getResource().getAdapter(classOf[IFile]).asInstanceOf[IFile], originalJJava)
+ SDTTestUtils.changeContentOfFile(JJavaCU.getResource().getAdapter(classOf[IFile]).asInstanceOf[IFile], originalJJava)
rebuild(project)
val problems2 = getProblemMarkers
assertTrue("Build errors found: " + userFriendlyMarkers(problems2), problems2.isEmpty)
@@ -64,12 +64,12 @@ class ScalaJavaDepTest {
assertTrue("Build errors found: " + userFriendlyMarkers(problems0), problems0.isEmpty)
val originalSScala = SDTTestUtils.slurpAndClose(project.underlying.getFile("src/test/S.scala").getContents)
- SDTTestUtils.changeContentOfFile(project.underlying, SScalaCU.getResource().getAdapter(classOf[IFile]).asInstanceOf[IFile], changedSScala)
+ SDTTestUtils.changeContentOfFile(SScalaCU.getResource().getAdapter(classOf[IFile]).asInstanceOf[IFile], changedSScala)
rebuild(project)
val problems1 = getProblemMarkers
assertTrue("One build error expected: " + userFriendlyMarkers(problems1), problems1.length == 1) // do more precise matching later
- SDTTestUtils.changeContentOfFile(project.underlying, SScalaCU.getResource().getAdapter(classOf[IFile]).asInstanceOf[IFile], originalSScala)
+ SDTTestUtils.changeContentOfFile(SScalaCU.getResource().getAdapter(classOf[IFile]).asInstanceOf[IFile], originalSScala)
rebuild(project)
val problems2 = getProblemMarkers
assertTrue("Build errors found: " + userFriendlyMarkers(problems2), problems2.isEmpty)
@@ -97,7 +97,7 @@ class ScalaJavaDepTest {
assertEquals("One error expected: " + userFriendlyMarkers(problems), 1, problems.size)
// "fix" the scala code
- SDTTestUtils.changeContentOfFile(project.underlying, aClass.getResource().asInstanceOf[IFile], changed_ticket_1000607_A)
+ SDTTestUtils.changeContentOfFile(aClass.getResource().asInstanceOf[IFile], changed_ticket_1000607_A)
// trigger incremental compile
project.underlying.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, new NullProgressMonitor)
View
25 org.scala-ide.sdt.core.tests/src/scala/tools/eclipse/testsetup/SDTTestUtils.scala
@@ -15,6 +15,8 @@ import scala.util.matching.Regex
import org.eclipse.jdt.core.ICompilationUnit
import org.eclipse.jdt.core.IJavaModelMarker
import org.eclipse.core.resources.IResource
+import org.eclipse.core.resources.IncrementalProjectBuilder
+import org.eclipse.core.resources.IMarker
/** Utility functions for setting up test projects.
*
@@ -137,10 +139,29 @@ object SDTTestUtils {
file
}
- def changeContentOfFile(project : IProject, file : IFile, newContent : String) : IFile = {
- file.setContents(new ByteArrayInputStream(newContent.getBytes(project.getDefaultCharset())), 0, null)
+ def changeContentOfFile(file : IFile, newContent : String, encoding: String = workspace.getRoot.getDefaultCharset()) : IFile = {
+ file.setContents(new ByteArrayInputStream(newContent.getBytes(encoding)), 0, null)
file
}
+
+ def getProblemMarkers(units: ICompilationUnit*): List[IMarker] = {
+ units.flatMap(findProblemMarkers).toList
+ }
+
+ def getErrorMessages(units: ICompilationUnit*): List[String] =
+ for (p <- getProblemMarkers(units: _*)) yield p.getAttribute(IMarker.MESSAGE).toString
+
+ def buildWith(resource: IResource, contents: String, unitsToWatch: Seq[ICompilationUnit]): List[String] = {
+ SDTTestUtils.changeContentOfFile(resource.asInstanceOf[IFile], contents)
+
+ println("=== Rebuilding workspace === ")
+ SDTTestUtils.workspace.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, null)
+
+ val problems = getProblemMarkers(unitsToWatch:_*)
+
+ for (p <- problems) yield p.getAttribute(IMarker.MESSAGE).toString
+ }
+
def createProjectInLocalFileSystem(parentFile : File, projectName : String) : IProject = {
val project = ResourcesPlugin.getWorkspace.getRoot.getProject(projectName)
Please sign in to comment.
Something went wrong with that request. Please try again.