TRB-8: Incorrect handling of a null return value
Thanks to Gunther Olesch for the patch and Jürgen Hoffmann for assitance.
Note that addition of commons-io to dependencies relates to commented test code.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/turbine/core/trunk@417101 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/project.xml b/project.xml
index 90b807e..4a5ae86 100644
--- a/project.xml
+++ b/project.xml
@@ -442,6 +442,11 @@
       <url>http://jakarta.apache.org/commons/fileupload/</url>
     </dependency>
     <dependency>
+      <id>commons-io</id>
+      <version>1.1</version>
+      <url>http://jakarta.apache.org/commons/io/</url>
+    </dependency>
+    <dependency>
       <id>commons-lang</id>
       <version>2.1</version>
       <url>http://jakarta.apache.org/commons/lang/</url>
diff --git a/src/java/org/apache/turbine/util/uri/TemplateURI.java b/src/java/org/apache/turbine/util/uri/TemplateURI.java
index b0d1ea2..a8acc56 100644
--- a/src/java/org/apache/turbine/util/uri/TemplateURI.java
+++ b/src/java/org/apache/turbine/util/uri/TemplateURI.java
@@ -2,7 +2,7 @@
 
 
 /*
- * Copyright 2001-2004 The Apache Software Foundation.
+ * Copyright 2001-2006 The Apache Software Foundation.
  *
  * Licensed under the Apache License, Version 2.0 (the "License")
  * you may not use this file except in compliance with the License.
@@ -273,9 +273,16 @@
                     !key.equalsIgnoreCase(CGI_TEMPLATE_PARAM))
             {
                 String[] values = pp.getStrings(key);
-                for (int i = 0; i < values.length; i++)
+                if(values != null)
                 {
-                    add(type, key, values[i]);
+                    for (int i = 0; i < values.length; i++)
+                    {
+                        add(type, key, values[i]);
+                    }
+                }
+                else 
+                {
+                    add(type, key, "");
                 }
             }
         }
diff --git a/src/java/org/apache/turbine/util/uri/TurbineURI.java b/src/java/org/apache/turbine/util/uri/TurbineURI.java
index 926e70c..6bc290e 100644
--- a/src/java/org/apache/turbine/util/uri/TurbineURI.java
+++ b/src/java/org/apache/turbine/util/uri/TurbineURI.java
@@ -831,9 +831,16 @@
                     !key.equalsIgnoreCase(CGI_SCREEN_PARAM))
             {
                 String[] values = pp.getStrings(key);
-                for (int i = 0; i < values.length; i++)
+                if(values != null)
                 {
-                    add(type, key, values[i]);
+                    for (int i = 0; i < values.length; i++)
+                    {
+                        add(type, key, values[i]);
+                    }
+                }
+                else
+                {
+                    add(type, key, "");
                 }
             }
         }
diff --git a/src/test/org/apache/turbine/util/uri/TurbineURITest.java b/src/test/org/apache/turbine/util/uri/TurbineURITest.java
index 61224b0..023bc89 100644
--- a/src/test/org/apache/turbine/util/uri/TurbineURITest.java
+++ b/src/test/org/apache/turbine/util/uri/TurbineURITest.java
@@ -18,6 +18,10 @@
 
 import org.apache.commons.configuration.BaseConfiguration;
 import org.apache.commons.configuration.Configuration;
+//import org.apache.commons.fileupload.disk.DiskFileItemFactory;
+//import org.apache.commons.fileupload.FileItem;
+import org.apache.fulcrum.parser.DefaultParameterParser;
+import org.apache.fulcrum.parser.ParameterParser;
 import org.apache.turbine.services.ServiceManager;
 import org.apache.turbine.services.TurbineServices;
 import org.apache.turbine.test.BaseTestCase;
@@ -129,4 +133,43 @@
         assertEquals("/context/servlet/turbine", turi.getRelativeLink());
     }
 
+    public void testAddEmptyParameterParser()
+    {
+        ParameterParser pp = new DefaultParameterParser();
+        turi.add(1, pp); // 1 = query data
+        assertEquals("/context/servlet/turbine", turi.getRelativeLink());
+}
+    public void testAddParameterParser()
+    {
+        ParameterParser pp = new DefaultParameterParser();
+        pp.add("test", "");
+        turi.add(1, pp); // 1 = query data
+        assertEquals("/context/servlet/turbine?test=", turi.getRelativeLink());
+        turi.removeQueryData("test");
+        assertEquals("/context/servlet/turbine", turi.getRelativeLink());
+        
+        pp = new DefaultParameterParser();
+        pp.add("test", (String) null);
+        turi.add(1, pp); // 1 = query data
+        // Should make the following work so as to be consistent with directly added values. 
+        //assertEquals("/context/servlet/turbine?test=null", turi.getRelativeLink());
+        turi.removeQueryData("test");
+        assertEquals("/context/servlet/turbine", turi.getRelativeLink());
+
+        // TRB-8
+        //
+        // This is commented out for now as it results in a ClassCastException.
+        // The 2_3 branch parser changes need to be merged into the fulcrum code. 
+        //
+        //pp = new DefaultParameterParser();
+        //DiskFileItemFactory factory = new DiskFileItemFactory(10240, null);
+        //FileItem test = factory.createItem("upload-field", "application/octet-stream", false, null);
+        //pp.append("upload-field", test);
+        //// The following causes a ClassCastException with or without the TRB-8 fix.
+        //turi.add(1, pp); // 1 = query data
+        //assertEquals("/context/servlet/turbine?upload-field=", turi.getRelativeLink());
+        //turi.removeQueryData("upload-field");
+        //assertEquals("/context/servlet/turbine", turi.getRelativeLink());
+    }
+    
 }
diff --git a/xdocs/changes.xml b/xdocs/changes.xml
index 0b1587d..53affe7 100644
--- a/xdocs/changes.xml
+++ b/xdocs/changes.xml
@@ -24,6 +24,10 @@
 
   <body>
     <release version="2.4-M2" date="in Subversion">
+    <action type="fix" dev="seade" issue="TRB-8" due-to="Gunther Olesch">
+        A FileItem in a ParameterParser added to TurbineURI or TemplateURI was
+        resulting in a NPE.
+      </action>
       <action type="fix" dev="seade" issue="TRB-16" due-to="J&uuml;rgen Hoffmann">
         TurbineURI was mishandling the empty String.
       </action>