Author: atk
Date: Wed Jan 13 21:59:01 2010
New Revision: 898957

URL: http://svn.apache.org/viewvc?rev=898957&view=rev
Log:
ARIES-31 Adding integration tests & updating handler close

Added:
    
incubator/aries/trunk/jmx/jmx-itests/src/test/java/org/apache/aries/jmx/framework/BundleStateMBeanTest.java
   (with props)
Modified:
    
incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/BundleState.java
    
incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/BundleStateMBeanHandler.java
    
incubator/aries/trunk/jmx/jmx-itests/src/test/java/org/apache/aries/jmx/AbstractIntegrationTest.java

Modified: 
incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/BundleState.java
URL: 
http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/BundleState.java?rev=898957&r1=898956&r2=898957&view=diff
==============================================================================
--- 
incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/BundleState.java
 (original)
+++ 
incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/BundleState.java
 Wed Jan 13 21:59:01 2010
@@ -36,6 +36,7 @@
 import java.util.List;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.concurrent.RejectedExecutionException;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
@@ -63,8 +64,8 @@
 import org.osgi.service.startlevel.StartLevel;
 
 /**
- * Implementation of <code>BundleStateMBean</code> which emits JMX 
<code>Notification</code>
- * on <code>Bundle</code> state changes
+ * Implementation of <code>BundleStateMBean</code> which emits JMX 
<code>Notification</code> on <code>Bundle</code>
+ * state changes
  * 
  * @version $Rev$ $Date$
  */
@@ -108,7 +109,7 @@
     }
 
     /**
-     *  @see org.osgi.jmx.framework.BundleStateMBean#getHeaders(long)
+     * @see org.osgi.jmx.framework.BundleStateMBean#getHeaders(long)
      */
     @SuppressWarnings("unchecked")
     public TabularData getHeaders(long bundleId) throws IOException, 
IllegalArgumentException {
@@ -199,7 +200,7 @@
         return startLevel.getBundleStartLevel(bundle);
     }
 
-    /** 
+    /**
      * @see org.osgi.jmx.framework.BundleStateMBean#getState(long)
      */
     public String getState(long bundleId) throws IOException, 
IllegalArgumentException {
@@ -287,8 +288,7 @@
      */
     public void postDeregister() {
         if (registrations.decrementAndGet() < 1) {
-            bundleContext.removeBundleListener(bundleListener);
-            eventDispatcher.shutdown();
+            shutDownDispatcher();
         }
     }
 
@@ -296,10 +296,10 @@
      * @see javax.management.MBeanRegistration#postRegister(java.lang.Boolean)
      */
     public void postRegister(Boolean registrationDone) {
-            if (registrationDone && registrations.incrementAndGet() == 1) {
-                eventDispatcher = Executors.newSingleThreadExecutor();
-                bundleContext.addBundleListener(bundleListener);
-            } 
+        if (registrationDone && registrations.incrementAndGet() == 1) {
+            eventDispatcher = Executors.newSingleThreadExecutor();
+            bundleContext.addBundleListener(bundleListener);
+        }
     }
 
     /**
@@ -318,8 +318,8 @@
             if (bundleListener == null) {
                 bundleListener = new BundleListener() {
                     public void bundleChanged(BundleEvent event) {
-                        final Notification notification = new 
Notification(EVENT, OBJECTNAME, notificationSequenceNumber
-                                .getAndIncrement());
+                        final Notification notification = new 
Notification(EVENT, OBJECTNAME,
+                                notificationSequenceNumber.getAndIncrement());
                         try {
                             notification.setUserData(new 
BundleEventData(event).toCompositeData());
                             eventDispatcher.submit(new Runnable() {
@@ -327,24 +327,39 @@
                                     sendNotification(notification);
                                 }
                             });
+                        } catch (RejectedExecutionException re) {
+                            logger.log(LogService.LOG_WARNING, "Task rejected 
for JMX Notification dispatch of event ["
+                                    + event + "] - Dispatcher may have been 
shutdown");
                         } catch (Exception e) {
-                            logger.log(LogService.LOG_WARNING, "Exception 
occured on JMX Notification dispatch for event ["
-                                    + event + "]", e);
+                            logger.log(LogService.LOG_WARNING,
+                                    "Exception occured on JMX Notification 
dispatch for event [" + event + "]", e);
                         }
                     }
                 };
             }
         } finally {
             lock.unlock();
-        }  
+        }
         return name;
     }
 
     /*
+     * Shuts down the notification dispatcher
+     */
+    protected void shutDownDispatcher() {
+        if (bundleListener != null) {
+            bundleContext.removeBundleListener(bundleListener);  
+        }
+        if (eventDispatcher != null) {
+            eventDispatcher.shutdown(); 
+        }
+    }
+
+    /*
      * Returns the ExecutorService used to dispatch Notifications
      */
-    public ExecutorService getEventDispatcher() {
+    protected ExecutorService getEventDispatcher() {
         return eventDispatcher;
     }
-    
+
 }

Modified: 
incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/BundleStateMBeanHandler.java
URL: 
http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/BundleStateMBeanHandler.java?rev=898957&r1=898956&r2=898957&view=diff
==============================================================================
--- 
incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/BundleStateMBeanHandler.java
 (original)
+++ 
incubator/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/BundleStateMBeanHandler.java
 Wed Jan 13 21:59:01 2010
@@ -44,6 +44,7 @@
     private Logger logger;
     private String name;
     private StandardMBean mbean;
+    private BundleState bundleStateMBean;
     private BundleContext bundleContext;
     private ServiceReference packageAdminRef;
     private ServiceReference startLevelRef;
@@ -63,7 +64,7 @@
         PackageAdmin packageAdmin = (PackageAdmin) 
bundleContext.getService(packageAdminRef);
         startLevelRef = 
bundleContext.getServiceReference(StartLevel.class.getName());
         StartLevel startLevel = (StartLevel) 
bundleContext.getService(startLevelRef);
-        BundleStateMBean bundleStateMBean = new BundleState(bundleContext, 
packageAdmin, startLevel, logger);
+        bundleStateMBean = new BundleState(bundleContext, packageAdmin, 
startLevel, logger);
         try {
             mbean = new RegistrableStandardEmitterMBean(bundleStateMBean, 
BundleStateMBean.class);
         } catch (NotCompliantMBeanException e) {
@@ -106,6 +107,10 @@
             }
             startLevelRef = null;
         }
+        // ensure dispatcher is shutdown even if postDeRegister is not honored
+        if (bundleStateMBean != null) {
+            bundleStateMBean.shutDownDispatcher();
+        }
     }
 
    

Modified: 
incubator/aries/trunk/jmx/jmx-itests/src/test/java/org/apache/aries/jmx/AbstractIntegrationTest.java
URL: 
http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-itests/src/test/java/org/apache/aries/jmx/AbstractIntegrationTest.java?rev=898957&r1=898956&r2=898957&view=diff
==============================================================================
--- 
incubator/aries/trunk/jmx/jmx-itests/src/test/java/org/apache/aries/jmx/AbstractIntegrationTest.java
 (original)
+++ 
incubator/aries/trunk/jmx/jmx-itests/src/test/java/org/apache/aries/jmx/AbstractIntegrationTest.java
 Wed Jan 13 21:59:01 2010
@@ -34,9 +34,11 @@
 import org.ops4j.pax.exam.Inject;
 import org.ops4j.pax.exam.Option;
 import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
+import org.osgi.framework.Version;
 
 /**
  * 
@@ -73,9 +75,10 @@
     @After
     public void tearDown() throws Exception {
         bundleContext.ungetService(reference);
-        registration.unregister();
+        //plainRegistration.unregister();
     }
     
+    @SuppressWarnings("unchecked")
     protected <T> T getMBean(String name, Class<T> type) {
         ObjectName objectName = null;
         try {
@@ -89,7 +92,24 @@
                 type, false);
         return mbean;
     }
-
+    
+    protected Bundle getBundle(String symbolicName) {
+        return getBundle(symbolicName, null);
+    }
+    
+    protected Bundle getBundle(String bundleSymbolicName, String version) {
+        Bundle result = null;
+        for (Bundle b : bundleContext.getBundles()) {
+            if ( b.getSymbolicName().equals(bundleSymbolicName) ) {
+                if (version == null || 
b.getVersion().equals(Version.parseVersion(version))) {
+                    result = b;
+                    break;
+                }
+            }
+        }
+        return result;
+    }
+    
     public static MavenArtifactProvisionOption mavenBundle(String groupId, 
String artifactId) {
         return 
CoreOptions.mavenBundle().groupId(groupId).artifactId(artifactId).versionAsInProject();
     }

Added: 
incubator/aries/trunk/jmx/jmx-itests/src/test/java/org/apache/aries/jmx/framework/BundleStateMBeanTest.java
URL: 
http://svn.apache.org/viewvc/incubator/aries/trunk/jmx/jmx-itests/src/test/java/org/apache/aries/jmx/framework/BundleStateMBeanTest.java?rev=898957&view=auto
==============================================================================
--- 
incubator/aries/trunk/jmx/jmx-itests/src/test/java/org/apache/aries/jmx/framework/BundleStateMBeanTest.java
 (added)
+++ 
incubator/aries/trunk/jmx/jmx-itests/src/test/java/org/apache/aries/jmx/framework/BundleStateMBeanTest.java
 Wed Jan 13 21:59:01 2010
@@ -0,0 +1,271 @@
+/**
+ *  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.aries.jmx.framework;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.ops4j.pax.exam.CoreOptions.provision;
+import static org.ops4j.pax.swissbox.tinybundles.core.TinyBundles.newBundle;
+import static org.ops4j.pax.swissbox.tinybundles.core.TinyBundles.withBnd;
+import static org.osgi.jmx.framework.BundleStateMBean.OBJECTNAME;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import javax.management.InstanceNotFoundException;
+import javax.management.Notification;
+import javax.management.NotificationListener;
+import javax.management.ObjectName;
+import javax.management.openmbean.TabularData;
+
+import org.apache.aries.jmx.AbstractIntegrationTest;
+import org.apache.aries.jmx.codec.BundleData.Header;
+import org.junit.Before;
+import org.junit.Test;
+import org.ops4j.pax.exam.CoreOptions;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.Configuration;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
+import org.osgi.jmx.framework.BundleStateMBean;
+
+/**
+ * 
+ * 
+ * 
+ * @version $Rev$ $Date$
+ */
+public class BundleStateMBeanTest extends AbstractIntegrationTest {
+
+    @Configuration
+    public static Option[] configuration() {
+        Option[] options = CoreOptions
+                .options(
+                        CoreOptions.equinox(),
+                        mavenBundle("org.apache.felix", 
"org.apache.felix.configadmin"),
+                        mavenBundle("org.ops4j.pax.logging", 
"pax-logging-api"),
+                        mavenBundle("org.ops4j.pax.logging", 
"pax-logging-service"),
+                        mavenBundle("org.osgi", "org.osgi.compendium"),
+                        mavenBundle("org.apache.aries.jmx", 
"org.apache.aries.jmx"),
+                        provision(newBundle()
+                                
.add(org.apache.aries.jmx.test.bundlea.Activator.class)
+                                
.add(org.apache.aries.jmx.test.bundlea.api.InterfaceA.class)
+                                
.add(org.apache.aries.jmx.test.bundlea.impl.A.class)
+                                .set(Constants.BUNDLE_SYMBOLICNAME, 
"org.apache.aries.jmx.test.bundlea")
+                                .set(Constants.BUNDLE_VERSION, "2.0.0")
+                                .set(Constants.EXPORT_PACKAGE, 
"org.apache.aries.jmx.test.bundlea.api;version=2.0.0")
+                                .set(Constants.IMPORT_PACKAGE,
+                                        
"org.osgi.framework;version=1.5.0,org.osgi.util.tracker,org.apache.aries.jmx.test.bundleb.api;version=1.1.0;resolution:=optional"
 +
+                                        ",org.osgi.service.cm")
+                                .set(Constants.BUNDLE_ACTIVATOR,
+                                        
org.apache.aries.jmx.test.bundlea.Activator.class.getName())
+                                .build(withBnd())),
+                        provision(newBundle()
+                                
.add(org.apache.aries.jmx.test.bundleb.Activator.class)
+                                
.add(org.apache.aries.jmx.test.bundleb.api.InterfaceB.class)
+                                
.add(org.apache.aries.jmx.test.bundleb.api.MSF.class)
+                                
.add(org.apache.aries.jmx.test.bundleb.impl.B.class)
+                                
.set(Constants.BUNDLE_SYMBOLICNAME,"org.apache.aries.jmx.test.bundleb")
+                                .set(Constants.BUNDLE_VERSION, "1.0.0")
+                                
.set(Constants.EXPORT_PACKAGE,"org.apache.aries.jmx.test.bundleb.api;version=1.1.0")
+                                
.set(Constants.IMPORT_PACKAGE,"org.osgi.framework;version=1.5.0,org.osgi.util.tracker,"
 +
+                                               
"org.osgi.service.cm,org.apache.aries.jmx.test.fragmentc")
+                                .set(Constants.BUNDLE_ACTIVATOR,
+                                        
org.apache.aries.jmx.test.bundleb.Activator.class.getName())
+                                .build(withBnd())),
+                        provision(newBundle()
+                                
.add(org.apache.aries.jmx.test.fragmentc.C.class)
+                                .set(Constants.BUNDLE_SYMBOLICNAME, 
"org.apache.aries.jmx.test.fragc")
+                                .set(Constants.FRAGMENT_HOST, 
"org.apache.aries.jmx.test.bundlea")
+                                .set(Constants.EXPORT_PACKAGE, 
"org.apache.aries.jmx.test.fragmentc")
+                                .build(withBnd())),
+                        provision(newBundle()
+                                .set(Constants.BUNDLE_SYMBOLICNAME, 
"org.apache.aries.jmx.test.bundled")
+                                .set(Constants.BUNDLE_VERSION, "3.0.0")
+                                .set(Constants.REQUIRE_BUNDLE, 
"org.apache.aries.jmx.test.bundlea;bundle-version=2.0.0")
+                                .build(withBnd()))
+                        );
+        options = updateOptions(options);
+        return options;
+    }
+    
+    @Before
+    public void doSetUp() throws Exception {
+        super.setUp();
+        int i=0;
+        while (true) {
+            try {
+                mbeanServer.getObjectInstance(new 
ObjectName(BundleStateMBean.OBJECTNAME));
+                break;
+            } catch (InstanceNotFoundException e) {
+                if (i == 5) {
+                    throw new Exception("BundleStateMBean not available after 
waiting 5 seconds");
+                }
+            }
+            i++;
+            Thread.sleep(1000);
+        }
+    }
+
+    @Test
+    public void testMBeanInterface() throws Exception {
+        
+        BundleStateMBean mbean = getMBean(OBJECTNAME, BundleStateMBean.class);
+        assertNotNull(mbean);
+        
+        //get bundles
+        
+        Bundle a = getBundle("org.apache.aries.jmx.test.bundlea");
+        assertNotNull(a);
+        
+        Bundle b = getBundle("org.apache.aries.jmx.test.bundleb");
+        assertNotNull(b);
+        
+        Bundle frag = getBundle("org.apache.aries.jmx.test.fragc");
+        assertNotNull(frag);
+
+        Bundle d = getBundle("org.apache.aries.jmx.test.bundled");
+        assertNotNull(d);
+        
+        // exportedPackages
+        
+        String[] exports = mbean.getExportedPackages(a.getBundleId());
+        assertEquals(2, exports.length);
+        
+        List<String> packages = Arrays.asList(exports);
+        
assertTrue(packages.contains("org.apache.aries.jmx.test.bundlea.api;2.0.0"));
+        
assertTrue(packages.contains("org.apache.aries.jmx.test.fragmentc;0.0.0"));
+        
+        //fragments
+        
+        long[] fragments = mbean.getFragments(a.getBundleId());
+        assertEquals(1, fragments.length);
+        assertEquals(frag.getBundleId() , fragments[0]);
+       
+        //headers
+
+        TabularData headers = mbean.getHeaders(b.getBundleId());
+        assertNotNull(headers);
+        assertEquals(BundleStateMBean.HEADERS_TYPE, headers.getTabularType());
+        assertTrue(headers.values().size() >= 4 );
+        assertEquals("org.apache.aries.jmx.test.bundleb", 
Header.from(headers.get(new Object[] 
{Constants.BUNDLE_SYMBOLICNAME})).getValue());
+        
+        //hosts 
+        
+        long[] hosts = mbean.getHosts(frag.getBundleId());
+        assertEquals(1, hosts.length);
+        assertEquals(a.getBundleId() , hosts[0]);
+        
+        //imported packages
+           
+        String[] imports = mbean.getImportedPackages(a.getBundleId());
+        assertTrue(imports.length >= 3);
+        List<String> importedPackages = Arrays.asList(imports);
+        assertTrue(importedPackages.contains("org.osgi.framework;1.5.0"));
+        
assertTrue(importedPackages.contains("org.apache.aries.jmx.test.bundleb.api;1.1.0"));
+        
+        //last modified
+        
+        assertTrue(mbean.getLastModified(b.getBundleId()) > 0);
+        
+        //location
+        
+        assertEquals(b.getLocation(), mbean.getLocation(b.getBundleId()));
+        
+        //registered services
+        
+        long[] serviceIds = mbean.getRegisteredServices(a.getBundleId());
+        assertEquals(1, serviceIds.length);
+        
+        //required bundles
+        
+        long[] required = mbean.getRequiredBundles(d.getBundleId());
+        assertEquals(1, required.length);
+        assertEquals(a.getBundleId(), required[0]);
+        
+        //requiring bundles
+        
+        long[] requiring = mbean.getRequiringBundles(a.getBundleId());
+        assertEquals(1, requiring.length);
+        assertEquals(d.getBundleId(), requiring[0]);
+        
+        //services in use
+        
+        long[] servicesInUse = mbean.getServicesInUse(a.getBundleId());
+        assertEquals(1, servicesInUse.length);
+        
+        //start level
+        
+        long startLevel = mbean.getStartLevel(b.getBundleId());
+        assertTrue(startLevel >= 0);
+        
+        //state 
+        
+        assertEquals("ACTIVE", mbean.getState(b.getBundleId()));
+        
+        //isFragment
+        
+        assertFalse(mbean.isFragment(b.getBundleId()));
+        assertTrue(mbean.isFragment(frag.getBundleId()));
+        
+        //isRemovalPending
+        assertFalse(mbean.isRemovalPending(b.getBundleId()));
+        
+        // isRequired
+       
+        assertTrue(mbean.isRequired(a.getBundleId()));
+        assertFalse(mbean.isRequired(b.getBundleId()));
+        
+        // listBundles
+        
+        TabularData bundlesTable = mbean.listBundles();
+        assertNotNull(bundlesTable);
+        assertEquals(BundleStateMBean.BUNDLES_TYPE, 
bundlesTable.getTabularType());
+        assertEquals(bundleContext.getBundles().length, 
bundlesTable.values().size());
+        
+        
+        // notifications
+        
+        final List<Notification> received = new ArrayList<Notification>();
+      
+        mbeanServer.addNotificationListener(new 
ObjectName(BundleStateMBean.OBJECTNAME), new NotificationListener() {
+            public void handleNotification(Notification notification, Object 
handback) {
+               received.add(notification);
+            }
+        }, null, null);
+        
+        assertEquals(Bundle.ACTIVE, b.getState());
+        b.stop();
+        assertEquals(Bundle.RESOLVED, b.getState());
+        b.start();
+        assertEquals(Bundle.ACTIVE, b.getState());
+        
+        int i = 0;
+        while (received.size() < 2 && i < 3) {
+            Thread.sleep(1000);
+            i++;
+        }
+        
+        assertEquals(2, received.size());
+        
+    }
+    
+
+}

Propchange: 
incubator/aries/trunk/jmx/jmx-itests/src/test/java/org/apache/aries/jmx/framework/BundleStateMBeanTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/aries/trunk/jmx/jmx-itests/src/test/java/org/apache/aries/jmx/framework/BundleStateMBeanTest.java
------------------------------------------------------------------------------
    svn:keywords = Revision Date


Reply via email to