[jdom-interest] (no subject)

Jools Enticknap jools at jools.org
Thu Nov 23 03:56:42 PST 2000


Hi

I've been working my way through the JDOM test framework and I came
across a bug which relates to using PartialList.addAll(Collection)

It's weird that this has not already been logged, but anyhow.

A small test case is attached to the email along with a diff to fix the
problem.

The reason I found this bug in the first place was because I was trying to
decide if an Element could exist as a child of two parent. And if it could
what was the use if the getParent() method.



--Jools


-------------- next part --------------
Index: src/java/org/jdom/PartialList.java
===================================================================
RCS file: /home/cvspublic/jdom/src/java/org/jdom/PartialList.java,v
retrieving revision 1.5
diff -r1.5 PartialList.java
227,242c227,231
<         int index = backingList.indexOf(getLast());
< 
<         if (index != -1) {
<             backingList.addAll(index, c);
<         } else {
<             backingList.addAll(c);
<         }
< 
<         for (Iterator i = c.iterator(); i.hasNext(); ) {
<             Object o = i.next();
<             if (o instanceof Element) {
<                 ((Element)o).setParent(parent);  // null is OK
<             }
<         }
< 
<         return super.addAll(c);
---
> 		if (backingList.isEmpty()) {
> 			return addAll(0, c);
> 		} else {
> 			return addAll(backingList.indexOf(getLast()), c);
> 		}
260,266c249,254
<         int insertIndex = backingList.indexOf(get(index));
< 
<         if (insertIndex != -1) {
<             backingList.addAll(insertIndex, c);
<         } else {
<             backingList.addAll(c);
<         }
---
> 		// We can only add start of an empty list (of course) !
> 		if (backingList.isEmpty()) {
> 			backingList.addAll(c);
> 		} else {
> 			backingList.addAll(index, c);
> 		}
-------------- next part --------------


import java.util.*;

import org.jdom.*;

public class BugTest {

	public static void main(String[] args) {
		
		try {
			bug1();
		} catch (Throwable t1) {
			System.out.println("bug1 message:"+t1);
			t1.printStackTrace();
		}
		
		try {
			bug2();
		} catch (Throwable t2) {
			System.out.println("bug2 message:"+t2);
			t2.printStackTrace();
		}
	}

	public static void bug1() {
		// Create the new elemenent with no children.
		Element element1 = new Element("element1");	
		
		// This will create an empty list, which should reflect the state
		// of the internal list contained in the Element.
		List childrenOfElement1 = element1.getChildren();
		
		// Now if I have another Element, get the children and transfer them
		// the empty list using the addAll(Collection c) I should find that all
		// the elements are added to the empty list.
		Element element2 = new Element("element");
		element2.addContent(new Element("a"));
		
		List childrenOfElement2 = element2.getChildren();
		
		childrenOfElement1.addAll(childrenOfElement2);

		// element1 should have all the elements from element2
		printElements(element1);
		printElements(element2);
	}


	public static void bug2() {

		// Create the new elemenent.
		Element element1 = new Element("element1");	
		element1.addContent(new Element("fix"));
		
		// This will create an empty list, which should reflect the state
		// of the internal list contained in the Element.
		List childrenOfElement1 = element1.getChildren();
		
		// Now if I have another Element, get the children and transfer them
		// the empty list using the addAll(Collection c) I should find that all
		// the elements are added to the empty list.
		Element element2 = new Element("element");
		element2.addContent(new Element("a"));
		
		List childrenOfElement2 = element2.getChildren();
		
		childrenOfElement1.addAll(childrenOfElement2);

		printElements(element1);
		printElements(element2);
	}
	
	public static void printElements(Element elem) {
		System.out.println(elem);
		for (Iterator iter = elem.getChildren().iterator(); iter.hasNext();) {
			Element e = (Element) iter.next();
			System.out.println("++"+e);
			
			if (e.getParent() != elem ) {
				System.out.println("++ Not correct parent");
			}
		}
	}

}


More information about the jdom-interest mailing list