From 0960ac760e284015e0e9e7897f74f376e06d3a66 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 25 Mar 2014 14:57:12 -0700 Subject: [PATCH] Check that cli `jar` command only writes `.jar`s Update `JarCommand` to check that the file extension of the output is `.jar`. Fixes gh-581 --- .../boot/cli/command/jar/JarCommand.java | 2 ++ .../java/org/springframework/boot/cli/CliTester.java | 7 +++++++ .../boot/cli/ReproIntegrationTests.java | 12 +++++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/jar/JarCommand.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/jar/JarCommand.java index 51f3be65f80..392645a4ed1 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/jar/JarCommand.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/jar/JarCommand.java @@ -110,6 +110,8 @@ public class JarCommand extends OptionParsingCommand { "The name of the resulting jar and at least one source file must be specified"); File output = new File((String) nonOptionArguments.remove(0)); + Assert.isTrue(output.getName().toLowerCase().endsWith(".jar"), "The output '" + + output + "' is not a JAR file."); deleteIfExists(output); GroovyCompiler compiler = createCompiler(options); diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java index 8bd6b39f9f4..6032a336243 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java @@ -35,6 +35,7 @@ import org.junit.runners.model.Statement; import org.springframework.boot.cli.command.AbstractCommand; import org.springframework.boot.cli.command.OptionParsingCommand; import org.springframework.boot.cli.command.grab.GrabCommand; +import org.springframework.boot.cli.command.jar.JarCommand; import org.springframework.boot.cli.command.run.RunCommand; import org.springframework.boot.cli.command.test.TestCommand; import org.springframework.boot.cli.util.OutputCapture; @@ -82,6 +83,12 @@ public class CliTester implements TestRule { return getOutput(); } + public String jar(String... args) throws Exception { + Future future = submitCommand(new JarCommand(), args); + this.commands.add(future.get(this.timeout, TimeUnit.MILLISECONDS)); + return getOutput(); + } + private Future submitCommand(final T command, String... args) { final String[] sources = getSources(args); diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/ReproIntegrationTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/ReproIntegrationTests.java index 751fdf88e0a..7dbceb958fa 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/ReproIntegrationTests.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/ReproIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2014 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. @@ -18,6 +18,7 @@ package org.springframework.boot.cli; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertThat; @@ -32,6 +33,9 @@ public class ReproIntegrationTests { @Rule public CliTester cli = new CliTester("src/test/resources/repro-samples/"); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Test public void grabAntBuilder() throws Exception { this.cli.run("grab-ant-builder.groovy"); @@ -54,4 +58,10 @@ public class ReproIntegrationTests { containsString("{\"message\":\"Hello World\"}")); } + @Test + public void jarFileExtensionNeeded() throws Exception { + this.thrown.expect(IllegalStateException.class); + this.thrown.expectMessage("is not a JAR file"); + this.cli.jar("secure.groovy", "crsh.groovy"); + } }