You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2014/04/15 19:51:48 UTC

svn commit: r1587651 - in /commons/proper/net/trunk/src: changes/changes.xml main/java/org/apache/commons/net/nntp/Threader.java test/java/org/apache/commons/net/nntp/ test/java/org/apache/commons/net/nntp/TestThreader.java

Author: sebb
Date: Tue Apr 15 17:51:48 2014
New Revision: 1587651

URL: http://svn.apache.org/r1587651
Log:
NET-539 NPE if Threader.thread invoked with empty list or with null array

Added:
    commons/proper/net/trunk/src/test/java/org/apache/commons/net/nntp/
    commons/proper/net/trunk/src/test/java/org/apache/commons/net/nntp/TestThreader.java   (with props)
Modified:
    commons/proper/net/trunk/src/changes/changes.xml
    commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/Threader.java

Modified: commons/proper/net/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/changes/changes.xml?rev=1587651&r1=1587650&r2=1587651&view=diff
==============================================================================
--- commons/proper/net/trunk/src/changes/changes.xml [utf-8] (original)
+++ commons/proper/net/trunk/src/changes/changes.xml [utf-8] Tue Apr 15 17:51:48 2014
@@ -68,6 +68,9 @@ This is mainly a bug-fix release. See fu
   IMAPExportMbox (example app) allows IMAP folders to be exported into an mbox file.
   This is the inverse of the IMAPImportMbox example added previously
         ">
+            <action issue="NET-539" type="fix" dev="sebb">
+            NPE if Threader.thread invoked with empty list or with null array
+            </action>
             <action issue="NET-536" type="add" dev="sebb">
             IMAP FETCH example
             IMAPExportMbox can export selected nessages from an IMAP folder

Modified: commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/Threader.java
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/Threader.java?rev=1587651&r1=1587650&r2=1587651&view=diff
==============================================================================
--- commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/Threader.java (original)
+++ commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/Threader.java Tue Apr 15 17:51:48 2014
@@ -27,6 +27,7 @@ package org.apache.commons.net.nntp;
  *
  */
 
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
@@ -36,8 +37,8 @@ public class Threader {
     /**
      * The client passes in a list of Threadable objects, and
      * the Threader constructs a connected 'graph' of messages
-     * @param messages list of messages to thread
-     * @return null if messages == null or root.child == null
+     * @param messages list of messages to thread, must not be empty
+     * @return null if messages == null or root.child == null or messages list is empty
      * @since 2.2
      */
     public Threadable thread(List<? extends Threadable> messages) {
@@ -47,8 +48,8 @@ public class Threader {
     /**
      * The client passes in a list of Iterable objects, and
      * the Threader constructs a connected 'graph' of messages
-     * @param messages iterable of messages to thread
-     * @return null if messages == null or root.child == null
+     * @param messages iterable of messages to thread, must not be empty
+     * @return null if messages == null or root.child == null or messages list is empty
      * @since 3.0
      */
     public Threadable thread(Iterable<? extends Threadable> messages) {
@@ -65,6 +66,10 @@ public class Threader {
             }
         }
 
+        if (idTable.isEmpty()) {
+            return null;
+        }
+
         ThreadContainer root = findRootSet(idTable);
         idTable.clear();
         idTable = null;
@@ -446,13 +451,16 @@ public class Threader {
     /**
      * The client passes in an array of Threadable objects, and
      * the Threader constructs a connected 'graph' of messages
-     * @param messages array of messages to thread
-     * @return null if messages == null or root.child == null
+     * @param messages array of messages to thread, must not be empty
+     * @return null if messages == null or root.child == null or messages array is empty
      * @deprecated (2.2) prefer {@link #thread(List)}
      */
     @Deprecated
     public Threadable thread(Threadable[] messages) {
-        return thread(java.util.Arrays.asList(messages));
+        if (messages == null) {
+            return null;
+        }
+        return thread(Arrays.asList(messages));
     }
 
 }

Added: commons/proper/net/trunk/src/test/java/org/apache/commons/net/nntp/TestThreader.java
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/test/java/org/apache/commons/net/nntp/TestThreader.java?rev=1587651&view=auto
==============================================================================
--- commons/proper/net/trunk/src/test/java/org/apache/commons/net/nntp/TestThreader.java (added)
+++ commons/proper/net/trunk/src/test/java/org/apache/commons/net/nntp/TestThreader.java Tue Apr 15 17:51:48 2014
@@ -0,0 +1,77 @@
+/*
+ * 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.commons.net.nntp;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test the Threader
+ */
+public class TestThreader {
+    
+    @Test
+    @SuppressWarnings("deprecation") // test of deprecated method
+    public void testNullArray() { // NET-539
+        Threader t = new Threader();
+        Threadable[] messages=null;
+        Assert.assertNull(t.thread(messages));        
+    }
+
+    @Test
+    public void testNullList() {
+        Threader t = new Threader();
+        List<Threadable> messages=null;
+        Assert.assertNull(t.thread(messages));        
+    }
+
+    @Test
+    public void testNullIterable() {
+        Threader t = new Threader();
+        Iterable<Threadable> messages=null;
+        Assert.assertNull(t.thread(messages));        
+    }
+
+    @SuppressWarnings("deprecation") // test of deprecated method
+    @Test
+    public void testEmptyArray() { // NET-539
+        Threader t = new Threader();
+        Threadable[] messages=new Threadable[0];
+        Assert.assertNull(t.thread(messages));
+    }
+
+    @Test
+    public void testEmptyList() { // NET-539
+        Threader t = new Threader();
+        Threadable[] messages=new Threadable[0];
+        final List<Threadable> asList = Arrays.asList(messages);
+        Assert.assertNull(t.thread(asList));
+    }
+
+    @Test
+    public void testEmptyIterable() { // NET-539
+        Threader t = new Threader();
+        Threadable[] messages=new Threadable[0];
+        final Iterable<Threadable> asList = Arrays.asList(messages);
+        Assert.assertNull(t.thread(asList));
+    }
+
+}

Propchange: commons/proper/net/trunk/src/test/java/org/apache/commons/net/nntp/TestThreader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/net/trunk/src/test/java/org/apache/commons/net/nntp/TestThreader.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision