Package com.peterhi.property

Source Code of com.peterhi.property.TestPropModel

package com.peterhi.property;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import org.junit.Assert;
import org.junit.Test;

import com.peterhi.int24;
import com.peterhi.intx;
import com.peterhi.uintx;
import com.peterhi.io.PmInputStream;
import com.peterhi.io.PmOutputStream;

public class TestPropModel {

  @Test
  public void testPrimitives() throws Exception {
    Primitives p0 = new Primitives();
    PropUtil.setValue(p0, "booleanValue", true);
    PropUtil.setValue(p0, "byteValue", (byte )-1);
    PropUtil.setValue(p0, "charValue", 'c');
    PropUtil.setValue(p0, "shortValue", (short )-1);
    PropUtil.setValue(p0, "intValue", -1);
    PropUtil.setValue(p0, "longValue", -1L);
    PropUtil.setValue(p0, "floatValue", -1.0f);
    PropUtil.setValue(p0, "doubleValue", -1.0);
    PropUtil.setValue(p0, "int24Value",
      new int24(int24.MIN_24));
    PropUtil.setValue(p0, "intxValue",
      new intx(intx.MIN_62));
    PropUtil.setValue(p0, "uintxValue",
      new uintx(uintx.MAX_62));
   
    Assert.assertEquals(Boolean.TRUE,
      PropUtil.getValue(p0, "booleanValue"));
    Assert.assertEquals((byte )-1,
      PropUtil.getValue(p0, "byteValue"));
    Assert.assertEquals('c',
      PropUtil.getValue(p0, "charValue"));
    Assert.assertEquals((short )-1,
      PropUtil.getValue(p0, "shortValue"));
    Assert.assertEquals(-1,
      PropUtil.getValue(p0, "intValue"));
    Assert.assertEquals(-1L,
      PropUtil.getValue(p0, "longValue"));
    Assert.assertEquals(-1.0f,
      PropUtil.getValue(p0, "floatValue"));
    Assert.assertEquals(-1.0,
      PropUtil.getValue(p0, "doubleValue"));
    Assert.assertEquals(new int24(int24.MIN_24),
      PropUtil.getValue(p0, "int24Value"));
    Assert.assertEquals(new intx(intx.MIN_62),
      PropUtil.getValue(p0, "intxValue"));
    Assert.assertEquals(new uintx(uintx.MAX_62),
      PropUtil.getValue(p0, "uintxValue"));
   
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PmOutputStream pos = new PmOutputStream(baos);
    pos.writeModel(p0);
   
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    PmInputStream pis = new PmInputStream(bais);
    Primitives p1 = pis.readModel();
   
    Assert.assertEquals(0, pis.available());
    pos.close();
    pis.close();
   
    Assert.assertEquals(p0.isBooleanValue(), p1.isBooleanValue());
    Assert.assertEquals(p0.getByteValue(), p1.getByteValue());
    Assert.assertEquals(p0.getCharValue(), p1.getCharValue());
    Assert.assertEquals(p0.getShortValue(), p1.getShortValue());
    Assert.assertEquals(p0.getIntValue(), p1.getIntValue());
    Assert.assertEquals(p0.getLongValue(), p1.getLongValue());
    Assert.assertEquals((double )p0.getFloatValue(),
              (double )p1.getFloatValue(), 0.00001);
    Assert.assertEquals(p0.getDoubleValue(), p1.getDoubleValue(), 0.00001);
    Assert.assertEquals(p0.getInt24Value(), p1.getInt24Value());
    Assert.assertEquals(p0.getIntxValue(), p1.getIntxValue());
    Assert.assertEquals(p0.getUintxValue(), p1.getUintxValue());
  }

  @Test
  public void testBoxers() throws Exception {
    Boxers bNull = new Boxers();
   
    Boxers b0 = new Boxers();
    b0.setBooleanObject(true);
    b0.setByteObject(Byte.MAX_VALUE);
    b0.setCharacterObject(Character.MAX_VALUE);
    b0.setShortObject(Short.MAX_VALUE);
    b0.setIntegerObject(Integer.MAX_VALUE);
    b0.setLongObject(Long.MAX_VALUE);
    b0.setFloatObject(Float.MAX_VALUE);
    b0.setDoubleObject(Double.MAX_VALUE);
   
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PmOutputStream pos = new PmOutputStream(baos);
    pos.writeModel(bNull);
    pos.writeModel(b0);
   
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    PmInputStream pis = new PmInputStream(bais);
    Boxers bNull2 = pis.readModel();
    Boxers b1 = pis.readModel();
   
    Assert.assertEquals(0, pis.available());
    pos.close();
    pis.close();
   
    Assert.assertNull(bNull2.getBooleanObject());
    Assert.assertNull(bNull2.getByteObject());
    Assert.assertNull(bNull2.getCharacterObject());
    Assert.assertNull(bNull2.getShortObject());
    Assert.assertNull(bNull2.getIntegerObject());
    Assert.assertNull(bNull2.getLongObject());
    Assert.assertNull(bNull2.getFloatObject());
    Assert.assertNull(bNull2.getDoubleObject());
    Assert.assertNull(bNull2.getInteger24Object());
    Assert.assertNull(bNull.getSignedObject());
    Assert.assertNull(bNull.getUnsignedObject());
   
    Assert.assertEquals(b0.getBooleanObject(), b1.getBooleanObject());
    Assert.assertEquals(b0.getByteObject(), b1.getByteObject());
    Assert.assertEquals(b0.getCharacterObject(), b1.getCharacterObject());
    Assert.assertEquals(b0.getShortObject(), b1.getShortObject());
    Assert.assertEquals(b0.getIntegerObject(), b1.getIntegerObject());
    Assert.assertEquals(b0.getLongObject(), b1.getLongObject());
    Assert.assertEquals(b0.getFloatObject(), b1.getFloatObject());
    Assert.assertEquals(b0.getDoubleObject(), b1.getDoubleObject());
    Assert.assertEquals(b0.getInteger24Object(), b1.getInteger24Object());
    Assert.assertEquals(b0.getSignedObject(), b1.getSignedObject());
    Assert.assertEquals(b0.getUnsignedObject(), b1.getUnsignedObject());
  }
}
TOP

Related Classes of com.peterhi.property.TestPropModel

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.