changeset 254:ab5866e620c5

Merge
author Roman Kennke <rkennke@redhat.com>
date Fri, 20 Apr 2012 18:39:24 +0200
parents beb978ab0a6f (current diff) 738b0e7472dd (diff)
children 2dce17087aaf
files
diffstat 8 files changed, 82 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/README	Fri Apr 20 16:23:14 2012 +0200
+++ b/README	Fri Apr 20 18:39:24 2012 +0200
@@ -7,6 +7,7 @@
 MongoDB
 http://www.mongodb.org/
 
+Either a /etc/os-release file or lsb_release
 
 BUILDING THERMOSTAT:
 
--- a/common/src/main/java/com/redhat/thermostat/cli/Launcher.java	Fri Apr 20 16:23:14 2012 +0200
+++ b/common/src/main/java/com/redhat/thermostat/cli/Launcher.java	Fri Apr 20 18:39:24 2012 +0200
@@ -46,7 +46,7 @@
     public void run(String[] args) {
         this.args = args;
         registerDefaultCommands();
-        if (hasNoArguments()) {
+        if (hasNoArguments() || unknownCommand()) {
             runHelpCommand();
         } else {
             runCommandFromArguments();
@@ -57,6 +57,13 @@
         return args.length == 0;
     }
 
+    private boolean unknownCommand() {
+        CommandContextFactory cmdCtxFactory = CommandContextFactory.getInstance();
+        CommandRegistry registry = cmdCtxFactory.getCommandRegistry();
+        Command cmd = registry.getCommand(args[0]);
+        return cmd == null;
+    }
+
     private void runHelpCommand() {
         runCommand("help", new String[0]);
     }
--- a/common/src/main/java/com/redhat/thermostat/common/utils/LoggedExternalProcess.java	Fri Apr 20 16:23:14 2012 +0200
+++ b/common/src/main/java/com/redhat/thermostat/common/utils/LoggedExternalProcess.java	Fri Apr 20 18:39:24 2012 +0200
@@ -43,6 +43,8 @@
 import java.util.List;
 import java.util.logging.Logger;
 
+import com.redhat.thermostat.tools.ApplicationException;
+
 public class LoggedExternalProcess extends Thread {
 
     private static final Logger logger = LoggingUtils.getLogger(LoggedExternalProcess.class);
@@ -58,15 +60,20 @@
         setDaemon(true);
     }
 
-    public int runAndReturnResult() throws IOException, InterruptedException {
+    public int runAndReturnResult() throws IOException, InterruptedException, ApplicationException{
         Process p = runAndReturnProcess();
         return p.waitFor();
     }
 
-    public Process runAndReturnProcess() throws IOException {
+    public Process runAndReturnProcess() throws IOException, ApplicationException {
         ProcessBuilder b = new ProcessBuilder(commands);
         b.redirectErrorStream(true);
-        Process p = b.start();
+        Process p = null;
+        try {
+            p = b.start();
+        } catch (IOException ioe) {
+            throw new ApplicationException("unable to execute " + commands[0], ioe);
+        }
         reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
         this.start();
         return p;
--- a/common/src/main/java/com/redhat/thermostat/tools/unix/UnixProcessUtilities.java	Fri Apr 20 16:23:14 2012 +0200
+++ b/common/src/main/java/com/redhat/thermostat/tools/unix/UnixProcessUtilities.java	Fri Apr 20 18:39:24 2012 +0200
@@ -47,6 +47,7 @@
 
 import com.redhat.thermostat.common.utils.LoggedExternalProcess;
 import com.redhat.thermostat.common.utils.LoggingUtils;
+import com.redhat.thermostat.tools.ApplicationException;
 
 public class UnixProcessUtilities {
 
@@ -82,7 +83,7 @@
                 result = output[output.length - 1];
             }
             
-        } catch (IOException e) {
+        } catch (IOException | ApplicationException e) {
             logger.log(Level.WARNING, "can't run ps!", e);
         }
         
@@ -95,7 +96,7 @@
         return new BufferedReader(isr);
     }
     
-    public Process createAndRunProcess(List<String> args) throws IOException {
+    public Process createAndRunProcess(List<String> args) throws IOException, ApplicationException {
         LoggedExternalProcess process = new LoggedExternalProcess(args);
         return process.runAndReturnProcess();
     }
--- a/common/src/test/java/com/redhat/thermostat/cli/LauncherTest.java	Fri Apr 20 16:23:14 2012 +0200
+++ b/common/src/test/java/com/redhat/thermostat/cli/LauncherTest.java	Fri Apr 20 18:39:24 2012 +0200
@@ -121,9 +121,45 @@
     }
 
     @Test
+    public void testMainBadCommand1() {
+        String expected = "list of commands:\n\n"
+            + " help          show help for a given command or help overview\n"
+            + " test1         description 1\n"
+            + " test2         description 2\n";
+        runAndVerifyCommand(new String[] {"--help"}, expected);
+    }
+
+    @Test
+    public void testMainBadCommand2() {
+        String expected = "list of commands:\n\n"
+            + " help          show help for a given command or help overview\n"
+            + " test1         description 1\n"
+            + " test2         description 2\n";
+        runAndVerifyCommand(new String[] {"-help"}, expected);
+    }
+
+    @Test
+    public void testMainBadCommand3() {
+        String expected = "list of commands:\n\n"
+            + " help          show help for a given command or help overview\n"
+            + " test1         description 1\n"
+            + " test2         description 2\n";
+        runAndVerifyCommand(new String[] {"foobarbaz"}, expected);
+    }
+
+    @Test
+    public void testMainBadCommand4() {
+        String expected = "list of commands:\n\n"
+            + " help          show help for a given command or help overview\n"
+            + " test1         description 1\n"
+            + " test2         description 2\n";
+        runAndVerifyCommand(new String[] {"foo",  "--bar", "baz"}, expected);
+    }
+
+    @Test
     public void testMainExceptionInCommand() {
         TestCommand errorCmd = new TestCommand("error", new TestCommand.Handle() {
-            
+
             @Override
             public void run(CommandContext ctx) throws CommandException {
                 throw new CommandException("test error");
--- a/tools/src/main/java/com/redhat/thermostat/tools/ThermostatService.java	Fri Apr 20 16:23:14 2012 +0200
+++ b/tools/src/main/java/com/redhat/thermostat/tools/ThermostatService.java	Fri Apr 20 18:39:24 2012 +0200
@@ -135,6 +135,10 @@
                     notifier.fireAction(ApplicationState.FAIL);
                 }
                 break;
+            case FAIL:
+                System.err.println("error starting db");
+                notifier.fireAction(ApplicationState.FAIL);
+                break;
             }
         } else if (actionEvent.getSource().equals(agent)) {
             // agent is running
--- a/tools/src/main/java/com/redhat/thermostat/tools/db/MongoProcessRunner.java	Fri Apr 20 16:23:14 2012 +0200
+++ b/tools/src/main/java/com/redhat/thermostat/tools/db/MongoProcessRunner.java	Fri Apr 20 18:39:24 2012 +0200
@@ -167,7 +167,15 @@
         }
         
         LoggedExternalProcess process = new LoggedExternalProcess(commands);
-        int status = process.runAndReturnResult();
+        int status = -1;
+        try {
+            status = process.runAndReturnResult();
+        } catch (ApplicationException ae) {
+            String message = "can not execute mongo process. is it installed?";
+            display(message);
+            throw ae;
+        }
+
         if (status == 0) {
             pid = getPid();
             if (pid == null) status = -1;
--- a/tools/src/test/java/com/redhat/thermostat/tools/db/DBServiceTest.java	Fri Apr 20 16:23:14 2012 +0200
+++ b/tools/src/test/java/com/redhat/thermostat/tools/db/DBServiceTest.java	Fri Apr 20 18:39:24 2012 +0200
@@ -115,13 +115,18 @@
     @Test
     public void testConfig() throws CommandException {
         SimpleArguments args = new SimpleArguments();
-        args.addArgument("quiet", "--quiet");
-        args.addArgument("start", "--start");
-        args.addArgument("dry-run", "--dry-run");
+        args.addArgument("quiet", null);
+        args.addArgument("start", null);
+        args.addArgument("dryRun", null);
         CommandContext ctx = mock(CommandContext.class);
         when(ctx.getArguments()).thenReturn(args);
 
-        DBService service = new DBService();
+        DBService service = new DBService() {
+            @Override
+            MongoProcessRunner createRunner() {
+                throw new AssertionError("dry run should never create an actual runner");
+            }
+        };
 
         service.run(ctx);
         
@@ -180,7 +185,7 @@
                 case START:
                     result[1] = true;
                     latch.countDown();
-                    break;                    
+                    break;
                 }
             }
         });