Commits

Anonymous committed 8374cff

XW-672 Fix bug on duplicated items for AggregateIterator

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@1932e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits fc82908

Comments (0)

Files changed (2)

src/java/com/opensymphony/xwork2/util/ClassLoaderUtil.java

      * Aggregates Enumeration instances into one iterator and filters out duplicates.  Always keeps one
      * ahead of the enumerator to protect against returning duplicates.
      */
-    protected static class AggregateIterator<E> implements Iterator<E> {
+    static class AggregateIterator<E> implements Iterator<E> {
 
         LinkedList<Enumeration<E>> enums = new LinkedList<Enumeration<E>>();
         Enumeration<E> cur = null;
         private E loadNext() {
             if (determineCurrentEnumeration() != null) {
                 E tmp = cur.nextElement();
+                int loadedSize = loaded.size();
                 while (loaded.contains(tmp)) {
                     tmp = loadNext();
-                    if (tmp == null) {
+                    if (tmp == null || loaded.size() > loadedSize) {
                         break;
                     }
                 }

src/test/com/opensymphony/xwork2/util/ClassLoaderUtilTest.java

 import java.io.IOException;
 import java.net.URL;
 import java.util.Iterator;
+import java.util.Arrays;
+import java.util.Enumeration;
 
 public class ClassLoaderUtilTest extends TestCase {
 
         assertNull(url);
     }
 
+    public void testAggregateIterator() {
+       ClassLoaderUtil.AggregateIterator<String> aggr = new ClassLoaderUtil.AggregateIterator<String>();
+
+       Enumeration en1 = new Enumeration() {
+           private Iterator itt = Arrays.asList("str1", "str1", "str3", "str1").iterator();
+           public boolean hasMoreElements() {
+               return itt.hasNext();
+           }
+
+           public Object nextElement() {
+               return itt.next();
+           }
+       };
+
+       Enumeration en2 = new Enumeration() {
+           private Iterator itt = Arrays.asList("str4", "str5").iterator();
+           public boolean hasMoreElements() {
+               return itt.hasNext();
+           }
+
+           public Object nextElement() {
+               return itt.next();
+           }
+       };
+
+
+       aggr.addEnumeration(en1);
+       aggr.addEnumeration(en2);
+
+       assertTrue(aggr.hasNext());
+       assertEquals("str1", aggr.next());
+
+       assertTrue(aggr.hasNext());
+       assertEquals("str3", aggr.next());
+
+       assertTrue(aggr.hasNext());
+       assertEquals("str4", aggr.next());
+
+       assertTrue(aggr.hasNext());
+       assertEquals("str5", aggr.next());
+
+       assertFalse(aggr.hasNext());
+    }
 }