|
| 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.transformers.PropertiesFileTransformer.MergeStrategy |
| 7 | +import com.github.jengelman.gradle.plugins.shadow.util.Issue |
| 8 | +import com.github.jengelman.gradle.plugins.shadow.util.getContent |
| 9 | +import kotlin.io.path.appendText |
| 10 | +import org.junit.jupiter.api.Test |
| 11 | +import org.junit.jupiter.params.ParameterizedTest |
| 12 | +import org.junit.jupiter.params.provider.EnumSource |
| 13 | + |
| 14 | +class PropertiesFileTransformerTest : BaseTransformerTest() { |
| 15 | + |
| 16 | + @ParameterizedTest |
| 17 | + @EnumSource(MergeStrategy::class) |
| 18 | + fun mergePropertiesWithDifferentStrategies(strategy: MergeStrategy) { |
| 19 | + val one = buildJarOne { |
| 20 | + insert("META-INF/test.properties", "key1=one\nkey2=one") |
| 21 | + } |
| 22 | + val two = buildJarTwo { |
| 23 | + insert("META-INF/test.properties", "key2=two\nkey3=two") |
| 24 | + } |
| 25 | + projectScriptPath.appendText( |
| 26 | + transform<PropertiesFileTransformer>( |
| 27 | + dependenciesBlock = implementationFiles(one, two), |
| 28 | + transformerBlock = """ |
| 29 | + mergeStrategy = $mergeStrategyClassName.$strategy |
| 30 | + mergeSeparator = ";" |
| 31 | + paths = ["META-INF/test.properties"] |
| 32 | + """.trimIndent(), |
| 33 | + ), |
| 34 | + ) |
| 35 | + |
| 36 | + run(shadowJarTask) |
| 37 | + |
| 38 | + val expected = when (strategy) { |
| 39 | + MergeStrategy.First -> arrayOf("key1=one", "key2=one", "key3=two") |
| 40 | + MergeStrategy.Latest -> arrayOf("key1=one", "key2=two", "key3=two") |
| 41 | + MergeStrategy.Append -> arrayOf("key1=one", "key2=one;two", "key3=two") |
| 42 | + } |
| 43 | + val content = outputShadowJar.use { it.getContent("META-INF/test.properties") } |
| 44 | + assertThat(content).contains(*expected) |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + fun mergePropertiesWithKeyTransformer() { |
| 49 | + val one = buildJarOne { |
| 50 | + insert("META-INF/test.properties", "foo=bar") |
| 51 | + } |
| 52 | + val two = buildJarTwo { |
| 53 | + insert("META-INF/test.properties", "FOO=baz") |
| 54 | + } |
| 55 | + projectScriptPath.appendText( |
| 56 | + transform<PropertiesFileTransformer>( |
| 57 | + dependenciesBlock = implementationFiles(one, two), |
| 58 | + transformerBlock = """ |
| 59 | + mergeStrategy = $mergeStrategyClassName.Append |
| 60 | + keyTransformer = { key -> key.toUpperCase() } |
| 61 | + paths = ["META-INF/test.properties"] |
| 62 | + """.trimIndent(), |
| 63 | + ), |
| 64 | + ) |
| 65 | + |
| 66 | + run(shadowJarTask) |
| 67 | + |
| 68 | + val content = outputShadowJar.use { it.getContent("META-INF/test.properties") } |
| 69 | + assertThat(content).contains("FOO=bar,baz") |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + fun mergePropertiesWithSpecifiedCharset() { |
| 74 | + val one = buildJarOne { |
| 75 | + insert("META-INF/utf8.properties", "foo=第一") |
| 76 | + } |
| 77 | + val two = buildJarTwo { |
| 78 | + insert("META-INF/utf8.properties", "foo=第二") |
| 79 | + } |
| 80 | + projectScriptPath.appendText( |
| 81 | + transform<PropertiesFileTransformer>( |
| 82 | + dependenciesBlock = implementationFiles(one, two), |
| 83 | + transformerBlock = """ |
| 84 | + mergeStrategy = $mergeStrategyClassName.Append |
| 85 | + charsetName = "utf-8" |
| 86 | + paths = ["META-INF/utf8.properties"] |
| 87 | + """.trimIndent(), |
| 88 | + ), |
| 89 | + ) |
| 90 | + |
| 91 | + run(shadowJarTask) |
| 92 | + |
| 93 | + val content = outputShadowJar.use { it.getContent("META-INF/utf8.properties") } |
| 94 | + assertThat(content).contains("foo=第一,第二") |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + fun mergePropertiesWithMappings() { |
| 99 | + val one = buildJarOne { |
| 100 | + insert("META-INF/foo.properties", "foo=1") |
| 101 | + insert("META-INF/bar.properties", "bar=2") |
| 102 | + } |
| 103 | + val two = buildJarTwo { |
| 104 | + insert("META-INF/foo.properties", "foo=3") |
| 105 | + insert("META-INF/bar.properties", "bar=4") |
| 106 | + } |
| 107 | + projectScriptPath.appendText( |
| 108 | + transform<PropertiesFileTransformer>( |
| 109 | + dependenciesBlock = implementationFiles(one, two), |
| 110 | + transformerBlock = """ |
| 111 | + mappings = [ |
| 112 | + "META-INF/foo.properties": ["mergeStrategy": "append", "mergeSeparator": ";"], |
| 113 | + "META-INF/bar.properties": ["mergeStrategy": "latest"] |
| 114 | + ] |
| 115 | + """.trimIndent(), |
| 116 | + ), |
| 117 | + ) |
| 118 | + |
| 119 | + run(shadowJarTask) |
| 120 | + |
| 121 | + assertThat(outputShadowJar).useAll { |
| 122 | + getContent("META-INF/foo.properties").contains("foo=1;3") |
| 123 | + getContent("META-INF/bar.properties").contains("bar=4") |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @Issue( |
| 128 | + "https://github.com/GradleUp/shadow/issues/622", |
| 129 | + "https://github.com/GradleUp/shadow/issues/856", |
| 130 | + ) |
| 131 | + @Test |
| 132 | + fun mergedPropertiesDontContainDateComment() { |
| 133 | + val one = buildJarOne { |
| 134 | + insert("META-INF/test.properties", "foo=one") |
| 135 | + } |
| 136 | + val two = buildJarTwo { |
| 137 | + insert("META-INF/test.properties", "foo=two") |
| 138 | + } |
| 139 | + projectScriptPath.appendText( |
| 140 | + transform<PropertiesFileTransformer>( |
| 141 | + dependenciesBlock = implementationFiles(one, two), |
| 142 | + transformerBlock = """ |
| 143 | + mergeStrategy = $mergeStrategyClassName.Append |
| 144 | + paths = ["META-INF/test.properties"] |
| 145 | + """.trimIndent(), |
| 146 | + ), |
| 147 | + ) |
| 148 | + |
| 149 | + run(shadowJarTask) |
| 150 | + |
| 151 | + val content = outputShadowJar.use { it.getContent("META-INF/test.properties") } |
| 152 | + assertThat(content.trimIndent()).isEqualTo( |
| 153 | + """ |
| 154 | + # |
| 155 | +
|
| 156 | + foo=one,two |
| 157 | + """.trimIndent(), |
| 158 | + ) |
| 159 | + } |
| 160 | + |
| 161 | + private companion object { |
| 162 | + val mergeStrategyClassName = requireNotNull(MergeStrategy::class.java.canonicalName) |
| 163 | + } |
| 164 | +} |
0 commit comments