From 82ac7eef912a8e4d2c495e759b809b901eac096a Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Thu, 28 Jan 2021 14:24:18 -0800 Subject: [PATCH] Update tests following code changes See gh-22821 --- .../boot/jarmode/layertools/ContextTests.java | 12 ++++++++++-- .../jarmode/layertools/ExtractCommandTests.java | 16 +++++++++++++++- .../jarmode/layertools/HelpCommandTests.java | 16 ++++++++++++++-- .../jarmode/layertools/IndexedLayersTests.java | 8 ++++---- .../layertools/LayerToolsJarModeTests.java | 16 ++++++++++++++-- .../jarmode/layertools/ListCommandTests.java | 15 ++++++++++++++- .../boot/jarmode/layertools/test-manifest.MF | 10 +++++----- .../boot/jarmode/layertools/test-war-manifest.MF | 12 ++++++++++++ 8 files changed, 88 insertions(+), 17 deletions(-) create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/resources/org/springframework/boot/jarmode/layertools/test-war-manifest.MF diff --git a/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/ContextTests.java b/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/ContextTests.java index 9fccdfd392..cff04bd2e9 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/ContextTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/ContextTests.java @@ -39,7 +39,7 @@ class ContextTests { @Test void createWhenSourceIsNullThrowsException() { assertThatIllegalStateException().isThrownBy(() -> new Context(null, this.temp)) - .withMessage("Unable to find source JAR"); + .withMessage("Unable to find source archive"); } @Test @@ -47,7 +47,15 @@ class ContextTests { File directory = new File(this.temp, "test"); directory.mkdir(); assertThatIllegalStateException().isThrownBy(() -> new Context(directory, this.temp)) - .withMessage("Unable to find source JAR"); + .withMessage("Unable to find source archive"); + } + + @Test + void createWhenSourceIsNotJarOrWarThrowsException() throws Exception { + File zip = new File(this.temp, "test.zip"); + Files.createFile(zip.toPath()); + assertThatIllegalStateException().isThrownBy(() -> new Context(zip, this.temp)) + .withMessage("Unable to find source archive"); } @Test diff --git a/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/ExtractCommandTests.java b/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/ExtractCommandTests.java index 62369825b5..40058992f9 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/ExtractCommandTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/ExtractCommandTests.java @@ -20,9 +20,11 @@ import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; +import java.io.InputStreamReader; import java.util.Arrays; import java.util.Collections; import java.util.Iterator; +import java.util.jar.JarEntry; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -33,6 +35,9 @@ import org.junit.jupiter.api.io.TempDir; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.core.io.ClassPathResource; +import org.springframework.util.FileCopyUtils; + import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.mockito.BDDMockito.given; @@ -114,7 +119,7 @@ class ExtractCommandTests { .withMessageContaining("not compatible with layertools"); } - private File createJarFile(String name) throws IOException { + private File createJarFile(String name) throws Exception { File file = new File(this.temp, name); try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file))) { out.putNextEntry(new ZipEntry("a/")); @@ -131,10 +136,19 @@ class ExtractCommandTests { out.closeEntry(); out.putNextEntry(new ZipEntry("d/")); out.closeEntry(); + out.putNextEntry(new JarEntry("META-INF/MANIFEST.MF")); + out.write(getFile("test-manifest.MF").getBytes()); + out.closeEntry(); } return file; } + private String getFile(String fileName) throws Exception { + ClassPathResource resource = new ClassPathResource(fileName, getClass()); + InputStreamReader reader = new InputStreamReader(resource.getInputStream()); + return FileCopyUtils.copyToString(reader); + } + private static class TestLayers implements Layers { @Override diff --git a/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/HelpCommandTests.java b/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/HelpCommandTests.java index cf3994d25f..4491d8798f 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/HelpCommandTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/HelpCommandTests.java @@ -18,7 +18,7 @@ package org.springframework.boot.jarmode.layertools; import java.io.File; import java.io.FileOutputStream; -import java.io.IOException; +import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Writer; import java.nio.charset.StandardCharsets; @@ -31,6 +31,9 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; +import org.springframework.core.io.ClassPathResource; +import org.springframework.util.FileCopyUtils; + import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -70,9 +73,12 @@ class HelpCommandTests { assertThat(this.out).hasSameContentAsResource("help-extract-output.txt"); } - private File createJarFile(String name) throws IOException { + private File createJarFile(String name) throws Exception { File file = new File(this.temp, name); try (ZipOutputStream jarOutputStream = new ZipOutputStream(new FileOutputStream(file))) { + jarOutputStream.putNextEntry(new JarEntry("META-INF/MANIFEST.MF")); + jarOutputStream.write(getFile("test-manifest.MF").getBytes()); + jarOutputStream.closeEntry(); JarEntry indexEntry = new JarEntry("BOOT-INF/layers.idx"); jarOutputStream.putNextEntry(indexEntry); Writer writer = new OutputStreamWriter(jarOutputStream, StandardCharsets.UTF_8); @@ -88,4 +94,10 @@ class HelpCommandTests { return file; } + private String getFile(String fileName) throws Exception { + ClassPathResource resource = new ClassPathResource(fileName, getClass()); + InputStreamReader reader = new InputStreamReader(resource.getInputStream()); + return FileCopyUtils.copyToString(reader); + } + } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/IndexedLayersTests.java b/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/IndexedLayersTests.java index 6d39105a6a..c3ad14beed 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/IndexedLayersTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/IndexedLayersTests.java @@ -98,10 +98,10 @@ class IndexedLayersTests { } private String getIndex() throws Exception { - return getIndex("test-layers.idx"); + return getFile("test-layers.idx"); } - private String getIndex(String fileName) throws Exception { + private String getFile(String fileName) throws Exception { ClassPathResource resource = new ClassPathResource(fileName, getClass()); InputStreamReader reader = new InputStreamReader(resource.getInputStream()); return FileCopyUtils.copyToString(reader); @@ -123,10 +123,10 @@ class IndexedLayersTests { out.putNextEntry(new ZipEntry("WEB-INF/classes/Demo.class")); out.closeEntry(); out.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF")); - out.write(getIndex("test-manifest.MF").getBytes()); + out.write(getFile("test-war-manifest.MF").getBytes()); out.closeEntry(); out.putNextEntry(new ZipEntry("WEB-INF/layers.idx")); - out.write(getIndex("test-war-layers.idx").getBytes()); + out.write(getFile("test-war-layers.idx").getBytes()); out.closeEntry(); } return file; diff --git a/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/LayerToolsJarModeTests.java b/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/LayerToolsJarModeTests.java index a3db809b25..a8c5760751 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/LayerToolsJarModeTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/LayerToolsJarModeTests.java @@ -18,7 +18,7 @@ package org.springframework.boot.jarmode.layertools; import java.io.File; import java.io.FileOutputStream; -import java.io.IOException; +import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintStream; import java.io.Writer; @@ -31,6 +31,9 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; +import org.springframework.core.io.ClassPathResource; +import org.springframework.util.FileCopyUtils; + import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -98,9 +101,12 @@ class LayerToolsJarModeTests { assertThat(this.out).hasSameContentAsResource("error-option-missing-value-output.txt"); } - private File createJarFile(String name) throws IOException { + private File createJarFile(String name) throws Exception { File file = new File(this.temp, name); try (ZipOutputStream jarOutputStream = new ZipOutputStream(new FileOutputStream(file))) { + jarOutputStream.putNextEntry(new JarEntry("META-INF/MANIFEST.MF")); + jarOutputStream.write(getFile("test-manifest.MF").getBytes()); + jarOutputStream.closeEntry(); JarEntry indexEntry = new JarEntry("BOOT-INF/layers.idx"); jarOutputStream.putNextEntry(indexEntry); Writer writer = new OutputStreamWriter(jarOutputStream, StandardCharsets.UTF_8); @@ -116,4 +122,10 @@ class LayerToolsJarModeTests { return file; } + private String getFile(String fileName) throws Exception { + ClassPathResource resource = new ClassPathResource(fileName, getClass()); + InputStreamReader reader = new InputStreamReader(resource.getInputStream()); + return FileCopyUtils.copyToString(reader); + } + } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/ListCommandTests.java b/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/ListCommandTests.java index a0dbaf76cd..91856efecc 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/ListCommandTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/ListCommandTests.java @@ -19,6 +19,7 @@ package org.springframework.boot.jarmode.layertools; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Writer; import java.nio.charset.StandardCharsets; @@ -33,6 +34,9 @@ import org.junit.jupiter.api.io.TempDir; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.core.io.ClassPathResource; +import org.springframework.util.FileCopyUtils; + import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; @@ -72,7 +76,7 @@ class ListCommandTests { assertThat(this.out).hasSameContentAsResource("list-output.txt"); } - private File createJarFile(String name) throws IOException { + private File createJarFile(String name) throws Exception { File file = new File(this.temp, name); try (ZipOutputStream jarOutputStream = new ZipOutputStream(new FileOutputStream(file))) { writeLayersIndex(jarOutputStream); @@ -91,6 +95,9 @@ class ListCommandTests { jarOutputStream.closeEntry(); jarOutputStream.putNextEntry(new ZipEntry(entryPrefix + "d/")); jarOutputStream.closeEntry(); + jarOutputStream.putNextEntry(new JarEntry("META-INF/MANIFEST.MF")); + jarOutputStream.write(getFile("test-manifest.MF").getBytes()); + jarOutputStream.closeEntry(); } return file; } @@ -109,4 +116,10 @@ class ListCommandTests { writer.flush(); } + private String getFile(String fileName) throws Exception { + ClassPathResource resource = new ClassPathResource(fileName, getClass()); + InputStreamReader reader = new InputStreamReader(resource.getInputStream()); + return FileCopyUtils.copyToString(reader); + } + } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/resources/org/springframework/boot/jarmode/layertools/test-manifest.MF b/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/resources/org/springframework/boot/jarmode/layertools/test-manifest.MF index 8da12d2369..6f7c2bd88b 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/resources/org/springframework/boot/jarmode/layertools/test-manifest.MF +++ b/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/resources/org/springframework/boot/jarmode/layertools/test-manifest.MF @@ -1,12 +1,12 @@ Manifest-Version: 1.0 -Created-By: Maven WAR Plugin 3.3.1 +Created-By: Maven JAR Plugin Build-Jdk-Spec: 11 Implementation-Title: demo Implementation-Version: 0.0.1-SNAPSHOT Main-Class: org.springframework.boot.loader.WarLauncher Start-Class: com.example.DemoApplication Spring-Boot-Version: 2.5.0-SNAPSHOT -Spring-Boot-Classes: WEB-INF/classes/ -Spring-Boot-Lib: WEB-INF/lib/ -Spring-Boot-Classpath-Index: WEB-INF/classpath.idx -Spring-Boot-Layers-Index: WEB-INF/layers.idx +Spring-Boot-Classes: BOOT-INF/classes/ +Spring-Boot-Lib: BOOT-INF/lib/ +Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx +Spring-Boot-Layers-Index: BOOT-INF/layers.idx diff --git a/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/resources/org/springframework/boot/jarmode/layertools/test-war-manifest.MF b/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/resources/org/springframework/boot/jarmode/layertools/test-war-manifest.MF new file mode 100644 index 0000000000..8da12d2369 --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/resources/org/springframework/boot/jarmode/layertools/test-war-manifest.MF @@ -0,0 +1,12 @@ +Manifest-Version: 1.0 +Created-By: Maven WAR Plugin 3.3.1 +Build-Jdk-Spec: 11 +Implementation-Title: demo +Implementation-Version: 0.0.1-SNAPSHOT +Main-Class: org.springframework.boot.loader.WarLauncher +Start-Class: com.example.DemoApplication +Spring-Boot-Version: 2.5.0-SNAPSHOT +Spring-Boot-Classes: WEB-INF/classes/ +Spring-Boot-Lib: WEB-INF/lib/ +Spring-Boot-Classpath-Index: WEB-INF/classpath.idx +Spring-Boot-Layers-Index: WEB-INF/layers.idx