Skip to content

Commit 1b60f3a

Browse files
committed
Adding tests
1 parent d9f03c6 commit 1b60f3a

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/Log4j2PluginsCacheFileTransformerTest.groovy

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import java.util.zip.ZipFile
1111

1212
import static java.util.Collections.singletonList
1313
import static org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor.PLUGIN_CACHE_FILE
14+
import static org.junit.Assert.assertFalse
15+
import static org.junit.Assert.assertTrue
1416

1517
/**
1618
* @author Paul Nelson Baker
@@ -51,31 +53,34 @@ class Log4j2PluginsCacheFileTransformerTest {
5153
void testRelocate(String source, String pattern, String target) throws IOException {
5254
List<Relocator> relocators = singletonList((Relocator) new SimpleRelocator(source, pattern, null, null))
5355
transformer.transform(new TransformerContext(PLUGIN_CACHE_FILE, getResourceStream(PLUGIN_CACHE_FILE), relocators))
54-
assertTrue(transformer.hasTransformedResource(), "Transformer didn't transform resources")
56+
assertTrue("Transformer didn't transform resources", transformer.hasTransformedResource())
5557
// 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-
}
58+
def testableZipFile = File.createTempFile("testable-zip-file-", ".jar")
59+
def fileOutputStream = new FileOutputStream(testableZipFile)
60+
def bufferedOutputStream = new BufferedOutputStream(fileOutputStream)
61+
def zipOutputStream = new ZipOutputStream(bufferedOutputStream)
62+
transformer.modifyOutputStream(zipOutputStream)
63+
zipOutputStream.close()
64+
bufferedOutputStream.close()
65+
fileOutputStream.close()
6566
// Pull the data back out and make sure it was transformed
6667
ZipFile zipFile = new ZipFile(testableZipFile)
6768
ZipEntry zipFileEntry = zipFile.getEntry(PLUGIN_CACHE_FILE)
6869
InputStream inputStream = zipFile.getInputStream(zipFileEntry)
69-
new Scanner(inputStream).withReader { scanner ->
70+
new Scanner(inputStream).withCloseable { scanner ->
7071
boolean hasAtLeastOneTransform = false
7172
while (scanner.hasNextLine()) {
7273
String nextLine = scanner.nextLine()
7374
if (nextLine.contains(source)) {
7475
hasAtLeastOneTransform = true
75-
assertTrue(nextLine.contains(target), "Target wasn't included in transform")
76+
assertTrue("Target wasn't included in transform", nextLine.contains(target))
7677
}
7778
}
78-
assertTrue(hasAtLeastOneTransform, "There were no transformations inside the file")
79+
assertTrue("There were no transformations inside the file", hasAtLeastOneTransform)
7980
}
8081
}
82+
83+
InputStream getResourceStream(String resource) {
84+
return this.class.getClassLoader().getResourceAsStream(resource);
85+
}
8186
}

0 commit comments

Comments
 (0)