You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by da...@apache.org on 2013/02/07 22:58:15 UTC

svn commit: r1443748 - in /cxf/dosgi/trunk/dsw/cxf-dsw/src: main/java/org/apache/cxf/dosgi/dsw/service/EventAdminHelper.java test/java/org/apache/cxf/dosgi/dsw/service/RemoteServiceAdminCoreTest.java

Author: davidb
Date: Thu Feb  7 21:58:15 2013
New Revision: 1443748

URL: http://svn.apache.org/r1443748
Log:
Additional unit tests for RemoteServiceAdminCore class.

Modified:
    cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/service/EventAdminHelper.java
    cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/RemoteServiceAdminCoreTest.java

Modified: cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/service/EventAdminHelper.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/service/EventAdminHelper.java?rev=1443748&r1=1443747&r2=1443748&view=diff
==============================================================================
--- cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/service/EventAdminHelper.java (original)
+++ cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/service/EventAdminHelper.java Thu Feb  7 21:58:15 2013
@@ -35,7 +35,7 @@ import org.slf4j.LoggerFactory;
 public class EventAdminHelper {
 
     private static final Logger LOG = LoggerFactory.getLogger(EventAdminHelper.class);
-    
+
     private BundleContext bctx;
 
     public EventAdminHelper(BundleContext bc) {
@@ -49,10 +49,9 @@ public class EventAdminHelper {
         props.put("bundle", bctx.getBundle());
         props.put("bundle.id", bctx.getBundle().getBundleId());
         props.put("bundle.symbolicname", bctx.getBundle().getSymbolicName());
-        // FIXME is this correct?
-        
+
         String version = (String)bctx.getBundle().getHeaders().get("Bundle-Version");
-        
+
         Version v;
         if (version != null) {
             v = new Version(version);
@@ -71,7 +70,7 @@ public class EventAdminHelper {
         Properties props = new Properties();
         setIfNotNull(props, "cause", rsae.getException());
 
-        
+
         EndpointDescription epd = null;
         if (rsae.getImportReference() != null) {
             epd = ((ImportReferenceImpl)rsae.getImportReference()).getImportedEndpointAlways();

Modified: cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/RemoteServiceAdminCoreTest.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/RemoteServiceAdminCoreTest.java?rev=1443748&r1=1443747&r2=1443748&view=diff
==============================================================================
--- cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/RemoteServiceAdminCoreTest.java (original)
+++ cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/service/RemoteServiceAdminCoreTest.java Thu Feb  7 21:58:15 2013
@@ -18,41 +18,49 @@
  */
 package org.apache.cxf.dosgi.dsw.service;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.Dictionary;
 import java.util.HashMap;
+import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 
 import org.apache.cxf.dosgi.dsw.handlers.ConfigTypeHandlerFactory;
 import org.apache.cxf.dosgi.dsw.handlers.ConfigurationTypeHandler;
+import org.apache.cxf.dosgi.dsw.handlers.ExportResult;
 import org.apache.cxf.dosgi.dsw.handlers.HttpServiceManager;
 import org.apache.cxf.dosgi.dsw.qos.DefaultIntentMapFactory;
 import org.apache.cxf.dosgi.dsw.qos.IntentManager;
 import org.apache.cxf.dosgi.dsw.qos.IntentManagerImpl;
 import org.apache.cxf.dosgi.dsw.qos.IntentMap;
+import org.apache.cxf.endpoint.Server;
+import org.easymock.IAnswer;
 import org.easymock.IMocksControl;
 import org.easymock.classextension.EasyMock;
 import org.junit.Test;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
+import org.osgi.framework.FrameworkUtil;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.remoteserviceadmin.EndpointDescription;
 import org.osgi.service.remoteserviceadmin.ExportRegistration;
 import org.osgi.service.remoteserviceadmin.ImportRegistration;
 import org.osgi.service.remoteserviceadmin.RemoteConstants;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-
 
 public class RemoteServiceAdminCoreTest {
 
     @Test
-    public void dontExportOwnServiceProxies() {
-
+    public void testDontExportOwnServiceProxies() {
         IMocksControl c = EasyMock.createControl();
         Bundle b = c.createMock(Bundle.class);
         BundleContext bc = c.createMock(BundleContext.class);
@@ -64,7 +72,10 @@ public class RemoteServiceAdminCoreTest 
 
         ServiceReference sref = c.createMock(ServiceReference.class);
         EasyMock.expect(sref.getBundle()).andReturn(b).anyTimes();
-        EasyMock.expect(sref.getPropertyKeys()).andReturn(new String[]{}).anyTimes();
+        EasyMock.expect(sref.getPropertyKeys()).andReturn(new String[]{
+                "objectClass", "service.exported.interfaces"}).anyTimes();
+        EasyMock.expect(sref.getProperty("objectClass")).andReturn(new String [] {"a.b.C"}).anyTimes();
+        EasyMock.expect(sref.getProperty("service.exported.interfaces")).andReturn("*").anyTimes();
 
         ConfigTypeHandlerFactory configTypeHandlerFactory = c.createMock(ConfigTypeHandlerFactory.class);
         RemoteServiceAdminCore rsaCore = new RemoteServiceAdminCore(bc, configTypeHandlerFactory);
@@ -81,7 +92,6 @@ public class RemoteServiceAdminCoreTest 
         assertEquals(rsaCore.getExportedServices().size(), 0);
 
         c.verify();
-
     }
 
     @Test
@@ -96,7 +106,7 @@ public class RemoteServiceAdminCoreTest 
 
         EasyMock.expect(bc.getBundle()).andReturn(b).anyTimes();
         EasyMock.expect(b.getSymbolicName()).andReturn("BundleName").anyTimes();
-        
+
         Map<String, Object> p = new HashMap<String, Object>();
         p.put(RemoteConstants.ENDPOINT_ID, "http://google.de");
         p.put(Constants.OBJECTCLASS, new String[] {
@@ -107,7 +117,7 @@ public class RemoteServiceAdminCoreTest 
         IntentMap intentMap = new IntentMap(new DefaultIntentMapFactory().create());
         IntentManager intentManager = new IntentManagerImpl(intentMap, 10000);
         HttpServiceManager httpServiceManager = c.createMock(HttpServiceManager.class);
-        ConfigTypeHandlerFactory configTypeHandlerFactory 
+        ConfigTypeHandlerFactory configTypeHandlerFactory
             = new ConfigTypeHandlerFactory(bc, intentManager, httpServiceManager);
         RemoteServiceAdminCore rsaCore = new RemoteServiceAdminCore(bc, configTypeHandlerFactory) {
             @Override
@@ -153,7 +163,133 @@ public class RemoteServiceAdminCoreTest 
         assertEquals(0, rsaCore.getImportedEndpoints().size());
 
         c.verify();
+    }
+
+    @Test
+    public void testExport() throws Exception {
+        BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
+
+        Bundle b = EasyMock.createNiceMock(Bundle.class);
+        EasyMock.expect(b.getBundleContext()).andReturn(bc).anyTimes();
+        EasyMock.expect(b.getSymbolicName()).andReturn("rsabundle").anyTimes();
+        EasyMock.expect(b.getHeaders()).andReturn(new Hashtable<String, String>()).anyTimes();
+        EasyMock.replay(b);
+
+        final Map<String, Object> sProps = new HashMap<String, Object>();
+        sProps.put("objectClass", new String[] {"java.lang.Runnable"});
+        sProps.put("service.id", 51L);
+        sProps.put("service.exported.interfaces", "*");
+        ServiceReference sref = mockServiceReference(sProps);
+
+        Runnable svcObject = EasyMock.createNiceMock(Runnable.class);
+        EasyMock.replay(svcObject);
 
+        EasyMock.expect(bc.getService(sref)).andReturn(svcObject).anyTimes();
+        EasyMock.expect(bc.getBundle()).andReturn(b).anyTimes();
+        EasyMock.expect(bc.createFilter("(service.id=51)")).andReturn(FrameworkUtil.createFilter("(service.id=51)")).anyTimes();
+        EasyMock.replay(bc);
+
+        // The service properties but with Arrays converted into lists (which is what the impl internally uses
+        // to allow these things to be compared).
+        Map<String, Object> sPropsMod = new HashMap<String, Object>();
+        sPropsMod.put("objectClass", Collections.singletonList("java.lang.Runnable"));
+        sPropsMod.put("service.id", 51L);
+        sPropsMod.put("service.exported.interfaces", "*");
+
+        HashMap<String, Object> eProps = new HashMap<String, Object>(sProps);
+        eProps.put("endpoint.id", "http://something");
+        eProps.put("service.imported.configs", new String[] {"org.apache.cxf.ws"});
+        ExportResult er = new ExportResult(eProps, (Server) null);
+
+        ConfigurationTypeHandler handler = EasyMock.createNiceMock(ConfigurationTypeHandler.class);
+        EasyMock.expect(handler.createServer(sref, bc, sref.getBundle().getBundleContext(), sPropsMod, Runnable.class, svcObject)).andReturn(er);
+        EasyMock.replay(handler);
+
+        ConfigTypeHandlerFactory handlerFactory = EasyMock.createNiceMock(ConfigTypeHandlerFactory.class);
+        EasyMock.expect(handlerFactory.getHandler(bc, sPropsMod)).andReturn(handler).anyTimes();
+        EasyMock.replay(handlerFactory);
+        RemoteServiceAdminCore rsaCore = new RemoteServiceAdminCore(bc, handlerFactory) {};
+
+        List<ExportRegistration> ereg = rsaCore.exportService(sref, null);
+        assertEquals(1, ereg.size());
+        assertNull(ereg.get(0).getException());
+        assertSame(sref, ereg.get(0).getExportReference().getExportedService());
+        EndpointDescription ed = ereg.get(0).getExportReference().getExportedEndpoint();
+
+        Map<String, Object> edProps = ed.getProperties();
+        assertEquals("http://something", edProps.get("endpoint.id"));
+        assertNotNull(edProps.get("service.imported"));
+        assertTrue(Arrays.equals(new String [] {"java.lang.Runnable"}, (Object[]) edProps.get("objectClass")));
+        assertTrue(Arrays.equals(new String[] {"org.apache.cxf.ws"}, (Object[]) edProps.get("service.imported.configs")));
+    }
+
+    @Test
+    public void testExportException() throws Exception {
+        BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
+
+        Bundle b = EasyMock.createNiceMock(Bundle.class);
+        EasyMock.expect(b.getBundleContext()).andReturn(bc).anyTimes();
+        EasyMock.expect(b.getSymbolicName()).andReturn("rsabundle").anyTimes();
+        EasyMock.expect(b.getHeaders()).andReturn(new Hashtable<String, String>()).anyTimes();
+        EasyMock.replay(b);
+
+        final Map<String, Object> sProps = new HashMap<String, Object>();
+        sProps.put("objectClass", new String[] {"java.lang.Runnable"});
+        sProps.put("service.id", 51L);
+        sProps.put("service.exported.interfaces", "*");
+        ServiceReference sref = mockServiceReference(sProps);
+
+        Runnable svcObject = EasyMock.createNiceMock(Runnable.class);
+        EasyMock.replay(svcObject);
+
+        EasyMock.expect(bc.getService(sref)).andReturn(svcObject).anyTimes();
+        EasyMock.expect(bc.getBundle()).andReturn(b).anyTimes();
+        EasyMock.replay(bc);
+
+        // The service properties but with Arrays converted into lists (which is what the impl internally uses
+        // to allow these things to be compared).
+        Map<String, Object> sPropsMod = new HashMap<String, Object>();
+        sPropsMod.put("objectClass", Collections.singletonList("java.lang.Runnable"));
+        sPropsMod.put("service.id", 51L);
+        sPropsMod.put("service.exported.interfaces", "*");
+
+        HashMap<String, Object> eProps = new HashMap<String, Object>(sProps);
+        eProps.put("endpoint.id", "http://something");
+        eProps.put("service.imported.configs", new String[] {"org.apache.cxf.ws"});
+        ExportResult er = new ExportResult(eProps, new TestException());
+
+        ConfigurationTypeHandler handler = EasyMock.createNiceMock(ConfigurationTypeHandler.class);
+        EasyMock.expect(handler.createServer(sref, bc, sref.getBundle().getBundleContext(), sPropsMod, Runnable.class, svcObject)).andReturn(er);
+        EasyMock.replay(handler);
+
+        ConfigTypeHandlerFactory handlerFactory = EasyMock.createNiceMock(ConfigTypeHandlerFactory.class);
+        EasyMock.expect(handlerFactory.getHandler(bc, sPropsMod)).andReturn(handler).anyTimes();
+        EasyMock.replay(handlerFactory);
+        RemoteServiceAdminCore rsaCore = new RemoteServiceAdminCore(bc, handlerFactory) {};
+
+        List<ExportRegistration> ereg = rsaCore.exportService(sref, null);
+        assertEquals(1, ereg.size());
+        assertTrue(ereg.get(0).getException() instanceof TestException);
+        assertSame(sref, ereg.get(0).getExportReference().getExportedService());
+    }
+
+    private ServiceReference mockServiceReference(final Map<String, Object> sProps) {
+        Bundle b = EasyMock.createNiceMock(Bundle.class);
+        EasyMock.replay(b);
+
+        ServiceReference sref = EasyMock.createNiceMock(ServiceReference.class);
+        EasyMock.expect(sref.getBundle()).andReturn(b).anyTimes();
+        EasyMock.expect(sref.getPropertyKeys()).andReturn(sProps.keySet().toArray(new String [] {})).anyTimes();
+        EasyMock.expect(sref.getProperty((String) EasyMock.anyObject())).andAnswer(new IAnswer<Object>() {
+            @Override
+            public Object answer() throws Throwable {
+                return sProps.get(EasyMock.getCurrentArguments()[0]);
+            }
+        }).anyTimes();
+        EasyMock.replay(sref);
+        return sref;
     }
 
+    @SuppressWarnings("serial")
+    private static class TestException extends Exception {}
 }