# HG changeset patch # User Omair Majid # Date 1359474915 18000 # Node ID 4c4b627b3f9cb83a3cb5d542dc3593593797c169 # Parent 7e5cbd1593654e7e5fa6049fc97d7072975bdbfb Fix killvm-agent's activator Reviewed-by: jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-January/005397.html diff -r 7e5cbd159365 -r 4c4b627b3f9c killvm/agent/pom.xml --- 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 @@ com.redhat.thermostat + thermostat-common-test + ${project.version} + test + + + com.redhat.thermostat thermostat-agent-core ${project.version} diff -r 7e5cbd159365 -r 4c4b627b3f9c killvm/agent/src/main/java/com/redhat/thermostat/killvm/agent/internal/Activator.java --- 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(); } } diff -r 7e5cbd159365 -r 4c4b627b3f9c killvm/agent/src/test/java/com/redhat/thermostat/killvm/agent/internal/ActivatorTest.java --- 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()); + } } -