You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by rw...@apache.org on 2009/12/17 07:43:23 UTC

svn commit: r891557 - in /geronimo/sandbox/rex/org.apache.geronimo.blueprint.jmx.impl: ./ src/main/java/org/apache/geronimo/blueprint/jmx/impl/ src/test/java/org/apache/geronimo/blueprint/jmx/impl/ src/test/java/org/apache/geronimo/blueprint/jmx/impl/t...

Author: rwonly
Date: Thu Dec 17 06:43:23 2009
New Revision: 891557

URL: http://svn.apache.org/viewvc?rev=891557&view=rev
Log:
Add some unit tests for impl, thanks Siqi for the patch

Added:
    geronimo/sandbox/rex/org.apache.geronimo.blueprint.jmx.impl/src/test/java/org/apache/geronimo/blueprint/jmx/impl/BlueprintMetadataTest.java
    geronimo/sandbox/rex/org.apache.geronimo.blueprint.jmx.impl/src/test/java/org/apache/geronimo/blueprint/jmx/impl/BlueprintStateTest.java
Removed:
    geronimo/sandbox/rex/org.apache.geronimo.blueprint.jmx.impl/src/test/java/org/apache/geronimo/blueprint/jmx/impl/test/
Modified:
    geronimo/sandbox/rex/org.apache.geronimo.blueprint.jmx.impl/pom.xml
    geronimo/sandbox/rex/org.apache.geronimo.blueprint.jmx.impl/src/main/java/org/apache/geronimo/blueprint/jmx/impl/BlueprintMetadata.java

Modified: geronimo/sandbox/rex/org.apache.geronimo.blueprint.jmx.impl/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/rex/org.apache.geronimo.blueprint.jmx.impl/pom.xml?rev=891557&r1=891556&r2=891557&view=diff
==============================================================================
--- geronimo/sandbox/rex/org.apache.geronimo.blueprint.jmx.impl/pom.xml (original)
+++ geronimo/sandbox/rex/org.apache.geronimo.blueprint.jmx.impl/pom.xml Thu Dec 17 06:43:23 2009
@@ -49,18 +49,18 @@
             <version>4.7</version>
             <scope>test</scope>
         </dependency>
-        <dependency>
-            <groupId>org.easymock</groupId>
-            <artifactId>easymock</artifactId>
-            <version>2.5.2</version>
-            <scope>test</scope>
-        </dependency>
-        <dependency>
-            <groupId>org.easymock</groupId>
-            <artifactId>easymockclassextension</artifactId>
-            <version>2.4</version>
-            <scope>test</scope>
-        </dependency>  
+	<dependency>
+	  <groupId>org.jmock</groupId>
+	  <artifactId>jmock-junit4</artifactId>
+	  <version>2.5.1</version>
+	  <scope>test</scope>
+	</dependency>
+	<dependency>
+	  <groupId>org.jmock</groupId>
+	  <artifactId>jmock-legacy</artifactId>
+	  <version>2.5.1</version>
+	  <scope>test</scope>
+	</dependency> 
     </dependencies>
 
     <build>

Modified: geronimo/sandbox/rex/org.apache.geronimo.blueprint.jmx.impl/src/main/java/org/apache/geronimo/blueprint/jmx/impl/BlueprintMetadata.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/rex/org.apache.geronimo.blueprint.jmx.impl/src/main/java/org/apache/geronimo/blueprint/jmx/impl/BlueprintMetadata.java?rev=891557&r1=891556&r2=891557&view=diff
==============================================================================
--- geronimo/sandbox/rex/org.apache.geronimo.blueprint.jmx.impl/src/main/java/org/apache/geronimo/blueprint/jmx/impl/BlueprintMetadata.java (original)
+++ geronimo/sandbox/rex/org.apache.geronimo.blueprint.jmx.impl/src/main/java/org/apache/geronimo/blueprint/jmx/impl/BlueprintMetadata.java Thu Dec 17 06:43:23 2009
@@ -52,6 +52,9 @@
      */
     public long getBlueprintContainerServiceId(long bundleId) throws IOException {
         Bundle bpBundle = bundleContext.getBundle(bundleId);
+        if (null == bpBundle)
+            throw new IllegalArgumentException("Invalid bundle id " + bundleId);
+
         String filter = "(&(osgi.blueprint.container.symbolicname=" // no similar one in interfaces
                 + bpBundle.getSymbolicName() + ")(osgi.blueprint.container.version=" + bpBundle.getVersion() + "))";
         ServiceReference[] serviceReferences = null;
@@ -60,7 +63,10 @@
         } catch (InvalidSyntaxException e) {
             throw new RuntimeException(e);
         }
-        return serviceReferences == null ? -1 : (Long) serviceReferences[0].getProperty(Constants.SERVICE_ID);
+        if (serviceReferences == null || serviceReferences.length < 1)
+            return -1;
+        else
+            return (Long) serviceReferences[0].getProperty(Constants.SERVICE_ID);
     }
 
     public long[] getBlueprintContainerServiceIds() throws IOException {
@@ -70,6 +76,9 @@
         } catch (InvalidSyntaxException e) {
             throw new RuntimeException(e);
         }
+        if (serviceReferences == null || serviceReferences.length < 1)
+            return null;
+        
         long[] serviceIds = new long[serviceReferences.length];
         for (int i = 0; i < serviceReferences.length; i++) {
             serviceIds[i] = (Long) serviceReferences[i].getProperty(Constants.SERVICE_ID);
@@ -79,7 +88,7 @@
 
     public String[] getComponentIds(long containerServiceId) {
         BlueprintContainer container = getBlueprintContainer(containerServiceId);
-        return (String[])container.getComponentIds().toArray(new String[0]);
+        return (String[]) container.getComponentIds().toArray(new String[0]);
     }
 
     /*
@@ -103,6 +112,11 @@
         String ids[] = new String[components.size()];
         int i = 0;
         for (ComponentMetadata component : components) {
+            // from compendium 121.4.8, in-line managers can not be retrieved by getMetadata, which will return null.
+            // Because in-line managers are actually the object values.
+            // Here we ignore it.
+            if(null == component)
+                continue;
             ids[i++] = component.getId();
         }
         return ids;
@@ -124,7 +138,7 @@
             throw new RuntimeException(e);
         }
 
-        if (serviceReferences == null) {
+        if (serviceReferences == null || serviceReferences.length <1) {
             throw new IllegalArgumentException("Invalid BlueprintContainer service id: " + containerServiceId);
         }
         return (BlueprintContainer) bundleContext.getService(serviceReferences[0]);

Added: geronimo/sandbox/rex/org.apache.geronimo.blueprint.jmx.impl/src/test/java/org/apache/geronimo/blueprint/jmx/impl/BlueprintMetadataTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/rex/org.apache.geronimo.blueprint.jmx.impl/src/test/java/org/apache/geronimo/blueprint/jmx/impl/BlueprintMetadataTest.java?rev=891557&view=auto
==============================================================================
--- geronimo/sandbox/rex/org.apache.geronimo.blueprint.jmx.impl/src/test/java/org/apache/geronimo/blueprint/jmx/impl/BlueprintMetadataTest.java (added)
+++ geronimo/sandbox/rex/org.apache.geronimo.blueprint.jmx.impl/src/test/java/org/apache/geronimo/blueprint/jmx/impl/BlueprintMetadataTest.java Thu Dec 17 06:43:23 2009
@@ -0,0 +1,343 @@
+/*
+ * 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.geronimo.blueprint.jmx.impl;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.geronimo.blueprint.jmx.BlueprintMetadataMBean;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.integration.junit4.JMock;
+import org.jmock.integration.junit4.JUnit4Mockery;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.Version;
+import org.osgi.service.blueprint.container.BlueprintContainer;
+import org.osgi.service.blueprint.reflect.BeanMetadata;
+import org.osgi.service.blueprint.reflect.ServiceMetadata;
+
+@RunWith(JMock.class)
+public class BlueprintMetadataTest {
+
+    private BlueprintMetadata metadata;
+    private Mockery mockery = new JUnit4Mockery();
+    
+    private BundleContext mockContext;
+    private Bundle  mockBundle;
+    private ServiceReference[] mockServiceReferences = new ServiceReference[1];
+    //private ServiceReference mockServiceReference;
+    private BlueprintContainer mockContainer;
+    private ServiceMetadata mockServiceMetadata;
+    private BeanMetadata mockBeanMetadata;
+    
+    @Before
+    public void setUp() throws Exception {
+        mockContext = mockery.mock(BundleContext.class);
+        mockBundle = mockery.mock(Bundle.class);
+        mockServiceReferences[0] = mockery.mock(ServiceReference.class);
+        mockContainer = mockery.mock(BlueprintContainer.class);
+        mockServiceMetadata = mockery.mock(ServiceMetadata.class);
+        mockBeanMetadata = mockery.mock(BeanMetadata.class);
+        
+        metadata = new BlueprintMetadata(mockContext);
+    }
+
+    @After
+    public void tearDown() throws Exception {
+
+    }
+
+    @Test
+    public void validGetBlueprintContainerServiceId() throws Exception {
+        final long bundleId = 12;
+        final long serviceId = 7117;
+        
+        mockery.checking(new Expectations() {
+            {
+                allowing(mockContext).getBundle(bundleId);
+                will(returnValue(mockBundle));
+                oneOf(mockContext).getServiceReferences(with(any(String.class)), with(any(String.class)));
+                will(returnValue(mockServiceReferences));
+            }
+        });
+        // is there any need?
+        mockery.checking(new Expectations() {
+            {
+                allowing(mockBundle).getSymbolicName();
+                will(returnValue("org.apache.geronimo.blueprint.testXXX"));
+                allowing(mockBundle).getVersion();
+                will(returnValue(Version.emptyVersion));
+            }
+        });
+        mockery.checking(new Expectations() {
+            {
+                allowing(mockServiceReferences[0]).getProperty(Constants.SERVICE_ID);
+                will(returnValue(serviceId));
+            }
+        });
+
+        assertEquals(serviceId, metadata.getBlueprintContainerServiceId(bundleId));
+    }
+
+    @Test
+    public void invalidParaInGetBlueprintContainerServiceId() throws Exception
+    {
+        mockery.checking(new Expectations() {
+            {
+                allowing(mockContext).getBundle(with(any(Long.class)));
+                will(returnValue(null));
+            }
+        });
+        try{
+            metadata.getBlueprintContainerServiceId(-10);
+        } catch(Exception ex)
+        {
+            assertTrue(ex instanceof IllegalArgumentException);
+        }
+    }
+    
+    @Test
+    public void cannotFindAssociatedContainerServiceId() throws Exception
+    {
+        final long bundleId = 12;
+        
+        mockery.checking(new Expectations() {
+            {
+                allowing(mockContext).getBundle(bundleId);
+                will(returnValue(mockBundle));
+                oneOf(mockContext).getServiceReferences(with(any(String.class)), with(any(String.class)));
+                //return null if no services are registered which satisfy the search
+                will(returnValue(null));
+            }
+        });
+        // is there any need?
+        mockery.checking(new Expectations() {
+            {
+                allowing(mockBundle).getSymbolicName();
+                will(returnValue("org.apache.geronimo.blueprint.testXXX"));
+                allowing(mockBundle).getVersion();
+                will(returnValue(Version.emptyVersion));
+            }
+        });
+        assertEquals(-1, metadata.getBlueprintContainerServiceId(bundleId));
+    }
+    
+    @Test
+    public void normalBlueprintContainerServiceIds() throws Exception {
+        final long serviceId = 7117;
+        final long [] serviceIds = new long[]{serviceId};
+        
+        mockery.checking(new Expectations() {
+            {
+                oneOf(mockContext).getServiceReferences(with(any(String.class)), with(any(String.class)));
+                will(returnValue(mockServiceReferences));
+            }
+        });
+        mockery.checking(new Expectations() {
+            {
+                allowing(mockServiceReferences[0]).getProperty(Constants.SERVICE_ID);
+                will(returnValue(serviceId));
+            }
+        });
+        
+        assertArrayEquals(serviceIds, metadata.getBlueprintContainerServiceIds());
+    }
+
+    @Test 
+    public void noBlueprintContainerServiceIds() throws Exception
+    {//It is impossible according to osgi spec, here just test the robustness of code
+        mockery.checking(new Expectations() {
+            {
+                oneOf(mockContext).getServiceReferences(with(any(String.class)), with(any(String.class)));
+                //return null if no services are registered which satisfy the search
+                will(returnValue(null));
+            }
+        });
+        assertNull(metadata.getBlueprintContainerServiceIds());
+    }
+    
+    @Test
+    public void nomalGetComponentIds() throws Exception {
+        final long serviceId = 7117;
+        final Set cidset = new HashSet();
+        final String [] cidarray = new String[]{".component-1", ".component-2", ".component-5"};
+        cidset.addAll(Arrays.asList(cidarray));
+        
+        mockery.checking(new Expectations(){
+            {
+                oneOf(mockContext).getServiceReferences(with(any(String.class)), with(any(String.class)));
+                will(returnValue(mockServiceReferences));
+                oneOf(mockContext).getService(mockServiceReferences[0]);
+                will(returnValue(mockContainer));
+            }
+        });
+        mockery.checking(new Expectations(){
+            {
+                oneOf(mockContainer).getComponentIds();
+                will(returnValue(cidset));
+            }
+        });
+        assertArrayEquals(cidarray, metadata.getComponentIds(serviceId));
+    }
+
+    @Test
+    public void normalGetComponentIdsByType() throws Exception {
+        final long serviceId = 7117;
+        final String [] cidarray = new String[]{".component-1"};
+        final Collection cMetadatas = new ArrayList();
+        cMetadatas.add(mockServiceMetadata);
+        
+        mockery.checking(new Expectations(){
+            {
+                oneOf(mockContext).getServiceReferences(with(any(String.class)), with(any(String.class)));
+                will(returnValue(mockServiceReferences));
+                oneOf(mockContext).getService(mockServiceReferences[0]);
+                will(returnValue(mockContainer));
+            }
+        });
+        mockery.checking(new Expectations(){
+            {
+                oneOf(mockContainer).getMetadata(ServiceMetadata.class);
+                will(returnValue(cMetadatas));
+            }
+        });
+        mockery.checking(new Expectations(){
+            {
+                oneOf(mockServiceMetadata).getId();
+                will(returnValue(cidarray[0]));
+            }
+        });
+        
+        assertArrayEquals(cidarray, 
+                metadata.getComponentIdsByType(serviceId, BlueprintMetadataMBean.SERVICE_METADATA));
+    }
+
+    public void invalidParaInGetComponentIdsByType() throws Exception {
+        final long serviceId = 7117;
+                
+        mockery.checking(new Expectations(){
+            {
+                allowing(mockContext).getServiceReferences(with(any(String.class)), with(any(String.class)));
+                will(returnValue(mockServiceReferences));
+                allowing(mockContext).getService(mockServiceReferences[0]);
+                will(returnValue(mockContainer));
+            }
+        });
+        
+        try {
+            metadata.getComponentIdsByType(serviceId, null);
+        }catch(Exception ex)
+        {
+            assertTrue(ex instanceof IllegalArgumentException);
+        }
+        try {
+            metadata.getComponentIdsByType(serviceId, BlueprintMetadataMBean.COMPONENT_METADATA);
+        }catch(Exception ex)
+        {
+            assertTrue(ex instanceof IllegalArgumentException);
+        }
+    }
+    @Test
+    public void testGetComponentMetadata() throws Exception {
+        final long serviceId = 7117;
+        final String componentId = ".component-1";
+        final String [] cidarray = new String[]{componentId};
+        final List emptyList = new ArrayList();
+        
+        mockery.checking(new Expectations(){
+            {
+                oneOf(mockContext).getServiceReferences(with(any(String.class)), with(any(String.class)));
+                will(returnValue(mockServiceReferences));
+                oneOf(mockContext).getService(mockServiceReferences[0]);
+                will(returnValue(mockContainer));
+            }
+        });
+        mockery.checking(new Expectations(){
+            {
+                oneOf(mockContainer).getComponentMetadata(componentId);
+                will(returnValue(mockBeanMetadata));
+            }
+        });
+        mockery.checking(new Expectations(){
+            {
+                allowing(mockBeanMetadata).getDependsOn();
+                will(returnValue(emptyList));
+
+                allowing(mockBeanMetadata).getArguments();
+                will(returnValue(emptyList));
+                allowing(mockBeanMetadata).getFactoryComponent();
+                will(returnValue(null));
+                allowing(mockBeanMetadata).getProperties();
+                will(returnValue(emptyList));
+                ignoring(mockBeanMetadata);
+            }
+        });
+        metadata.getComponentMetadata(serviceId, componentId);
+        mockery.assertIsSatisfied();
+    }
+    
+    @Test
+    public void fail2GetBlueprintContainer() throws Exception
+    {
+        final long serviceId = 7117;
+        mockery.checking(new Expectations(){
+            {
+                exactly(3).of(mockContext).getServiceReferences(with(any(String.class)), with(any(String.class)));
+                will(returnValue(null));
+            }
+        });
+        
+        try{
+            metadata.getComponentIds(serviceId);
+        }catch(Exception ex)
+        {
+            assertTrue(ex instanceof IllegalArgumentException);
+        }
+        
+        try{
+            metadata.getComponentIdsByType(serviceId, BlueprintMetadataMBean.SERVICE_METADATA);
+        }catch(Exception ex)
+        {
+            assertTrue(ex instanceof IllegalArgumentException);
+        }
+        
+        try{
+            metadata.getComponentMetadata(serviceId, "xxxx");
+        }catch(Exception ex)
+        {
+            assertTrue(ex instanceof IllegalArgumentException);
+        }
+    }
+}

Added: geronimo/sandbox/rex/org.apache.geronimo.blueprint.jmx.impl/src/test/java/org/apache/geronimo/blueprint/jmx/impl/BlueprintStateTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/rex/org.apache.geronimo.blueprint.jmx.impl/src/test/java/org/apache/geronimo/blueprint/jmx/impl/BlueprintStateTest.java?rev=891557&view=auto
==============================================================================
--- geronimo/sandbox/rex/org.apache.geronimo.blueprint.jmx.impl/src/test/java/org/apache/geronimo/blueprint/jmx/impl/BlueprintStateTest.java (added)
+++ geronimo/sandbox/rex/org.apache.geronimo.blueprint.jmx.impl/src/test/java/org/apache/geronimo/blueprint/jmx/impl/BlueprintStateTest.java Thu Dec 17 06:43:23 2009
@@ -0,0 +1,58 @@
+/*
+ * 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.geronimo.blueprint.jmx.impl;
+
+import org.apache.geronimo.blueprint.jmx.BlueprintStateMBean;
+import org.jmock.Mockery;
+import org.jmock.integration.junit4.JMock;
+import org.jmock.integration.junit4.JUnit4Mockery;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.osgi.framework.BundleContext;
+
+//@RunWith(JMock.class)
+public class BlueprintStateTest {
+//    private Mockery mockery = new JUnit4Mockery();
+//    private BlueprintState state; 
+//    
+//    private BundleContext mockContext; 
+//    
+//    @Before
+//    public void setUp() throws Exception {
+//        mockContext = mockery.mock(BundleContext.class);
+//        
+//        state = new BlueprintState(mockContext);
+//    }
+    @Test
+    public void testGetBlueprintBundleIds(){
+        
+    }
+    
+    @Test
+    public void testGetLastEvent(){
+        
+    }
+    
+    @Test
+    public void testGetLastEvents(){
+        
+    }
+    
+}