All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.xma.serializer.test.TestSerializer Maven / Gradle / Ivy

There is a newer version: 6.0.2
Show newest version
/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

//@(#) $Id: TestSerializer.java 2089 2007-11-28 13:56:13Z s3460 $
package at.spardat.xma.serializer.test;

import java.io.IOException;
import java.io.Serializable;

import at.spardat.xma.serializer.AsciiDeserializer;
import at.spardat.xma.serializer.AsciiSerializer;
import at.spardat.xma.serializer.BinaryDeserializer;
import at.spardat.xma.serializer.BinarySerializer;
import at.spardat.xma.serializer.Deserializer;
import at.spardat.xma.serializer.Serializer;
import at.spardat.xma.serializer.TaggedAsciiDeserializer;
import at.spardat.xma.serializer.TaggedAsciiSerializer;
import at.spardat.xma.serializer.XmaInput;
import at.spardat.xma.serializer.XmaOutput;
import at.spardat.xma.serializer.XmaSerializable;
import at.spardat.xma.util.ByteArray;
import junit.framework.TestCase;

/**
 * Testcase for AsciiSerializer and Deserializer
 * 
 * @author MS, Feb 12, 2005 8:30:40 AM
 */
public class TestSerializer extends TestCase {

    /**
     * Constructor
     */
    public TestSerializer (String name) {
        super (name);
    }
    
    private static String[]     toWrite = {
        "justAscii", "", "\r\n ", "{{},,\\\\???????"
    };
    
    public void testNestedStreams () throws Exception {
        byte []                     bytes;
        // create nested stream
        TaggedAsciiSerializer     taser = new TaggedAsciiSerializer (false, 512);
        serialize (taser);
        bytes = taser.getResult().getBytes();
        // create outer stream
        taser = new TaggedAsciiSerializer (false, 512);
        serialize (taser);
        taser.writeSerializedBytes("nested", bytes);
        serialize (taser);
        bytes = taser.getResult().getBytes();
        System.out.println(new String(bytes, "US-ASCII"));
        
        // parse outer stream
        TaggedAsciiDeserializer   tadeser = new TaggedAsciiDeserializer (bytes);
        deserialize (tadeser);
        bytes = tadeser.readSerializedBytes();
        deserialize (tadeser);
        
        // parse inner stream
        tadeser = new TaggedAsciiDeserializer (bytes);
        deserialize (tadeser);
    }
    
    
    public void testInformationConservation () throws Exception {
        byte []             bytes;
        
        AsciiSerializer     ser = new AsciiSerializer (false, 512);
        serialize (ser);
        bytes = ser.getResult().getBytes();
        System.out.println(new String(bytes, "US-ASCII"));
        AsciiDeserializer   deser = new AsciiDeserializer (bytes);
        deserialize (deser);
        
        TaggedAsciiSerializer     taser = new TaggedAsciiSerializer (false, 512);
        serialize (taser);
        bytes = taser.getResult().getBytes();
        System.out.println(new String(bytes, "US-ASCII"));
        TaggedAsciiDeserializer   tadeser = new TaggedAsciiDeserializer (bytes);
        deserialize (tadeser);
        
        BinarySerializer     bser = new BinarySerializer (false, 512);
        serialize (bser);
        bytes = bser.getResult().getBytes();
        ByteArray.dump(bytes);
        BinaryDeserializer   bdeser = new BinaryDeserializer (bytes);
        deserialize (bdeser);
        
    }
    
    public static class LocalVal implements XmaSerializable, Serializable {
        private String          val;
        public LocalVal() {
        }
        public LocalVal (String value) {
            val = value;
        }
        public String getValue () {
            return val;
        }
        public void deserialize(XmaInput in) throws IOException, ClassNotFoundException {
            val = in.readString();
        }
        public void serialize(XmaOutput out) throws IOException {
            out.writeString("val", val);
        }
    }
    
    private void serialize (Serializer ser) throws Exception {
        ser.writeObject("Boolean", new Boolean(false));
        ser.writeObject("Integer", new Integer(4711));
        ser.writeObject("String", "\\{},=");
        ser.writeObject("Double", new Double(22));
        ser.writeObject("LocalVal", new LocalVal("heini"));
        ser.writeBoolean("true", true);
        ser.writeBoolean("false", false);
        ser.writeByte("byte", 56);
        ser.writeShort("short", 30561);
        ser.writeInt("int", 1000000);
        ser.writeInt("int", -1000000);
        for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy