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 2010/01/04 17:12:12 UTC

svn commit: r895676 - in /cxf/dosgi/trunk/discovery/local/src: main/java/org/apache/cxf/dosgi/discovery/local/ test/java/org/apache/cxf/dosgi/discovery/local/

Author: davidb
Date: Mon Jan  4 16:12:11 2010
New Revision: 895676

URL: http://svn.apache.org/viewvc?rev=895676&view=rev
Log:
More tests for local discovery.

Modified:
    cxf/dosgi/trunk/discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/LocalDiscovery.java
    cxf/dosgi/trunk/discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryUtils.java
    cxf/dosgi/trunk/discovery/local/src/test/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryTest.java
    cxf/dosgi/trunk/discovery/local/src/test/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryUtilsTest.java

Modified: cxf/dosgi/trunk/discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/LocalDiscovery.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/LocalDiscovery.java?rev=895676&r1=895675&r2=895676&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/LocalDiscovery.java (original)
+++ cxf/dosgi/trunk/discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/LocalDiscovery.java Mon Jan  4 16:12:11 2010
@@ -158,6 +158,7 @@
             findDeclaredRemoteServices(be.getBundle());
             break;
         case BundleEvent.STOPPING:
+            // TODO
             break;
         }
     }
@@ -196,12 +197,6 @@
     }
     
     private void triggerCallbacks(Collection<String> filters, EndpointListener listener) {
-        if (endpointDescriptions.size() > 0) {
-            LOG.info("search for matches to trigger callbacks with delta: " + filters);
-        } else {
-            LOG.info("nothing to search for matches to trigger callbacks with delta: " + filters);
-        }
-        
         for (String filter : filters) {
             for (EndpointDescription ed : endpointDescriptions.keySet()) {
                 triggerCallbacks(listener, filter, ed, true);

Modified: cxf/dosgi/trunk/discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryUtils.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryUtils.java?rev=895676&r1=895675&r2=895676&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryUtils.java (original)
+++ cxf/dosgi/trunk/discovery/local/src/main/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryUtils.java Mon Jan  4 16:12:11 2010
@@ -54,7 +54,7 @@
     
     private static final String REMOTE_SERVICES_HEADER_NAME = "Remote-Service";
     private static final String REMOTE_SERVICES_DIRECTORY =
-        "OSGI-INF/remote-service";
+        "OSGI-INF/remote-service/";
     private static final String REMOTE_SERVICES_NS =
         "http://www.osgi.org/xmlns/sd/v1.0.0"; // this one was replaced by the RSA one in the spec
     private static final String REMOTE_SERVICES_ADMIN_NS = 
@@ -328,18 +328,33 @@
 
     @SuppressWarnings("unchecked")
     static List<Element> getAllDescriptionElements(Bundle b) {
-        Object directory = null;
+        Object header = null;
         
         Dictionary headers = b.getHeaders();
         if (headers != null) {
-            directory = headers.get(REMOTE_SERVICES_HEADER_NAME);
+            header = headers.get(REMOTE_SERVICES_HEADER_NAME);
         }
         
-        if (directory == null) {
-            directory = REMOTE_SERVICES_DIRECTORY;
+        if (header == null) {
+            header = REMOTE_SERVICES_DIRECTORY;
         }
         
-        Enumeration urls = b.findEntries(directory.toString(), "*.xml", false);
+        String dir = header.toString();
+        String filePattern = "*.xml";
+        if (dir.endsWith("/")) {
+            dir = dir.substring(0, dir.length()-1);
+        } else {
+            int idx = dir.lastIndexOf('/');
+            if (idx >= 0 & dir.length() > idx) {
+                filePattern = dir.substring(idx + 1);
+                dir = dir.substring(0, idx);
+            } else {
+                filePattern = dir;
+                dir = "";
+            }
+        }
+        
+        Enumeration urls = b.findEntries(dir, filePattern, false);
         if (urls == null) {
             return Collections.emptyList();
         }

Modified: cxf/dosgi/trunk/discovery/local/src/test/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryTest.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/local/src/test/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryTest.java?rev=895676&r1=895675&r2=895676&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/local/src/test/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryTest.java (original)
+++ cxf/dosgi/trunk/discovery/local/src/test/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryTest.java Mon Jan  4 16:12:11 2010
@@ -19,6 +19,7 @@
 package org.apache.cxf.dosgi.discovery.local;
 
 import java.net.URL;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Dictionary;
@@ -34,10 +35,15 @@
 import org.easymock.IAnswer;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
 import org.osgi.framework.BundleListener;
 import org.osgi.framework.Filter;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceReference;
 import org.osgi.service.remoteserviceadmin.EndpointDescription;
+import org.osgi.service.remoteserviceadmin.EndpointListener;
 
 public class LocalDiscoveryTest extends TestCase {
     public void testLocalDiscovery() throws Exception {
@@ -65,6 +71,16 @@
         assertNotNull(ld.listenerTracker);        
         
         EasyMock.verify(bc);
+        
+        EasyMock.reset(bc);
+        bc.removeBundleListener(ld);        
+        EasyMock.expectLastCall();
+        bc.removeServiceListener((ServiceListener) EasyMock.anyObject());
+        EasyMock.expectLastCall();
+        EasyMock.replay(bc);
+
+        ld.shutDown();
+        EasyMock.verify(bc);
     }
     
     public void testPreExistingBundles() throws Exception {
@@ -78,12 +94,7 @@
         EasyMock.expectLastCall();
         EasyMock.expect(bc.getServiceReferences("org.osgi.service.remoteserviceadmin.EndpointListener", null)).andReturn(null);
         bc.addBundleListener((BundleListener) EasyMock.anyObject());
-        EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {            
-            public Object answer() throws Throwable {
-                assertEquals(LocalDiscovery.class, EasyMock.getCurrentArguments()[0].getClass());
-                return null;
-            }
-        });
+        EasyMock.expectLastCall();
         
         Bundle b1 = EasyMock.createMock(Bundle.class);
         EasyMock.expect(b1.getState()).andReturn(Bundle.RESOLVED);
@@ -91,7 +102,7 @@
         Bundle b2 = EasyMock.createMock(Bundle.class);
         EasyMock.expect(b2.getState()).andReturn(Bundle.ACTIVE);
         Dictionary<String, Object> headers = new Hashtable<String, Object>();
-        headers.put("Remote-Service", "OSGI-INF/remote-service");
+        headers.put("Remote-Service", "OSGI-INF/remote-service/");
         EasyMock.expect(b2.getHeaders()).andReturn(headers);
         
         URL rs3URL = getClass().getResource("/ed3.xml");
@@ -116,4 +127,104 @@
         }
         assertEquals(expected, actual);
     }
+    
+    public void testBundleChanged() throws Exception {
+        LocalDiscovery ld = getLocalDiscovery();
+
+        Bundle bundle = EasyMock.createMock(Bundle.class);
+        EasyMock.expect(bundle.getState()).andReturn(Bundle.ACTIVE);
+        Dictionary<String, Object> headers = new Hashtable<String, Object>();
+        headers.put("Remote-Service", "OSGI-INF/rsa/");
+        EasyMock.expect(bundle.getHeaders()).andReturn(headers);
+        EasyMock.expect(bundle.findEntries("OSGI-INF/rsa", "*.xml", false))
+            .andReturn(Collections.enumeration(
+                Collections.singleton(getClass().getResource("/ed3.xml"))));
+        EasyMock.replay(bundle);
+                
+        BundleEvent be0 = new BundleEvent(BundleEvent.INSTALLED, bundle);
+        ld.bundleChanged(be0);
+        assertEquals(0, ld.endpointDescriptions.size());
+        
+        BundleEvent be = new BundleEvent(BundleEvent.STARTED, bundle);
+        ld.bundleChanged(be);
+        assertEquals(1, ld.endpointDescriptions.size());
+        EndpointDescription ed = ld.endpointDescriptions.keySet().iterator().next();
+        assertEquals("http://somewhere:12345", ed.getRemoteURI());
+        assertSame(bundle, ld.endpointDescriptions.get(ed));
+    }
+    
+    public void testAddingService() throws Exception {
+        LocalDiscovery ld = getLocalDiscovery();
+        ///
+        Bundle bundle = EasyMock.createMock(Bundle.class);
+        EasyMock.expect(bundle.getState()).andReturn(Bundle.ACTIVE);
+        Dictionary<String, Object> headers = new Hashtable<String, Object>();
+        headers.put("Remote-Service", "OSGI-INF/rsa/ed4.xml");
+        EasyMock.expect(bundle.getHeaders()).andReturn(headers);
+        EasyMock.expect(bundle.findEntries("OSGI-INF/rsa", "ed4.xml", false))
+            .andReturn(Collections.enumeration(
+                Collections.singleton(getClass().getResource("/ed4.xml"))));
+        EasyMock.replay(bundle);
+                        
+        BundleEvent be = new BundleEvent(BundleEvent.STARTED, bundle);
+        ld.bundleChanged(be);
+        assertEquals(2, ld.endpointDescriptions.size());        
+        ///
+        final Hashtable<String, Object> props = new Hashtable<String, Object>();
+        props.put(EndpointListener.ENDPOINT_LISTENER_SCOPE, "(objectClass=org.example.ClassA)");
+        ServiceReference sr = EasyMock.createMock(ServiceReference.class);
+        EasyMock.expect(sr.getPropertyKeys()).andReturn(props.keySet().toArray(new String [] {})).anyTimes();
+        EasyMock.expect(sr.getProperty((String) EasyMock.anyObject())).andAnswer(new IAnswer<Object>() {
+            public Object answer() throws Throwable {
+                return props.get(EasyMock.getCurrentArguments()[0]);
+            }
+        }).anyTimes();
+                
+        EasyMock.replay(sr);
+        
+        EasyMock.reset(ld.bundleContext);
+        EndpointListener el = EasyMock.createMock(EndpointListener.class);
+        EasyMock.expect(ld.bundleContext.getService(sr)).andReturn(el);
+        EasyMock.expect(ld.bundleContext.createFilter((String) EasyMock.anyObject())).andAnswer(new IAnswer<Filter>() {
+            public Filter answer() throws Throwable {
+                return FrameworkUtil.createFilter((String) EasyMock.getCurrentArguments()[0]);
+            }
+        }).anyTimes();
+        EasyMock.replay(ld.bundleContext);
+        
+        el.endpointAdded((EndpointDescription) EasyMock.anyObject(), 
+                EasyMock.eq("(objectClass=org.example.ClassA)"));
+        EasyMock.expectLastCall();
+        EasyMock.replay(el);
+        
+        assertEquals("Precondition failed", 0, ld.listenerToFilters.size());
+        assertEquals("Precondition failed", 0, ld.filterToListeners.size());        
+        assertSame(el, ld.listenerTracker.addingService(sr));
+        
+        assertEquals(1, ld.listenerToFilters.size());
+        assertEquals(Collections.singletonList("(objectClass=org.example.ClassA)"), ld.listenerToFilters.get(el));
+        assertEquals(1, ld.filterToListeners.size()); 
+        assertEquals(Collections.singletonList(el), ld.filterToListeners.get("(objectClass=org.example.ClassA)"));
+
+        EasyMock.verify(el);
+    }
+
+    private LocalDiscovery getLocalDiscovery() throws InvalidSyntaxException {
+        Filter filter = EasyMock.createMock(Filter.class);
+        EasyMock.replay(filter);
+        
+        BundleContext bc = EasyMock.createMock(BundleContext.class);
+        EasyMock.expect(bc.createFilter("(objectClass=org.osgi.service.remoteserviceadmin.EndpointListener)")).andReturn(filter);
+        bc.addServiceListener((ServiceListener) EasyMock.anyObject(), 
+            EasyMock.eq("(objectClass=org.osgi.service.remoteserviceadmin.EndpointListener)"));
+        EasyMock.expectLastCall();
+        EasyMock.expect(bc.getServiceReferences("org.osgi.service.remoteserviceadmin.EndpointListener", null)).andReturn(null);
+        bc.addBundleListener((BundleListener) EasyMock.anyObject());
+        EasyMock.expectLastCall();
+        EasyMock.expect(bc.getBundles()).andReturn(null);
+        EasyMock.replay(bc);
+                
+        LocalDiscovery ld = new LocalDiscovery(bc);
+        return ld;
+    }
 }

Modified: cxf/dosgi/trunk/discovery/local/src/test/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryUtilsTest.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/local/src/test/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryUtilsTest.java?rev=895676&r1=895675&r2=895676&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/local/src/test/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryUtilsTest.java (original)
+++ cxf/dosgi/trunk/discovery/local/src/test/java/org/apache/cxf/dosgi/discovery/local/LocalDiscoveryUtilsTest.java Mon Jan  4 16:12:11 2010
@@ -95,7 +95,7 @@
     public void testRemoteServicesXMLFileAlternateLocation() {
         URL rs1URL = getClass().getResource("/rs1.xml");
         Dictionary headers = new Hashtable();        
-        headers.put("Remote-Service", "META-INF/osgi");
+        headers.put("Remote-Service", "META-INF/osgi/");
         headers.put("Bundle-Name", "testing bundle");
         
         Bundle b = EasyMock.createNiceMock(Bundle.class);