Skip to content

Commit

Permalink
Add test case for classpath resources
Browse files Browse the repository at this point in the history
  • Loading branch information
pwhittlesea committed May 12, 2024
1 parent 820e0b1 commit 53c60b0
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,28 @@ However, if you would like to get the benefit of the community maintained defini
</plugin>
```

In some cases your definition file might be shared across many projects.
As an alternative to copying and pasting this file into each build, you can package your definitions into a JAR and reference it like so:
```xml
<plugin>
<groupId>biz.lermitage.oga</groupId>
<artifactId>oga-maven-plugin</artifactId>
<configuration>
<additionalDefinitionFiles>
<additionalDefinitionFile>classpath-og-definitions.json</additionalDefinitionFile>
</additionalDefinitionFiles>
</configuration>
<dependencies>
<!-- Dependency contains /classpath-og-definitions.json -->
<dependency>
<groupId>com.example.oga</groupId>
<artifactId>build-config</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</plugin>
```

#### Ignoring definitions

You can also provide a JSON ignore-list in order to exclude some *groupIds* or *groupId + artifactIds*:
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/biz/lermitage/oga/CheckMojoITest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@ class CheckMojoITest {
@JvmField
var mockServerRule: MockServerRule = MockServerRule(this)

@Test
fun testProjectWithClasspathDefinitionFiles() {
// GIVEN a project that contains our definition file
val classpathResourcesDir = ResourceExtractor.simpleExtractResources(javaClass, "/biz/lermitage/oga/classpath_build_config")
val classpathVerifier = Verifier(classpathResourcesDir.absolutePath)
classpathVerifier.deleteArtifact("biz.lermitage.oga", "classpath-build-config", "1.0.0-SNAPSHOT", "jar")
classpathVerifier.executeGoal("install")

// AND a project which uses the definition file as a classpath resource
val testDir = ResourceExtractor.simpleExtractResources(javaClass, "/biz/lermitage/oga/ko_classpath_definitions")
val verifier = Verifier(testDir.absolutePath)

verifier.deleteArtifact("biz.lermitage.oga", "project-to-test", "1.0.0-SNAPSHOT", "pom")

// WHEN checking the projects dependencies
// THEN the build fails
assertThrows(VerificationException::class.java) { verifier.executeGoal("biz.lermitage.oga:oga-maven-plugin:check") }

// AND the build contains a failure message from the classpath definition file
verifier.verifyTextInLog("[ERROR] (dependency) 'org.mock-server' groupId should be replaced by 'com.example.classpath.dependency'")
}

@Throws(Exception::class)
@Test
fun testProjectWithAdditionalDefinitionFiles() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<!-- Intentionally does not have a parent to emulate a 3rd party -->

<groupId>biz.lermitage.oga</groupId>
<artifactId>classpath-build-config</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>Build Configuration</name>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"version": "2",
"date": "2021/02/03",
"migration": [
{
"old": "org.mock-server",
"new": "com.example.classpath.dependency"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>project-to-test</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Test CheckMojo</name>

<parent>
<groupId>biz.lermitage.oga</groupId>
<artifactId>parent-pom</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<dependencies>
<!-- From the classpath og-definitions file -->
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-junit-rule-no-dependencies</artifactId>
<version>5.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>biz.lermitage.oga</groupId>
<artifactId>oga-maven-plugin</artifactId>
<configuration>
<additionalDefinitionFiles>
<!-- A file in the plugin dependency -->
<additionalDefinitionFile>classpath-og-definitions.json</additionalDefinitionFile>
</additionalDefinitionFiles>
</configuration>
<dependencies>
<dependency>
<groupId>com.example.oga</groupId>
<artifactId>build-config</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

0 comments on commit 53c60b0

Please sign in to comment.