Amf0OutputTest.java :  » Google-tech » s3blazeds » flex » messaging » io » amf » Java Open Source

Java Open Source » Google tech » s3blazeds 
s3blazeds » flex » messaging » io » amf » Amf0OutputTest.java
package flex.messaging.io.amf;

import java.nio.charset.MalformedInputException;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;

import flex.messaging.io.SerializationContext;

/**
 * UnitTest for {@link Amf0Output}.
 *
 * @author MAKOTO Yamazaki <makoto1975@gmail.com>
 */
public class Amf0OutputTest extends TestCase {
    private SerializationContext context_;
    private Amf0Output target_;
    private ByteOutputStream bOut_;

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

    protected void setUp() {
        context_ = new SerializationContext();
        target_ = new Amf0Output(context_);
        bOut_ = new ByteOutputStream();
        target_.setOutputStream(bOut_);
    }

    public static Test suite() {
        return new TestSuite(Amf0OutputTest.class);
    }

    private static byte[] toUIntBytes(int value) {
        if (value <= 0xFFFF) {
            final byte[] bytes = new byte[2];
            bytes[0] = (byte) ((value >>> 8) & 0xFF);
            bytes[1] = (byte) ((value >>> 0) & 0xFF);
            return bytes;
        }
        final byte[] bytes = new byte[4];
        bytes[0] = (byte) ((value >>> 24) & 0xFF);
        bytes[1] = (byte) ((value >>> 16) & 0xFF);
        bytes[2] = (byte) ((value >>> 8) & 0xFF);
        bytes[3] = (byte) ((value >>> 0) & 0xFF);
        return bytes;
    }


    public void testAscii() throws Exception {
        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 128; i++) {
            sb.append((char) i);
        }
        target_.writeObject(sb.toString());
        target_.close();

        final byte[] lenBytes = toUIntBytes(sb.length() * 1);

        final int headerLength = 1/*type*/ + lenBytes.length;
        assertEquals(128 + headerLength, bOut_.getCount());
        final byte[] bytes = bOut_.getBytes();
        assertEquals((byte) 2 /*kStringType*/, bytes[0]);
        // checking length field
        for (int i = 0; i < lenBytes.length; i++) {
            assertEquals("i: " + i, lenBytes[i], bytes[1 + i]);
        }
        // checking serialized String
        for (int i = 0; i < 128; i++) {
            assertEquals("failed at " + i, (byte) i, bytes[i + headerLength]);
        }
    }

    public void test2BytesUtf8() throws Exception {
        final int minCodePoint = 0x80;
        final int maxCodePoint = 0x7FF;
        final int charCount = maxCodePoint + 1 - minCodePoint;
        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < charCount; i++) {
            sb.append((char) (i + minCodePoint));
        }
        target_.writeObject(sb.toString());
        target_.close();

        final byte[] lenBytes = toUIntBytes(sb.length() * 2);

        final int headerLength = 1/*type*/ + lenBytes.length;
        assertEquals(headerLength + (charCount * 2), bOut_.getCount());
        final byte[] bytes = bOut_.getBytes();
        assertEquals((byte) 2 /*kStringType*/, bytes[0]);
        // checking length field
        for (int i = 0; i < lenBytes.length; i++) {
            assertEquals("i: " + i, lenBytes[i], bytes[1 + i]);
        }
        // checking serialized String
        int baseIndex = headerLength;
        for (int i = 0; i < charCount; i++) {
            final int codePoint = minCodePoint + i;
            assertEquals("failed at " + codePoint,
                    (byte) (0xC0 + (codePoint >> 6)), bytes[baseIndex]);
            assertEquals("failed at " + codePoint,
                    (byte) (0x80 + (codePoint & 0x3F)), bytes[baseIndex + 1]);
            baseIndex += 2;
        }
    }

    public void test3BytesUtf8() throws Exception {
        final int minCodePoint = 0x800;
        final int maxCodePoint = 0xFFFF;

        final int charCount = maxCodePoint + 1 - minCodePoint;
        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < charCount; i++) {
            final int codePoint = minCodePoint + i;
            if (Character.isHighSurrogate((char) codePoint)
                    || Character.isLowSurrogate((char) codePoint)) {
                continue;
            }
            sb.append((char) codePoint);
        }
        target_.writeObject(sb.toString());
        target_.close();

        final byte[] lenBytes = toUIntBytes(sb.length() * 3);

        final int headerLength = 1/*type*/ + lenBytes.length;
        assertEquals(headerLength + (sb.length() * 3), bOut_.getCount());
        final byte[] bytes = bOut_.getBytes();
        assertEquals((byte) 12 /*kLongStringType*/, bytes[0]);
        // checking length field
        for (int i = 0; i < lenBytes.length; i++) {
            assertEquals("i: " + i, lenBytes[i], bytes[1 + i]);
        }
        // checking serialized String
        int baseIndex = headerLength;
        for (int i = 0; i < charCount; i++) {
            final int codePoint = minCodePoint + i;
            if (Character.isHighSurrogate((char) codePoint)
                    || Character.isLowSurrogate((char) codePoint)) {
                continue;
            }
            assertEquals("failed at " + codePoint,
                    (byte) (0xE0 + ((codePoint >> 12) & 0xF)), bytes[baseIndex]);
            assertEquals("failed at " + codePoint,
                    (byte) (0x80 + ((codePoint >> 6) & 0x3F)),
                    bytes[baseIndex + 1]);
            assertEquals("failed at " + codePoint,
                    (byte) (0x80 + (codePoint & 0x3F)), bytes[baseIndex + 2]);
            baseIndex += 3;
        }
    }

    public void test4BytesUtf8() throws Exception {
        final int minCodePoint = 0x10000;
        final int maxCodePoint = 0x10FFFF;

        final int charCount = maxCodePoint + 1 - minCodePoint;
        final StringBuilder sb = new StringBuilder();
        for (int codePoint = minCodePoint; codePoint <= maxCodePoint; codePoint++) {
            final char[] chars = Character.toChars(codePoint);
            sb.append(chars);
        }
        target_.writeObject(sb.toString());
        target_.close();

        final byte[] lenBytes = toUIntBytes(sb.length() * 2);

        final int headerLength = 1/*type*/ + lenBytes.length;
        assertEquals(headerLength + (sb.length() * 2), bOut_.getCount());
        final byte[] bytes = bOut_.getBytes();
        assertEquals((byte) 12 /*kLongStringType*/, bytes[0]);
        // checking length field
        for (int i = 0; i < lenBytes.length; i++) {
            assertEquals("i: " + i, lenBytes[i], bytes[1 + i]);
        }
        // checking serialized String
        int baseIndex = headerLength;
        for (int i = 0; i < charCount; i++) {
            final int codePoint = minCodePoint + i;
            assertEquals("failed at " + codePoint,
                    (byte) (0xF0 + ((codePoint >> 18) & 0x7)), bytes[baseIndex]);
            assertEquals("failed at " + codePoint,
                    (byte) (0x80 + ((codePoint >> 12) & 0x3F)),
                    bytes[baseIndex + 1]);
            assertEquals("failed at " + codePoint,
                    (byte) (0x80 + ((codePoint >> 6) & 0x3F)),
                    bytes[baseIndex + 2]);
            assertEquals("failed at " + codePoint,
                    (byte) (0x80 + (codePoint & 0x3F)), bytes[baseIndex + 3]);
            baseIndex += 4;
        }
    }

    public void testHighSurrogateOnly() throws Exception {
        final int highSurrogateCount = Character.MAX_HIGH_SURROGATE
                - Character.MIN_HIGH_SURROGATE;
        final char[] highSurrogate = new char[1];
        for (int i = 0; i < highSurrogateCount; i++) {
            try {
                highSurrogate[0] = (char) (i + Character.MIN_HIGH_SURROGATE);
                target_.writeObject(highSurrogate);
                fail(MalformedInputException.class.getSimpleName()
                        + " must be thrown for " + ((int) highSurrogate[0])
                        + ".");
            } catch (MalformedInputException e) {
                // OK
                assert true;
            }
        }
    }

    public void testHighSurrogateFollowedByAscii() throws Exception {
        final int highSurrogateCount = Character.MAX_HIGH_SURROGATE
                - Character.MIN_HIGH_SURROGATE;
        final char[] highSurrogate = new char[2];
        for (int i = 0; i < highSurrogateCount; i++) {
            try {
                highSurrogate[0] = (char) (i + Character.MIN_HIGH_SURROGATE);
                highSurrogate[1] = 'a';
                target_.writeObject(highSurrogate);
                fail(MalformedInputException.class.getSimpleName()
                        + " must be thrown for " + ((int) highSurrogate[0])
                        + ".");
            } catch (MalformedInputException e) {
                // OK
                assert true;
            }
        }
    }

    public void testLowSurrogateOnly() throws Exception {
        final int lowSurrogateCount = Character.MAX_LOW_SURROGATE
                - Character.MIN_LOW_SURROGATE;
        final char[] lowSurrogate = new char[1];
        for (int i = 0; i < lowSurrogateCount; i++) {
            try {
                lowSurrogate[0] = (char) (i + Character.MIN_LOW_SURROGATE);
                target_.writeObject(lowSurrogate);
                fail(MalformedInputException.class.getSimpleName()
                        + " must be thrown for " + ((int) lowSurrogate[0])
                        + ".");
            } catch (MalformedInputException e) {
                // OK
                assert true;
            }
        }
    }

    public void testLowSurrogateNotFollowingHighSurrogate() throws Exception {
        final int lowSurrogateCount = Character.MAX_LOW_SURROGATE
                - Character.MIN_LOW_SURROGATE;
        final char[] lowSurrogate = new char[3];
        for (int i = 0; i < lowSurrogateCount; i++) {
            try {
                lowSurrogate[0] = 'a';
                lowSurrogate[1] = (char) (i + Character.MIN_LOW_SURROGATE);
                lowSurrogate[0] = 'z';
                target_.writeObject(lowSurrogate);
                fail(MalformedInputException.class.getSimpleName()
                        + " must be thrown for " + ((int) lowSurrogate[1])
                        + ".");
            } catch (MalformedInputException e) {
                // OK
                assert true;
            }
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.