Package org.jboss.serial.util

Source Code of org.jboss.serial.util.StringUtilTestCase

/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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.jboss.serial.util;

import junit.framework.TestCase;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;

import org.jboss.serial.io.JBossObjectInputStream;
import org.jboss.serial.io.JBossObjectOutputStream;

/**
* $Id: StringUtilTestCase.java 339 2006-09-20 15:49:43Z csuconic $
*
* @author <a href="mailto:clebert.suconic@jboss.com">Clebert Suconic</a>
*/
public class StringUtilTestCase extends TestCase
{

    public static final String TEST1="123456734982723428734982734982734029873402897340287340298734029873409287349287340298374208374349872349872394872398472938472983742983472983742983472983748912";
    public static final String TEST2="\u0080\u0800\u0000\u0080\u0800\u0000\u0080\u0800\u0000\u0080\u0800\u0000\u0080\u0800\u0000\u0080\u0800\u0000\u0080\u0800\u0000\u0080\u0800\u0000\u0080\u0800\u0000\u0080\u0800\u0000\u0080\u0800\u0000\u0080\u0800\u0000";

    private static long calculateSizeUsingDataOutput(String str) throws Exception
    {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        DataOutputStream dataOut = new DataOutputStream(byteOut);
        dataOut.writeUTF(str);

        byte [] producedArray = byteOut.toByteArray();

        return producedArray.length-2;

    }
    public void testLargeStringMultiByte() throws Exception
    {
       doLargeStringTest(32767, true);
    }

    public void testLargerStringMultiByte() throws Exception
    {
       doLargeStringTest(32768, true);
    }

   
    protected void doLargeStringTest(int stringSize, boolean multiByteChars) throws Exception
    {
      
       StringBuffer sb = new StringBuffer();

       int startingChar = multiByteChars ? 210 : 65;
       for (int i = 0; i < stringSize; i++) sb.append((char) (startingChar + (i % 26)));

       String largeString = sb.toString();
       assertEquals(stringSize, largeString.length());
      
       ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
       JBossObjectOutputStream objOut = new JBossObjectOutputStream(byteArray);
      
       objOut.writeObject(largeString);
       objOut.close();
      
       ByteArrayInputStream byteInput = new ByteArrayInputStream(byteArray.toByteArray());
       JBossObjectInputStream objInp = new JBossObjectInputStream(byteInput);
      
       String newString = (String)objInp.readObject();
      
       assertEquals(largeString.length(), newString.length());
       assertEquals(largeString,newString);

    }
   
    public void testLargeString() throws Exception
    {
     
    }
   

   

    public void testCalculateLen() throws Exception
    {
      StringUtilBuffer buffer = new StringUtilBuffer(10,10);

        String strtest = TEST1;

        // This should be 11 according to http://java.sun.com/j2se/1.5.0/docs/api/java/io/DataInput.html#modified-utf-8
        this.assertEquals(calculateSizeUsingDataOutput(strtest),StringUtil.calculateUTFSize(strtest,buffer));

        strtest = TEST1+TEST2;

        // This should be 11 + 1 + 2 + 3 + 2 according to http://java.sun.com/j2se/1.5.0/docs/api/java/io/DataInput.html#modified-utf-8
        this.assertEquals(calculateSizeUsingDataOutput(strtest),StringUtil.calculateUTFSize(strtest,buffer));
    }

    public void testValidateWrite() throws Exception
    {

        String str = TEST1;
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        DataOutputStream dataOut = new DataOutputStream(byteOut);
        dataOut.writeUTF(str);

        byte [] producedArray = byteOut.toByteArray();

        byteOut = new ByteArrayOutputStream();

        dataOut = new DataOutputStream(byteOut);

        StringUtil.saveString(dataOut,str);

        byte[] producedArrayByStringUtil = byteOut.toByteArray();

        assertEquals (producedArray.length+1,producedArrayByStringUtil.length);

        testSame(producedArray, producedArrayByStringUtil);

        ByteArrayInputStream byteInput = new ByteArrayInputStream(producedArrayByStringUtil);
        DataInputStream dataInput = new DataInputStream(byteInput);
        String readString = StringUtil.readString(dataInput, new StringUtilBuffer(10,10));

        assertEquals(str,readString);

    }


    public void testValidateEnconding() throws Throwable
    {

       StringBuffer buffer = new StringBuffer(10000);

       for (char c=1000;c<3000;c+=100)
       {
           buffer.append(c);
       }

        String str = buffer.toString();
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        DataOutputStream dataOut = new DataOutputStream(byteOut);
        dataOut.writeUTF(str);

        byte [] producedArray = byteOut.toByteArray();

        byteOut = new ByteArrayOutputStream();

        dataOut = new DataOutputStream(byteOut);

        StringUtil.saveString(dataOut,str);

        byte[] producedArrayByStringUtil = byteOut.toByteArray();

        assertEquals (producedArray.length+1,producedArrayByStringUtil.length);

        testSame(producedArray, producedArrayByStringUtil);

        ByteArrayInputStream byteInput = new ByteArrayInputStream(producedArrayByStringUtil);
        DataInputStream dataInput = new DataInputStream(byteInput);
        String readString = StringUtil.readString(dataInput, new StringUtilBuffer(100,100));

        assertEquals(str.length(),readString.length());
        if (!str.equals(readString))
        {
            fail("String comparisson failure on small buffer");
        }

        byteInput = new ByteArrayInputStream(producedArrayByStringUtil);
        dataInput = new DataInputStream(byteInput);
        readString = StringUtil.readString(dataInput, new StringUtilBuffer(10000,10000));

        assertEquals(str.length(),readString.length());
        if (!str.equals(readString))
        {
            fail("String comparisson failure on large buffer");
        }


    }


    public void testValidateWriteUsingPartialBuffer() throws Exception
    {

        String str = TEST1 + TEST2;
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        DataOutputStream dataOut = new DataOutputStream(byteOut);
        dataOut.writeUTF(str);

        byte [] producedArray = byteOut.toByteArray();

        byteOut = new ByteArrayOutputStream();

        dataOut = new DataOutputStream(byteOut);

        StringUtil.saveString(dataOut,str,new StringUtilBuffer(14,14));

        byte[] producedArrayByStringUtil = byteOut.toByteArray();

        assertEquals (producedArray.length+1,producedArrayByStringUtil.length);

        testSame(producedArray, producedArrayByStringUtil);

    }

    private void testSame(byte[] producedArray, byte[] producedArrayByStringUtil) {
        for (int i=0;i<producedArray.length;i++)
        {
            if (producedArray[i] != producedArrayByStringUtil[i+1])
            {
                fail("byte at position " + i + " was different on the string comparisson");
            }
        }
    }
}
TOP

Related Classes of org.jboss.serial.util.StringUtilTestCase

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.