changeset 1656:4414429cfe31

Switch to logging in ServiceCommand. Reviewed-by: omajid Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2015-March/012958.html PR2261
author Severin Gehwolf <sgehwolf@redhat.com>
date Mon, 09 Mar 2015 19:53:53 +0100
parents 8071943ac3b7
children 331e21088194
files agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/ServiceCommand.java agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/locale/LocaleResources.java agent/cli/src/main/resources/com/redhat/thermostat/agent/cli/impl/strings.properties agent/cli/src/test/java/com/redhat/thermostat/agent/cli/impl/ServiceCommandTest.java
diffstat 4 files changed, 45 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/ServiceCommand.java	Tue Mar 10 00:20:38 2015 -0600
+++ b/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/ServiceCommand.java	Mon Mar 09 19:53:53 2015 +0100
@@ -39,6 +39,8 @@
 import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.Semaphore;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
@@ -51,6 +53,7 @@
 import com.redhat.thermostat.common.cli.CommandContext;
 import com.redhat.thermostat.common.cli.CommandException;
 import com.redhat.thermostat.common.tools.ApplicationState;
+import com.redhat.thermostat.common.utils.LoggingUtils;
 import com.redhat.thermostat.launcher.Launcher;
 import com.redhat.thermostat.shared.locale.Translate;
 
@@ -61,6 +64,7 @@
 public class ServiceCommand extends AbstractCommand implements ActionListener<ApplicationState> {
     
     private static final Translate<LocaleResources> translator = LocaleResources.createLocalizer();
+    private static final Logger logger = LoggingUtils.getLogger(ServiceCommand.class);
 
     private List<ActionListener<ApplicationState>> listeners;
     private Semaphore agentBarrier = new Semaphore(0);
@@ -117,7 +121,7 @@
                     }
                     String dbUrl = (String) payload;
                     String[] agentArgs =  new String[] {"agent", "-d", dbUrl};
-                    cmdCtx.getConsole().getError().println(translator.localize(LocaleResources.STARTING_AGENT).getContents());
+                    logger.fine("starting agent now...");
                     launcher.run(agentArgs, false);
                     break;
                 case FAIL:
@@ -129,6 +133,7 @@
                     }
                     Exception ex = (Exception) payload;
                     cmdCtx.getConsole().getError().println(ex.getMessage());
+                    logger.log(Level.WARNING, ex.getMessage(), ex);
                     break;
                 }
             } catch (CommandException e) {
--- a/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/locale/LocaleResources.java	Tue Mar 10 00:20:38 2015 -0600
+++ b/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/locale/LocaleResources.java	Mon Mar 09 19:53:53 2015 +0100
@@ -40,8 +40,6 @@
 
 public enum LocaleResources {
 
-    STARTING_AGENT,
-
     SERVICE_FAILED_TO_START_DB,
     LAUNCHER_UNAVAILABLE,
     UNEXPECTED_RESULT_STORAGE,
--- a/agent/cli/src/main/resources/com/redhat/thermostat/agent/cli/impl/strings.properties	Tue Mar 10 00:20:38 2015 -0600
+++ b/agent/cli/src/main/resources/com/redhat/thermostat/agent/cli/impl/strings.properties	Mon Mar 09 19:53:53 2015 +0100
@@ -1,5 +1,3 @@
-STARTING_AGENT = starting agent now...
-
 SERVICE_FAILED_TO_START_DB = Service failed to start due to error starting storage.
 LAUNCHER_UNAVAILABLE = Launcher is not available
 UNEXPECTED_RESULT_STORAGE = Unexpected result from storage.
--- a/agent/cli/src/test/java/com/redhat/thermostat/agent/cli/impl/ServiceCommandTest.java	Tue Mar 10 00:20:38 2015 -0600
+++ b/agent/cli/src/test/java/com/redhat/thermostat/agent/cli/impl/ServiceCommandTest.java	Mon Mar 09 19:53:53 2015 +0100
@@ -46,6 +46,7 @@
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
+import java.io.ByteArrayOutputStream;
 import java.io.PrintStream;
 import java.util.Collection;
 
@@ -69,6 +70,7 @@
 
 public class ServiceCommandTest {
 
+    private ByteArrayOutputStream stdErrOut;
     private Launcher mockLauncher;
     private ServiceCommand serviceCommand;
     private CommandContext mockCommandContext;
@@ -95,7 +97,8 @@
         when(mockActionEvent.getSource()).thenReturn(mockStorageCommand);
         mockCommandContext = mock(CommandContext.class);
         Console console = mock(Console.class);
-        PrintStream err = mock(PrintStream.class);
+        stdErrOut = new ByteArrayOutputStream();
+        PrintStream err = new PrintStream(stdErrOut);
         when(console.getError()).thenReturn(err);
         when(mockCommandContext.getConsole()).thenReturn(console);
         
@@ -183,6 +186,7 @@
             Assert.assertEquals(e.getLocalizedMessage(), "Service failed to start due to error starting storage.");
         }
         Assert.assertTrue(exTriggered);
+        Assert.assertEquals("Test Exception\n", stdErrOut.toString());
         
         verify(mockLauncher, times(2)).run(eq(STORAGE_START_ARGS), isA(Collection.class), anyBoolean());
         verify(mockLauncher, times(1)).run(eq(STORAGE_STOP_ARGS), anyBoolean());
@@ -215,6 +219,40 @@
             exTriggered = true;
         }
         Assert.assertTrue(exTriggered);
+        Assert.assertEquals("Test Exception\n", stdErrOut.toString());
+        
+        verify(mockLauncher, times(1)).run(eq(STORAGE_START_ARGS), isA(Collection.class), anyBoolean());
+        verify(mockLauncher, never()).run(eq(STORAGE_STOP_ARGS), anyBoolean());
+        verify(mockLauncher, never()).run(eq(AGENT_ARGS), anyBoolean());
+        verify(mockActionEvent, times(1)).getActionId();
+    }
+    
+    @Test(timeout=1000)
+    public void testStorageFailStartUnknown()  throws CommandException {
+        doAnswer(new Answer<Void>() {
+            public Void answer(InvocationOnMock invocation) throws Throwable {
+                Object[] args = invocation.getArguments();
+                listeners = (Collection<ActionListener<ApplicationState>>)args[1];
+                
+                when(mockActionEvent.getActionId()).thenReturn(ApplicationState.FAIL);
+                // Return a null payload in order to trigger unknown path
+                when(mockActionEvent.getPayload()).thenReturn(null);
+                
+                for(ActionListener<ApplicationState> listener : listeners) {
+                    listener.actionPerformed(mockActionEvent);
+                }
+                return null;
+            }
+        }).when(mockLauncher).run(eq(STORAGE_START_ARGS), isA(Collection.class), anyBoolean());
+        
+        boolean exTriggered = false;
+        try {
+            serviceCommand.run(mockCommandContext);
+        } catch (CommandException e) {
+            exTriggered = true;
+        }
+        Assert.assertTrue(exTriggered);
+        Assert.assertEquals("Unexpected result from storage.\n", stdErrOut.toString());
         
         verify(mockLauncher, times(1)).run(eq(STORAGE_START_ARGS), isA(Collection.class), anyBoolean());
         verify(mockLauncher, never()).run(eq(STORAGE_STOP_ARGS), anyBoolean());