|
| 1 | +package com.github.jengelman.gradle.plugins.shadow.transformers |
| 2 | + |
| 3 | +import assertk.assertThat |
| 4 | +import assertk.assertions.contains |
| 5 | +import assertk.assertions.isEqualTo |
| 6 | +import com.github.jengelman.gradle.plugins.shadow.util.getContent |
| 7 | +import kotlin.io.path.appendText |
| 8 | +import org.junit.jupiter.api.Test |
| 9 | +import org.junit.jupiter.params.ParameterizedTest |
| 10 | +import org.junit.jupiter.params.provider.ValueSource |
| 11 | + |
| 12 | +class PropertiesFileTransformerFunctionalTest : BaseTransformerTest() { |
| 13 | + |
| 14 | + @ParameterizedTest |
| 15 | + @ValueSource(strings = ["First", "Latest", "Append"]) |
| 16 | + fun `should merge properties files with different strategies`(strategy: String) { |
| 17 | + val one = buildJarOne { |
| 18 | + insert("META-INF/test.properties", "key1=one\nkey2=one") |
| 19 | + } |
| 20 | + val two = buildJarTwo { |
| 21 | + insert("META-INF/test.properties", "key2=two\nkey3=two") |
| 22 | + } |
| 23 | + projectScriptPath.appendText( |
| 24 | + """ |
| 25 | + dependencies { |
| 26 | + ${implementationFiles(one, two)} |
| 27 | + } |
| 28 | + $shadowJar { |
| 29 | + transform(com.github.jengelman.gradle.plugins.shadow.transformers.PropertiesFileTransformer) { |
| 30 | + mergeStrategy = com.github.jengelman.gradle.plugins.shadow.transformers.PropertiesFileTransformer.MergeStrategy.$strategy |
| 31 | + mergeSeparator = ";" |
| 32 | + paths = ["META-INF/test.properties"] |
| 33 | + } |
| 34 | + } |
| 35 | + """.trimIndent(), |
| 36 | + ) |
| 37 | + |
| 38 | + run(shadowJarTask) |
| 39 | + |
| 40 | + val content = outputShadowJar.use { it.getContent("META-INF/test.properties") } |
| 41 | + when (strategy) { |
| 42 | + "First" -> assertThat(content).contains("key1=one", "key2=one", "key3=two") |
| 43 | + "Latest" -> assertThat(content).contains("key1=one", "key2=two", "key3=two") |
| 44 | + "Append" -> assertThat(content).contains("key1=one", "key2=one;two", "key3=two") |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + fun `should merge properties with key transformer`() { |
| 50 | + val one = buildJarOne { |
| 51 | + insert("META-INF/test.properties", "foo=bar") |
| 52 | + } |
| 53 | + val two = buildJarTwo { |
| 54 | + insert("META-INF/test.properties", "FOO=baz") |
| 55 | + } |
| 56 | + projectScriptPath.appendText( |
| 57 | + """ |
| 58 | + dependencies { |
| 59 | + ${implementationFiles(one, two)} |
| 60 | + } |
| 61 | + $shadowJar { |
| 62 | + transform(com.github.jengelman.gradle.plugins.shadow.transformers.PropertiesFileTransformer) { |
| 63 | + mergeStrategy = com.github.jengelman.gradle.plugins.shadow.transformers.PropertiesFileTransformer.MergeStrategy.Append |
| 64 | + keyTransformer = { key -> key.toUpperCase() } |
| 65 | + paths = ["META-INF/test.properties"] |
| 66 | + } |
| 67 | + } |
| 68 | + """.trimIndent(), |
| 69 | + ) |
| 70 | + |
| 71 | + run(shadowJarTask) |
| 72 | + |
| 73 | + val content = outputShadowJar.use { it.getContent("META-INF/test.properties") } |
| 74 | + assertThat(content).contains("FOO=bar,baz") |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + fun `should merge properties with mappings`() { |
| 79 | + val one = buildJarOne { |
| 80 | + insert("META-INF/a.properties", "k=1") |
| 81 | + insert("META-INF/b.properties", "k=2") |
| 82 | + } |
| 83 | + val two = buildJarTwo { |
| 84 | + insert("META-INF/a.properties", "k=3") |
| 85 | + insert("META-INF/b.properties", "k=4") |
| 86 | + } |
| 87 | + projectScriptPath.appendText( |
| 88 | + """ |
| 89 | + dependencies { |
| 90 | + ${implementationFiles(one, two)} |
| 91 | + } |
| 92 | + $shadowJar { |
| 93 | + transform(com.github.jengelman.gradle.plugins.shadow.transformers.PropertiesFileTransformer) { |
| 94 | + mappings = [ |
| 95 | + "META-INF/a.properties": ["mergeStrategy": "append", "mergeSeparator": ":"], |
| 96 | + "META-INF/b.properties": ["mergeStrategy": "latest"] |
| 97 | + ] |
| 98 | + } |
| 99 | + } |
| 100 | + """.trimIndent(), |
| 101 | + ) |
| 102 | + |
| 103 | + run(shadowJarTask) |
| 104 | + |
| 105 | + assertThat(outputShadowJar).useAll { |
| 106 | + getContent("META-INF/a.properties").isEqualTo("k=1:3") |
| 107 | + getContent("META-INF/b.properties").isEqualTo("k=4") |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + fun `should merge properties with specified charset`() { |
| 113 | + val one = buildJarOne { |
| 114 | + insert("META-INF/utf8.properties", "foo=传傳") |
| 115 | + } |
| 116 | + projectScriptPath.appendText( |
| 117 | + """ |
| 118 | + dependencies { |
| 119 | + ${implementationFiles(one)} |
| 120 | + } |
| 121 | + $shadowJar { |
| 122 | + transform(com.github.jengelman.gradle.plugins.shadow.transformers.PropertiesFileTransformer) { |
| 123 | + charsetName = "utf-8" |
| 124 | + paths = ["META-INF/utf8.properties"] |
| 125 | + } |
| 126 | + } |
| 127 | + """.trimIndent(), |
| 128 | + ) |
| 129 | + |
| 130 | + run(shadowJarTask) |
| 131 | + |
| 132 | + val content = outputShadowJar.use { it.getContent("META-INF/utf8.properties") } |
| 133 | + assertThat(content).contains("foo=传傳") |
| 134 | + } |
| 135 | +} |
0 commit comments