/*

License $Id: SIXBSTest.java,v 1.2 2001/05/11 16:45:17 Hendrik Exp $

Copyright (c) 2001 tagtraum industries.

The sixbs library 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 library 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 library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

For LGPL see <http://www.fsf.org/copyleft/lesser.txt>

We encourage your feedback and suggestions and want to use your feedback to
improve the Software. Send all such feedback to:
feedback@tagtraum.com

For more information on tagtraum industries, Hendrik Schreiber and sixbs
please see <http://www.tagtraum.com/>.

*/

package com.tagtraum.sixbs.test;

import com.tagtraum.sixbs.*;
import junit.framework.*;
import java.io.*;
import java.util.*;
import java.lang.reflect.*;



/**
 * 
 * 
 * @author 	Hendrik Schreiber
 * @version $Id: SIXBSTest.java,v 1.2 2001/05/11 16:45:17 Hendrik Exp $
 */
public class SIXBSTest extends TestCase {

    /**
     * Source-Version
     */
    public static String	vcid = 
	"$Id: SIXBSTest.java,v 1.2 2001/05/11 16:45:17 Hendrik Exp $";
    
    private StringWriter out;
    private StringReader in;
    private SIXBSWriter writer;
    private SIXBSReader reader;


    public SIXBSTest(String name) {
        super(name);
    }


    public void setUp() throws IOException {
        out = new StringWriter();
        writer = new SIXBSWriter(out, true);
    }
    
    public void tearDown() {
        out = null;
        in = null;
        writer = null;
        reader = null;
    }

    public void testTestBeanEquality() {
        assert(new TestBean().equals(new TestBean()));
    }

/*
    public void testArrayMethodInvocation() throws Exception {
        Method[] methods = TestBean.class.getMethods();
        for (int i=0; i<methods.length; i++) {
            System.out.println(methods[i]);
        }
        Method method = TestBean.class.getMethod("setIntegerArray", new Class[] {(new int[0]).getClass()});
        method.invoke(new TestBean(), new Object[] {new int[] {1,2,3,4}});
    }
*/
    
    public void testEmptyTestBean() throws Exception {
        Object writeObject = new TestBean();
        writer.writeObject(writeObject);
        in = new StringReader(out.toString());
        //System.out.println("testEmptyTestBean()");
        //System.out.println(out.toString());
        reader = new SIXBSReader(in);
        Object readObject = reader.readObject();
        
        //out = new StringWriter();
        //writer = new SIXBSWriter(out);
        //writer.writeObject(readObject);
        //System.out.println("read testEmptyTestBean()");
        //System.out.println(out.toString());

        assert("Equals of empty TestBean should return true", writeObject.equals(readObject));
    }

    public void testMultipleEmptyTestBean() throws Exception {
        SIXBSWriter writer = new SIXBSWriter(out, false);
        Object writeObject1 = new TestBean();
        Object writeObject2 = new TestBean();
        writer.writeObject(writeObject1);
        writer.writeObject(writeObject2);
        writer.close();
        in = new StringReader(out.toString());
        /*
        System.out.println("testMultipleEmptyTestBean()");
        System.out.println(out.toString());
        */
        reader = new SIXBSReader(in);
        Object readObject1 = reader.readObject();
        Object readObject2 = reader.readObject();
        assert("Failed to read two beans.", writeObject1.equals(readObject1) && writeObject2.equals(readObject2));
    }

    public void testManyTestBeans() throws Exception {
        SIXBSWriter writer = new SIXBSWriter(out);
        ArrayList list = new ArrayList();
        for (int i=0; i<5000; i++) {
            //System.out.print("w");
            //System.out.println("write" + i);
            TestBean tb = new TestBean();
            list.add(tb);
            writer.writeObject(tb);
        }
        writer.close();
        in = new StringReader(out.toString());        
        reader = new SIXBSReader(in);
        reader.setReadAhead(500);
        Thread.currentThread().sleep(5000);
        while (reader.hasNext()) {
            //System.out.print("r");
            TestBean readBean = (TestBean)reader.readObject();
            TestBean writeBean = (TestBean)list.remove(0);
            assert("Many beans failed.", writeBean.equals(readBean));
        }
    }

    public void testTestBeanWithValues() throws Exception {
        TestBean writeObject = new TestBean();
        writeObject.setInteger(-10);
        TestBean intBean = new TestBean();
        intBean.setNullObject(" < &&;; Not null &&;; > ");
        writeObject.setTestBean(intBean);
        writer.writeObject(writeObject);
        in = new StringReader(out.toString());
        //System.out.println("testTestBeanWithValues()");
        //System.out.println(out.toString());
        reader = new SIXBSReader(in);
        Object readObject = reader.readObject();
        
        out = new StringWriter();
        writer = new SIXBSWriter(out);
        writer.writeObject(readObject);
        //System.out.println("read testTestBeanWithValues()");
        //System.out.println(out.toString());

        assert("Equals of TestBean should return true", writeObject.equals(readObject));
    }

    public void testTestBeanWithArray() throws Exception {
        TestBean writeObject = new TestBean();
        writeObject.setIntegerArray(new int[] {1,2,3,4,5});
        writer.writeObject(writeObject);
        in = new StringReader(out.toString());
        //System.out.println("testTestBeanWithArray()");
        //System.out.println(out.toString());
        reader = new SIXBSReader(in);
        Object readObject = reader.readObject();
        
        out = new StringWriter();
        writer = new SIXBSWriter(out);
        writer.writeObject(readObject);
        //System.out.println("read testTestBeanWithArray()");
        //System.out.println(out.toString());

        assert("Equals of TestBean should return true", writeObject.equals(readObject));
    }

    public static TestSuite suite() {
        TestSuite testSuite = new TestSuite(SIXBSTest.class);
        return testSuite;
    }
    
    public static void main(String[] args) {
        junit.textui.TestRunner.run(suite());
    }
    
}