You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ri...@apache.org on 2009/06/12 17:01:30 UTC

svn commit: r784148 - in /felix/trunk/framework/src/test/java/org/apache/felix/framework/util: EventDispatcherListenerWrapperTest.java EventDispatcherTest.java

Author: rickhall
Date: Fri Jun 12 15:01:29 2009
New Revision: 784148

URL: http://svn.apache.org/viewvc?rev=784148&view=rev
Log:
Applied patch (FELIX-1208) to update tests to new version of EasyMock.

Added:
    felix/trunk/framework/src/test/java/org/apache/felix/framework/util/EventDispatcherListenerWrapperTest.java
Modified:
    felix/trunk/framework/src/test/java/org/apache/felix/framework/util/EventDispatcherTest.java

Added: felix/trunk/framework/src/test/java/org/apache/felix/framework/util/EventDispatcherListenerWrapperTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/test/java/org/apache/felix/framework/util/EventDispatcherListenerWrapperTest.java?rev=784148&view=auto
==============================================================================
--- felix/trunk/framework/src/test/java/org/apache/felix/framework/util/EventDispatcherListenerWrapperTest.java (added)
+++ felix/trunk/framework/src/test/java/org/apache/felix/framework/util/EventDispatcherListenerWrapperTest.java Fri Jun 12 15:01:29 2009
@@ -0,0 +1,461 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.framework.util;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+import junit.framework.TestCase;
+
+import org.easymock.EasyMock;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+
+public class EventDispatcherListenerWrapperTest extends TestCase
+{
+    public void testRemove()
+    {
+        Bundle b1 = getMockBundle();
+        BundleContext bc1 = b1.getBundleContext();
+        Bundle b2 = getMockBundle();
+        BundleContext bc2 = b2.getBundleContext();
+        Bundle b3 = getMockBundle();
+        BundleContext bc3 = b3.getBundleContext();
+        Bundle b4 = getMockBundle();
+        BundleContext bc4 = b4.getBundleContext();
+
+        Object[] listeners = new Object[]
+        {
+            b1,
+            String.class,
+            new Object(),
+            "(some=filter)",
+            null,
+            b2,
+            Integer.class,
+            new Object(),
+            "(some.other=filter)",
+            new Integer(15),
+            b3,
+            BundleContext.class,
+            new Object(),
+            null,
+            Boolean.TRUE,
+        };
+
+        Collection c = new EventDispatcher.ListenerBundleContextCollectionWrapper(listeners);
+        assertEquals(3, c.size());
+        assertFalse(c.isEmpty());
+        assertTrue(c.contains(bc1));
+        assertTrue(c.contains(bc2));
+        assertTrue(c.contains(bc3));
+        assertFalse(c.contains(bc4));
+
+        assertTrue(c.remove(bc2));
+        assertEquals(2, c.size());
+        assertTrue(c.contains(bc1));
+        assertFalse(c.contains(bc2));
+        assertTrue(c.contains(bc3));
+        assertFalse(c.contains(bc4));
+        assertFalse("Already removed", c.remove(bc2));
+
+        Object[] actualListeners =
+            ((EventDispatcher.ListenerBundleContextCollectionWrapper) c).getListeners();
+        Object[] expectedListeners = new Object[10];
+        System.arraycopy(listeners, 0, expectedListeners, 0, 5);
+        System.arraycopy(listeners, 10, expectedListeners, 5, 5);
+        assertTrue(Arrays.equals(expectedListeners, actualListeners));
+
+        assertTrue(c.remove(bc1));
+        assertEquals(1, c.size());
+        assertFalse(c.contains(bc1));
+        assertFalse(c.contains(bc2));
+        assertTrue(c.contains(bc3));
+        assertFalse(c.contains(bc4));
+        assertFalse(c.isEmpty());
+
+        Object[] actualListeners2 =
+            ((EventDispatcher.ListenerBundleContextCollectionWrapper) c).getListeners();
+        Object[] expectedListeners2 = new Object[5];
+        System.arraycopy(listeners, 10, expectedListeners2, 0, 5);
+        assertTrue(Arrays.equals(expectedListeners2, actualListeners2));
+
+        assertTrue(c.remove(bc3));
+        assertEquals(0, c.size());
+        assertFalse(c.contains(bc1));
+        assertFalse(c.contains(bc2));
+        assertFalse(c.contains(bc3));
+        assertFalse(c.contains(bc4));
+        assertTrue(c.isEmpty());
+
+        Object[] actualListeners3 =
+            ((EventDispatcher.ListenerBundleContextCollectionWrapper) c).getListeners();
+        assertEquals(0, actualListeners3.length);
+    }
+
+    public void testIterator()
+    {
+        Bundle b1 = getMockBundle();
+        BundleContext bc1 = b1.getBundleContext();
+        Bundle b2 = getMockBundle();
+        BundleContext bc2 = b2.getBundleContext();
+
+        Object[] listeners = new Object[]
+        {
+            b1,
+            String.class,
+            new Object(),
+            "(some=filter)",
+            null,
+            b2,
+            Integer.class,
+            new Object(),
+            "(some.other=filter)",
+            new Integer(15)
+        };
+
+        Collection c = new EventDispatcher.ListenerBundleContextCollectionWrapper(listeners);
+        Iterator it = c.iterator();
+
+        assertEquals(2, c.size());
+        assertTrue(it.hasNext());
+        try
+        {
+            it.remove();
+            fail("Should throw an exception");
+        }
+        catch (IllegalStateException ise)
+        {
+            // good
+        }
+        assertSame(bc1, it.next());
+        it.remove();
+        assertEquals(1, c.size());
+
+        // Create another iterator and make sure it sees the removal of it
+        Iterator it2 = c.iterator();
+        assertTrue(it2.hasNext());
+        assertSame(bc2, it2.next());
+        assertFalse(it2.hasNext());
+
+        // back to the origial iterator
+
+        try
+        {
+            it.remove();
+            fail("Should throw an exception");
+        }
+        catch (IllegalStateException ise)
+        {
+            // good
+        }
+        assertTrue(it.hasNext());
+        try
+        {
+            it.remove();
+            fail("Should throw an exception");
+        }
+        catch (IllegalStateException ise)
+        {
+            // good
+        }
+        assertSame(bc2, it.next());
+        it.remove();
+        assertEquals(0, c.size());
+
+        assertFalse(it.hasNext());
+        try
+        {
+            it.next();
+            fail("Should throw an exception");
+        }
+        catch (NoSuchElementException nse)
+        {
+            // good
+        }
+        try
+        {
+            it.remove();
+            fail("Should throw an exception");
+        }
+        catch (IllegalStateException ise)
+        {
+            // good
+        }
+    }
+
+    public void testAdd()
+    {
+        Bundle b1 = getMockBundle();
+        b1.getBundleContext();
+
+        Object[] listeners = new Object[]
+        {
+            b1,
+            String.class,
+            new Object(),
+            "(some=filter)",
+            null,
+        };
+
+        Collection c = new EventDispatcher.ListenerBundleContextCollectionWrapper(listeners);
+        try
+        {
+            c.add(new Object());
+            fail("Should not have been able to add to the collection");
+        }
+        catch (UnsupportedOperationException uoe)
+        {
+            // good
+        }
+    }
+
+    public void testAddAll()
+    {
+        Bundle b1 = getMockBundle();
+        BundleContext bc1 = b1.getBundleContext();
+        Object[] listeners =
+        {
+        };
+
+        Collection c = new EventDispatcher.ListenerBundleContextCollectionWrapper(listeners);
+        try
+        {
+            c.addAll(Collections.singleton(bc1));
+            fail("Should not have been able to add to the collection");
+        }
+        catch (UnsupportedOperationException uoe)
+        {
+            // good
+        }
+    }
+
+    public void testContainsAll()
+    {
+        Bundle b1 = getMockBundle();
+        BundleContext bc1 = b1.getBundleContext();
+        Bundle b2 = getMockBundle();
+        BundleContext bc2 = b2.getBundleContext();
+        Bundle b3 = getMockBundle();
+        BundleContext bc3 = b3.getBundleContext();
+
+        Object[] listeners = new Object[]
+        {
+            b1,
+            String.class,
+            new Object(),
+            "(some=filter)",
+            null,
+            b2,
+            Integer.class,
+            new Object(),
+            "(some.other=filter)",
+            new Integer(15),
+            b3,
+            BundleContext.class,
+            new Object(),
+            null,
+            Boolean.TRUE,
+        };
+
+        Collection c = new EventDispatcher.ListenerBundleContextCollectionWrapper(listeners);
+
+        assertTrue(c.containsAll(Collections.emptySet()));
+        assertTrue(c.containsAll(Collections.singleton(bc2)));
+        assertTrue(c.containsAll(Arrays.asList(new Object[]
+            {
+                bc2, bc1
+            })));
+        assertTrue(c.containsAll(Arrays.asList(new Object[]
+            {
+                bc3, bc2, bc1
+            })));
+        assertFalse(c.containsAll(Arrays.asList(new Object[]
+            {
+                bc3, bc2, bc1, new Object()
+            })));
+
+        assertEquals(3, c.size());
+        c.clear();
+        assertEquals(0, c.size());
+    }
+
+    public void testRemoveAll()
+    {
+        Bundle b1 = getMockBundle();
+        BundleContext bc1 = b1.getBundleContext();
+        Bundle b2 = getMockBundle();
+        BundleContext bc2 = b2.getBundleContext();
+        Bundle b3 = getMockBundle();
+        BundleContext bc3 = b3.getBundleContext();
+
+        Object[] listeners = new Object[]
+        {
+            b1,
+            String.class,
+            new Object(),
+            "(some=filter)",
+            null,
+            b2,
+            Integer.class,
+            new Object(),
+            "(some.other=filter)",
+            new Integer(15),
+            b3,
+            BundleContext.class,
+            new Object(),
+            null,
+            Boolean.TRUE,
+        };
+
+        Collection c = new EventDispatcher.ListenerBundleContextCollectionWrapper(listeners);
+        assertFalse(c.removeAll(Collections.emptyList()));
+        assertFalse(c.removeAll(Collections.singleton(new Object())));
+        assertTrue(c.contains(bc2));
+        assertTrue(c.removeAll(Arrays.asList(new Object[]
+            {
+                new Object(), bc2
+            })));
+        assertFalse(c.contains(bc2));
+
+        assertEquals(2, c.size());
+        assertTrue(c.removeAll(Arrays.asList(new Object[]
+            {
+                bc1, bc3
+            })));
+        assertEquals(0, c.size());
+    }
+
+    public void testRetainAll()
+    {
+        Bundle b1 = getMockBundle();
+        BundleContext bc1 = b1.getBundleContext();
+        Bundle b2 = getMockBundle();
+        BundleContext bc2 = b2.getBundleContext();
+        Bundle b3 = getMockBundle();
+        BundleContext bc3 = b3.getBundleContext();
+
+        Object[] listeners = new Object[]
+        {
+            b1,
+            String.class,
+            new Object(),
+            "(some=filter)",
+            null,
+            b2,
+            Integer.class,
+            new Object(),
+            "(some.other=filter)",
+            new Integer(15),
+            b3,
+            BundleContext.class,
+            new Object(),
+            null,
+            Boolean.TRUE,
+        };
+
+        Collection c = new EventDispatcher.ListenerBundleContextCollectionWrapper(listeners);
+        assertFalse(c.retainAll(Arrays.asList(new Object[]
+            {
+                bc3, bc1, bc2
+            })));
+        assertTrue(Arrays.equals(new Object[]
+            {
+                bc1, bc2, bc3
+            }, c.toArray()));
+
+        assertTrue(c.retainAll(Arrays.asList(new Object[]
+            {
+                bc1, bc2, new Object()
+            })));
+        assertTrue(Arrays.equals(new Object[]
+            {
+                bc1, bc2
+            }, c.toArray()));
+
+        assertTrue(c.retainAll(Collections.emptyList()));
+        assertEquals(0, c.size());
+    }
+
+    public void testToArray()
+    {
+        Bundle b1 = getMockBundle();
+        BundleContext bc1 = b1.getBundleContext();
+        Bundle b2 = getMockBundle();
+        BundleContext bc2 = b2.getBundleContext();
+
+        Object[] listeners = new Object[]
+        {
+            b1,
+            String.class,
+            new Object(),
+            "(some=filter)",
+            null,
+            b2,
+            Integer.class,
+            new Object(),
+            "(some.other=filter)",
+            new Integer(15),
+        };
+
+        Collection c = new EventDispatcher.ListenerBundleContextCollectionWrapper(listeners);
+        assertTrue(Arrays.equals(new Object[]
+            {
+                bc1, bc2
+            }, c.toArray()));
+        assertTrue(Arrays.equals(new Object[]
+            {
+                bc1, bc2
+            }, c.toArray(new Object[]
+            {
+            })));
+
+        try
+        {
+            c.toArray(new String[]
+                {
+                });
+            fail("Should not be allowed");
+        }
+        catch (ArrayStoreException ase)
+        {
+            // good
+        }
+    }
+
+    private Bundle getMockBundle()
+    {
+        BundleContext bc = (BundleContext) EasyMock.createNiceMock(BundleContext.class);
+        Bundle b = (Bundle) EasyMock.createNiceMock(Bundle.class);
+        EasyMock.expect(b.getBundleContext()).andReturn(bc).anyTimes();
+        b.getState();
+        EasyMock.expectLastCall().andReturn(Integer.valueOf(Bundle.ACTIVE)).anyTimes();
+
+        EasyMock.expect(bc.getBundle()).andReturn(b).anyTimes();
+        EasyMock.replay(new Object[]
+            {
+                b, bc
+            });
+
+        return b;
+    }
+}
\ No newline at end of file

Modified: felix/trunk/framework/src/test/java/org/apache/felix/framework/util/EventDispatcherTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/test/java/org/apache/felix/framework/util/EventDispatcherTest.java?rev=784148&r1=784147&r2=784148&view=diff
==============================================================================
--- felix/trunk/framework/src/test/java/org/apache/felix/framework/util/EventDispatcherTest.java (original)
+++ felix/trunk/framework/src/test/java/org/apache/felix/framework/util/EventDispatcherTest.java Fri Jun 12 15:01:29 2009
@@ -0,0 +1,184 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.framework.util;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import junit.framework.TestCase;
+
+import org.apache.felix.framework.Logger;
+import org.apache.felix.framework.ServiceRegistry;
+import org.easymock.EasyMock;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.hooks.service.EventHook;
+import org.osgi.framework.launch.Framework;
+
+public class EventDispatcherTest extends TestCase
+{
+    public void testFireServiceEvent()
+    {
+        final Bundle b1 = getMockBundle();
+        final Bundle b2 = getMockBundle();
+        final Bundle b3 = getMockBundle();
+
+        final Set calledHooks = new HashSet();
+        final EventHook eh1 = new EventHook()
+        {
+            public void event(ServiceEvent event, Collection contexts)
+            {
+                calledHooks.add(this);
+            }
+        };
+        final EventHook eh2 = new EventHook()
+        {
+            public void event(ServiceEvent event, Collection contexts)
+            {
+                calledHooks.add(this);
+                for (Iterator it = contexts.iterator(); it.hasNext();)
+                {
+                    BundleContext bc = (BundleContext) it.next();
+                    if (bc.getBundle() == b1)
+                    {
+                        it.remove();
+                    }
+                    if (bc.getBundle() == b2)
+                    {
+                        it.remove();
+                    }
+                }
+            }
+        };
+
+        Logger logger = new Logger();
+        ServiceRegistry registry = new ServiceRegistry(logger)
+        {
+            public List getEventHooks()
+            {
+                return Collections.unmodifiableList(
+                    Arrays.asList(new EventHook[]
+                    {
+                        eh1, eh2
+                    }));
+            }
+        };
+
+        // -- Set up event dispatcher
+
+        EventDispatcher ed = EventDispatcher.start(logger);
+        EventDispatcher.shutdown(); // stop the thread - would be nicer if we could create one without a thread for testing
+        ed.setServiceRegistry(registry);
+
+        // -- Register some listeners
+        final List fired = Collections.synchronizedList(new ArrayList());
+        ServiceListener sl1 = new ServiceListener()
+        {
+            public void serviceChanged(ServiceEvent arg0)
+            {
+                fired.add(this);
+                System.out.println("*** sl1");
+            }
+        };
+        ed.addListener(b1, ServiceListener.class, sl1, null);
+
+        ServiceListener sl2 = new ServiceListener()
+        {
+            public void serviceChanged(ServiceEvent arg0)
+            {
+                fired.add(this);
+                System.out.println("*** sl2");
+            }
+        };
+        ed.addListener(b2, ServiceListener.class, sl2, null);
+
+        ServiceListener sl3 = new ServiceListener()
+        {
+            public void serviceChanged(ServiceEvent arg0)
+            {
+                fired.add(this);
+                System.out.println("*** sl3");
+            }
+        };
+        ed.addListener(b3, ServiceListener.class, sl3, null);
+
+        // --- make the invocation
+        ServiceReference sr = (ServiceReference) EasyMock.createNiceMock(ServiceReference.class);
+        EasyMock.expect(sr.getProperty(Constants.OBJECTCLASS)).andReturn(new String[]
+            {
+                "java.lang.String"
+            }).anyTimes();
+        sr.isAssignableTo(b1, String.class.getName());
+        EasyMock.expectLastCall().andReturn(Boolean.TRUE).anyTimes();
+        sr.isAssignableTo(b2, String.class.getName());
+        EasyMock.expectLastCall().andReturn(Boolean.TRUE).anyTimes();
+        sr.isAssignableTo(b3, String.class.getName());
+        EasyMock.expectLastCall().andReturn(Boolean.TRUE).anyTimes();
+        EasyMock.replay(new Object[]
+            {
+                sr
+            });
+
+        ServiceEvent event = new ServiceEvent(ServiceEvent.REGISTERED, sr);
+
+        assertEquals("Precondition failed", 0, fired.size());
+
+        Framework framework = (Framework) EasyMock.createNiceMock(Framework.class);
+        EasyMock.replay(new Object[]
+            {
+                framework
+            });
+
+        ed.fireServiceEvent(event, framework);
+        assertEquals(1, fired.size());
+        assertSame(sl3, fired.iterator().next());
+
+        assertEquals(2, calledHooks.size());
+        assertTrue(calledHooks.contains(eh1));
+        assertTrue(calledHooks.contains(eh2));
+    }
+
+    private Bundle getMockBundle()
+    {
+        BundleContext bc = (BundleContext) EasyMock.createNiceMock(BundleContext.class);
+        Bundle b = (Bundle) EasyMock.createNiceMock(Bundle.class);
+        EasyMock.expect(b.getBundleContext()).andReturn(bc).anyTimes();
+        b.getState();
+        EasyMock.expectLastCall().andReturn(Integer.valueOf(Bundle.ACTIVE)).anyTimes();
+
+        EasyMock.expect(bc.getBundle()).andReturn(b).anyTimes();
+
+        EasyMock.replay(new Object[]
+            {
+                bc, b
+            });
+
+        return b;
+    }
+}
\ No newline at end of file