Skip to content

Commit

Permalink
add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
RoRoche committed May 4, 2020
1 parent b105752 commit a362540
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,43 @@
package com.github.roroche.plantuml.classes

import com.github.roroche.plantuml.classes.exceptions.InvalidPackageException
import org.reflections.Reflections
import org.reflections.scanners.SubTypesScanner
import org.reflections.scanners.TypeAnnotationsScanner
import org.reflections.util.ClasspathHelper
import org.reflections.util.ConfigurationBuilder
import java.net.URL
import java.util.concurrent.Executors

/**
* Utility class to find [Classes] in a given package.
*
* @property packageName The name of the package to scan.
* @property packageUrls URL of the package (passed as [Collection]).
* @property reflections Utility to find classes in pckage.
*/
class ClsInPackage(
private val packageName: String,
private val packageUrls: Collection<URL>,
private val reflections: Reflections
) : Classes {

/**
* Secondary constructor.
* Secondary constructor using URLs.
*
* @param packageName The name of the package to scan.
* @param packageUrls URL of the package (passed as [Collection]).
*/
constructor(packageName: String) : this(
constructor(
packageName: String,
packageUrls: Collection<URL>
) : this(
packageName,
packageUrls,
Reflections(
ConfigurationBuilder()
.setUrls(
ClasspathHelper.forPackage(packageName)
packageUrls
).setScanners(
SubTypesScanner(false),
TypeAnnotationsScanner()
Expand All @@ -35,10 +47,23 @@ class ClsInPackage(
)
)

/**
* Secondary constructor.
*
* @param packageName The name of the package to scan.
*/
constructor(packageName: String) : this(
packageName,
ClasspathHelper.forPackage(packageName)
)

/**
* @return Classes to be used for diagram generation.
*/
override fun list(): List<Class<out Any>> {
if (packageUrls.isNullOrEmpty()) {
throw InvalidPackageException(packageName)
}
return reflections.getSubTypesOf(
Any::class.java
).asIterable().toList()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.roroche.plantuml.classes.exceptions

class InvalidPackageException(
packageName: String
) : RuntimeException("Invalid package '$packageName', maybe missing or empty")
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.roroche.plantuml.assertions

import com.github.roroche.plantuml.classes.Classes
import com.pragmaticobjects.oo.tests.Assertion
import org.assertj.core.api.Assertions.assertThatThrownBy

/**
* Assertion that checks that [Classes] throws exception.
*
* @property classes The [Classes] to check.
* @property expectedClass The [Exception] [Class] expected to be thrown.
* @property expectedMessage The expected [Exception] message.
*/
class ClsThrowsAssertion(
private val classes: Classes,
private val expectedClass: Class<out Exception>,
private val expectedMessage: String
) : Assertion {
/**
* Check the assertion.
*/
override fun check() {
assertThatThrownBy {
classes.list()
}.isInstanceOf(
expectedClass
).hasMessageContaining(
expectedMessage
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,18 @@ class ClsFilteredTest : TestsSuite(
)
),
TestCase(
"origin and empty filtered classes returns empty list",
ClsIsEmptyAssertion(
"origin and empty filtered classes returns concrete classes",
ClsContainsExactlyAssertion(
classes = ClsFiltered(
origin = Classes.Simple(
listOf(ClsFilteredTest::class.java)
),
ignored = Classes.Simple(
emptyList()
)
),
expectedClasses = listOf(
ClsFilteredTest::class.java
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.github.roroche.examples.Car
import com.github.roroche.examples.Driver
import com.github.roroche.examples.Vehicle
import com.github.roroche.plantuml.assertions.ClsContainsExactlyAssertion
import com.github.roroche.plantuml.assertions.ClsThrowsAssertion
import com.github.roroche.plantuml.classes.exceptions.InvalidPackageException
import com.pragmaticobjects.oo.tests.TestCase
import com.pragmaticobjects.oo.tests.junit5.TestsSuite

Expand All @@ -25,21 +27,23 @@ class ClsInPackageTest : TestsSuite(
)
),
TestCase(
"empty package returns empty list",
ClsContainsExactlyAssertion(
"empty package returns throws",
ClsThrowsAssertion(
classes = ClsInPackage(
packageName = "com.github.roroche.examples.empty"
),
expectedClasses = emptyList()
expectedClass = InvalidPackageException::class.java,
expectedMessage = "Invalid package 'com.github.roroche.examples.empty', maybe missing or empty"
)
),
TestCase(
"not existing package returns empty list",
ClsContainsExactlyAssertion(
"not existing package throws",
ClsThrowsAssertion(
classes = ClsInPackage(
packageName = "com.github.roroche.examples.missing"
),
expectedClasses = emptyList()
expectedClass = InvalidPackageException::class.java,
expectedMessage = "Invalid package 'com.github.roroche.examples.missing', maybe missing or empty"
)
)
)

0 comments on commit a362540

Please sign in to comment.