|
| 1 | +package com.github.jengelman.gradle.plugins.shadow.transformers |
| 2 | + |
| 3 | +import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator |
| 4 | +import com.github.jengelman.gradle.plugins.shadow.relocation.SimpleRelocator |
| 5 | +import org.apache.tools.zip.ZipOutputStream |
| 6 | +import org.junit.Before |
| 7 | +import org.junit.Test |
| 8 | + |
| 9 | +import java.util.zip.ZipEntry |
| 10 | +import java.util.zip.ZipFile |
| 11 | + |
| 12 | +import static java.util.Collections.singletonList |
| 13 | +import static org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor.PLUGIN_CACHE_FILE |
| 14 | + |
| 15 | +/** |
| 16 | + * @author Paul Nelson Baker |
| 17 | + * @since 2018-08 |
| 18 | + * @see <a href="https://github.com/paul-nelson-baker/">GitHub</a> |
| 19 | + * @see <a href="https://www.linkedin.com/in/paul-n-baker/">LinkedIn</a> |
| 20 | + */ |
| 21 | +//@RunWith(Parameterized.class) |
| 22 | +class Log4j2PluginsCacheFileTransformerTest { |
| 23 | + |
| 24 | + Log4j2PluginsCacheFileTransformer transformer |
| 25 | + |
| 26 | + @Before |
| 27 | + void setUp() { |
| 28 | + transformer = new Log4j2PluginsCacheFileTransformer() |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + void testShouldNotTransform() { |
| 33 | + transformer.transform(new TransformerContext(PLUGIN_CACHE_FILE, getResourceStream(PLUGIN_CACHE_FILE), null)) |
| 34 | + assertFalse(transformer.hasTransformedResource()) |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void testShouldTransform() { |
| 39 | + List<Relocator> relocators = new ArrayList<>() |
| 40 | + relocators.add(new SimpleRelocator(null, null, null, null)) |
| 41 | + transformer.transform(new TransformerContext(PLUGIN_CACHE_FILE, getResourceStream(PLUGIN_CACHE_FILE), relocators)) |
| 42 | + assertTrue(transformer.hasTransformedResource()) |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void testRelocators() { |
| 47 | + testRelocate("org.apache.logging", "new.location.org.apache.logging", "new.location.org.apache.logging") |
| 48 | + testRelocate("org.apache.logging", "new.location.org.apache.logging", "org.apache.logging") |
| 49 | + } |
| 50 | + |
| 51 | + void testRelocate(String source, String pattern, String target) throws IOException { |
| 52 | + List<Relocator> relocators = singletonList((Relocator) new SimpleRelocator(source, pattern, null, null)) |
| 53 | + transformer.transform(new TransformerContext(PLUGIN_CACHE_FILE, getResourceStream(PLUGIN_CACHE_FILE), relocators)) |
| 54 | + assertTrue(transformer.hasTransformedResource(), "Transformer didn't transform resources") |
| 55 | + // Write out to a fake jar file |
| 56 | + File.createTempFile("testable-zip-file-", ".jar").withCloseable { testableZipFile -> |
| 57 | + new FileOutputStream(testableZipFile).withCloseable { fileOutputStream -> |
| 58 | + new BufferedOutputStream(fileOutputStream).withCloseable { bufferedOutputStream -> |
| 59 | + new ZipOutputStream(bufferedOutputStream).withCloseable { zipOutputStream -> |
| 60 | + transformer.modifyOutputStream(zipOutputStream) |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + // Pull the data back out and make sure it was transformed |
| 66 | + ZipFile zipFile = new ZipFile(testableZipFile) |
| 67 | + ZipEntry zipFileEntry = zipFile.getEntry(PLUGIN_CACHE_FILE) |
| 68 | + InputStream inputStream = zipFile.getInputStream(zipFileEntry) |
| 69 | + new Scanner(inputStream).withReader { scanner -> |
| 70 | + boolean hasAtLeastOneTransform = false |
| 71 | + while (scanner.hasNextLine()) { |
| 72 | + String nextLine = scanner.nextLine() |
| 73 | + if (nextLine.contains(source)) { |
| 74 | + hasAtLeastOneTransform = true |
| 75 | + assertTrue(nextLine.contains(target), "Target wasn't included in transform") |
| 76 | + } |
| 77 | + } |
| 78 | + assertTrue(hasAtLeastOneTransform, "There were no transformations inside the file") |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments