changeset 943:4c4b627b3f9c

Fix killvm-agent's activator Reviewed-by: jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-January/005397.html
author Omair Majid <omajid@redhat.com>
date Tue, 29 Jan 2013 10:55:15 -0500
parents 7e5cbd159365
children 4aa310fa7589
files killvm/agent/pom.xml killvm/agent/src/main/java/com/redhat/thermostat/killvm/agent/internal/Activator.java killvm/agent/src/test/java/com/redhat/thermostat/killvm/agent/internal/ActivatorTest.java
diffstat 3 files changed, 53 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/killvm/agent/pom.xml	Tue Jan 29 10:24:50 2013 -0500
+++ b/killvm/agent/pom.xml	Tue Jan 29 10:55:15 2013 -0500
@@ -73,6 +73,12 @@
     </dependency>
     <dependency>
       <groupId>com.redhat.thermostat</groupId>
+      <artifactId>thermostat-common-test</artifactId>
+      <version>${project.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>com.redhat.thermostat</groupId>
       <artifactId>thermostat-agent-core</artifactId>
       <version>${project.version}</version>
     </dependency>
--- a/killvm/agent/src/main/java/com/redhat/thermostat/killvm/agent/internal/Activator.java	Tue Jan 29 10:24:50 2013 -0500
+++ b/killvm/agent/src/main/java/com/redhat/thermostat/killvm/agent/internal/Activator.java	Tue Jan 29 10:55:15 2013 -0500
@@ -38,28 +38,42 @@
 
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.util.tracker.ServiceTracker;
 
 import com.redhat.thermostat.agent.command.ReceiverRegistry;
-import com.redhat.thermostat.common.utils.OSGIUtils;
 import com.redhat.thermostat.service.process.UNIXProcessHandler;
 
 public class Activator implements BundleActivator {
 
     private ReceiverRegistry registry;
-    private UNIXProcessHandler unixService;
+    private ServiceTracker killActionTracker;
 
     @Override
-    public void start(final BundleContext context) throws Exception {
-        unixService = OSGIUtils.getInstance().getService(UNIXProcessHandler.class);
+    public void start(final BundleContext context) {
         registry = new ReceiverRegistry(context);
-        registry.registerReceiver(new KillVmReceiver(unixService));
+
+        killActionTracker = new ServiceTracker(context, UNIXProcessHandler.class, null) {
+            @Override
+            public Object addingService(ServiceReference reference) {
+                UNIXProcessHandler processHandler = (UNIXProcessHandler) super.addingService(reference);
+                registry.registerReceiver(new KillVmReceiver(processHandler));
+                return processHandler;
+            }
+
+            @Override
+            public void removedService(ServiceReference reference, Object service) {
+                registry.unregisterReceivers();
+                super.removedService(reference, service);
+            }
+        };
+
+        killActionTracker.open();
     }
 
     @Override
-    public void stop(BundleContext context) throws Exception {
-        // This only unregisters receivers which we've registered
-        // in start()
-        registry.unregisterReceivers();
+    public void stop(BundleContext context) {
+        killActionTracker.close();
     }
 }
 
--- a/killvm/agent/src/test/java/com/redhat/thermostat/killvm/agent/internal/ActivatorTest.java	Tue Jan 29 10:24:50 2013 -0500
+++ b/killvm/agent/src/test/java/com/redhat/thermostat/killvm/agent/internal/ActivatorTest.java	Tue Jan 29 10:55:15 2013 -0500
@@ -36,55 +36,46 @@
 
 package com.redhat.thermostat.killvm.agent.internal;
 
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyString;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Matchers.isA;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-import java.util.Dictionary;
 
 import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceRegistration;
-import org.powermock.api.mockito.PowerMockito;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
 
 import com.redhat.thermostat.agent.command.RequestReceiver;
-import com.redhat.thermostat.common.utils.OSGIUtils;
-import com.redhat.thermostat.killvm.agent.internal.Activator;
-import com.redhat.thermostat.killvm.agent.internal.KillVmReceiver;
 import com.redhat.thermostat.service.process.UNIXProcessHandler;
+import com.redhat.thermostat.testutils.StubBundleContext;
 
-@RunWith(PowerMockRunner.class)
-@PrepareForTest(OSGIUtils.class)
 public class ActivatorTest {
 
     /**
      * Makes sure receiver is registered and unix service gets set.
-     * 
-     * @throws Exception
      */
-    @SuppressWarnings({ "rawtypes", "unchecked" })
     @Test
-    public void startStopTest() throws Exception {
-        OSGIUtils utils = mock(OSGIUtils.class);
-        PowerMockito.mockStatic(OSGIUtils.class);
-        when(OSGIUtils.getInstance()).thenReturn(utils);
-        BundleContext ctx = mock(BundleContext.class);
-        ServiceRegistration serviceReg = mock(ServiceRegistration.class);
-        when(ctx.registerService(anyString(), any(), any(Dictionary.class))).thenReturn(serviceReg);
+    public void verifyKillReciverIsNotRegisteredWithoutDependencies() {
+        StubBundleContext ctx = new StubBundleContext();
         Activator activator = new Activator();
         activator.start(ctx);
-        verify(utils).getService(UNIXProcessHandler.class);
-        verify(ctx).registerService(eq(RequestReceiver.class.getName()), isA(KillVmReceiver.class), any(Dictionary.class));
+
+        assertEquals(0, ctx.getAllServices().size());
+
         activator.stop(ctx);
-        verify(serviceReg).unregister();
     }
 
+    @Test
+    public void verifyKillReciverIsRegistered() {
+        StubBundleContext ctx = new StubBundleContext();
+
+        ctx.registerService(UNIXProcessHandler.class, mock(UNIXProcessHandler.class), null);
+
+        Activator activator = new Activator();
+        activator.start(ctx);
+
+        assertEquals(2, ctx.getAllServices().size());
+        assertTrue(ctx.isServiceRegistered(RequestReceiver.class.getName(), KillVmReceiver.class));
+
+        activator.stop(ctx);
+
+        assertEquals(1, ctx.getAllServices().size());
+    }
 }
-