FileOptions -> SourceOptions to work with String paths instead of Files
We check for existence of the sources and (as before) resolve multiple resources on the classpath if a path is not a File. In addition supports Spring pseudo-URL prefixes as well as normal URLs as source locations. In addition sources can now be specified as a directory (searched recursively by default), or a resource pattern (e.g. app/**/*.groovy). Fixes gh-207
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package org.springframework.boot.cli;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URI;
|
||||
@@ -104,8 +105,13 @@ public class CliTester implements TestRule {
|
||||
final String[] sources = new String[args.length];
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
String arg = args[i];
|
||||
if (arg.startsWith("-")) {
|
||||
sources[i] = arg;
|
||||
if (!arg.endsWith(".groovy") && !arg.endsWith(".xml")) {
|
||||
if (new File(this.prefix + arg).isDirectory()) {
|
||||
sources[i] = this.prefix + arg;
|
||||
}
|
||||
else {
|
||||
sources[i] = arg;
|
||||
}
|
||||
}
|
||||
else {
|
||||
sources[i] = this.prefix + arg;
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2012-2013 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
|
||||
*
|
||||
* http://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.cli;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Integration tests for code in directories.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class DirectorySourcesIntegrationTests {
|
||||
|
||||
@Rule
|
||||
public CliTester cli = new CliTester("src/test/resources/dir-sample/");
|
||||
|
||||
@Test
|
||||
public void runDirectory() throws Exception {
|
||||
this.cli.run("code");
|
||||
assertThat(this.cli.getOutput(), containsString("Hello World"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runDirectoryRecursive() throws Exception {
|
||||
this.cli.run("");
|
||||
assertThat(this.cli.getOutput(), containsString("Hello World"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runPathPattern() throws Exception {
|
||||
this.cli.run("**/*.groovy");
|
||||
assertThat(this.cli.getOutput(), containsString("Hello World"));
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,6 @@ package org.springframework.boot.cli.command;
|
||||
|
||||
import groovy.lang.Closure;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -55,8 +54,8 @@ public class ScriptCompilationCustomizerTests {
|
||||
|
||||
@Test
|
||||
public void simpleCompile() throws Exception {
|
||||
Class<?>[] types = this.compiler.compile(new File(
|
||||
"src/test/resources/scripts/command.groovy"));
|
||||
Class<?>[] types = this.compiler
|
||||
.compile("src/test/resources/scripts/command.groovy");
|
||||
Class<?> main = types[0];
|
||||
assertEquals("org.test.command.TestCommand", main.getName());
|
||||
assertTrue(Command.class.isAssignableFrom(main));
|
||||
@@ -64,24 +63,21 @@ public class ScriptCompilationCustomizerTests {
|
||||
|
||||
@Test
|
||||
public void addsOptionHandler() throws Exception {
|
||||
Class<?>[] types = this.compiler.compile(new File(
|
||||
"src/test/resources/scripts/handler.groovy"));
|
||||
Class<?>[] types = this.compiler.compile("classpath:/scripts/handler.groovy");
|
||||
Class<?> main = types[0];
|
||||
assertTrue(OptionHandler.class.isAssignableFrom(main));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addsCommands() throws Exception {
|
||||
Class<?>[] types = this.compiler.compile(new File(
|
||||
"src/test/resources/scripts/commands.groovy"));
|
||||
Class<?>[] types = this.compiler.compile("classpath:scripts/commands.groovy");
|
||||
Class<?> main = types[0];
|
||||
assertTrue(Commands.class.isAssignableFrom(main));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void closureWithStringArgs() throws Exception {
|
||||
Class<?>[] types = this.compiler.compile(new File(
|
||||
"src/test/resources/scripts/commands.groovy"));
|
||||
Class<?>[] types = this.compiler.compile("classpath:scripts/commands.groovy");
|
||||
Class<?> main = types[0];
|
||||
Map<String, Closure<?>> commands = ((Commands) main.newInstance()).getCommands();
|
||||
assertEquals(1, commands.size());
|
||||
@@ -93,8 +89,8 @@ public class ScriptCompilationCustomizerTests {
|
||||
|
||||
@Test
|
||||
public void closureWithEmptyArgs() throws Exception {
|
||||
Class<?>[] types = this.compiler.compile(new File(
|
||||
"src/test/resources/scripts/commands.groovy"));
|
||||
Class<?>[] types = this.compiler
|
||||
.compile("src/test/resources/scripts/commands.groovy");
|
||||
Class<?> main = types[0];
|
||||
Map<String, Closure<?>> commands = ((Commands) main.newInstance()).getCommands();
|
||||
assertEquals(1, commands.size());
|
||||
@@ -106,8 +102,8 @@ public class ScriptCompilationCustomizerTests {
|
||||
|
||||
@Test
|
||||
public void closureAndOptionsDefined() throws Exception {
|
||||
Class<?>[] types = this.compiler.compile(new File(
|
||||
"src/test/resources/scripts/options.groovy"));
|
||||
Class<?>[] types = this.compiler
|
||||
.compile("src/test/resources/scripts/options.groovy");
|
||||
Class<?> main = types[0];
|
||||
Commands commands = (Commands) main.newInstance();
|
||||
Map<String, Closure<?>> closures = commands.getCommands();
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* Copyright 2012-2013 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
|
||||
*
|
||||
* http://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.cli.util;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class ResourceUtilsTests {
|
||||
|
||||
@Test
|
||||
public void explicitClasspathResource() {
|
||||
List<String> urls = ResourceUtils.getUrls("classpath:init.groovy",
|
||||
ClassUtils.getDefaultClassLoader());
|
||||
assertEquals(1, urls.size());
|
||||
assertTrue(urls.get(0).startsWith("file:"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void explicitClasspathResourceWithSlash() {
|
||||
List<String> urls = ResourceUtils.getUrls("classpath:/init.groovy",
|
||||
ClassUtils.getDefaultClassLoader());
|
||||
assertEquals(1, urls.size());
|
||||
assertTrue(urls.get(0).startsWith("file:"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void implicitClasspathResource() {
|
||||
List<String> urls = ResourceUtils.getUrls("init.groovy",
|
||||
ClassUtils.getDefaultClassLoader());
|
||||
assertEquals(1, urls.size());
|
||||
assertTrue(urls.get(0).startsWith("file:"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void implicitClasspathResourceWithSlash() {
|
||||
List<String> urls = ResourceUtils.getUrls("/init.groovy",
|
||||
ClassUtils.getDefaultClassLoader());
|
||||
assertEquals(1, urls.size());
|
||||
assertTrue(urls.get(0).startsWith("file:"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nonexistentClasspathResource() {
|
||||
List<String> urls = ResourceUtils.getUrls("classpath:nonexistent.groovy", null);
|
||||
assertEquals(0, urls.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void explicitFile() {
|
||||
List<String> urls = ResourceUtils.getUrls("file:src/test/resources/init.groovy",
|
||||
ClassUtils.getDefaultClassLoader());
|
||||
assertEquals(1, urls.size());
|
||||
assertTrue(urls.get(0).startsWith("file:"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void implicitFile() {
|
||||
List<String> urls = ResourceUtils.getUrls("src/test/resources/init.groovy",
|
||||
ClassUtils.getDefaultClassLoader());
|
||||
assertEquals(1, urls.size());
|
||||
assertTrue(urls.get(0).startsWith("file:"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nonexistentFile() {
|
||||
List<String> urls = ResourceUtils.getUrls("file:nonexistent.groovy", null);
|
||||
assertEquals(0, urls.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void recursiveFiles() {
|
||||
List<String> urls = ResourceUtils.getUrls("src/test/resources/dir-sample",
|
||||
ClassUtils.getDefaultClassLoader());
|
||||
assertEquals(1, urls.size());
|
||||
assertTrue(urls.get(0).startsWith("file:"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void recursiveFilesByPatternWithPrefix() {
|
||||
List<String> urls = ResourceUtils.getUrls(
|
||||
"file:src/test/resources/dir-sample/**/*.groovy",
|
||||
ClassUtils.getDefaultClassLoader());
|
||||
assertEquals(1, urls.size());
|
||||
assertTrue(urls.get(0).startsWith("file:"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void recursiveFilesByPattern() {
|
||||
List<String> urls = ResourceUtils.getUrls(
|
||||
"src/test/resources/dir-sample/**/*.groovy",
|
||||
ClassUtils.getDefaultClassLoader());
|
||||
assertEquals(1, urls.size());
|
||||
assertTrue(urls.get(0).startsWith("file:"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void directoryOfFilesWithPrefix() {
|
||||
List<String> urls = ResourceUtils.getUrls(
|
||||
"file:src/test/resources/dir-sample/code/*",
|
||||
ClassUtils.getDefaultClassLoader());
|
||||
assertEquals(1, urls.size());
|
||||
assertTrue(urls.get(0).startsWith("file:"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void directoryOfFiles() {
|
||||
List<String> urls = ResourceUtils.getUrls("src/test/resources/dir-sample/code/*",
|
||||
ClassUtils.getDefaultClassLoader());
|
||||
assertEquals(1, urls.size());
|
||||
assertTrue(urls.get(0).startsWith("file:"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user