Applied patch to add some unit tests to File Install. (FELIX-942)


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@749327 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/fileinstall/src/test/java/org/apache/felix/fileinstall/DirectoryWatcherTest.java b/fileinstall/src/test/java/org/apache/felix/fileinstall/DirectoryWatcherTest.java
new file mode 100644
index 0000000..dc87374
--- /dev/null
+++ b/fileinstall/src/test/java/org/apache/felix/fileinstall/DirectoryWatcherTest.java
@@ -0,0 +1,307 @@
+/*
+ * 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.fileinstall;
+
+
+import java.io.File;
+import java.util.Dictionary;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.Set;
+
+import junit.framework.TestCase;
+
+import org.easymock.ArgumentsMatcher;
+import org.easymock.MockControl;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+import org.osgi.service.packageadmin.PackageAdmin;
+
+
+/**
+ * Test class for the DirectoryWatcher
+ */
+public class DirectoryWatcherTest extends TestCase
+{
+
+    private final static String TEST = "test.key";
+    Dictionary props = new Hashtable();
+    DirectoryWatcher dw;
+    MockControl mockBundleContextControl;
+    BundleContext mockBundleContext;
+    MockControl mockPackageAdminControl;
+    PackageAdmin mockPackageAdmin;
+    MockControl mockBundleControl;
+    Bundle mockBundle;
+    MockControl mockConfigurationAdminControl;
+    ConfigurationAdmin mockConfigurationAdmin;
+    MockControl mockConfigurationControl;
+    Configuration mockConfiguration;
+
+
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+        mockBundleContextControl = MockControl.createControl( BundleContext.class );
+        mockBundleContext = ( BundleContext ) mockBundleContextControl.getMock();
+        mockPackageAdminControl = MockControl.createControl( PackageAdmin.class );
+        mockPackageAdmin = ( PackageAdmin ) mockPackageAdminControl.getMock();
+        mockBundleControl = MockControl.createControl( Bundle.class );
+        mockBundle = ( Bundle ) mockBundleControl.getMock();
+        mockConfigurationAdminControl = MockControl.createControl( ConfigurationAdmin.class );
+        mockConfigurationAdmin = ( ConfigurationAdmin ) mockConfigurationAdminControl.getMock();
+        mockConfigurationControl = MockControl.createControl( Configuration.class );
+        mockConfiguration = ( Configuration ) mockConfigurationControl.getMock();
+    }
+
+
+    public void testGetLongWithNonExistentProperty()
+    {
+        mockBundleContextControl.replay();
+        dw = new DirectoryWatcher( props, mockBundleContext );
+        assertEquals( "getLong gives the default value for non-existing properties", 100, dw.getLong( props, TEST, 100 ) );
+    }
+
+
+    public void testGetLongWithExistentProperty()
+    {
+        props.put( TEST, "33" );
+        mockBundleContextControl.replay();
+        dw = new DirectoryWatcher( props, mockBundleContext );
+        assertEquals( "getLong retrieves the right property value", 33, dw.getLong( props, TEST, 100 ) );
+    }
+
+
+    public void testGetLongWithIncorrectValue()
+    {
+        props.put( TEST, "incorrect" );
+
+        mockBundleContext.getServiceReference( "org.osgi.service.log.LogService" );
+        mockBundleContextControl.setReturnValue( null );
+        mockBundleContextControl.replay();
+        dw = new DirectoryWatcher( props, mockBundleContext );
+        assertEquals( "getLong retrieves the right property value", 100, dw.getLong( props, TEST, 100 ) );
+    }
+
+
+    public void testParameterAfterInitialization()
+    {
+        props.put( DirectoryWatcher.POLL, "500" );
+        props.put( DirectoryWatcher.DEBUG, "1" );
+        props.put( DirectoryWatcher.START_NEW_BUNDLES, "false" );
+        props.put( DirectoryWatcher.DIR, new File( "src/test/resources" ).getAbsolutePath() );
+        mockBundleContextControl.replay();
+        dw = new DirectoryWatcher( props, mockBundleContext );
+
+        assertEquals( "POLL parameter correctly read", 500l, dw.poll );
+        assertEquals( "DEBUG parameter correctly read", 1l, dw.debug );
+        assertTrue( "DIR parameter correctly read", dw.watchedDirectory.getAbsolutePath().endsWith(
+            "src/test/resources" ) );
+        assertEquals( "START_NEW_BUNDLES parameter correctly read", false, dw.startBundles );
+    }
+
+
+    public void testDefaultParametersAreSetAfterEmptyInitialization()
+    {
+        props.put( DirectoryWatcher.DIR, new File( "src/test/resources" ).getAbsolutePath() );
+        mockBundleContextControl.replay();
+        dw = new DirectoryWatcher( props, mockBundleContext );
+
+        assertEquals( "Default POLL parameter correctly read", 2000l, dw.poll );
+        assertEquals( "Default DEBUG parameter correctly read", -1l, dw.debug );
+        assertEquals( "Default START_NEW_BUNDLES parameter correctly read", true, dw.startBundles );
+    }
+
+
+    public void testParsePidWithoutFactoryPid()
+    {
+        mockBundleContextControl.replay();
+        dw = new DirectoryWatcher( props, mockBundleContext );
+        String path = "pid.cfg";
+        assertEquals( "Pid without Factory Pid calculated", "pid", dw.parsePid( path )[0] );
+        assertEquals( "Pid without Factory Pid calculated", null, dw.parsePid( path )[1] );
+    }
+
+
+    public void testParsePidWithFactoryPid()
+    {
+        mockBundleContextControl.replay();
+        dw = new DirectoryWatcher( props, mockBundleContext );
+
+        String path = "factory-pid.cfg";
+        assertEquals( "Pid with Factory Pid calculated", "factory", dw.parsePid( path )[0] );
+        assertEquals( "Pid with Factory Pid calculated", "pid", dw.parsePid( path )[1] );
+    }
+
+
+    public void testIsFragment() throws Exception
+    {
+        mockBundleContext.createFilter( "" );
+        mockBundleContextControl.setMatcher( MockControl.ALWAYS_MATCHER );
+        mockBundleContextControl.setReturnValue( null );
+        mockBundleContextControl.replay();
+        mockPackageAdmin.getBundleType( mockBundle );
+        mockPackageAdminControl.setReturnValue( PackageAdmin.BUNDLE_TYPE_FRAGMENT );
+        mockPackageAdminControl.replay();
+        mockBundleControl.replay();
+
+        FileInstall.padmin = new MockServiceTracker( mockBundleContext, mockPackageAdmin );
+        dw = new DirectoryWatcher( props, mockBundleContext );
+
+        assertTrue( "Fragment type correctly retrieved from Package Admin service", dw.isFragment( mockBundle ) );
+
+        mockPackageAdminControl.verify();
+        mockBundleContextControl.verify();
+    }
+
+
+    public void testGetNewFactoryConfiguration() throws Exception
+    {
+        mockConfigurationControl.replay();
+        mockConfigurationAdmin.listConfigurations( "" );
+        mockConfigurationAdminControl.setMatcher( MockControl.ALWAYS_MATCHER );
+        mockConfigurationAdminControl.setReturnValue( null );
+        mockConfigurationAdmin.createFactoryConfiguration( "pid", null );
+        mockConfigurationAdminControl.setReturnValue( mockConfiguration );
+        mockConfigurationAdminControl.replay();
+        mockBundleContext.createFilter( "" );
+        mockBundleContextControl.setMatcher( MockControl.ALWAYS_MATCHER );
+        mockBundleContextControl.setReturnValue( null );
+        mockBundleContextControl.replay();
+
+        FileInstall.cmTracker = new MockServiceTracker( mockBundleContext, mockConfigurationAdmin );
+        dw = new DirectoryWatcher( props, mockBundleContext );
+
+        assertEquals( "Factory configuration retrieved", mockConfiguration, dw.getConfiguration( "pid", "factoryPid" ) );
+
+        mockConfigurationAdminControl.verify();
+        mockConfigurationControl.verify();
+        mockBundleContextControl.verify();
+    }
+
+
+    public void testGetExistentFactoryConfiguration() throws Exception
+    {
+        mockConfigurationControl.replay();
+        mockConfigurationAdmin.listConfigurations( "" );
+        mockConfigurationAdminControl.setMatcher( MockControl.ALWAYS_MATCHER );
+        mockConfigurationAdminControl.setReturnValue( new Configuration[]
+            { mockConfiguration } );
+        mockConfigurationAdminControl.replay();
+        mockBundleContext.createFilter( "" );
+        mockBundleContextControl.setMatcher( MockControl.ALWAYS_MATCHER );
+        mockBundleContextControl.setReturnValue( null );
+        mockBundleContextControl.replay();
+
+        FileInstall.cmTracker = new MockServiceTracker( mockBundleContext, mockConfigurationAdmin );
+        dw = new DirectoryWatcher( props, mockBundleContext );
+
+        assertEquals( "Factory configuration retrieved", mockConfiguration, dw.getConfiguration( "pid", "factoryPid" ) );
+
+        mockConfigurationAdminControl.verify();
+        mockConfigurationControl.verify();
+        mockBundleContextControl.verify();
+    }
+
+
+    public void testGetExistentNoFactoryConfiguration() throws Exception
+    {
+        mockConfigurationControl.replay();
+        mockConfigurationAdmin.getConfiguration( "pid", null );
+        mockConfigurationAdminControl.setReturnValue( mockConfiguration );
+        mockConfigurationAdminControl.replay();
+        mockBundleContext.createFilter( "" );
+        mockBundleContextControl.setMatcher( MockControl.ALWAYS_MATCHER );
+        mockBundleContextControl.setReturnValue( null );
+        mockBundleContextControl.replay();
+
+        FileInstall.cmTracker = new MockServiceTracker( mockBundleContext, mockConfigurationAdmin );
+        dw = new DirectoryWatcher( props, mockBundleContext );
+
+        assertEquals( "Factory configuration retrieved", mockConfiguration, dw.getConfiguration( "pid", null ) );
+
+        mockConfigurationAdminControl.verify();
+        mockConfigurationControl.verify();
+        mockBundleContextControl.verify();
+    }
+
+
+    public void testDeleteConfig() throws Exception
+    {
+        mockConfiguration.delete();
+        mockConfigurationControl.replay();
+        mockConfigurationAdmin.getConfiguration( "pid", null );
+        mockConfigurationAdminControl.setReturnValue( mockConfiguration );
+        mockConfigurationAdminControl.replay();
+        mockBundleContext.createFilter( "" );
+        mockBundleContextControl.setMatcher( MockControl.ALWAYS_MATCHER );
+        mockBundleContextControl.setReturnValue( null );
+        mockBundleContextControl.replay();
+
+        FileInstall.cmTracker = new MockServiceTracker( mockBundleContext, mockConfigurationAdmin );
+        dw = new DirectoryWatcher( props, mockBundleContext );
+
+        assertTrue( dw.deleteConfig( new File( "pid.cfg" ) ) );
+
+        mockConfigurationAdminControl.verify();
+        mockConfigurationControl.verify();
+        mockBundleContextControl.verify();
+    }
+
+
+    public void testSetConfiguration() throws Exception
+    {
+        mockConfiguration.getBundleLocation();
+        mockConfigurationControl.setReturnValue( null );
+        mockConfiguration.update( new Hashtable() );
+        mockConfigurationControl.setMatcher( new ArgumentsMatcher()
+        {
+            public boolean matches( Object[] expected, Object[] actual )
+            {
+                return ( actual.length == 1 ) && ( ( Dictionary ) actual[0] ).get( "testkey" ).equals( "testvalue" );
+            }
+
+
+            public String toString( Object[] arg0 )
+            {
+                return arg0.toString();
+            }
+        } );
+        mockConfigurationControl.replay();
+        mockConfigurationAdmin.getConfiguration( "firstcfg", null );
+        mockConfigurationAdminControl.setReturnValue( mockConfiguration );
+        mockConfigurationAdminControl.replay();
+        mockBundleContext.createFilter( "" );
+        mockBundleContextControl.setMatcher( MockControl.ALWAYS_MATCHER );
+        mockBundleContextControl.setReturnValue( null );
+        mockBundleContextControl.replay();
+
+        FileInstall.cmTracker = new MockServiceTracker( mockBundleContext, mockConfigurationAdmin );
+        dw = new DirectoryWatcher( props, mockBundleContext );
+
+        assertTrue( dw.setConfig( new File( "src/test/resources/watched/firstcfg.cfg" ) ) );
+
+        mockConfigurationAdminControl.verify();
+        mockConfigurationControl.verify();
+        mockBundleContextControl.verify();
+    }
+
+}
diff --git a/fileinstall/src/test/java/org/apache/felix/fileinstall/MockServiceTracker.java b/fileinstall/src/test/java/org/apache/felix/fileinstall/MockServiceTracker.java
new file mode 100644
index 0000000..fbba9eb
--- /dev/null
+++ b/fileinstall/src/test/java/org/apache/felix/fileinstall/MockServiceTracker.java
@@ -0,0 +1,87 @@
+/*
+ * 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.fileinstall;
+
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Filter;
+import org.osgi.framework.ServiceReference;
+import org.osgi.util.tracker.ServiceTracker;
+import org.osgi.util.tracker.ServiceTrackerCustomizer;
+
+
+/**
+ * Mock class to simulate a ServiceTracker 
+ * Use the public constructor to set the bundleContext and the 
+ * service instance to be returned
+ */
+public class MockServiceTracker extends ServiceTracker
+{
+    private Object service;
+
+
+    /**
+     * Use this constructor
+     * 
+     * @param context - the bundle context
+     * @param service - the service instance returned by getService()
+     */
+    public MockServiceTracker( BundleContext context, Object service )
+    {
+        super( context, service.getClass().getName(), null );
+        this.service = service;
+    }
+
+
+    MockServiceTracker( BundleContext context, Filter filter, ServiceTrackerCustomizer customizer )
+    {
+        super( context, filter, customizer );
+    }
+
+
+    MockServiceTracker( BundleContext arg0, ServiceReference arg1, ServiceTrackerCustomizer arg2 )
+    {
+        super( arg0, arg1, arg2 );
+    }
+
+
+    MockServiceTracker( BundleContext arg0, String arg1, ServiceTrackerCustomizer arg2 )
+    {
+        super( arg0, arg1, arg2 );
+    }
+
+
+    public Object waitForService( long arg0 ) throws InterruptedException
+    {
+        return service;
+    }
+
+
+    public Object getService()
+    {
+        return service;
+    }
+
+
+    public Object getService( ServiceReference reference )
+    {
+        return getService();
+    }
+
+}
diff --git a/fileinstall/src/test/resources/watched/firstcfg.cfg b/fileinstall/src/test/resources/watched/firstcfg.cfg
new file mode 100644
index 0000000..8ec9331
--- /dev/null
+++ b/fileinstall/src/test/resources/watched/firstcfg.cfg
@@ -0,0 +1 @@
+testkey=testvalue
diff --git a/fileinstall/src/test/resources/watched/firstjar.jar b/fileinstall/src/test/resources/watched/firstjar.jar
new file mode 100644
index 0000000..2a20387
--- /dev/null
+++ b/fileinstall/src/test/resources/watched/firstjar.jar
@@ -0,0 +1 @@
+only a fake jar for testing
\ No newline at end of file
diff --git a/fileinstall/src/test/resources/watched/secondjar.jar b/fileinstall/src/test/resources/watched/secondjar.jar
new file mode 100644
index 0000000..2a20387
--- /dev/null
+++ b/fileinstall/src/test/resources/watched/secondjar.jar
@@ -0,0 +1 @@
+only a fake jar for testing
\ No newline at end of file