Blame | Last modification | View Log | RSS feed
package org.hibernatespatial.wkb;import org.junit.Test;import static junit.framework.Assert.assertEquals;import static org.junit.Assert.assertTrue;import static org.junit.Assert.fail;/*** @author Karel Maesen, Geovise BVBA* creation-date: Oct 29, 2010*/public class TestBytes {@Testpublic void testBytesFromString() {String byteTxt = "0120E6FF";Bytes bStream = Bytes.from(byteTxt);assertEquals(4, bStream.limit());assertEquals(1, bStream.get());assertEquals(32, bStream.get());assertEquals((byte) 230, bStream.get());assertEquals((byte) 255, bStream.get());}@Testpublic void test_bytes_from_odd_length_string() {String byteTxt = "0120E6FFE";Bytes bStream = Bytes.from(byteTxt);assertEquals(4, bStream.limit());}@Testpublic void test_throws_NumberFormatException_if_string_not_hexadecimal() {try {Bytes.from("02FFKK");fail();} catch (NumberFormatException e) {//OK}}@Testpublic void test_return_empty_Bytes_on_empty_string() {Bytes bytes = Bytes.from("");assertTrue(bytes.isEmpty());assertEquals(0, bytes.limit());}@Testpublic void test_throw_IllegalArgumentException_on_null_string() {try {Bytes bytes = Bytes.from((String) null);fail();} catch (IllegalArgumentException e) {//OK}}@Testpublic void test_toString(){String hxStr = "037FB4C7";Bytes bytes = Bytes.from(hxStr);assertEquals(hxStr, bytes.toString());}@Testpublic void test_big_endian_int() {String hxStr = "037FB4C7";int expectedValue = 58700999;Bytes bytes = Bytes.from(hxStr);bytes.setWKBByteOrder(WKBByteOrder.XDR);assertEquals(expectedValue, bytes.getInt());}@Testpublic void test_string_or_bytes_gives_same_result() {String hxStr = "037FB4C7";byte[] byteArray = new byte[]{0x03, 0x7F, (byte) 0xB4, (byte) 0xC7};int expectedValue = 58700999;Bytes bytes = Bytes.from(hxStr);bytes.setWKBByteOrder(WKBByteOrder.XDR);assertEquals(expectedValue, bytes.getInt());Bytes bytesFromArray = Bytes.from(byteArray);bytesFromArray.setWKBByteOrder(WKBByteOrder.XDR);assertEquals(expectedValue, bytesFromArray.getInt());}@Testpublic void test_little_endian_int() {String hxStr = "C7B47F03";int expectedValue = 58700999;Bytes bytes = Bytes.from(hxStr);bytes.setWKBByteOrder(WKBByteOrder.NDR);assertEquals(expectedValue, bytes.getInt());}@Testpublic void test_insufficient_bytes_for_retrieving_long_throws_exception() {String hxStr = "C7B47F03";Bytes bytes = Bytes.from(hxStr);try {Long lng = bytes.getLong();} catch (RuntimeException e) {//OK}}@Testpublic void test_get_unsigned_as_long() {String hxStr = "FFFFFFFF";Bytes bytes = Bytes.from(hxStr);long expected = 4294967295L;assertEquals(expected, bytes.getUInt());bytes = Bytes.from("80000000");assertEquals(2147483648L, bytes.getUInt());bytes = Bytes.from("00000001");assertEquals(1L, bytes.getUInt());bytes = Bytes.from("7FFFFFFF");assertEquals(2147483647L, bytes.getUInt());}@Testpublic void test_retrieve_byteorder() {String hxStr = "FFFFFFFF";Bytes bytes = Bytes.from(hxStr);bytes.setWKBByteOrder(WKBByteOrder.XDR);assertEquals(WKBByteOrder.XDR, bytes.getWKBByteOrder());bytes.setWKBByteOrder(WKBByteOrder.NDR);assertEquals(WKBByteOrder.NDR, bytes.getWKBByteOrder());}@Testpublic void test_create_bytes_having_specified_size() {Bytes bytes = Bytes.allocate(100);assertEquals(100, bytes.capacity());}@Testpublic void test_put_xdr_double() {double expected = 1234.56789;Bytes bytes = Bytes.allocate(8);bytes.setWKBByteOrder(WKBByteOrder.XDR);bytes.putDouble(expected);bytes.rewind();double d = bytes.getDouble();assertEquals(expected, d);}@Testpublic void test_put_ndr_double() {double expected = 1234.56789;Bytes bytes = Bytes.allocate(8);bytes.setWKBByteOrder(WKBByteOrder.NDR);bytes.putDouble(expected);bytes.rewind();double d = bytes.getDouble();assertEquals(expected, d);}@Testpublic void test_put_byte() {byte expected = (byte) 0xFF;Bytes bytes = Bytes.allocate(1);bytes.put(expected);bytes.rewind();assertEquals(expected, bytes.get());}@Testpublic void test_put_int() {int expected = 0xFF;Bytes bytes = Bytes.allocate(4);bytes.putInt(expected);bytes.rewind();assertEquals(expected, bytes.getInt());}@Testpublic void test_put_long() {long expected = Long.MAX_VALUE;Bytes bytes = Bytes.allocate(8);bytes.putLong(expected);bytes.rewind();assertEquals(expected, bytes.getLong());}@Testpublic void test_put_float() {float expected = 1234.567f;Bytes bytes = Bytes.allocate(4);bytes.putFloat(expected);bytes.rewind();assertEquals(expected, bytes.getFloat());}@Testpublic void test_put_uint() {long expected = 4294967295L;Bytes bytes = Bytes.allocate(4);bytes.putUInt(expected);bytes.rewind();assertEquals(expected, bytes.getUInt());}@Testpublic void test_put_too_large_value_in_uint_throws_exception() {long value = Bytes.UINT_MAX_VALUE;Bytes bytes = Bytes.allocate(4);bytes.putUInt(value);bytes.rewind();assertEquals(value, bytes.getUInt());value++;Bytes bytes2 = Bytes.allocate(4);try {bytes2.putUInt(value);fail();} catch (RuntimeException e) {//OK}}}