/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.juniform;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
/**
*
* @author acherkashin
*/
public class JUniformMessagePackTest
{
private static JUniformPackerMessagePack _packer;
@BeforeClass
public static void preparePacker() {
_packer = new JUniformPackerMessagePack();
}
public static byte[] hexStringToByteArray(String s) {
s = s.replaceAll(" ", "");
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
@Test
public void testValue() {
byte[] bytes = new byte[] { 01 }; // 1
JUniformObject object = _packer.toUniformObjectFromBytes(bytes);
Assert.assertEquals(1L, object.getValue());
}
@Test
public void testMapFloatValue() {
String result;
byte[] originalBytes = hexStringToByteArray("81 a7 63 6f 6d 70 61 63 74 cb 40 16 b4 39 58 10 62 4e"); // {"compact":5.676}
byte[] bytes;
JUniformObject object = _packer.toUniformObjectFromBytes(originalBytes);
Assert.assertEquals(5.676f, object.getProperty("compact").toFloat(), 0.000001f);
bytes = (byte[])_packer.fromUniformObject(object);
JUniformObject newObject = _packer.toUniformObjectFromBytes(bytes);
Assert.assertEquals(5.676f, newObject.getProperty("compact").toFloat(), 0.000001f);
}
@Test
public void testComplexMap() {
String result;
byte[] originalBytes = hexStringToByteArray("81 a7 63 6f 6d 70 61 63 74 82 a4 74 65 73 74 a4 68 65 6c 70 a4 68 61 68 61 92 cd 01 b9 0b"); // {"compact":{"test":"help","haha":[441,11]}}
byte[] bytes;
JUniformObject object = _packer.toUniformObjectFromBytes(originalBytes);
Assert.assertEquals("help", object.getProperty("compact").getProperty("test").getValue());
Assert.assertEquals(2, object.getProperty("compact").getProperty("haha").getElementsCount());
Assert.assertTrue(11 == object.getProperty("compact").getProperty("haha").getElementByIndex(1).toInteger());
bytes = (byte[])_packer.fromUniformObject(object);
JUniformObject newObject = _packer.toUniformObjectFromBytes(bytes);
// the same checks after export/import
Assert.assertEquals("help", newObject.getProperty("compact").getProperty("test").getValue());
Assert.assertEquals(2, newObject.getProperty("compact").getProperty("haha").getElementsCount());
Assert.assertTrue(11 == newObject.getProperty("compact").getProperty("haha").getElementByIndex(1).toInteger());
}
}