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 2009/07/03 15:27:55 UTC

svn commit: r790906 - in /cxf/dosgi/trunk/dsw/cxf-dsw/src: main/java/org/apache/cxf/dosgi/dsw/decorator/ test/java/org/apache/cxf/dosgi/dsw/decorator/ test/resources/test-resources/

Author: davidb
Date: Fri Jul  3 13:27:55 2009
New Revision: 790906

URL: http://svn.apache.org/viewvc?rev=790906&view=rev
Log:
Additional unit tests for the Service Decorator.

Added:
    cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd-1.xml   (with props)
    cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd0.xml   (with props)
    cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd1.xml   (with props)
    cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd2.xml   (with props)
Modified:
    cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/decorator/InterfaceRule.java
    cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/decorator/ServiceDecoratorImpl.java
    cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/decorator/ServiceDecoratorImplTest.java

Modified: cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/decorator/InterfaceRule.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/decorator/InterfaceRule.java?rev=790906&r1=790905&r2=790906&view=diff
==============================================================================
--- cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/decorator/InterfaceRule.java (original)
+++ cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/decorator/InterfaceRule.java Fri Jul  3 13:27:55 2009
@@ -89,7 +89,6 @@
         }
         
         LOG.info("Adding the following properties to " + sref + ": " + addProps);
-        System.out.println("*** Adding the following properties to " + sref + ": " + addProps);
         target.putAll(addProps);
     }
 

Modified: cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/decorator/ServiceDecoratorImpl.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/decorator/ServiceDecoratorImpl.java?rev=790906&r1=790905&r2=790906&view=diff
==============================================================================
--- cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/decorator/ServiceDecoratorImpl.java (original)
+++ cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/decorator/ServiceDecoratorImpl.java Fri Jul  3 13:27:55 2009
@@ -44,7 +44,7 @@
     
     private final BundleContext bundleContext;
     private final BundleListenerImpl bundleListener;
-    private final List<Rule> decorations = new CopyOnWriteArrayList<Rule>();
+    final List<Rule> decorations = new CopyOnWriteArrayList<Rule>();
 
     public ServiceDecoratorImpl(BundleContext bc) {
         bundleContext = bc;
@@ -63,7 +63,7 @@
     }
     
     @SuppressWarnings("unchecked")
-    public void addDecorations(Bundle bundle) {
+    void addDecorations(Bundle bundle) {
         Namespace ns = Namespace.getNamespace("http://cxf.apache.org/xmlns/service-decoration/1.0.0");
         for (Element decoration : getDecorationElements(bundle)) {
             for (Element match : (List<Element>) decoration.getChildren("match", ns)) {
@@ -100,10 +100,11 @@
         return elements;
     }
     
-    public void removeDecorations(Bundle bundle) {
+    void removeDecorations(Bundle bundle) {
         for (Iterator<Rule> i = decorations.iterator(); i.hasNext(); ) {
-            if (bundle.equals(i.next().getBundle())) {
-                i.remove();
+            Rule r = i.next();
+            if (bundle.equals(r.getBundle())) {
+                decorations.remove(r); // The iterator doesn't support 'remove'
             }
         }
     }

Modified: cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/decorator/ServiceDecoratorImplTest.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/decorator/ServiceDecoratorImplTest.java?rev=790906&r1=790905&r2=790906&view=diff
==============================================================================
--- cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/decorator/ServiceDecoratorImplTest.java (original)
+++ cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/decorator/ServiceDecoratorImplTest.java Fri Jul  3 13:27:55 2009
@@ -18,28 +18,50 @@
   */
 package org.apache.cxf.dosgi.dsw.decorator;
 
+import java.net.URL;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import junit.framework.TestCase;
 
 import org.easymock.EasyMock;
+import org.easymock.IAnswer;
 import org.jdom.Element;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleListener;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
 
 public class ServiceDecoratorImplTest extends TestCase {
     public void testServiceDecorator() {
-        BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
+        final BundleListener[] bundleListener = new BundleListener[1]; 
+        
+        BundleContext bc = EasyMock.createMock(BundleContext.class);
+        bc.addBundleListener((BundleListener) EasyMock.anyObject());
+        EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {            
+            public Object answer() throws Throwable {
+                bundleListener[0] = (BundleListener) EasyMock.getCurrentArguments()[0];
+                return null;
+            }
+        });
         EasyMock.replay(bc);
         
         ServiceDecoratorImpl sd = new ServiceDecoratorImpl(bc);
+        EasyMock.verify(bc);
+        assertNotNull(bundleListener[0]);
         
-        Bundle b = EasyMock.createMock(Bundle.class);
-        EasyMock.expect(b.findEntries("OSGI-INF/remote-service", "*.xml", false)).andReturn(
-            Collections.enumeration(Arrays.asList(getClass().getResource("/test-resources/sd.xml")))).anyTimes();
-        EasyMock.replay(b);
+        EasyMock.reset(bc);
+        bc.removeBundleListener(bundleListener[0]);
+        EasyMock.replay(bc);
+        sd.shutdown();
+        
+        EasyMock.verify(bc);
     }
     
     public void testGetDecoratorElements() {
@@ -53,4 +75,218 @@
         assertEquals("service-decoration", elements.get(0).getName());
         assertEquals("http://cxf.apache.org/xmlns/service-decoration/1.0.0", elements.get(0).getNamespaceURI());
     }
+
+    public void testGetDecoratorElements2() {
+        Bundle b = EasyMock.createMock(Bundle.class);
+        EasyMock.expect(b.findEntries("OSGI-INF/remote-service", "*.xml", false)).andReturn(null).anyTimes();
+        EasyMock.replay(b);
+
+        List<Element> elements = ServiceDecoratorImpl.getDecorationElements(b);
+        assertEquals(0, elements.size());
+    }
+
+    public void testAddRemoveDecorations() {
+        URL res = getClass().getResource("/test-resources/sd.xml");
+        final Map<String, Object> serviceProps = new HashMap<String, Object>();
+        serviceProps.put(Constants.OBJECTCLASS, new String [] {"org.acme.foo.Bar"});
+        serviceProps.put("test.prop", "xyz");
+        
+        Bundle b = EasyMock.createMock(Bundle.class);
+        EasyMock.expect(b.findEntries("OSGI-INF/remote-service", "*.xml", false)).andReturn(
+            Collections.enumeration(Arrays.asList(res))).anyTimes();
+        EasyMock.replay(b);
+
+        BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
+        EasyMock.replay(bc);
+        ServiceDecoratorImpl sd = new ServiceDecoratorImpl(bc);
+        assertEquals("Precondition failed", 0, sd.decorations.size());
+        sd.addDecorations(b);
+        assertEquals(1, sd.decorations.size());
+        
+        Map<String, Object> target = new HashMap<String, Object>();
+        ServiceReference sref = EasyMock.createMock(ServiceReference.class);
+        EasyMock.expect(sref.getProperty((String) EasyMock.anyObject())).andAnswer(new IAnswer<Object>() {
+            public Object answer() throws Throwable {
+                return serviceProps.get(EasyMock.getCurrentArguments()[0]);
+            }
+        }).anyTimes();
+        EasyMock.replay(sref);
+        sd.decorate(sref, target);
+        
+        Map<String, Object> expected = new HashMap<String, Object>();
+        expected.put("test.too", "ahaha");
+        assertEquals(expected, target);
+        
+        // remove it again
+        sd.removeDecorations(b);
+        assertEquals(0, sd.decorations.size());
+        Map<String, Object> target2 = new HashMap<String, Object>();
+        sd.decorate(sref, target2);        
+        Map<String, Object> expected2 = new HashMap<String, Object>();
+        assertEquals(expected2, target2);
+    }
+   
+    public void testAddDecorations() {
+        URL res = getClass().getResource("/test-resources/sd.xml");
+        final Map<String, Object> serviceProps = new HashMap<String, Object>();
+        serviceProps.put(Constants.OBJECTCLASS, new String [] {"org.acme.foo.Bar"});
+        serviceProps.put("test.prop", "xyz");
+        
+        Map<String, Object> target = testDecorate(serviceProps, res);
+        Map<String, Object> expected = new HashMap<String, Object>();
+        expected.put("test.too", "ahaha");
+        assertEquals(expected, target);
+    }
+    
+    public void testAddDecorations1() {
+        URL r1 = getClass().getResource("/test-resources/sd1.xml");
+        URL r2 = getClass().getResource("/test-resources/sd2.xml");
+        
+        Map<String, Object> serviceProps = new HashMap<String, Object>();
+        serviceProps.put(Constants.OBJECTCLASS, new String [] {"org.test.A"});
+
+        Map<String, Object> actual = testDecorate(serviceProps, r1, r2);
+        Map<String, Object> expected = new HashMap<String, Object>();
+        expected.put("A", "B");
+        expected.put("C", new Integer(2));
+        assertEquals(expected, actual);
+    }
+
+    public void testAddDecorations2() {
+        URL r1 = getClass().getResource("/test-resources/sd1.xml");
+        URL r2 = getClass().getResource("/test-resources/sd2.xml");
+        
+        Map<String, Object> serviceProps = new HashMap<String, Object>();
+        serviceProps.put(Constants.OBJECTCLASS, new String [] {"org.test.D"});
+
+        Map<String, Object> actual = testDecorate(serviceProps, r1, r2);
+        Map<String, Object> expected = new HashMap<String, Object>();
+        assertEquals(expected, actual);
+    }
+
+    public void testAddDecorations3() {
+        URL r1 = getClass().getResource("/test-resources/sd1.xml");
+        URL r2 = getClass().getResource("/test-resources/sd2.xml");
+        
+        Map<String, Object> serviceProps = new HashMap<String, Object>();
+        serviceProps.put(Constants.OBJECTCLASS, new String [] {"org.test.B"});
+        serviceProps.put("x", "y");
+
+        Map<String, Object> actual = testDecorate(serviceProps, r1, r2);
+        Map<String, Object> expected = new HashMap<String, Object>();
+        expected.put("bool", Boolean.TRUE);
+        assertEquals(expected, actual);
+    }
+
+    public void testAddDecorations4() {
+        URL r1 = getClass().getResource("/test-resources/sd1.xml");
+        URL r2 = getClass().getResource("/test-resources/sd2.xml");
+        
+        Map<String, Object> serviceProps = new HashMap<String, Object>();
+        serviceProps.put(Constants.OBJECTCLASS, new String [] {"org.test.C"});
+        serviceProps.put("x", "z");
+
+        Map<String, Object> actual = testDecorate(serviceProps, r1, r2);
+        Map<String, Object> expected = new HashMap<String, Object>();
+        expected.put("bool", Boolean.FALSE);
+        assertEquals(expected, actual);
+    }
+
+    public void testAddDecorations5() {
+        URL r1 = getClass().getResource("/test-resources/sd1.xml");
+        URL r2 = getClass().getResource("/test-resources/sd2.xml");
+        
+        Map<String, Object> serviceProps = new HashMap<String, Object>();
+        serviceProps.put(Constants.OBJECTCLASS, new String [] {"org.test.C"});
+        serviceProps.put("x", "x");
+
+        Map<String, Object> actual = testDecorate(serviceProps, r1, r2);
+        Map<String, Object> expected = new HashMap<String, Object>();
+        assertEquals(expected, actual);
+    }
+
+    public void testAddDecorations6() {
+        URL r1 = getClass().getResource("/test-resources/sd0.xml");
+        
+        Map<String, Object> serviceProps = new HashMap<String, Object>();
+        serviceProps.put(Constants.OBJECTCLASS, new String [] {"org.test.D"});
+
+        Map<String, Object> actual = testDecorate(serviceProps, r1);
+        Map<String, Object> expected = new HashMap<String, Object>();
+        assertEquals(expected, actual);
+    }
+
+    public void testAddDecorations7() {
+        URL r1 = getClass().getResource("/test-resources/sd-1.xml");
+        
+        Map<String, Object> serviceProps = new HashMap<String, Object>();
+        serviceProps.put(Constants.OBJECTCLASS, new String [] {"org.test.D"});
+
+        Map<String, Object> actual = testDecorate(serviceProps, r1);
+        Map<String, Object> expected = new HashMap<String, Object>();
+        assertEquals(expected, actual);
+    }
+
+    private Map<String, Object> testDecorate(final Map<String, Object> serviceProps, URL ... resources) {
+        Bundle b = EasyMock.createMock(Bundle.class);
+        EasyMock.expect(b.findEntries("OSGI-INF/remote-service", "*.xml", false)).andReturn(
+            Collections.enumeration(Arrays.asList(resources))).anyTimes();
+        EasyMock.replay(b);
+
+        BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
+        EasyMock.replay(bc);
+        ServiceDecoratorImpl sd = new ServiceDecoratorImpl(bc);
+        sd.addDecorations(b);
+        
+        Map<String, Object> target = new HashMap<String, Object>();
+        ServiceReference sref = EasyMock.createMock(ServiceReference.class);
+        EasyMock.expect(sref.getProperty((String) EasyMock.anyObject())).andAnswer(new IAnswer<Object>() {
+            public Object answer() throws Throwable {
+                return serviceProps.get(EasyMock.getCurrentArguments()[0]);
+            }
+        }).anyTimes();
+        EasyMock.replay(sref);
+        sd.decorate(sref, target);
+        return target;
+    }
+    
+    public void testBundleListener() {
+        final BundleListener[] bundleListener = new BundleListener[1]; 
+        
+        BundleContext bc = EasyMock.createMock(BundleContext.class);
+        bc.addBundleListener((BundleListener) EasyMock.anyObject());
+        EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {            
+            public Object answer() throws Throwable {
+                bundleListener[0] = (BundleListener) EasyMock.getCurrentArguments()[0];
+                return null;
+            }
+        });
+        EasyMock.replay(bc);
+        
+        final List<String> called = new ArrayList<String>();
+        new ServiceDecoratorImpl(bc) {
+            @Override
+            void addDecorations(Bundle bundle) {
+                called.add("addDecorations");
+            }
+
+            @Override
+            void removeDecorations(Bundle bundle) {
+                called.add("removeDecorations");
+            }            
+        };
+        
+        Bundle b = EasyMock.createMock(Bundle.class);
+        EasyMock.replay(b);
+        
+        assertEquals("Precondition failed", 0, called.size());
+        bundleListener[0].bundleChanged(new BundleEvent(BundleEvent.INSTALLED, b));
+        assertEquals(0, called.size());
+        
+        bundleListener[0].bundleChanged(new BundleEvent(BundleEvent.STARTED, b));
+        assertEquals(Arrays.asList("addDecorations"), called);
+        
+        bundleListener[0].bundleChanged(new BundleEvent(BundleEvent.STOPPING, b));
+        assertEquals(Arrays.asList("addDecorations", "removeDecorations"), called);
+    }
 }

Added: cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd-1.xml
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd-1.xml?rev=790906&view=auto
==============================================================================
--- cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd-1.xml (added)
+++ cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd-1.xml Fri Jul  3 13:27:55 2009
@@ -0,0 +1,3 @@
+<test>
+  <some-other-tag/>
+</test>
\ No newline at end of file

Propchange: cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd-1.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd-1.xml
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd-1.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Added: cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd0.xml
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd0.xml?rev=790906&view=auto
==============================================================================
--- cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd0.xml (added)
+++ cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd0.xml Fri Jul  3 13:27:55 2009
@@ -0,0 +1,2 @@
+<service-decorations xmlns="http://cxf.apache.org/xmlns/service-decoration/1.0.0">
+</service-decorations>
\ No newline at end of file

Propchange: cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd0.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd0.xml
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd0.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Added: cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd1.xml
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd1.xml?rev=790906&view=auto
==============================================================================
--- cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd1.xml (added)
+++ cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd1.xml Fri Jul  3 13:27:55 2009
@@ -0,0 +1,8 @@
+<service-decorations xmlns="http://cxf.apache.org/xmlns/service-decoration/1.0.0">
+  <service-decoration>
+    <match interface="org.test.A">
+      <add-property name="A" value="B"/>
+      <add-property name="C" value="2" type="java.lang.Integer"/>
+    </match>
+  </service-decoration>
+</service-decorations>
\ No newline at end of file

Propchange: cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd1.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd1.xml
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd1.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Added: cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd2.xml
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd2.xml?rev=790906&view=auto
==============================================================================
--- cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd2.xml (added)
+++ cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd2.xml Fri Jul  3 13:27:55 2009
@@ -0,0 +1,14 @@
+<service-decorations xmlns="http://cxf.apache.org/xmlns/service-decoration/1.0.0">
+  <service-decoration>
+    <match interface="org.test.(B|C)">
+      <match-property name="x" value="y"/>
+      <add-property name="bool" value="true" type="java.lang.Boolean"/>
+    </match>
+  </service-decoration>
+  <service-decoration>
+    <match interface="org.test.(B|C)">
+      <match-property name="x" value="z"/>
+      <add-property name="bool" value="false" type="java.lang.Boolean"/>
+    </match>
+  </service-decoration>
+</service-decorations>
\ No newline at end of file

Propchange: cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd2.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd2.xml
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: cxf/dosgi/trunk/dsw/cxf-dsw/src/test/resources/test-resources/sd2.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml