下面列出了怎么用org.springframework.boot.DefaultApplicationArguments的API类实例代码及写法,或者点击链接到github查看源代码。
public static void main(String[] args) {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
String releaseDir = PropertyUtil.getProperty("trackray.release.dir");
String root = System.getProperty("user.dir");
if (!root.contains(releaseDir)){
System.err.println("请检查工作目录是否在配置文件指定的 "+releaseDir+" 发布目录");
System.exit(0);
}
if (applicationArguments.containsOption("help")){
help(applicationArguments);
}else{
run(args,applicationArguments);
}
}
@Test
public void runCallsHandler() throws Exception {
InRequest request = mock(InRequest.class);
InResponse response = mock(InResponse.class);
given(this.systemInput.read(InRequest.class)).willReturn(request);
given(this.handler.handle(eq(request), any())).willReturn(response);
File tempFolder = this.temporaryFolder.newFolder();
String dir = StringUtils.cleanPath(tempFolder.getPath());
this.command.run(new DefaultApplicationArguments(new String[] { "in", dir }));
verify(this.handler).handle(eq(request), this.directoryCaptor.capture());
verify(this.systemOutput).write(response);
assertThat(this.directoryCaptor.getValue().getFile()).isEqualTo(tempFolder);
}
@Test
public void runWhenFolderArgIsMissingThrowsException() throws Exception {
InRequest request = mock(InRequest.class);
given(this.systemInput.read(InRequest.class)).willReturn(request);
assertThatIllegalStateException()
.isThrownBy(() -> this.command.run(new DefaultApplicationArguments(new String[] {})))
.withMessage("No directory argument specified");
}
@Test
public void runCallsHandler() throws Exception {
CheckRequest request = mock(CheckRequest.class);
CheckResponse response = mock(CheckResponse.class);
given(this.systemInput.read(CheckRequest.class)).willReturn(request);
given(this.handler.handle(request)).willReturn(response);
this.command.run(new DefaultApplicationArguments(new String[] {}));
verify(this.handler).handle(request);
verify(this.systemOutput).write(response);
}
@Test
public void runWhenUnknownCommandThrowsException() throws Exception {
Command fooCommand = mock(Command.class);
given(fooCommand.getName()).willReturn("foo");
CommandProcessor processor = new CommandProcessor(Collections.singletonList(fooCommand));
DefaultApplicationArguments args = new DefaultApplicationArguments(new String[] { "bar", "go" });
assertThatIllegalStateException().isThrownBy(() -> processor.run(args)).withMessage("Unknown command 'bar'");
}
@Test
public void runDelegatesToCommand() throws Exception {
Command fooCommand = mock(Command.class);
given(fooCommand.getName()).willReturn("foo");
Command barCommand = mock(Command.class);
given(barCommand.getName()).willReturn("bar");
CommandProcessor processor = new CommandProcessor(Arrays.asList(fooCommand, barCommand));
DefaultApplicationArguments args = new DefaultApplicationArguments(new String[] { "bar", "go" });
processor.run(args);
verify(fooCommand, never()).run(any());
verify(barCommand).run(args);
}
@Test
public void runCallsHandler() throws Exception {
OutRequest request = mock(OutRequest.class);
OutResponse response = mock(OutResponse.class);
given(this.systemInput.read(OutRequest.class)).willReturn(request);
given(this.handler.handle(eq(request), any())).willReturn(response);
File tempFolder = this.temporaryFolder.newFolder();
String dir = StringUtils.cleanPath(tempFolder.getPath());
this.command.run(new DefaultApplicationArguments(new String[] { "out", dir }));
verify(this.handler).handle(eq(request), this.directoryCaptor.capture());
verify(this.systemOutput).write(response);
assertThat(this.directoryCaptor.getValue().getFile()).isEqualTo(tempFolder);
}
@Test
public void runWhenFolderArgIsMissingThrowsException() throws Exception {
InRequest request = mock(InRequest.class);
given(this.systemInput.read(InRequest.class)).willReturn(request);
assertThatIllegalStateException()
.isThrownBy(() -> this.command.run(new DefaultApplicationArguments(new String[] {})))
.withMessage("No directory argument specified");
}
@Test
public void createFromArgsUsesArgument() throws Exception {
File file = this.temporaryFolder.newFolder();
String path = StringUtils.cleanPath(file.getPath());
ApplicationArguments args = new DefaultApplicationArguments(new String[] { "in", path });
Directory directory = Directory.fromArgs(args);
assertThat(directory.getFile()).isEqualTo(file);
}
private ApplicationArguments updateArguments(ApplicationArguments arguments) {
List<String> originalArguments = new ArrayList<String>(Arrays.asList(arguments.getSourceArgs()));
if (arguments.containsOption("function.name")) {
originalArguments.add(FunctionProperties.PREFIX + ".definition=" + arguments.getOptionValues("function.name").get(0));
}
if (arguments.containsOption("function.location")) {
originalArguments.add(FunctionProperties.PREFIX + ".location=" + arguments.getOptionValues("function.location").get(0));
}
ApplicationArguments updatedArguments = new DefaultApplicationArguments(originalArguments.toArray(new String[] {}));
return updatedArguments;
}
@Test
public void runWhenNoArgumentThrowsException() throws Exception {
CommandProcessor processor = new CommandProcessor(Collections.singletonList(mock(Command.class)));
assertThatIllegalStateException().isThrownBy(() -> processor.run(new DefaultApplicationArguments(NO_ARGS)))
.withMessage("No command argument specified");
}
public static ProgramCommand parseArgs(String[] args) {
if (args == null) {
throw new NullPointerException("args");
}
return parseArgs(new DefaultApplicationArguments(args));
}