Repository: aries-rsa Updated Branches: refs/heads/master d6b354daf -> f1edbeeda
[ARIES-1763] Simplify EventProducerTest Project: http://git-wip-us.apache.org/repos/asf/aries-rsa/repo Commit: http://git-wip-us.apache.org/repos/asf/aries-rsa/commit/f1edbeed Tree: http://git-wip-us.apache.org/repos/asf/aries-rsa/tree/f1edbeed Diff: http://git-wip-us.apache.org/repos/asf/aries-rsa/diff/f1edbeed Branch: refs/heads/master Commit: f1edbeedaf2d176692d7d67d648a5dd97dabcf38 Parents: d6b354d Author: Christian Schneider <[email protected]> Authored: Wed Jan 31 12:42:05 2018 +0100 Committer: Christian Schneider <[email protected]> Committed: Wed Jan 31 12:42:05 2018 +0100 ---------------------------------------------------------------------- .../aries/rsa/core/event/EventProducerTest.java | 194 +++++++++---------- 1 file changed, 91 insertions(+), 103 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/f1edbeed/rsa/src/test/java/org/apache/aries/rsa/core/event/EventProducerTest.java ---------------------------------------------------------------------- diff --git a/rsa/src/test/java/org/apache/aries/rsa/core/event/EventProducerTest.java b/rsa/src/test/java/org/apache/aries/rsa/core/event/EventProducerTest.java index 6bb693e..5703580 100644 --- a/rsa/src/test/java/org/apache/aries/rsa/core/event/EventProducerTest.java +++ b/rsa/src/test/java/org/apache/aries/rsa/core/event/EventProducerTest.java @@ -18,149 +18,137 @@ */ package org.apache.aries.rsa.core.event; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.expectLastCall; + import java.util.Arrays; import java.util.Dictionary; +import java.util.HashMap; import java.util.Hashtable; -import java.util.List; +import java.util.Map; import java.util.UUID; import org.apache.aries.rsa.core.ExportRegistrationImpl; import org.apache.aries.rsa.core.RemoteServiceAdminCore; -import org.apache.aries.rsa.core.event.EventProducer; import org.apache.aries.rsa.spi.Endpoint; +import org.easymock.Capture; import org.easymock.EasyMock; -import org.easymock.IAnswer; +import org.easymock.IMocksControl; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; +import org.osgi.framework.Constants; +import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceReference; import org.osgi.service.remoteserviceadmin.EndpointDescription; import org.osgi.service.remoteserviceadmin.ExportReference; import org.osgi.service.remoteserviceadmin.ExportRegistration; +import org.osgi.service.remoteserviceadmin.RemoteConstants; import org.osgi.service.remoteserviceadmin.RemoteServiceAdminEvent; import org.osgi.service.remoteserviceadmin.RemoteServiceAdminListener; @SuppressWarnings({"rawtypes", "unchecked"}) public class EventProducerTest { + private IMocksControl c; + private RemoteServiceAdminCore rsaCore; + private Capture<RemoteServiceAdminEvent> capturedEvent; + private Bundle bundle; + private BundleContext bc; + + @Before + public void before() throws InvalidSyntaxException { + c = EasyMock.createNiceControl(); + rsaCore = c.createMock(RemoteServiceAdminCore.class); + capturedEvent = EasyMock.newCapture(); + bundle = createBundle(); + bc = bundleContextWithRsal(bundle); + } @Test public void testPublishNotification() throws Exception { - RemoteServiceAdminCore rsaCore = EasyMock.createNiceMock(RemoteServiceAdminCore.class); - EasyMock.replay(rsaCore); + final EndpointDescription epd = dummyEndpoint(); + Endpoint endpoint = c.createMock(Endpoint.class); + expect(endpoint.description()).andReturn(epd); - final EndpointDescription epd = EasyMock.createNiceMock(EndpointDescription.class); - EasyMock.expect(epd.getServiceId()).andReturn(Long.MAX_VALUE).anyTimes(); - final String uuid = UUID.randomUUID().toString(); - EasyMock.expect(epd.getFrameworkUUID()).andReturn(uuid).anyTimes(); - EasyMock.expect(epd.getId()).andReturn("foo://bar").anyTimes(); - final List<String> interfaces = Arrays.asList("org.foo.Bar", "org.boo.Far"); - EasyMock.expect(epd.getInterfaces()).andReturn(interfaces).anyTimes(); - EasyMock.expect(epd.getConfigurationTypes()).andReturn(Arrays.asList("org.apache.cxf.ws")).anyTimes(); - EasyMock.replay(epd); - final ServiceReference sref = EasyMock.createNiceMock(ServiceReference.class); - EasyMock.replay(sref); - - final Bundle bundle = EasyMock.createNiceMock(Bundle.class); - EasyMock.expect(bundle.getBundleId()).andReturn(42L).anyTimes(); - EasyMock.expect(bundle.getSymbolicName()).andReturn("test.bundle").anyTimes(); - Dictionary<String, String> headers = new Hashtable<String, String>(); - headers.put("Bundle-Version", "1.2.3.test"); - EasyMock.expect(bundle.getHeaders()).andReturn(headers).anyTimes(); - EasyMock.replay(bundle); - - RemoteServiceAdminListener rsal = EasyMock.createNiceMock(RemoteServiceAdminListener.class); - rsal.remoteAdminEvent((RemoteServiceAdminEvent) EasyMock.anyObject()); - EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { - @Override - public Object answer() throws Throwable { - RemoteServiceAdminEvent rsae = (RemoteServiceAdminEvent) EasyMock.getCurrentArguments()[0]; - Assert.assertNull(rsae.getException()); - Assert.assertEquals(RemoteServiceAdminEvent.EXPORT_REGISTRATION, rsae.getType()); - Assert.assertSame(bundle, rsae.getSource()); - ExportReference er = rsae.getExportReference(); - Assert.assertSame(epd, er.getExportedEndpoint()); - Assert.assertSame(sref, er.getExportedService()); - - return null; - } - }); - EasyMock.replay(rsal); - - ServiceReference rsalSref = EasyMock.createNiceMock(ServiceReference.class); - EasyMock.expect(rsalSref.getBundle()).andReturn(bundle).anyTimes(); - EasyMock.replay(rsalSref); - - BundleContext bc = EasyMock.createNiceMock(BundleContext.class); - EasyMock.expect(bc.getBundle()).andReturn(bundle).anyTimes(); - EasyMock.expect(bc.getServiceReferences(RemoteServiceAdminListener.class.getName(), null)) - .andReturn(new ServiceReference[] {rsalSref}).anyTimes(); - EasyMock.expect(bc.getService(rsalSref)).andReturn(rsal).anyTimes(); - Endpoint endpoint = EasyMock.mock(Endpoint.class); - EasyMock.expect(endpoint.description()).andReturn(epd); - EasyMock.replay(endpoint); - EasyMock.replay(bc); - EventProducer eventProducer = new EventProducer(bc); + final ServiceReference sref = c.createMock(ServiceReference.class); + c.replay(); + + EventProducer eventProducer = new EventProducer(bc); ExportRegistrationImpl ereg = new ExportRegistrationImpl(sref, endpoint, rsaCore, eventProducer); eventProducer.publishNotification(ereg); - EasyMock.verify(rsaCore, sref, bundle, rsal, rsalSref, bc); + RemoteServiceAdminEvent rsae = capturedEvent.getValue(); + Assert.assertNull(rsae.getException()); + Assert.assertEquals(RemoteServiceAdminEvent.EXPORT_REGISTRATION, rsae.getType()); + Assert.assertSame(bundle, rsae.getSource()); + ExportReference er = rsae.getExportReference(); + Assert.assertSame(epd, er.getExportedEndpoint()); + Assert.assertSame(sref, er.getExportedService()); + + c.verify(); } @Test public void testPublishErrorNotification() throws Exception { - RemoteServiceAdminCore rsaCore = EasyMock.createNiceMock(RemoteServiceAdminCore.class); - EasyMock.replay(rsaCore); + c.replay(); + + EventProducer eventProducer = new EventProducer(bc); + final Exception exportException = new Exception(); + ExportRegistrationImpl ereg = new ExportRegistrationImpl(rsaCore, eventProducer, exportException); + eventProducer.publishNotification(Arrays.<ExportRegistration>asList(ereg)); - final EndpointDescription endpoint = EasyMock.createNiceMock(EndpointDescription.class); - EasyMock.expect(endpoint.getInterfaces()).andReturn(Arrays.asList("org.foo.Bar")).anyTimes(); - EasyMock.replay(endpoint); - final ServiceReference sref = EasyMock.createNiceMock(ServiceReference.class); - EasyMock.replay(sref); + RemoteServiceAdminEvent rsae = capturedEvent.getValue(); + Assert.assertSame(exportException, rsae.getException()); + Assert.assertEquals(RemoteServiceAdminEvent.EXPORT_ERROR, rsae.getType()); + Assert.assertSame(bundle, rsae.getSource()); + Assert.assertNull(rsae.getImportReference()); + Assert.assertNull(rsae.getExportReference()); - final Bundle bundle = EasyMock.createNiceMock(Bundle.class); - EasyMock.expect(bundle.getBundleId()).andReturn(42L).anyTimes(); - EasyMock.expect(bundle.getSymbolicName()).andReturn("test.bundle").anyTimes(); - EasyMock.expect(bundle.getHeaders()).andReturn(new Hashtable<String, String>()).anyTimes(); - EasyMock.replay(bundle); + c.verify(); + } - final Exception exportException = new Exception(); + private Bundle createBundle() { + final Bundle bundle = c.createMock(Bundle.class); + expect(bundle.getBundleId()).andReturn(42L).anyTimes(); + expect(bundle.getSymbolicName()).andReturn("test.bundle").anyTimes(); + Dictionary<String, String> headers = new Hashtable<String, String>(); + headers.put("Bundle-Version", "1.2.3.test"); + expect(bundle.getHeaders()).andReturn(headers).anyTimes(); + return bundle; + } - RemoteServiceAdminListener rsal = EasyMock.createNiceMock(RemoteServiceAdminListener.class); - rsal.remoteAdminEvent((RemoteServiceAdminEvent) EasyMock.anyObject()); - EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { - @Override - public Object answer() throws Throwable { - RemoteServiceAdminEvent rsae = (RemoteServiceAdminEvent) EasyMock.getCurrentArguments()[0]; - Assert.assertSame(exportException, rsae.getException()); - Assert.assertEquals(RemoteServiceAdminEvent.EXPORT_ERROR, rsae.getType()); - Assert.assertSame(bundle, rsae.getSource()); - Assert.assertNull(rsae.getImportReference()); - Assert.assertNull(rsae.getExportReference()); - - return null; - } - }); - EasyMock.replay(rsal); - - ServiceReference rsalSref = EasyMock.createNiceMock(ServiceReference.class); - EasyMock.expect(rsalSref.getBundle()).andReturn(bundle).anyTimes(); - EasyMock.replay(rsalSref); - - BundleContext bc = EasyMock.createNiceMock(BundleContext.class); - - EasyMock.expect(bc.getBundle()).andReturn(bundle).anyTimes(); - EasyMock.expect(bc.getServiceReferences(RemoteServiceAdminListener.class.getName(), null)) - .andReturn(new ServiceReference[] {rsalSref}).anyTimes(); - EasyMock.expect(bc.getService(rsalSref)).andReturn(rsal).anyTimes(); - EasyMock.replay(bc); - EventProducer eventProducer = new EventProducer(bc); + private BundleContext bundleContextWithRsal(Bundle bundle) + throws InvalidSyntaxException { + + RemoteServiceAdminListener rsal = c.createMock(RemoteServiceAdminListener.class); + rsal.remoteAdminEvent(EasyMock.capture(capturedEvent)); + expectLastCall().atLeastOnce(); + + ServiceReference rsalSref = c.createMock(ServiceReference.class); + expect(rsalSref.getBundle()).andReturn(bundle).anyTimes(); - ExportRegistrationImpl ereg = new ExportRegistrationImpl(rsaCore, eventProducer, exportException); - eventProducer.publishNotification(Arrays.<ExportRegistration>asList(ereg)); + BundleContext bc = c.createMock(BundleContext.class); - EasyMock.verify(rsaCore, sref, bundle, rsal, rsalSref, bc); + expect(bc.getBundle()).andReturn(bundle).anyTimes(); + expect(bc.getServiceReferences(RemoteServiceAdminListener.class.getName(), null)) + .andReturn(new ServiceReference[] {rsalSref}).anyTimes(); + expect(bc.getService(rsalSref)).andReturn(rsal).anyTimes(); + return bc; + } + + private EndpointDescription dummyEndpoint() { + final String uuid = UUID.randomUUID().toString(); + Map<String, Object> props = new HashMap<>(); + props.put(RemoteConstants.ENDPOINT_SERVICE_ID, Long.MAX_VALUE); + props.put(RemoteConstants.ENDPOINT_FRAMEWORK_UUID, uuid); + props.put(RemoteConstants.ENDPOINT_ID, "foo://bar"); + props.put(Constants.OBJECTCLASS, new String[] {"org.foo.Bar", "org.boo.Far"}); + props.put(RemoteConstants.SERVICE_IMPORTED_CONFIGS, new String[] {"org.apache.cxf.ws"}); + final EndpointDescription epd = new EndpointDescription(props); + return epd; } }
