/* * Copyright (C) 2003, 2004, 2005 Joe Walnes. * Copyright (C) 2006, 2007 XStream Committers. * All rights reserved. * * The software in this package is published under the terms of the BSD * style license a copy of which has been included with this distribution in * the LICENSE.txt file. * * Created on 26. September 2003 by Joe Walnes */ package com.thoughtworks.acceptance; import java.math.BigDecimal; import java.math.BigInteger; public class BasicTypesTest extends AbstractAcceptanceTest { public void testPrimitiveNumbers() { assertBothWays(new Integer(99), "99"); assertBothWays(new Integer(-99), "-99"); assertBothWays(new Integer(0), "0"); assertBothWays(new Float(-123.45f), "-123.45"); assertBothWays(new Double(-1234567890.12345), "-1.23456789012345E9"); assertBothWays(new Long(123456789123456L), "123456789123456"); assertBothWays(new Short((short) 123), "123"); } public void testDifferentBaseIntegers() { assertEquals(new Integer(255), xstream.fromXML("0xFF")); assertEquals(new Integer(8), xstream.fromXML("010")); } public void testNegativeIntegersInHex() { assertEquals(new Byte((byte)-1), xstream.fromXML("0xFF")); assertEquals(new Short((short)-1), xstream.fromXML("0xFFFF")); assertEquals(new Integer(-1), xstream.fromXML("0xFFFFFFFF")); assertEquals(new Long(Long.MAX_VALUE), xstream.fromXML("0x7FFFFFFFFFFFFFFF")); } public void testNegativeIntegersInOctal() { assertEquals(new Byte((byte)-1), xstream.fromXML("0377")); assertEquals(new Short((short)-1), xstream.fromXML("0177777")); assertEquals(new Integer(-1), xstream.fromXML("037777777777")); assertEquals(new Long(Long.MAX_VALUE), xstream.fromXML("0777777777777777777777")); } public void testOtherPrimitives() { assertBothWays(new Character('z'), "z"); assertBothWays(Boolean.TRUE, "true"); assertBothWays(Boolean.FALSE, "false"); assertBothWays(new Byte((byte) 44), "44"); } public void testNullCharacter() { assertEquals(new Character('\0'), xstream.fromXML("")); // pre XStream 1.3 assertBothWays(new Character('\0'), ""); } public void testNonUnicodeCharacter() { assertBothWays(new Character('\uffff'), "￿"); } public void testStrings() { assertBothWays("hello world", "hello world"); } public void testStringsWithISOControlCharacter() { assertBothWays("hello\u0004world", "helloworld"); assertBothWays("hello\u0096world", "hello–world"); } public void testStringBuffer() { StringBuffer buffer = new StringBuffer(); buffer.append("woo"); String xml = xstream.toXML(buffer); assertEquals(xml, "woo"); StringBuffer out = (StringBuffer) xstream.fromXML(xml); assertEquals("woo", out.toString()); } public void testBigInteger() { BigInteger bigInteger = new BigInteger("1234567890123456"); assertBothWays(bigInteger, "1234567890123456"); } public void testBigDecimal() { BigDecimal bigDecimal = new BigDecimal("1234567890123456.987654321"); assertBothWays(bigDecimal, "1234567890123456.987654321"); } public void testNull() { assertBothWays(null, ""); } public void testNumberFormats() { assertEquals(1.0, ((Double)xstream.fromXML("1")).doubleValue(), 0.001); assertEquals(1.0f, ((Float)xstream.fromXML("1")).floatValue(), 0.001); } }