Rename spring-boot-layertools
Rename `spring-boot-layertools` to `spring-boot-jarmode-layertools`. Closes gh-19853
This commit is contained in:
@@ -0,0 +1,166 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.jarmode.layertools;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.assertj.core.api.InstanceOfAssertFactories;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.jarmode.layertools.Command.Option;
|
||||
import org.springframework.boot.jarmode.layertools.Command.Options;
|
||||
import org.springframework.boot.jarmode.layertools.Command.Parameters;
|
||||
|
||||
import static org.assertj.core.api.Assertions.as;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link Command}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
class CommandTests {
|
||||
|
||||
private static final Option VERBOSE_FLAG = Option.flag("verbose", "Verbose output");
|
||||
|
||||
private static final Option LOG_LEVEL_OPTION = Option.of("log-level", "Logging level (debug or info)", "string");
|
||||
|
||||
@Test
|
||||
void getNameReturnsName() {
|
||||
TestCommand command = new TestCommand("test");
|
||||
assertThat(command.getName()).isEqualTo("test");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getDescriptionReturnsDescription() {
|
||||
TestCommand command = new TestCommand("test", "Test description", Options.none(), Parameters.none());
|
||||
assertThat(command.getDescription()).isEqualTo("Test description");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getOptionsReturnsOptions() {
|
||||
Options options = Options.of(LOG_LEVEL_OPTION);
|
||||
TestCommand command = new TestCommand("test", "test", options, Parameters.none());
|
||||
assertThat(command.getOptions()).isEqualTo(options);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getParametersReturnsParameters() {
|
||||
Parameters parameters = Parameters.of("[<param>]");
|
||||
TestCommand command = new TestCommand("test", "test", Options.none(), parameters);
|
||||
assertThat(command.getParameters()).isEqualTo(parameters);
|
||||
}
|
||||
|
||||
@Test
|
||||
void runWithOptionsAndParametersParsesOptionsAndParameters() {
|
||||
TestCommand command = new TestCommand("test", VERBOSE_FLAG, LOG_LEVEL_OPTION);
|
||||
run(command, "--verbose", "--log-level", "test1", "test2", "test3");
|
||||
assertThat(command.getRunOptions()).containsEntry(VERBOSE_FLAG, null);
|
||||
assertThat(command.getRunOptions()).containsEntry(LOG_LEVEL_OPTION, "test1");
|
||||
assertThat(command.getRunParameters()).containsExactly("test2", "test3");
|
||||
}
|
||||
|
||||
@Test
|
||||
void findWhenNameMatchesReturnsCommand() {
|
||||
TestCommand test1 = new TestCommand("test1");
|
||||
TestCommand test2 = new TestCommand("test2");
|
||||
List<Command> commands = Arrays.asList(test1, test2);
|
||||
assertThat(Command.find(commands, "test1")).isEqualTo(test1);
|
||||
assertThat(Command.find(commands, "test2")).isEqualTo(test2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void findWhenNameDoesNotMatchReturnsNull() {
|
||||
TestCommand test1 = new TestCommand("test1");
|
||||
TestCommand test2 = new TestCommand("test2");
|
||||
List<Command> commands = Arrays.asList(test1, test2);
|
||||
assertThat(Command.find(commands, "test3")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void parametersOfCreatesParametersInstance() {
|
||||
Parameters parameters = Parameters.of("test1", "test2");
|
||||
assertThat(parameters.getDescriptions()).containsExactly("test1", "test2");
|
||||
}
|
||||
|
||||
@Test
|
||||
void optionsNoneReturnsEmptyOptions() {
|
||||
Options options = Options.none();
|
||||
assertThat(options).extracting("values", as(InstanceOfAssertFactories.ARRAY)).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void optionsOfReturnsOptions() {
|
||||
Option option = Option.of("test", "value description", "description");
|
||||
Options options = Options.of(option);
|
||||
assertThat(options).extracting("values", as(InstanceOfAssertFactories.ARRAY)).containsExactly(option);
|
||||
}
|
||||
|
||||
@Test
|
||||
void optionFlagCreatesFlagOption() {
|
||||
Option option = Option.flag("test", "description");
|
||||
assertThat(option.getName()).isEqualTo("test");
|
||||
assertThat(option.getDescription()).isEqualTo("description");
|
||||
assertThat(option.getValueDescription()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void optionOfCreatesValueOption() {
|
||||
Option option = Option.of("test", "value description", "description");
|
||||
assertThat(option.getName()).isEqualTo("test");
|
||||
assertThat(option.getDescription()).isEqualTo("description");
|
||||
assertThat(option.getValueDescription()).isEqualTo("value description");
|
||||
}
|
||||
|
||||
private void run(TestCommand command, String... args) {
|
||||
command.run(new ArrayDeque<>(Arrays.asList(args)));
|
||||
}
|
||||
|
||||
static class TestCommand extends Command {
|
||||
|
||||
private Map<Option, String> runOptions;
|
||||
|
||||
private List<String> runParameters;
|
||||
|
||||
TestCommand(String name, Option... options) {
|
||||
this(name, "test", Options.of(options), Parameters.none());
|
||||
}
|
||||
|
||||
TestCommand(String name, String description, Options options, Parameters parameters) {
|
||||
super(name, description, options, parameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void run(Map<Option, String> options, List<String> parameters) {
|
||||
this.runOptions = options;
|
||||
this.runParameters = parameters;
|
||||
}
|
||||
|
||||
Map<Option, String> getRunOptions() {
|
||||
return this.runOptions;
|
||||
}
|
||||
|
||||
List<String> getRunParameters() {
|
||||
return this.runParameters;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.jarmode.layertools;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
|
||||
/**
|
||||
* Tests for {@link Context}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
class ContextTests {
|
||||
|
||||
@TempDir
|
||||
File temp;
|
||||
|
||||
@Test
|
||||
void createWhenSourceIsNullThrowsException() {
|
||||
assertThatIllegalStateException().isThrownBy(() -> new Context(null, this.temp))
|
||||
.withMessage("Unable to find source JAR");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWhenSourceIsFolderThrowsException() {
|
||||
File folder = new File(this.temp, "test");
|
||||
folder.mkdir();
|
||||
assertThatIllegalStateException().isThrownBy(() -> new Context(folder, this.temp))
|
||||
.withMessage("Unable to find source JAR");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWhenSourceIsNotJarThrowsException() 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 JAR");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getJarFileReturnsJar() throws Exception {
|
||||
File jar = new File(this.temp, "test.jar");
|
||||
Files.createFile(jar.toPath());
|
||||
Context context = new Context(jar, this.temp);
|
||||
assertThat(context.getJarFile()).isEqualTo(jar);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getWorkingDirectoryReturnsWorkingDir() throws IOException {
|
||||
File jar = new File(this.temp, "test.jar");
|
||||
Files.createFile(jar.toPath());
|
||||
Context context = new Context(jar, this.temp);
|
||||
assertThat(context.getWorkingDir()).isEqualTo(this.temp);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void getRelativePathReturnsRelativePath() throws Exception {
|
||||
File target = new File(this.temp, "target");
|
||||
target.mkdir();
|
||||
File jar = new File(target, "test.jar");
|
||||
Files.createFile(jar.toPath());
|
||||
Context context = new Context(jar, this.temp);
|
||||
assertThat(context.getRelativeJarDir()).isEqualTo("target");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getRelativePathWhenWorkingDirReturnsNull() throws Exception {
|
||||
File jar = new File(this.temp, "test.jar");
|
||||
Files.createFile(jar.toPath());
|
||||
Context context = new Context(jar, this.temp);
|
||||
assertThat(context.getRelativeJarDir()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getRelativePathWhenCannotBeDeducedReturnsNull() throws Exception {
|
||||
File folder1 = new File(this.temp, "folder1");
|
||||
folder1.mkdir();
|
||||
File folder2 = new File(this.temp, "folder1");
|
||||
folder2.mkdir();
|
||||
File jar = new File(folder1, "test.jar");
|
||||
Files.createFile(jar.toPath());
|
||||
Context context = new Context(jar, folder2);
|
||||
assertThat(context.getRelativeJarDir()).isNull();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.jarmode.layertools;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
* Tests for {@link ExtractCommand}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
class ExtractCommandTests {
|
||||
|
||||
@TempDir
|
||||
File temp;
|
||||
|
||||
@Mock
|
||||
private Context context;
|
||||
|
||||
private File jarFile;
|
||||
|
||||
private File extract;
|
||||
|
||||
private Layers layers = new TestLayers();
|
||||
|
||||
private ExtractCommand command;
|
||||
|
||||
@BeforeEach
|
||||
void setup() throws Exception {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
this.jarFile = createJarFile("test.jar");
|
||||
this.extract = new File(this.temp, "extract");
|
||||
this.extract.mkdir();
|
||||
given(this.context.getJarFile()).willReturn(this.jarFile);
|
||||
given(this.context.getWorkingDir()).willReturn(this.extract);
|
||||
this.command = new ExtractCommand(this.context, this.layers);
|
||||
}
|
||||
|
||||
@Test
|
||||
void runExtractsLayers() throws Exception {
|
||||
this.command.run(Collections.emptyMap(), Collections.emptyList());
|
||||
assertThat(this.extract.list()).containsOnly("a", "b", "c");
|
||||
assertThat(new File(this.extract, "a/a/a.jar")).exists();
|
||||
assertThat(new File(this.extract, "b/b/b.jar")).exists();
|
||||
assertThat(new File(this.extract, "c/c/c.jar")).exists();
|
||||
}
|
||||
|
||||
@Test
|
||||
void runWhenHasDestinationOptionExtractsLayers() {
|
||||
File out = new File(this.extract, "out");
|
||||
this.command.run(Collections.singletonMap(ExtractCommand.DESTINATION_OPTION, out.getAbsolutePath()),
|
||||
Collections.emptyList());
|
||||
assertThat(this.extract.list()).containsOnly("out");
|
||||
assertThat(new File(this.extract, "out/a/a/a.jar")).exists();
|
||||
assertThat(new File(this.extract, "out/b/b/b.jar")).exists();
|
||||
assertThat(new File(this.extract, "out/c/c/c.jar")).exists();
|
||||
}
|
||||
|
||||
@Test
|
||||
void runWhenHasLayerParamsExtractsLimitedLayers() {
|
||||
this.command.run(Collections.emptyMap(), Arrays.asList("a", "c"));
|
||||
assertThat(this.extract.list()).containsOnly("a", "c");
|
||||
assertThat(new File(this.extract, "a/a/a.jar")).exists();
|
||||
assertThat(new File(this.extract, "c/c/c.jar")).exists();
|
||||
}
|
||||
|
||||
private File createJarFile(String name) throws IOException {
|
||||
File file = new File(this.temp, name);
|
||||
try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file))) {
|
||||
out.putNextEntry(new ZipEntry("a/"));
|
||||
out.closeEntry();
|
||||
out.putNextEntry(new ZipEntry("a/a.jar"));
|
||||
out.closeEntry();
|
||||
out.putNextEntry(new ZipEntry("b/"));
|
||||
out.closeEntry();
|
||||
out.putNextEntry(new ZipEntry("b/b.jar"));
|
||||
out.closeEntry();
|
||||
out.putNextEntry(new ZipEntry("c/"));
|
||||
out.closeEntry();
|
||||
out.putNextEntry(new ZipEntry("c/c.jar"));
|
||||
out.closeEntry();
|
||||
out.putNextEntry(new ZipEntry("d/"));
|
||||
out.closeEntry();
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
private static class TestLayers implements Layers {
|
||||
|
||||
@Override
|
||||
public Iterator<String> iterator() {
|
||||
return Arrays.asList("a", "b", "c").iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLayer(ZipEntry entry) {
|
||||
if (entry.getName().startsWith("a")) {
|
||||
return "a";
|
||||
}
|
||||
if (entry.getName().startsWith("b")) {
|
||||
return "b";
|
||||
}
|
||||
return "c";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.jarmode.layertools;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link HelpCommand}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
class HelpCommandTests {
|
||||
|
||||
private HelpCommand command;
|
||||
|
||||
private TestPrintStream out;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
Context context = mock(Context.class);
|
||||
given(context.getJarFile()).willReturn(new File("test.jar"));
|
||||
this.command = new HelpCommand(context, LayerToolsJarMode.Runner.getCommands(context));
|
||||
this.out = new TestPrintStream(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
void runWhenHasNoParametersPrintsUsage() {
|
||||
this.command.run(this.out, Collections.emptyMap(), Collections.emptyList());
|
||||
assertThat(this.out).hasSameContentAsResource("help-output.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
void runWhenHasNoCommandParameterPrintsUsage() {
|
||||
this.command.run(this.out, Collections.emptyMap(), Arrays.asList("extract"));
|
||||
System.out.println(this.out);
|
||||
assertThat(this.out).hasSameContentAsResource("help-extract-output.txt");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.jarmode.layertools;
|
||||
|
||||
import java.util.zip.ZipEntry;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link ImplicitLayers}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
class ImplicitLayersTests {
|
||||
|
||||
private Layers layers = new ImplicitLayers();
|
||||
|
||||
@Test
|
||||
void iteratorReturnsLayers() {
|
||||
assertThat(this.layers).containsExactly("dependencies", "snapshot-dependencies", "resources", "application");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getLayerWhenSnapshotJarReturnsSnapshotDependencies() {
|
||||
assertThat(this.layers.getLayer(zipEntry("BOOT-INF/lib/mylib-SNAPSHOT.jar")))
|
||||
.isEqualTo("snapshot-dependencies");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getLayerWhenNonSnapshotJarReturnsDependencies() {
|
||||
assertThat(this.layers.getLayer(zipEntry("BOOT-INF/lib/mylib.jar"))).isEqualTo("dependencies");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getLayerWhenLoaderClassReturnsApplication() {
|
||||
assertThat(this.layers.getLayer(zipEntry("org/springframework/boot/loader/Example.class")))
|
||||
.isEqualTo("application");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getLayerWhenStaticResourceReturnsResources() {
|
||||
assertThat(this.layers.getLayer(zipEntry("BOOT-INF/classes/META-INF/resources/image.gif")))
|
||||
.isEqualTo("resources");
|
||||
assertThat(this.layers.getLayer(zipEntry("BOOT-INF/classes/resources/image.gif"))).isEqualTo("resources");
|
||||
assertThat(this.layers.getLayer(zipEntry("BOOT-INF/classes/static/image.gif"))).isEqualTo("resources");
|
||||
assertThat(this.layers.getLayer(zipEntry("BOOT-INF/classes/public/image.gif"))).isEqualTo("resources");
|
||||
assertThat(this.layers.getLayer(zipEntry("META-INF/resources/image.gif"))).isEqualTo("resources");
|
||||
assertThat(this.layers.getLayer(zipEntry("resources/image.gif"))).isEqualTo("resources");
|
||||
assertThat(this.layers.getLayer(zipEntry("static/image.gif"))).isEqualTo("resources");
|
||||
assertThat(this.layers.getLayer(zipEntry("public/image.gif"))).isEqualTo("resources");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getLayerWhenRegularClassReturnsApplication() {
|
||||
assertThat(this.layers.getLayer(zipEntry("BOOT-INF/classes/com.example/App.class"))).isEqualTo("application");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getLayerWhenClassResourceReturnsApplication() {
|
||||
assertThat(this.layers.getLayer(zipEntry("BOOT-INF/classes/application.properties"))).isEqualTo("application");
|
||||
}
|
||||
|
||||
private ZipEntry zipEntry(String name) {
|
||||
return new ZipEntry(name);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.jarmode.layertools;
|
||||
|
||||
import java.util.zip.ZipEntry;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link IndexedLayers}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
class IndexedLayersTests {
|
||||
|
||||
@Test
|
||||
void createWhenIndexFileIsEmptyThrowsException() {
|
||||
assertThatIllegalStateException().isThrownBy(() -> new IndexedLayers(" \n "))
|
||||
.withMessage("Empty layer index file loaded");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWhenIndexFileHasNoApplicationLayerAddSpringBootApplication() {
|
||||
IndexedLayers layers = new IndexedLayers("test");
|
||||
assertThat(layers).contains("springbootapplication");
|
||||
}
|
||||
|
||||
@Test
|
||||
void iteratorReturnsLayers() {
|
||||
IndexedLayers layers = new IndexedLayers("test\napplication");
|
||||
assertThat(layers).containsExactly("test", "application");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getLayerWhenMatchesLayerPatterReturnsLayer() {
|
||||
IndexedLayers layers = new IndexedLayers("test");
|
||||
assertThat(layers.getLayer(mockEntry("BOOT-INF/layers/test/lib/file.jar"))).isEqualTo("test");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getLayerWhenMatchesLayerPatterForMissingLayerThrowsException() {
|
||||
IndexedLayers layers = new IndexedLayers("test");
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> layers.getLayer(mockEntry("BOOT-INF/layers/missing/lib/file.jar")))
|
||||
.withMessage("Unexpected layer 'missing'");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getLayerWhenDoesNotMatchLayerPatternReturnsApplication() {
|
||||
IndexedLayers layers = new IndexedLayers("test\napplication");
|
||||
assertThat(layers.getLayer(mockEntry("META-INF/MANIFEST.MF"))).isEqualTo("application");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getLayerWhenDoesNotMatchLayerPatternAndHasNoApplicationLayerReturnsSpringApplication() {
|
||||
IndexedLayers layers = new IndexedLayers("test");
|
||||
assertThat(layers.getLayer(mockEntry("META-INF/MANIFEST.MF"))).isEqualTo("springbootapplication");
|
||||
}
|
||||
|
||||
private ZipEntry mockEntry(String name) {
|
||||
ZipEntry entry = mock(ZipEntry.class);
|
||||
given(entry.getName()).willReturn(name);
|
||||
return entry;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.jarmode.layertools;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link LayerToolsJarMode}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
class LayerToolsJarModeTests {
|
||||
|
||||
private static final String[] NO_ARGS = {};
|
||||
|
||||
private TestPrintStream out;
|
||||
|
||||
private PrintStream systemOut;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
Context context = mock(Context.class);
|
||||
given(context.getJarFile()).willReturn(new File("test.jar"));
|
||||
this.out = new TestPrintStream(this);
|
||||
this.systemOut = System.out;
|
||||
System.setOut(this.out);
|
||||
LayerToolsJarMode.Runner.contextOverride = context;
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void restore() {
|
||||
System.setOut(this.systemOut);
|
||||
LayerToolsJarMode.Runner.contextOverride = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
void mainWithNoParamersShowsHelp() {
|
||||
new LayerToolsJarMode().run("layertools", NO_ARGS);
|
||||
assertThat(this.out).hasSameContentAsResource("help-output.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
void mainWithArgRunsCommand() {
|
||||
new LayerToolsJarMode().run("layertools", new String[] { "list" });
|
||||
assertThat(this.out).hasSameContentAsResource("list-output.txt");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.jarmode.layertools;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link ListCommand}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
class ListCommandTests {
|
||||
|
||||
private ListCommand command;
|
||||
|
||||
private TestPrintStream out;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
this.command = new ListCommand(mock(Context.class));
|
||||
this.out = new TestPrintStream(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
void listLayersShouldListLayers() {
|
||||
this.command.printLayers(new ImplicitLayers(), this.out);
|
||||
assertThat(this.out).hasSameContentAsResource("list-output.txt");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.jarmode.layertools;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.assertj.core.api.AbstractAssert;
|
||||
import org.assertj.core.api.AssertProvider;
|
||||
import org.assertj.core.api.Assertions;
|
||||
|
||||
import org.springframework.boot.jarmode.layertools.TestPrintStream.PrintStreamAssert;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
/**
|
||||
* {@link PrintStream} that can be used for testing.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
class TestPrintStream extends PrintStream implements AssertProvider<PrintStreamAssert> {
|
||||
|
||||
private Class<? extends Object> testClass;
|
||||
|
||||
TestPrintStream(Object testInstance) {
|
||||
super(new ByteArrayOutputStream());
|
||||
this.testClass = testInstance.getClass();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PrintStreamAssert assertThat() {
|
||||
return new PrintStreamAssert(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.out.toString();
|
||||
}
|
||||
|
||||
static final class PrintStreamAssert extends AbstractAssert<PrintStreamAssert, TestPrintStream> {
|
||||
|
||||
private PrintStreamAssert(TestPrintStream actual) {
|
||||
super(actual, PrintStreamAssert.class);
|
||||
}
|
||||
|
||||
void hasSameContentAsResource(String resource) {
|
||||
try {
|
||||
InputStream stream = this.actual.testClass.getResourceAsStream(resource);
|
||||
String content = FileCopyUtils.copyToString(new InputStreamReader(stream, StandardCharsets.UTF_8));
|
||||
Assertions.assertThat(this.actual.toString()).isEqualTo(content);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
Extracts layers from the jar for image creation
|
||||
|
||||
Usage:
|
||||
java -Djarmode=layertools -jar test.jar extract [options] [<layer>...]
|
||||
|
||||
Options:
|
||||
--destination string The destination to extract files to
|
||||
@@ -0,0 +1,7 @@
|
||||
Usage:
|
||||
java -Djarmode=layertools -jar test.jar
|
||||
|
||||
Available commands:
|
||||
list List layers from the jar that can be extracted
|
||||
extract Extracts layers from the jar for image creation
|
||||
help Help about any command
|
||||
@@ -0,0 +1,4 @@
|
||||
dependencies
|
||||
snapshot-dependencies
|
||||
resources
|
||||
application
|
||||
Reference in New Issue
Block a user