Skip to content

Commit 0960ac7

Browse files
author
Phillip Webb
committed
Check that cli jar command only writes .jars
Update `JarCommand` to check that the file extension of the output is `.jar`. Fixes gh-581
1 parent 1dcd4dd commit 0960ac7

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

spring-boot-cli/src/main/java/org/springframework/boot/cli/command/jar/JarCommand.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ protected void run(OptionSet options) throws Exception {
110110
"The name of the resulting jar and at least one source file must be specified");
111111

112112
File output = new File((String) nonOptionArguments.remove(0));
113+
Assert.isTrue(output.getName().toLowerCase().endsWith(".jar"), "The output '"
114+
+ output + "' is not a JAR file.");
113115
deleteIfExists(output);
114116

115117
GroovyCompiler compiler = createCompiler(options);

spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.boot.cli.command.AbstractCommand;
3636
import org.springframework.boot.cli.command.OptionParsingCommand;
3737
import org.springframework.boot.cli.command.grab.GrabCommand;
38+
import org.springframework.boot.cli.command.jar.JarCommand;
3839
import org.springframework.boot.cli.command.run.RunCommand;
3940
import org.springframework.boot.cli.command.test.TestCommand;
4041
import org.springframework.boot.cli.util.OutputCapture;
@@ -82,6 +83,12 @@ public String grab(String... args) throws Exception {
8283
return getOutput();
8384
}
8485

86+
public String jar(String... args) throws Exception {
87+
Future<JarCommand> future = submitCommand(new JarCommand(), args);
88+
this.commands.add(future.get(this.timeout, TimeUnit.MILLISECONDS));
89+
return getOutput();
90+
}
91+
8592
private <T extends OptionParsingCommand> Future<T> submitCommand(final T command,
8693
String... args) {
8794
final String[] sources = getSources(args);

spring-boot-cli/src/test/java/org/springframework/boot/cli/ReproIntegrationTests.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2013 the original author or authors.
2+
* Copyright 2012-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import org.junit.Rule;
2020
import org.junit.Test;
21+
import org.junit.rules.ExpectedException;
2122

2223
import static org.hamcrest.Matchers.containsString;
2324
import static org.junit.Assert.assertThat;
@@ -32,6 +33,9 @@ public class ReproIntegrationTests {
3233
@Rule
3334
public CliTester cli = new CliTester("src/test/resources/repro-samples/");
3435

36+
@Rule
37+
public ExpectedException thrown = ExpectedException.none();
38+
3539
@Test
3640
public void grabAntBuilder() throws Exception {
3741
this.cli.run("grab-ant-builder.groovy");
@@ -54,4 +58,10 @@ public void shellDependencies() throws Exception {
5458
containsString("{\"message\":\"Hello World\"}"));
5559
}
5660

61+
@Test
62+
public void jarFileExtensionNeeded() throws Exception {
63+
this.thrown.expect(IllegalStateException.class);
64+
this.thrown.expectMessage("is not a JAR file");
65+
this.cli.jar("secure.groovy", "crsh.groovy");
66+
}
5767
}

0 commit comments

Comments
 (0)