FELIX-316 Configuration Admin should use the bundle data storage area by default.
Cope with BundleContext.getDataFile() returning a relative File
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@553090 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/configadmin/src/main/java/org/apache/felix/cm/file/FilePersistenceManager.java b/configadmin/src/main/java/org/apache/felix/cm/file/FilePersistenceManager.java
index dda38bc..7696efe 100644
--- a/configadmin/src/main/java/org/apache/felix/cm/file/FilePersistenceManager.java
+++ b/configadmin/src/main/java/org/apache/felix/cm/file/FilePersistenceManager.java
@@ -254,13 +254,11 @@
*/
public FilePersistenceManager( BundleContext bundleContext, String location )
{
- File locationFile = null;
-
// no configured location, use the config dir in the bundle persistent
// area
if ( location == null && bundleContext != null )
{
- locationFile = bundleContext.getDataFile( DEFAULT_CONFIG_DIR );
+ File locationFile = bundleContext.getDataFile( DEFAULT_CONFIG_DIR );
if ( locationFile != null )
{
location = locationFile.getAbsolutePath();
@@ -274,13 +272,8 @@
location = System.getProperty( "user.dir" ) + "/config";
}
- // ensure the File object for the location (may have been set already)
- if ( locationFile == null )
- {
- locationFile = new File( location );
- }
-
// ensure the file is absolute
+ File locationFile = new File( location );
if ( !locationFile.isAbsolute() )
{
if ( bundleContext != null )
diff --git a/configadmin/src/test/java/org/apache/felix/cm/file/FilePersistenceManagerConstructorTest.java b/configadmin/src/test/java/org/apache/felix/cm/file/FilePersistenceManagerConstructorTest.java
index 6e684df..3fb4cf5 100644
--- a/configadmin/src/test/java/org/apache/felix/cm/file/FilePersistenceManagerConstructorTest.java
+++ b/configadmin/src/test/java/org/apache/felix/cm/file/FilePersistenceManagerConstructorTest.java
@@ -51,17 +51,17 @@
// with null
fpm = new FilePersistenceManager(null);
- assertFpm(fpm, new File(FilePersistenceManager.DEFAULT_CONFIG_DIR).getAbsoluteFile() );
+ assertFpm(fpm, new File(FilePersistenceManager.DEFAULT_CONFIG_DIR) );
// with a relative path
relPath = "test";
fpm = new FilePersistenceManager(relPath);
- assertFpm(fpm, new File(relPath).getAbsoluteFile() );
+ assertFpm(fpm, new File(relPath) );
// with an absolute path
absPath = new File("test").getAbsolutePath();
fpm = new FilePersistenceManager(absPath);
- assertFpm(fpm, new File(absPath).getAbsoluteFile() );
+ assertFpm(fpm, new File(absPath) );
}
@@ -75,22 +75,23 @@
FilePersistenceManager fpm;
String relPath;
String absPath;
+ File dataArea;
// first suite: no BundleContext at all
// with null
fpm = new FilePersistenceManager(null);
- assertFpm(fpm, new File(FilePersistenceManager.DEFAULT_CONFIG_DIR).getAbsoluteFile() );
+ assertFpm(fpm, new File(FilePersistenceManager.DEFAULT_CONFIG_DIR) );
// with a relative path
relPath = "test";
fpm = new FilePersistenceManager(relPath);
- assertFpm(fpm, new File(relPath).getAbsoluteFile() );
+ assertFpm(fpm, new File(relPath) );
// with an absolute path
absPath = new File("test").getAbsolutePath();
fpm = new FilePersistenceManager(absPath);
- assertFpm(fpm, new File(absPath).getAbsoluteFile() );
+ assertFpm(fpm, new File(absPath) );
// second suite: BundleContext without data file
@@ -98,41 +99,59 @@
// with null
fpm = new FilePersistenceManager(bundleContext, null);
- assertFpm(fpm, new File(FilePersistenceManager.DEFAULT_CONFIG_DIR).getAbsoluteFile() );
+ assertFpm(fpm, new File(FilePersistenceManager.DEFAULT_CONFIG_DIR) );
// with a relative path
relPath = "test";
fpm = new FilePersistenceManager(bundleContext, relPath);
- assertFpm(fpm, new File(relPath).getAbsoluteFile() );
+ assertFpm(fpm, new File(relPath) );
// with an absolute path
absPath = new File("test").getAbsolutePath();
fpm = new FilePersistenceManager(bundleContext, absPath);
- assertFpm(fpm, new File(absPath).getAbsoluteFile() );
+ assertFpm(fpm, new File(absPath) );
- // third suite: BundleContext with data file
- File dataArea = new File("bundleData");
+ // third suite: BundleContext with relative data file
+ dataArea = new File("bundleData");
bundleContext = new FilePersistenceManagerBundleContext(dataArea);
// with null
fpm = new FilePersistenceManager(bundleContext, null);
- assertFpm(fpm, new File(dataArea, FilePersistenceManager.DEFAULT_CONFIG_DIR).getAbsoluteFile() );
+ assertFpm(fpm, new File(dataArea, FilePersistenceManager.DEFAULT_CONFIG_DIR) );
// with a relative path
relPath = "test";
fpm = new FilePersistenceManager(bundleContext, relPath);
- assertFpm(fpm, new File(dataArea, relPath).getAbsoluteFile() );
+ assertFpm(fpm, new File(dataArea, relPath) );
// with an absolute path
absPath = new File("test").getAbsolutePath();
fpm = new FilePersistenceManager(bundleContext, absPath);
- assertFpm(fpm, new File(absPath).getAbsoluteFile() );
+ assertFpm(fpm, new File(absPath) );
+
+ // fourth suite: BundleContext with absolute data file
+ dataArea = new File("bundleData").getAbsoluteFile();
+ bundleContext = new FilePersistenceManagerBundleContext(dataArea);
+
+ // with null
+ fpm = new FilePersistenceManager(bundleContext, null);
+ assertFpm(fpm, new File(dataArea, FilePersistenceManager.DEFAULT_CONFIG_DIR) );
+
+ // with a relative path
+ relPath = "test";
+ fpm = new FilePersistenceManager(bundleContext, relPath);
+ assertFpm(fpm, new File(dataArea, relPath) );
+
+ // with an absolute path
+ absPath = new File("test").getAbsolutePath();
+ fpm = new FilePersistenceManager(bundleContext, absPath);
+ assertFpm(fpm, new File(absPath) );
}
private void assertFpm(FilePersistenceManager fpm, File expected) {
- assertEquals( expected, fpm.getLocation() );
+ assertEquals( expected.getAbsoluteFile(), fpm.getLocation() );
}
private static final class FilePersistenceManagerBundleContext extends MockBundleContext {
@@ -141,7 +160,7 @@
private FilePersistenceManagerBundleContext( File dataArea )
{
- this.dataArea = (dataArea != null) ? dataArea.getAbsoluteFile() : null;
+ this.dataArea = dataArea;
}
public File getDataFile( String path )