package simple.xml.graph;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import simple.xml.Attribute;
import simple.xml.ElementList;
import simple.xml.Root;
import simple.xml.Text;
import simple.xml.ValidationTestCase;
import simple.xml.load.Persister;

public class CycleTest extends ValidationTestCase {
   
   @Root(name="example")
   public static class CycleExample {
      
      @ElementList(name="list", type=Entry.class)
      private List<Entry> list;
      
      public CycleExample() {
         this.list = new ArrayList();
      }
      
      public void add(Entry entry) {
         list.add(entry);
      }
      
      public Entry get(int index) {
         return list.get(index);
      }
   }
   
   @Root(name="entry")
   public static class Entry {
      
      @Attribute(name="key")
      private String name;
      
      @Text
      private String value;
      
      protected Entry() {
         super();
      }
      
      public Entry(String name, String value) {
         this.name = name;
         this.value = value;
      }
   }
   
   private Persister persister;
   
   public void setUp() throws Exception {
      persister = new Persister(new CycleStrategy("class", "id", "ref"));
   }
   
   public void testCycle() throws Exception {
      CycleExample example = new CycleExample();
      Entry one = new Entry("1", "one");
      Entry two = new Entry("2", "two");
      Entry three = new Entry("3", "three");
      
      example.add(one);
      example.add(two);
      example.add(three);
      example.add(one);
      example.add(two);
      
      assertEquals(example.get(0).value, "one");
      assertEquals(example.get(1).value, "two");
      assertEquals(example.get(2).value, "three");
      assertEquals(example.get(3).value, "one");
      assertEquals(example.get(4).value, "two");
      assertTrue(example.get(0) == example.get(3));
      assertTrue(example.get(1) == example.get(4));
      
      StringWriter out = new StringWriter();
      persister.write(example, out);
      
      example = persister.read(CycleExample.class, out.toString());
      
      assertEquals(example.get(0).value, "one");
      assertEquals(example.get(1).value, "two");
      assertEquals(example.get(2).value, "three");
      assertEquals(example.get(3).value, "one");
      assertEquals(example.get(4).value, "two");
      assertTrue(example.get(0) == example.get(3));
      assertTrue(example.get(1) == example.get(4)); 
      
      
      validate(example, persister);
   }

}

@@ -133,24 +133,52 @@
 Attribute;%0D%0A
+import simple.xml.Element;%0D%0A
 import simpl
@@ -193,24 +193,24 @@
 ementList;%0D%0A
-
 import simpl
@@ -538,32 +538,105 @@
  list;%0D%0A      %0D%0A
+      @Element(name=%22cycle%22)%0D%0A      private CycleExample cycle;%0D%0A      %0D%0A
       public Cyc
@@ -685,24 +685,53 @@
 rayList();%0D%0A
+         this.cycle = this;%0D%0A
       %7D%0D%0A   
@@ -1668,24 +1668,79 @@
  %22three%22);%0D%0A
+      Entry threeDuplicate = new Entry(%223%22, %22three%22);%0D%0A
       %0D%0A    
@@ -1858,24 +1858,60 @@
 .add(two);%0D%0A
+      example.add(threeDuplicate);%0D%0A
       %0D%0A    
@@ -2150,32 +2150,84 @@
 value, %22two%22);%0D%0A
+      assertEquals(example.get(5).value, %22three%22);%0D%0A
       assertTrue
@@ -2312,24 +2312,78 @@
 e.get(4));%0D%0A
+      assertFalse(example.get(2) == example.get(5));%0D%0A
       %0D%0A    
@@ -2460,16 +2460,22 @@
 e, out);
+      
 %0D%0A      
@@ -2909,25 +2909,24 @@
 get(4));
- 
 %0D%0A      
 %0D%0A      
@@ -2909,32 +2909,78 @@
 get(4));%0D%0A      
+assertFalse(example.get(2) == example.get(5));
 %0D%0A      %0D%0A      

Update cycle test case