Author: niallp
Date: Mon Feb  4 00:55:29 2008
New Revision: 618207

URL: http://svn.apache.org/viewvc?rev=618207&view=rev
Log:
Fix for BEANUTILS-302 - NPE in ArrayConverter when converting a string with 
underscores to a string array - reported by Martin Bartlett

Modified:
    
commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ArrayConverter.java
    
commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ArrayConverterTestCase.java

Modified: 
commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ArrayConverter.java
URL: 
http://svn.apache.org/viewvc/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ArrayConverter.java?rev=618207&r1=618206&r2=618207&view=diff
==============================================================================
--- 
commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ArrayConverter.java
 (original)
+++ 
commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ArrayConverter.java
 Mon Feb  4 00:55:29 2008
@@ -434,10 +434,12 @@
             while (true) {
                 int ttype = st.nextToken();
                 if ((ttype == StreamTokenizer.TT_WORD) || (ttype > 0)) {
-                    if (list == null) {
-                        list = new ArrayList();
+                    if (st.sval != null) {
+                        if (list == null) {
+                            list = new ArrayList();
+                        }
+                        list.add(st.sval);
                     }
-                    list.add(st.sval.trim());
                 } else if (ttype == StreamTokenizer.TT_EOF) {
                     break;
                 } else {

Modified: 
commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ArrayConverterTestCase.java
URL: 
http://svn.apache.org/viewvc/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ArrayConverterTestCase.java?rev=618207&r1=618206&r2=618207&view=diff
==============================================================================
--- 
commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ArrayConverterTestCase.java
 (original)
+++ 
commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ArrayConverterTestCase.java
 Mon Feb  4 00:55:29 2008
@@ -361,6 +361,33 @@
     }
 
     /**
+     * Test for BEANUTILS-302 - throwing a NPE when underscore used
+     */
+    public void testUnderscore_BEANUTILS_302() {
+        String value = "first_value,second_value";
+        ArrayConverter converter = new ArrayConverter(String[].class, new 
StringConverter());
+
+        // test underscore not allowed (the default)
+        String[] result = (String[])converter.convert(String[].class, value);
+        assertNotNull("result.null", result);
+        assertEquals("result.length", 4, result.length);
+        assertEquals("result[0]", "first", result[0]);
+        assertEquals("result[1]", "value", result[1]);
+        assertEquals("result[2]", "second", result[2]);
+        assertEquals("result[3]", "value", result[3]);
+
+        // configure the converter to allow underscore
+        converter.setAllowedChars(new char[] {'.', '-', '_'});
+
+        // test underscore allowed
+        result = (String[])converter.convert(String[].class, value);
+        assertNotNull("result.null", result);
+        assertEquals("result.length", 2, result.length);
+        assertEquals("result[0]", "first_value", result[0]);
+        assertEquals("result[1]", "second_value", result[1]);
+    }
+
+    /**
      * Check that two arrays are the same.
      * @param msg Test prefix msg
      * @param expected Expected Array value


Reply via email to