From 24b7d0dbe1a61bb93cdc25468bf870e6ed6da3d4 Mon Sep 17 00:00:00 2001 From: Romain Rochegude Date: Wed, 6 May 2020 21:50:52 +0200 Subject: [PATCH] fix Task unit tests --- .../tasks/BuildClassDiagramTask.groovy | 26 +++++++++++-------- .../roroche/plantuml/classes/ClsWithNames.kt | 14 ++++++++++ .../plantuml/BuildClassDiagramTaskTest.groovy | 12 ++++----- .../roroche/plantuml/BuiltProject.groovy | 1 - .../plantuml/MockBuildClassDiagramTask.groovy | 18 +++++++++++++ .../plantuml/classes/ClsWithNamesTest.kt | 9 +++---- 6 files changed, 56 insertions(+), 24 deletions(-) create mode 100644 src/test/groovy/com/github/roroche/plantuml/MockBuildClassDiagramTask.groovy diff --git a/src/main/groovy/com/github/roroche/plantuml/tasks/BuildClassDiagramTask.groovy b/src/main/groovy/com/github/roroche/plantuml/tasks/BuildClassDiagramTask.groovy index 0cd14c0..7d04398 100644 --- a/src/main/groovy/com/github/roroche/plantuml/tasks/BuildClassDiagramTask.groovy +++ b/src/main/groovy/com/github/roroche/plantuml/tasks/BuildClassDiagramTask.groovy @@ -41,17 +41,7 @@ class BuildClassDiagramTask extends DefaultTask implements CustomTask { "Classes to ignore: {}", extension.ignoredClasses ) - final URL[] urls = project.sourceSets.main.output.classesDirs.files.collect { - if (it != null) { - it.toURI().toURL() - } - }.findAll { - it != null - } as URL[] - getLogger().lifecycle( - "URLs to scan: " + urls - ) - final ClassLoader classLoader = new URLClassLoader(urls) + final ClassLoader classLoader = getClassLoader() final Classes classes = new ClsWithLog( new ClsFiltered( new ClsInPackage( @@ -71,4 +61,18 @@ class BuildClassDiagramTask extends DefaultTask implements CustomTask { ) diagram.print(extension.outputFile) } + + protected ClassLoader getClassLoader() { + final URL[] urls = project.sourceSets.main.output.classesDirs.files.collect { + if (it != null) { + it.toURI().toURL() + } + }.findAll { + it != null + } as URL[] + getLogger().debug( + "URLs to scan: " + urls + ) + return new URLClassLoader(urls) + } } diff --git a/src/main/kotlin/com/github/roroche/plantuml/classes/ClsWithNames.kt b/src/main/kotlin/com/github/roroche/plantuml/classes/ClsWithNames.kt index 2b69364..e0b7bac 100644 --- a/src/main/kotlin/com/github/roroche/plantuml/classes/ClsWithNames.kt +++ b/src/main/kotlin/com/github/roroche/plantuml/classes/ClsWithNames.kt @@ -4,11 +4,25 @@ package com.github.roroche.plantuml.classes * Utility class to find [Classes] for names. * * @property names The names of the classes to find. + * @property classLoader The [ClassLoader] to be used. */ class ClsWithNames( private val names: List?, private val classLoader: ClassLoader ) : Classes { + + /** + * Secondary constructor that builds default [ClassLoader]. + * + * @param names The names of the classes to find. + */ + constructor( + names: List? + ) : this( + names = names, + classLoader = Thread.currentThread().contextClassLoader + ) + /** * @return Classes to be used for diagram generation. */ diff --git a/src/test/groovy/com/github/roroche/plantuml/BuildClassDiagramTaskTest.groovy b/src/test/groovy/com/github/roroche/plantuml/BuildClassDiagramTaskTest.groovy index fe78b16..e530a6a 100644 --- a/src/test/groovy/com/github/roroche/plantuml/BuildClassDiagramTaskTest.groovy +++ b/src/test/groovy/com/github/roroche/plantuml/BuildClassDiagramTaskTest.groovy @@ -4,10 +4,10 @@ import com.github.roroche.plantuml.assertions.CreateTaskAssertion import com.github.roroche.plantuml.assertions.CreatedTask import com.github.roroche.plantuml.assertions.ExecuteTaskAssertion import com.github.roroche.plantuml.assertions.GrvFileHasContentAssertion -import com.github.roroche.plantuml.tasks.BuildClassDiagramTask import com.github.roroche.plantuml.tasks.ClassDiagramExtension import com.pragmaticobjects.oo.tests.TestCase import com.pragmaticobjects.oo.tests.junit5.TestsSuite +import org.gradle.testfixtures.ProjectBuilder import org.junit.jupiter.api.io.TempDir import java.nio.file.Path @@ -22,25 +22,25 @@ class BuildClassDiagramTaskTest extends TestsSuite { "test buildClassDiagramTask", new CreateTaskAssertion( new CreatedTask( - BuiltProject.instance.toProject(), + ProjectBuilder.builder().build(), "buildClassDiagramTask", - BuildClassDiagramTask.class, + MockBuildClassDiagramTask.class, new ClassDiagramExtension( "com.github.roroche.examples", tmpDirPath.resolve("output.txt").toFile(), List.of() ) ), - BuildClassDiagramTask.class + MockBuildClassDiagramTask.class ) ), new TestCase( "test buildClassDiagramTask print diagram to file", new ExecuteTaskAssertion( new CreatedTask( - BuiltProject.instance.toProject(), + ProjectBuilder.builder().build(), "buildClassDiagramTask", - BuildClassDiagramTask.class, + MockBuildClassDiagramTask.class, new ClassDiagramExtension( "com.github.roroche.examples", tmpDirPath.resolve("output.txt").toFile(), diff --git a/src/test/groovy/com/github/roroche/plantuml/BuiltProject.groovy b/src/test/groovy/com/github/roroche/plantuml/BuiltProject.groovy index 2d4c34f..b7eed4e 100644 --- a/src/test/groovy/com/github/roroche/plantuml/BuiltProject.groovy +++ b/src/test/groovy/com/github/roroche/plantuml/BuiltProject.groovy @@ -14,7 +14,6 @@ class BuiltProject { * @return The Project instance. */ Project toProject() { - project.sourceSets.main.output.classesDirs = project.buildDir.toPath().resolve("classes").resolve("java") return project } } diff --git a/src/test/groovy/com/github/roroche/plantuml/MockBuildClassDiagramTask.groovy b/src/test/groovy/com/github/roroche/plantuml/MockBuildClassDiagramTask.groovy new file mode 100644 index 0000000..2bcb54b --- /dev/null +++ b/src/test/groovy/com/github/roroche/plantuml/MockBuildClassDiagramTask.groovy @@ -0,0 +1,18 @@ +package com.github.roroche.plantuml + +import com.github.roroche.plantuml.tasks.BuildClassDiagramTask +import com.github.roroche.plantuml.tasks.ClassDiagramExtension + +import javax.inject.Inject + +class MockBuildClassDiagramTask extends BuildClassDiagramTask { + @Inject + MockBuildClassDiagramTask(final ClassDiagramExtension extension) { + super(extension) + } + + @Override + protected ClassLoader getClassLoader() { + return Thread.currentThread().getContextClassLoader() + } +} diff --git a/src/test/kotlin/com/github/roroche/plantuml/classes/ClsWithNamesTest.kt b/src/test/kotlin/com/github/roroche/plantuml/classes/ClsWithNamesTest.kt index 07223a5..ceba891 100644 --- a/src/test/kotlin/com/github/roroche/plantuml/classes/ClsWithNamesTest.kt +++ b/src/test/kotlin/com/github/roroche/plantuml/classes/ClsWithNamesTest.kt @@ -20,8 +20,7 @@ class ClsWithNamesTest : TestsSuite( "com.github.roroche.examples.Car", "com.github.roroche.examples.Driver", "com.github.roroche.examples.Vehicle" - ), - classLoader = Thread.currentThread().contextClassLoader + ) ), expectedClasses = listOf( Car::class.java, @@ -34,8 +33,7 @@ class ClsWithNamesTest : TestsSuite( "classes with empty names returns empty list", ClsIsEmptyAssertion( classes = ClsWithNames( - names = emptyList(), - classLoader = Thread.currentThread().contextClassLoader + names = emptyList() ) ) ), @@ -43,8 +41,7 @@ class ClsWithNamesTest : TestsSuite( "classes with null names returns empty list", ClsIsEmptyAssertion( classes = ClsWithNames( - names = null, - classLoader = Thread.currentThread().contextClassLoader + names = null ) ) )