Skip to content

Commit

Permalink
Split ServiceFileTransformerTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed Jan 4, 2025
1 parent c139a52 commit 4cf754d
Show file tree
Hide file tree
Showing 2 changed files with 215 additions and 204 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
package com.github.jengelman.gradle.plugins.shadow.transformers

import assertk.assertThat
import assertk.assertions.exists
import assertk.assertions.isEqualTo
import com.github.jengelman.gradle.plugins.shadow.util.Issue
import kotlin.io.path.appendText
import kotlin.io.path.writeText
import org.junit.jupiter.api.Test

class ServiceFileTransformerTest : BaseTransformerTest() {

@Test
fun serviceResourceTransformer() {
projectScriptPath.appendText(
transform<ServiceFileTransformer>(
shadowBlock = fromJar(buildJarOne(), buildJarTwo()),
transformerBlock = """
exclude 'META-INF/services/com.acme.*'
""".trimIndent(),
),
)

run(shadowJarTask)

assertThat(outputShadowJar).exists()

val text1 = getJarFileContents(outputShadowJar, ENTRY_SERVICES_SHADE)
assertThat(text1).isEqualTo(CONTENT_ONE_TWO)

val text2 = getJarFileContents(outputShadowJar, ENTRY_SERVICES_FOO)
assertThat(text2).isEqualTo("one")
}

@Test
fun serviceResourceTransformerAlternatePath() {
val one = buildJarOne {
insert(ENTRY_FOO_SHADE, CONTENT_ONE)
}
val two = buildJarTwo {
insert(ENTRY_FOO_SHADE, CONTENT_TWO)
}
projectScriptPath.appendText(
transform<ServiceFileTransformer>(
shadowBlock = fromJar(one, two),
transformerBlock = """
path = "META-INF/foo"
""".trimIndent(),
),
)

run(shadowJarTask)

assertThat(outputShadowJar).exists()

val text = getJarFileContents(outputShadowJar, ENTRY_FOO_SHADE)
assertThat(text).isEqualTo(CONTENT_ONE_TWO)
}

@Test
fun serviceResourceTransformerShortSyntax() {
projectScriptPath.appendText(
"""
$shadowJar {
${fromJar(buildJarOne(), buildJarTwo())}
mergeServiceFiles {
exclude("META-INF/services/com.acme.*")
}
}
""".trimIndent(),
)

run(shadowJarTask)

assertThat(outputShadowJar).exists()

val text1 = getJarFileContents(outputShadowJar, ENTRY_SERVICES_SHADE)
assertThat(text1).isEqualTo(CONTENT_ONE_TWO)

val text2 = getJarFileContents(outputShadowJar, ENTRY_SERVICES_FOO)
assertThat(text2).isEqualTo("one")
}

@Test
fun serviceResourceTransformerShortSyntaxRelocation() {
val one = buildJarOne {
insert(
"META-INF/services/java.sql.Driver",
"oracle.jdbc.OracleDriver\norg.apache.hive.jdbc.HiveDriver",
)
insert(
"META-INF/services/org.apache.axis.components.compiler.Compiler",
"org.apache.axis.components.compiler.Javac",
)
insert(
"META-INF/services/org.apache.commons.logging.LogFactory",
"org.apache.commons.logging.impl.LogFactoryImpl",
)
}
val two = buildJarTwo {
insert(
"META-INF/services/java.sql.Driver",
"org.apache.derby.jdbc.AutoloadedDriver\ncom.mysql.jdbc.Driver",
)
insert(
"META-INF/services/org.apache.axis.components.compiler.Compiler",
"org.apache.axis.components.compiler.Jikes",
)
insert("META-INF/services/org.apache.commons.logging.LogFactory", "org.mortbay.log.Factory")
}

projectScriptPath.appendText(
"""
$shadowJar {
${fromJar(one, two)}
mergeServiceFiles()
relocate("org.apache", "myapache") {
exclude("org.apache.axis.components.compiler.Jikes")
exclude("org.apache.commons.logging.LogFactory")
}
}
""".trimIndent(),
)

run(shadowJarTask)

assertThat(outputShadowJar).exists()

val text1 = getJarFileContents(outputShadowJar, "META-INF/services/java.sql.Driver")
assertThat(text1).isEqualTo(
"""
oracle.jdbc.OracleDriver
myapache.hive.jdbc.HiveDriver
myapache.derby.jdbc.AutoloadedDriver
com.mysql.jdbc.Driver
""".trimIndent(),
)

val text2 = getJarFileContents(outputShadowJar, "META-INF/services/myapache.axis.components.compiler.Compiler")
assertThat(text2).isEqualTo(
"""
myapache.axis.components.compiler.Javac
org.apache.axis.components.compiler.Jikes
""".trimIndent(),
)

val text3 = getJarFileContents(outputShadowJar, "META-INF/services/org.apache.commons.logging.LogFactory")
assertThat(text3).isEqualTo(
"""
myapache.commons.logging.impl.LogFactoryImpl
org.mortbay.log.Factory
""".trimIndent(),
)
}

@Test
fun serviceResourceTransformerShortSyntaxAlternatePath() {
val one = buildJarOne {
insert(ENTRY_FOO_SHADE, CONTENT_ONE)
}
val two = buildJarTwo {
insert(ENTRY_FOO_SHADE, CONTENT_TWO)
}
projectScriptPath.appendText(
"""
$shadowJar {
${fromJar(one, two)}
mergeServiceFiles("META-INF/foo")
}
""".trimIndent(),
)

run(shadowJarTask)

assertThat(outputShadowJar).exists()

val text = getJarFileContents(outputShadowJar, ENTRY_FOO_SHADE)
assertThat(text).isEqualTo(CONTENT_ONE_TWO)
}

@Issue(
"https://github.com/GradleUp/shadow/issues/70",
"https://github.com/GradleUp/shadow/issues/71",
)
@Test
fun applyTransformersToProjectResources() {
val servicesShadowEntry = "META-INF/services/shadow.Shadow"
val one = buildJarOne {
insert(servicesShadowEntry, CONTENT_ONE)
}.toUri().toURL().path
repo.module("shadow", "two", "1.0")
.insertFile(servicesShadowEntry, CONTENT_TWO)
.publish()

projectScriptPath.appendText(
"""
dependencies {
implementation("shadow:two:1.0")
implementation(files('$one'))
}
$shadowJar {
mergeServiceFiles()
}
""".trimIndent(),
)
path("src/main/resources/$servicesShadowEntry").writeText(CONTENT_THREE)

run(shadowJarTask)

assertThat(outputShadowJar).exists()

val text = getJarFileContents(outputShadowJar, servicesShadowEntry)
assertThat(text).isEqualTo(CONTENT_THREE + "\n" + CONTENT_ONE_TWO)
}
}
Loading

0 comments on commit 4cf754d

Please sign in to comment.