extract spring-shell module
This commit is contained in:
@@ -1,10 +0,0 @@
|
||||
package org.baeldung.shell;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.springframework.shell.Bootstrap;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws IOException {
|
||||
Bootstrap.main(args);
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package org.baeldung.shell.simple;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.shell.plugin.support.DefaultBannerProvider;
|
||||
import org.springframework.shell.support.util.OsUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public class SimpleBannerProvider extends DefaultBannerProvider {
|
||||
|
||||
public String getBanner() {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
buf.append("=======================================").append(OsUtils.LINE_SEPARATOR);
|
||||
buf.append("* Baeldung Shell *").append(OsUtils.LINE_SEPARATOR);
|
||||
buf.append("=======================================").append(OsUtils.LINE_SEPARATOR);
|
||||
buf.append("Version:").append(this.getVersion());
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public String getVersion() {
|
||||
return "1.0.1";
|
||||
}
|
||||
|
||||
public String getWelcomeMessage() {
|
||||
return "Welcome to Baeldung CLI";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getProviderName() {
|
||||
return "Baeldung Banner";
|
||||
}
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
package org.baeldung.shell.simple;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.URL;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import org.springframework.shell.Bootstrap;
|
||||
import org.springframework.shell.core.CommandMarker;
|
||||
import org.springframework.shell.core.annotation.CliAvailabilityIndicator;
|
||||
import org.springframework.shell.core.annotation.CliCommand;
|
||||
import org.springframework.shell.core.annotation.CliOption;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class SimpleCLI implements CommandMarker {
|
||||
|
||||
private String getContentsOfUrlAsString(URL url) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
try {
|
||||
try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()))) {
|
||||
String inputLine;
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
sb.append(inputLine);
|
||||
}
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
sb.append("ERROR");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@CliCommand(value = { "web-get", "wg" }, help = "Displays the contents of a URL.")
|
||||
public String webGet(@CliOption(key = { "", "url" }, help = "URL whose contents will be displayed.") URL url) {
|
||||
return getContentsOfUrlAsString(url);
|
||||
}
|
||||
|
||||
@CliCommand(value = { "web-save", "ws" }, help = "Saves the contents of a URL.")
|
||||
public String webSave(@CliOption(key = { "", "url" }, help = "URL whose contents will be saved.") URL url, @CliOption(key = { "out", "file" }, mandatory = true, help = "The name of the file.") String file) {
|
||||
String contents = getContentsOfUrlAsString(url);
|
||||
try (PrintWriter out = new PrintWriter(file)) {
|
||||
out.write(contents);
|
||||
} catch (FileNotFoundException ex) {
|
||||
// Ignore
|
||||
}
|
||||
return "Done.";
|
||||
}
|
||||
|
||||
private boolean adminEnableExecuted = false;
|
||||
|
||||
@CliAvailabilityIndicator(value = { "web-save" })
|
||||
public boolean isAdminEnabled() {
|
||||
return adminEnableExecuted;
|
||||
}
|
||||
|
||||
@CliCommand(value = "admin-enable")
|
||||
public String adminEnable() {
|
||||
adminEnableExecuted = true;
|
||||
return "Admin commands enabled.";
|
||||
}
|
||||
|
||||
@CliCommand(value = "admin-disable")
|
||||
public String adminDisable() {
|
||||
adminEnableExecuted = false;
|
||||
return "Admin commands disabled.";
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package org.baeldung.shell.simple;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.shell.plugin.support.DefaultHistoryFileNameProvider;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public class SimpleHistoryFileNameProvider extends DefaultHistoryFileNameProvider {
|
||||
|
||||
@Override
|
||||
public String getHistoryFileName() {
|
||||
return "baeldung-shell.log";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getProviderName() {
|
||||
return "Baeldung History";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package org.baeldung.shell.simple;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.shell.plugin.support.DefaultPromptProvider;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public class SimplePromptProvider extends DefaultPromptProvider {
|
||||
|
||||
@Override
|
||||
public String getPrompt() {
|
||||
return "baeldung-shell>";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getProviderName() {
|
||||
return "Baeldung Prompt";
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package org.baeldung.shell.simple;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import org.springframework.shell.core.Completion;
|
||||
import org.springframework.shell.core.Converter;
|
||||
import org.springframework.shell.core.MethodTarget;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class SimpleURLConverter implements Converter<URL> {
|
||||
|
||||
@Override
|
||||
public URL convertFromText(String value, Class<?> requiredType, String optionContext) {
|
||||
try {
|
||||
return new URL(value);
|
||||
} catch (MalformedURLException ex) {
|
||||
// Ignore
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getAllPossibleValues(List<Completion> completions, Class<?> requiredType, String existingData, String optionContext, MethodTarget target) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supports(Class<?> requiredType, String optionContext) {
|
||||
return URL.class.isAssignableFrom(requiredType);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
|
||||
|
||||
<context:component-scan base-package="org.baeldung.shell.simple" />
|
||||
|
||||
</beans>
|
||||
@@ -1,82 +0,0 @@
|
||||
package org.baeldung.shell.simple;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.springframework.shell.Bootstrap;
|
||||
import org.springframework.shell.core.CommandResult;
|
||||
import org.springframework.shell.core.JLineShellComponent;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class SimpleCLIIntegrationTest {
|
||||
|
||||
static JLineShellComponent shell;
|
||||
|
||||
@BeforeClass
|
||||
public static void startUp() throws InterruptedException {
|
||||
final Bootstrap bootstrap = new Bootstrap();
|
||||
shell = bootstrap.getJLineShellComponent();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void shutdown() {
|
||||
shell.stop();
|
||||
// delete contents.txt
|
||||
final File testFile = new File("contents.txt");
|
||||
if (testFile.exists()) {
|
||||
testFile.delete();
|
||||
}
|
||||
}
|
||||
|
||||
public static JLineShellComponent getShell() {
|
||||
return shell;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCommandConfig_whenExecutingWebGetCommand_thenCorrectResult() {
|
||||
|
||||
final CommandResult resultWebSave = shell.executeCommand("web-get --url https://www.google.com");
|
||||
|
||||
Assert.assertTrue(resultWebSave.isSuccess());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCommandConfig_whenExecutingWebSaveCommand_thenCorrectResult() {
|
||||
|
||||
shell.executeCommand("admin-enable");
|
||||
final CommandResult result = shell.executeCommand("web-save --url https://www.google.com --out contents.txt");
|
||||
|
||||
Assert.assertArrayEquals(new boolean[] { result.isSuccess(), new File("contents.txt").exists() }, new boolean[] { true, true });
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCommandConfig_whenAdminEnableCommandExecuted_thenCorrectAvailability() {
|
||||
|
||||
final CommandResult resultAdminDisable = shell.executeCommand("admin-disable");
|
||||
final CommandResult resultWebSaveUnavailable = shell.executeCommand("web-save --url https://www.google.com --out contents.txt");
|
||||
final CommandResult resultAdminEnable = shell.executeCommand("admin-enable");
|
||||
final CommandResult resultWebSaveAvailable = shell.executeCommand("web-save --url https://www.google.com --out contents.txt");
|
||||
|
||||
Assert.assertArrayEquals(new boolean[] { resultAdminDisable.isSuccess(), resultWebSaveUnavailable.isSuccess(), resultAdminEnable.isSuccess(), resultWebSaveAvailable.isSuccess() }, new boolean[] { true, false, true, true });
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCommandConfig_whenWebSaveCommandExecutedNoOutArgument_thenError() {
|
||||
|
||||
shell.executeCommand("admin-enable");
|
||||
final CommandResult resultWebSave = shell.executeCommand("web-save --url https://www.google.com");
|
||||
|
||||
Assert.assertEquals(resultWebSave.isSuccess(), false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCommandConfig_whenExecutingWebGetCommandWithDefaultArgument_thenCorrectResult() {
|
||||
|
||||
final CommandResult result = shell.executeCommand("web-get https://www.google.com");
|
||||
|
||||
Assert.assertEquals(result.isSuccess(), true);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user