changeset 1065:ef9be67db1ad

The gui heap dumper should use launcher to run Command Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-April/006312.html
author Omair Majid <omajid@redhat.com>
date Mon, 08 Apr 2013 17:06:45 -0400
parents 653f57559b42
children c8d427902c6b
files vm-heap-analysis/client-core/src/main/java/com/redhat/thermostat/vm/heap/analysis/client/core/internal/HeapDumper.java vm-heap-analysis/client-core/src/test/java/com/redhat/thermostat/vm/heap/analysis/client/core/internal/HeapDumperTest.java
diffstat 2 files changed, 29 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/vm-heap-analysis/client-core/src/main/java/com/redhat/thermostat/vm/heap/analysis/client/core/internal/HeapDumper.java	Mon Apr 08 16:40:39 2013 -0400
+++ b/vm-heap-analysis/client-core/src/main/java/com/redhat/thermostat/vm/heap/analysis/client/core/internal/HeapDumper.java	Mon Apr 08 17:06:45 2013 -0400
@@ -38,47 +38,30 @@
 
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.ServiceReference;
 
-import com.redhat.thermostat.common.cli.Command;
-import com.redhat.thermostat.common.cli.CommandContext;
-import com.redhat.thermostat.common.cli.CommandContextFactory;
+import com.redhat.thermostat.common.Launcher;
 import com.redhat.thermostat.common.cli.CommandException;
-import com.redhat.thermostat.common.cli.SimpleArguments;
 import com.redhat.thermostat.storage.core.VmRef;
 
 public class HeapDumper {
-    
-    private static final String HEAP_DUMP_COMMAND = "dump-heap";
-    private static final String HOST_ID_ARGUMENT = "hostId";
-    private static final String VM_ID_ARGUMENT = "vmId";
-    
-    private Command heapDumpCommand;
-    private CommandContext commandCtx;
-    private VmRef ref;
+
+    private final VmRef ref;
+    private final BundleContext context;
     
     public HeapDumper(VmRef ref) {
-        this.ref = ref;
-        BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext();
-        CommandContextFactory ctxFactory = new CommandContextFactory(context);
-        init(ctxFactory);
+        this(ref, FrameworkUtil.getBundle(HeapDumper.class).getBundleContext());
     }
     
-    HeapDumper(VmRef ref, CommandContextFactory ctxFactory) {
+    HeapDumper(VmRef ref, BundleContext context) {
         this.ref = ref;
-        init(ctxFactory);
-    }
-    
-    private void init(CommandContextFactory ctxFactory) {
-        // Setup heap dump command
-        heapDumpCommand = ctxFactory.getCommandRegistry().getCommand(HEAP_DUMP_COMMAND);
-        SimpleArguments args = new SimpleArguments();
-        args.addArgument(HOST_ID_ARGUMENT, ref.getAgent().getStringID());
-        args.addArgument(VM_ID_ARGUMENT, ref.getStringID());
-        commandCtx = ctxFactory.createContext(args);
+        this.context = context;
     }
     
     public void dump() throws CommandException {
-        heapDumpCommand.run(commandCtx);
+        ServiceReference launcherRef = context.getServiceReference(Launcher.class.getName());
+        Launcher launcher = (Launcher) context.getService(launcherRef);
+        launcher.run(new String[] { "dump-heap", "--hostId", ref.getAgent().getStringID(), "--vmId", ref.getStringID()}, true);
     }
 
 }
--- a/vm-heap-analysis/client-core/src/test/java/com/redhat/thermostat/vm/heap/analysis/client/core/internal/HeapDumperTest.java	Mon Apr 08 16:40:39 2013 -0400
+++ b/vm-heap-analysis/client-core/src/test/java/com/redhat/thermostat/vm/heap/analysis/client/core/internal/HeapDumperTest.java	Mon Apr 08 17:06:45 2013 -0400
@@ -36,66 +36,53 @@
 
 package com.redhat.thermostat.vm.heap.analysis.client.core.internal;
 
-import static org.junit.Assert.assertEquals;
-import static org.mockito.Matchers.any;
+import static org.junit.Assert.assertArrayEquals;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
 
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
 
-import com.redhat.thermostat.common.cli.Arguments;
-import com.redhat.thermostat.common.cli.Command;
-import com.redhat.thermostat.common.cli.CommandContext;
-import com.redhat.thermostat.common.cli.CommandContextFactory;
+import com.redhat.thermostat.common.Launcher;
 import com.redhat.thermostat.common.cli.CommandException;
-import com.redhat.thermostat.common.cli.CommandRegistry;
 import com.redhat.thermostat.storage.core.HostRef;
 import com.redhat.thermostat.storage.core.VmRef;
+import com.redhat.thermostat.testutils.StubBundleContext;
 
 public class HeapDumperTest {
     private static final String TEST_HOST_ID = "1111111";
     private static final int TEST_VM_ID = 2222222;
 
-    private Command command;
     private HeapDumper dumper;
-    private CommandContextFactory ctxFactory;
-    private CommandRegistry reg;
+
+    private StubBundleContext bundleContext;
+    private Launcher launcher;
     
     @Before
     public void setUp() throws Exception {
-        command = mock(Command.class);
-        ctxFactory = mock(CommandContextFactory.class);
-        reg = mock(CommandRegistry.class);
-        CommandContext ctx = mock(CommandContext.class);
+        bundleContext = new StubBundleContext();
         
-        when(reg.getCommand(any(String.class))).thenReturn(command);
-        when(ctxFactory.getCommandRegistry()).thenReturn(reg);
-        when(ctxFactory.createContext(any(Arguments.class))).thenReturn(ctx);
+        launcher = mock(Launcher.class);
+        bundleContext.registerService(Launcher.class, launcher, null);
         
         HostRef hostRef = new HostRef(TEST_HOST_ID, "myHost");
         VmRef ref = new VmRef(hostRef, TEST_VM_ID, "myVM");
-        dumper = new HeapDumper(ref, ctxFactory);
+        dumper = new HeapDumper(ref, bundleContext);
     }
 
     @Test
-    public void testCommand() {
-        verify(reg).getCommand(eq("dump-heap"));
-    }
-    
-    @Test
     public void testDump() throws CommandException {
         dumper.dump();
-        
-        ArgumentCaptor<Arguments> captor = ArgumentCaptor.forClass(Arguments.class);
-        verify(ctxFactory).createContext(captor.capture());
-        Arguments args = captor.getValue();
-        
-        assertEquals(TEST_HOST_ID, args.getArgument("hostId"));
-        assertEquals(String.valueOf(TEST_VM_ID), args.getArgument("vmId"));
+
+        ArgumentCaptor<String[]> captor = ArgumentCaptor.forClass(String[].class);
+        verify(launcher).run(captor.capture(), eq(true));
+        verifyNoMoreInteractions(launcher);
+
+        String[] args = captor.getValue();
+        assertArrayEquals(new String[] { "dump-heap", "--hostId", TEST_HOST_ID, "--vmId", String.valueOf(TEST_VM_ID) }, args);
     }
 
 }