/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.job.internal.xstream;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.io.StringWriter;

import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class SafeXStreamTest
{
    static class RecursiveObject
    {
        RecursiveObject recurse;

        public RecursiveObject()
        {
            this.recurse = this;
        }

        @Override
        public String toString()
        {
            return "recursive object";
        }
    }

    private SafeXStream xstream;

    @Before
    public void before()
    {
        this.xstream = new SafeXStream();
    }

    private Object writeread(Object obj)
    {
        StringWriter writer = new StringWriter();

        this.xstream.toXML(obj, writer);

        return this.xstream.fromXML(writer.toString());
    }

    private Object assertReadwrite(String resource) throws IOException
    {
        String content = IOUtils.toString(getClass().getResourceAsStream(resource));

        Object obj = this.xstream.fromXML(content);

        String str = this.xstream.toXML(obj);

        assertEquals(content, str);

        return obj;
    }

    // Tests

    @Test
    public void testRecursiveObject()
    {
        RecursiveObject obj = (RecursiveObject) writeread(new RecursiveObject());

        Assert.assertNotNull(obj);
    }

    @Test
    public void testReference() throws IOException
    {
        assertReadwrite("/xstream/arraywithreference.xml");
    }
}

@@ -932,54 +932,8 @@
 m;%0A%0A
-import static org.junit.Assert.assertEquals;%0A%0A
 impo
@@ -956,16 +956,16 @@
 eption;%0A
+
 import j
@@ -1098,16 +1098,104 @@
 .Test;%0A%0A
+import com.thoughtworks.xstream.XStream;%0A%0Aimport static org.junit.Assert.assertEquals;%0A%0A
 public c
@@ -1498,24 +1498,389 @@
  %7D%0A%0A    
+static class RecursiveObjectThroughArray%0A    %7B%0A        RecursiveObjectThroughArray%5B%5D recurse;%0A%0A        public RecursiveObjectThroughArray()%0A        %7B%0A            this.recurse = new RecursiveObjectThroughArray%5B%5D %7Bthis%7D;%0A        %7D%0A%0A        @Override%0A        public String toString()%0A        %7B%0A            return %22recursive object through array%22;%0A        %7D%0A    %7D%0A%0A    
 private 
 SafeXStr
@@ -1871,20 +1871,16 @@
 private 
-Safe
 XStream 
@@ -2527,32 +2527,32 @@
 ests%0A%0A    @Test%0A
-
     public void 
@@ -2700,24 +2700,215 @@
 bj);%0A    %7D%0A%0A
+    @Test%0A    public void testRecursiveObjectThroughArray()%0A    %7B%0A        RecursiveObject obj = (RecursiveObject) writeread(new RecursiveObject());%0A%0A        Assert.assertNotNull(obj);%0A    %7D%0A%0A
     @Test%0A  

Add test