FELIX-3046 Ensure empty arrays and collections are written out to the files and read again

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1186839 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/configadmin/src/main/java/org/apache/felix/cm/file/ConfigurationHandler.java b/configadmin/src/main/java/org/apache/felix/cm/file/ConfigurationHandler.java
index bb4b012..1d53ad2 100644
--- a/configadmin/src/main/java/org/apache/felix/cm/file/ConfigurationHandler.java
+++ b/configadmin/src/main/java/org/apache/felix/cm/file/ConfigurationHandler.java
@@ -96,8 +96,8 @@
 
     protected static final String CRLF = "\r\n";
 
-    protected static final Map type2Code;
     protected static final Map code2Type;
+    protected static final Map type2Code;
 
     // set of valid characters for "symblic-name"
     private static final BitSet NAME_CHARS;
@@ -336,23 +336,23 @@
         List list = new ArrayList();
         for ( ;; )
         {
-            if ( !checkNext( pr, TOKEN_VAL_OPEN ) )
+            int c = read(pr);
+            if ( c == TOKEN_VAL_OPEN )
             {
-                return null;
+                Object value = readSimple( typeCode, pr );
+                if ( value == null )
+                {
+                    // abort due to error
+                    return null;
+                }
+
+                ensureNext( pr, TOKEN_VAL_CLOS );
+
+                list.add( value );
+
+                c = read( pr );
             }
 
-            Object value = readSimple( typeCode, pr );
-            if ( value == null )
-            {
-                // abort due to error
-                return null;
-            }
-
-            ensureNext( pr, TOKEN_VAL_CLOS );
-
-            list.add( value );
-
-            int c = read( pr );
             if ( c == TOKEN_ARR_CLOS )
             {
                 Class type = ( Class ) code2Type.get( new Integer( typeCode ) );
@@ -380,23 +380,23 @@
         Collection collection = new ArrayList();
         for ( ;; )
         {
-            if ( !checkNext( pr, TOKEN_VAL_OPEN ) )
-            {
-                return null;
-            }
-
-            Object value = readSimple( typeCode, pr );
-            if ( value == null )
-            {
-                // abort due to error
-                return null;
-            }
-
-            ensureNext( pr, TOKEN_VAL_CLOS );
-
-            collection.add( value );
-
             int c = read( pr );
+            if ( c == TOKEN_VAL_OPEN )
+            {
+                Object value = readSimple( typeCode, pr );
+                if ( value == null )
+                {
+                    // abort due to error
+                    return null;
+                }
+
+                ensureNext( pr, TOKEN_VAL_CLOS );
+
+                collection.add( value );
+
+                c = read( pr );
+            }
+
             if ( c == TOKEN_VEC_CLOS )
             {
                 return collection;
@@ -676,11 +676,6 @@
     private static void writeArray( Writer out, Object arrayValue ) throws IOException
     {
         int size = Array.getLength( arrayValue );
-        if ( size == 0 )
-        {
-            return;
-        }
-
         writeType( out, arrayValue.getClass().getComponentType() );
         out.write( TOKEN_ARR_OPEN );
         for ( int i = 0; i < size; i++ )
@@ -697,22 +692,25 @@
     {
         if ( collection.isEmpty() )
         {
-            return;
+            out.write( TOKEN_VEC_OPEN );
+            out.write( TOKEN_VEC_CLOS );
         }
-
-        Iterator ci = collection.iterator();
-        Object firstElement = ci.next();
-
-        writeType( out, firstElement.getClass() );
-        out.write( TOKEN_VEC_OPEN );
-        writeSimple( out, firstElement );
-
-        while ( ci.hasNext() )
+        else
         {
-            out.write( TOKEN_COMMA );
-            writeSimple( out, ci.next() );
+            Iterator ci = collection.iterator();
+            Object firstElement = ci.next();
+
+            writeType( out, firstElement.getClass() );
+            out.write( TOKEN_VEC_OPEN );
+            writeSimple( out, firstElement );
+
+            while ( ci.hasNext() )
+            {
+                out.write( TOKEN_COMMA );
+                writeSimple( out, ci.next() );
+            }
+            out.write( TOKEN_VEC_CLOS );
         }
-        out.write( TOKEN_VEC_CLOS );
     }
 
 
diff --git a/configadmin/src/test/java/org/apache/felix/cm/file/FilePersistenceManagerTest.java b/configadmin/src/test/java/org/apache/felix/cm/file/FilePersistenceManagerTest.java
index f4ef903..0b6444e 100644
--- a/configadmin/src/test/java/org/apache/felix/cm/file/FilePersistenceManagerTest.java
+++ b/configadmin/src/test/java/org/apache/felix/cm/file/FilePersistenceManagerTest.java
@@ -22,6 +22,7 @@
 import java.io.File;
 import java.io.IOException;
 import java.lang.reflect.Array;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Dictionary;
 import java.util.Enumeration;
@@ -115,6 +116,15 @@
     }
 
 
+    public void testEmptyArray() throws IOException
+    {
+        check( "StringArray", new String[0] );
+        check( "IntArray", new int[0] );
+        check( "CharArray", new char[0] );
+        check( "ShortArray", new short[0] );
+    }
+
+
     public void testVector() throws IOException
     {
         check( "StringVector", new Vector( Arrays.asList( new String[]
@@ -124,6 +134,15 @@
     }
 
 
+    public void testEmptyVector() throws IOException
+    {
+        check( "StringArray", new Vector() );
+        check( "IntArray", new Vector() );
+        check( "CharArray", new Vector() );
+        check( "ShortArray", new Vector() );
+    }
+
+
     public void testList() throws IOException
     {
         check( "StringList", Arrays.asList( new String[]
@@ -133,6 +152,15 @@
     }
 
 
+    public void testEmptyList() throws IOException
+    {
+        check( "StringArray", new ArrayList(0) );
+        check( "IntArray", new ArrayList(0) );
+        check( "CharArray", new ArrayList(0) );
+        check( "ShortArray", new ArrayList(0) );
+    }
+
+
     public void testMultiValue() throws IOException
     {
         Dictionary props = new Hashtable();