/*
 * Copyright (C) 2008, 2010 XStream Committers.
 * All rights reserved.
 *
 * The software in this package is published under the terms of the BSD
 * style license a copy of which has been included with this distribution in
 * the LICENSE.txt file.
 * 
 * Created on 12. November 2008 by Joerg Schaible
 */
package com.thoughtworks.acceptance;

import com.thoughtworks.xstream.converters.ConverterLookup;
import com.thoughtworks.xstream.core.ReferenceByIdMarshaller;
import com.thoughtworks.xstream.core.ReferenceByIdMarshallingStrategy;
import com.thoughtworks.xstream.core.TreeMarshaller;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.mapper.Mapper;

public class BeanIDCircularReferenceTest extends AbstractCircularReferenceTest {

    private ReferenceByFirstnameMarshallingStrategy marshallingStrategy;

	private static final class ReferenceByFirstnameMarshallingStrategy extends ReferenceByIdMarshallingStrategy
	{
		protected TreeMarshaller createMarshallingContext(HierarchicalStreamWriter writer,
		    ConverterLookup converterLookup, Mapper mapper) {
		    return new ReferenceByIdMarshaller(writer, converterLookup, mapper, new ReferenceByIdMarshaller.IDGenerator(){

		        public String next(Object item) {
		            // we have only persons here
		            return ((Person)item).firstname;
		        }});
		}
	}

	// inherits test from superclass
    protected void setUp() throws Exception {
        super.setUp();
        marshallingStrategy = new ReferenceByFirstnameMarshallingStrategy();
		xstream.setMarshallingStrategy(marshallingStrategy);
    }

    public void testCircularReferenceXml() {
        Person bob = new Person("bob");
        Person jane = new Person("jane");
        bob.likes = jane;
        jane.likes = bob;

        String expected = "" +
                "<person id=\"bob\">\n" +
                "  <firstname>bob</firstname>\n" +
                "  <likes id=\"jane\">\n" +
                "    <firstname>jane</firstname>\n" +
                "    <likes reference=\"bob\"/>\n" +
                "  </likes>\n" +
                "</person>";

        assertEquals(expected, xstream.toXML(bob));
    }

    public void testCircularReferenceToSelfXml() {
        Person bob = new Person("bob");
        bob.likes = bob;

        String expected = "" +
                "<person id=\"bob\">\n" +
                "  <firstname>bob</firstname>\n" +
                "  <likes reference=\"bob\"/>\n" +
                "</person>";

        assertEquals(expected, xstream.toXML(bob));
    }

    public void testCanAvoidMemberIfUsedAsId() throws Exception
	{
		xstream.omitField(Person.class, "firstname");

        Person bob = new Person("bob");
        Person jane = new Person("jane");
        bob.likes = jane;
        jane.likes = bob;

        String expected = "" +
                "<person id=\"bob\">\n" +
                "  <likes id=\"jane\">\n" +
                "    <likes reference=\"bob\"/>\n" +
                "  </likes>\n" +
                "</person>";

        assertEquals(expected, xstream.toXML(bob));
        
        setUp(); // new XStream instance, since marshal and unmarshal is asymmetric
        xstream.useAttributeFor("firstname", String.class);
        xstream.aliasField("id", Person.class, "firstname");
        
        Person bobAgain = (Person)xstream.fromXML(expected);
        assertEquals("bob", bobAgain.firstname);
        assertEquals("jane", bobAgain.likes.firstname);
	}
}

@@ -2643,22 +2643,27 @@
 xception
-%0A%09%7B%0A%09%09
+ %7B%0A        
 xstream.
@@ -2856,34 +2856,32 @@
 ng expected = %22%22
- +
 %0A               
@@ -2869,35 +2869,33 @@
  %22%22%0A            
-   
++
  %22%3Cperson id=%5C%22b
@@ -2894,34 +2894,32 @@
 n id=%5C%22bob%5C%22%3E%5Cn%22
- +
 %0A               
@@ -2907,35 +2907,33 @@
 %5Cn%22%0A            
-   
++
  %22  %3Clikes id=%5C%22
@@ -2934,34 +2934,32 @@
  id=%5C%22jane%5C%22%3E%5Cn%22
- +
 %0A               
@@ -2947,35 +2947,33 @@
 %5Cn%22%0A            
-   
++
  %22    %3Clikes ref
@@ -2983,37 +2983,33 @@
 nce=%5C%22bob%5C%22/%3E%5Cn%22
- +%0A  
+%0A
               %22 
@@ -2996,33 +2996,33 @@
 %5Cn%22%0A            
- 
++
  %22  %3C/likes%3E%5Cn%22 
@@ -3012,34 +3012,32 @@
 + %22  %3C/likes%3E%5Cn%22
- +
 %0A               
@@ -3025,35 +3025,33 @@
 %5Cn%22%0A            
-   
++
  %22%3C/person%3E%22;%0A%0A 
@@ -3101,32 +3101,24 @@
 (bob));%0A
-        
 %0A       
  setUp()
@@ -3113,17 +3113,8 @@
     
- setUp();
  // 
@@ -3177,16 +3177,68 @@
 mmetric%0A
+        xstream = createXStream();%0A        setUp();%0A
         
@@ -3350,24 +3350,16 @@
 name%22);%0A
-        
 %0A       
@@ -3517,13 +3517,16 @@
 tname);%0A
-%09
+    
 %7D%0A%7D%0A

Fix failing unit test.