FELIX-903 testcases to verify felix.cm.loglevel property use

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@738383 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/configadmin/src/test/java/org/apache/felix/cm/MockBundleContext.java b/configadmin/src/test/java/org/apache/felix/cm/MockBundleContext.java
index cb79b8c..c0aaae2 100644
--- a/configadmin/src/test/java/org/apache/felix/cm/MockBundleContext.java
+++ b/configadmin/src/test/java/org/apache/felix/cm/MockBundleContext.java
@@ -18,9 +18,11 @@
  */
 package org.apache.felix.cm;
 
+
 import java.io.File;
 import java.io.InputStream;
 import java.util.Dictionary;
+import java.util.Properties;
 
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
@@ -33,50 +35,80 @@
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 
+
 /**
  * The <code>MockBundleContext</code> is a dummy implementation of the
  * <code>BundleContext</code> interface. No methods are implemented here, that
- * is all methods have no effect and return <code>null</code> if a return
- * value is specified.
+ * is all methods have no effect and return <code>null</code> if a return value
+ * is specified.
  * <p>
  * Extensions may overwrite methods as see fit.
  */
 public class MockBundleContext implements BundleContext
 {
 
-    /* (non-Javadoc)
-     * @see org.osgi.framework.BundleContext#addBundleListener(org.osgi.framework.BundleListener)
+    private final Properties properties = new Properties();
+
+
+    public void setProperty( String name, String value )
+    {
+        if ( value == null )
+        {
+            properties.remove( name );
+        }
+        else
+        {
+            properties.setProperty( name, value );
+        }
+    }
+
+
+    /*
+     * (non-Javadoc)
+     * @see
+     * org.osgi.framework.BundleContext#addBundleListener(org.osgi.framework
+     * .BundleListener)
      */
     public void addBundleListener( BundleListener arg0 )
     {
     }
 
 
-    /* (non-Javadoc)
-     * @see org.osgi.framework.BundleContext#addFrameworkListener(org.osgi.framework.FrameworkListener)
+    /*
+     * (non-Javadoc)
+     * @see
+     * org.osgi.framework.BundleContext#addFrameworkListener(org.osgi.framework
+     * .FrameworkListener)
      */
     public void addFrameworkListener( FrameworkListener arg0 )
     {
     }
 
 
-    /* (non-Javadoc)
-     * @see org.osgi.framework.BundleContext#addServiceListener(org.osgi.framework.ServiceListener)
+    /*
+     * (non-Javadoc)
+     * @see
+     * org.osgi.framework.BundleContext#addServiceListener(org.osgi.framework
+     * .ServiceListener)
      */
     public void addServiceListener( ServiceListener arg0 )
     {
     }
 
 
-    /* (non-Javadoc)
-     * @see org.osgi.framework.BundleContext#addServiceListener(org.osgi.framework.ServiceListener, java.lang.String)
+    /*
+     * (non-Javadoc)
+     * @see
+     * org.osgi.framework.BundleContext#addServiceListener(org.osgi.framework
+     * .ServiceListener, java.lang.String)
      */
     public void addServiceListener( ServiceListener arg0, String arg1 ) throws InvalidSyntaxException
     {
     }
 
 
-    /* (non-Javadoc)
+    /*
+     * (non-Javadoc)
      * @see org.osgi.framework.BundleContext#createFilter(java.lang.String)
      */
     public Filter createFilter( String arg0 ) throws InvalidSyntaxException
@@ -85,8 +117,11 @@
     }
 
 
-    /* (non-Javadoc)
-     * @see org.osgi.framework.BundleContext#getAllServiceReferences(java.lang.String, java.lang.String)
+    /*
+     * (non-Javadoc)
+     * @see
+     * org.osgi.framework.BundleContext#getAllServiceReferences(java.lang.String
+     * , java.lang.String)
      */
     public ServiceReference[] getAllServiceReferences( String arg0, String arg1 ) throws InvalidSyntaxException
     {
@@ -94,7 +129,8 @@
     }
 
 
-    /* (non-Javadoc)
+    /*
+     * (non-Javadoc)
      * @see org.osgi.framework.BundleContext#getBundle()
      */
     public Bundle getBundle()
@@ -103,7 +139,8 @@
     }
 
 
-    /* (non-Javadoc)
+    /*
+     * (non-Javadoc)
      * @see org.osgi.framework.BundleContext#getBundle(long)
      */
     public Bundle getBundle( long arg0 )
@@ -112,7 +149,8 @@
     }
 
 
-    /* (non-Javadoc)
+    /*
+     * (non-Javadoc)
      * @see org.osgi.framework.BundleContext#getBundles()
      */
     public Bundle[] getBundles()
@@ -121,7 +159,8 @@
     }
 
 
-    /* (non-Javadoc)
+    /*
+     * (non-Javadoc)
      * @see org.osgi.framework.BundleContext#getDataFile(java.lang.String)
      */
     public File getDataFile( String arg0 )
@@ -130,17 +169,20 @@
     }
 
 
-    /* (non-Javadoc)
+    /*
+     * (non-Javadoc)
      * @see org.osgi.framework.BundleContext#getProperty(java.lang.String)
      */
-    public String getProperty( String arg0 )
+    public String getProperty( String name )
     {
-        return null;
+        return properties.getProperty( name );
     }
 
 
-    /* (non-Javadoc)
-     * @see org.osgi.framework.BundleContext#getService(org.osgi.framework.ServiceReference)
+    /*
+     * (non-Javadoc)
+     * @seeorg.osgi.framework.BundleContext#getService(org.osgi.framework.
+     * ServiceReference)
      */
     public Object getService( ServiceReference arg0 )
     {
@@ -148,8 +190,10 @@
     }
 
 
-    /* (non-Javadoc)
-     * @see org.osgi.framework.BundleContext#getServiceReference(java.lang.String)
+    /*
+     * (non-Javadoc)
+     * @see
+     * org.osgi.framework.BundleContext#getServiceReference(java.lang.String)
      */
     public ServiceReference getServiceReference( String arg0 )
     {
@@ -157,8 +201,11 @@
     }
 
 
-    /* (non-Javadoc)
-     * @see org.osgi.framework.BundleContext#getServiceReferences(java.lang.String, java.lang.String)
+    /*
+     * (non-Javadoc)
+     * @see
+     * org.osgi.framework.BundleContext#getServiceReferences(java.lang.String,
+     * java.lang.String)
      */
     public ServiceReference[] getServiceReferences( String arg0, String arg1 ) throws InvalidSyntaxException
     {
@@ -166,7 +213,8 @@
     }
 
 
-    /* (non-Javadoc)
+    /*
+     * (non-Javadoc)
      * @see org.osgi.framework.BundleContext#installBundle(java.lang.String)
      */
     public Bundle installBundle( String arg0 ) throws BundleException
@@ -175,8 +223,10 @@
     }
 
 
-    /* (non-Javadoc)
-     * @see org.osgi.framework.BundleContext#installBundle(java.lang.String, java.io.InputStream)
+    /*
+     * (non-Javadoc)
+     * @see org.osgi.framework.BundleContext#installBundle(java.lang.String,
+     * java.io.InputStream)
      */
     public Bundle installBundle( String arg0, InputStream arg1 ) throws BundleException
     {
@@ -184,8 +234,10 @@
     }
 
 
-    /* (non-Javadoc)
-     * @see org.osgi.framework.BundleContext#registerService(java.lang.String[], java.lang.Object, java.util.Dictionary)
+    /*
+     * (non-Javadoc)
+     * @see org.osgi.framework.BundleContext#registerService(java.lang.String[],
+     * java.lang.Object, java.util.Dictionary)
      */
     public ServiceRegistration registerService( String[] arg0, Object arg1, Dictionary arg2 )
     {
@@ -193,8 +245,10 @@
     }
 
 
-    /* (non-Javadoc)
-     * @see org.osgi.framework.BundleContext#registerService(java.lang.String, java.lang.Object, java.util.Dictionary)
+    /*
+     * (non-Javadoc)
+     * @see org.osgi.framework.BundleContext#registerService(java.lang.String,
+     * java.lang.Object, java.util.Dictionary)
      */
     public ServiceRegistration registerService( String arg0, Object arg1, Dictionary arg2 )
     {
@@ -202,32 +256,43 @@
     }
 
 
-    /* (non-Javadoc)
-     * @see org.osgi.framework.BundleContext#removeBundleListener(org.osgi.framework.BundleListener)
+    /*
+     * (non-Javadoc)
+     * @see
+     * org.osgi.framework.BundleContext#removeBundleListener(org.osgi.framework
+     * .BundleListener)
      */
     public void removeBundleListener( BundleListener arg0 )
     {
     }
 
 
-    /* (non-Javadoc)
-     * @see org.osgi.framework.BundleContext#removeFrameworkListener(org.osgi.framework.FrameworkListener)
+    /*
+     * (non-Javadoc)
+     * @see
+     * org.osgi.framework.BundleContext#removeFrameworkListener(org.osgi.framework
+     * .FrameworkListener)
      */
     public void removeFrameworkListener( FrameworkListener arg0 )
     {
     }
 
 
-    /* (non-Javadoc)
-     * @see org.osgi.framework.BundleContext#removeServiceListener(org.osgi.framework.ServiceListener)
+    /*
+     * (non-Javadoc)
+     * @see
+     * org.osgi.framework.BundleContext#removeServiceListener(org.osgi.framework
+     * .ServiceListener)
      */
     public void removeServiceListener( ServiceListener arg0 )
     {
     }
 
 
-    /* (non-Javadoc)
-     * @see org.osgi.framework.BundleContext#ungetService(org.osgi.framework.ServiceReference)
+    /*
+     * (non-Javadoc)
+     * @seeorg.osgi.framework.BundleContext#ungetService(org.osgi.framework.
+     * ServiceReference)
      */
     public boolean ungetService( ServiceReference arg0 )
     {
diff --git a/configadmin/src/test/java/org/apache/felix/cm/impl/ConfigurationManagerTest.java b/configadmin/src/test/java/org/apache/felix/cm/impl/ConfigurationManagerTest.java
index d438c9e..9a26440 100644
--- a/configadmin/src/test/java/org/apache/felix/cm/impl/ConfigurationManagerTest.java
+++ b/configadmin/src/test/java/org/apache/felix/cm/impl/ConfigurationManagerTest.java
@@ -27,6 +27,7 @@
 
 import org.apache.felix.cm.MockBundleContext;
 import org.apache.felix.cm.MockLogService;
+import org.osgi.framework.BundleContext;
 import org.osgi.service.log.LogService;
 import org.osgi.util.tracker.ServiceTracker;
 
@@ -160,6 +161,35 @@
     }
 
 
+    public void testLogSetup()
+    {
+        final MockBundleContext bundleContext = new MockBundleContext();
+        ConfigurationManager configMgr = createConfigurationManager( null );
+
+        // default value is 2
+        bundleContext.setProperty( "felix.cm.loglevel", null );
+        configMgr.start( bundleContext );
+        assertEquals( 2, getLogLevel( configMgr ) );
+        configMgr.stop( bundleContext );
+
+        // illegal number yields default value
+        bundleContext.setProperty( "felix.cm.loglevel", "not-a-number" );
+        configMgr.start( bundleContext );
+        assertEquals( 2, getLogLevel( configMgr ) );
+        configMgr.stop( bundleContext );
+
+        bundleContext.setProperty( "felix.cm.loglevel", "-100" );
+        configMgr.start( bundleContext );
+        assertEquals( -100, getLogLevel( configMgr ) );
+        configMgr.stop( bundleContext );
+
+        bundleContext.setProperty( "felix.cm.loglevel", "4" );
+        configMgr.start( bundleContext );
+        assertEquals( 4, getLogLevel( configMgr ) );
+        configMgr.stop( bundleContext );
+    }
+
+
     private void assertNoLog( ConfigurationManager configMgr, int level, String message, Throwable t )
     {
         try
@@ -206,7 +236,25 @@
         }
         catch ( Throwable ignore )
         {
-            ignore.printStackTrace( System.out );
+            throw ( IllegalArgumentException ) new IllegalArgumentException( "Cannot set logLevel field value" )
+                .initCause( ignore );
+        }
+    }
+
+
+    private static int getLogLevel( ConfigurationManager configMgr )
+    {
+        final String fieldName = "logLevel";
+        try
+        {
+            Field field = configMgr.getClass().getDeclaredField( fieldName );
+            field.setAccessible( true );
+            return field.getInt( configMgr );
+        }
+        catch ( Throwable ignore )
+        {
+            throw ( IllegalArgumentException ) new IllegalArgumentException( "Cannot get logLevel field value" )
+                .initCause( ignore );
         }
     }
 
@@ -229,7 +277,8 @@
         }
         catch ( Throwable ignore )
         {
-            ignore.printStackTrace( System.out );
+            throw ( IllegalArgumentException ) new IllegalArgumentException( "Cannot set logTracker field value" )
+                .initCause( ignore );
         }
 
         return configMgr;