Check that cli jar command only writes .jars

Update `JarCommand` to check that the file extension of the output is
`.jar`.

Fixes gh-581
This commit is contained in:
Phillip Webb 2014-03-25 14:57:12 -07:00
parent 1dcd4ddf9e
commit 0960ac760e
3 changed files with 20 additions and 1 deletions

View File

@ -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);

View File

@ -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<JarCommand> future = submitCommand(new JarCommand(), args);
this.commands.add(future.get(this.timeout, TimeUnit.MILLISECONDS));
return getOutput();
}
private <T extends OptionParsingCommand> Future<T> submitCommand(final T command,
String... args) {
final String[] sources = getSources(args);

View File

@ -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");
}
}