Skip to content

Commit

Permalink
fix Task unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RoRoche committed May 6, 2020
1 parent 7f4f50f commit 24b7d0d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>?,
private val classLoader: ClassLoader
) : Classes {

/**
* Secondary constructor that builds default [ClassLoader].
*
* @param names The names of the classes to find.
*/
constructor(
names: List<String>?
) : this(
names = names,
classLoader = Thread.currentThread().contextClassLoader
)

/**
* @return Classes to be used for diagram generation.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -34,17 +33,15 @@ class ClsWithNamesTest : TestsSuite(
"classes with empty names returns empty list",
ClsIsEmptyAssertion(
classes = ClsWithNames(
names = emptyList(),
classLoader = Thread.currentThread().contextClassLoader
names = emptyList()
)
)
),
TestCase(
"classes with null names returns empty list",
ClsIsEmptyAssertion(
classes = ClsWithNames(
names = null,
classLoader = Thread.currentThread().contextClassLoader
names = null
)
)
)
Expand Down

0 comments on commit 24b7d0d

Please sign in to comment.