1 package org.umu.cops.stack;
3 import org.junit.Assert;
5 import org.pcmm.rcd.IPCMMClient;
8 * Tests the public static COPSMsgParser methods
10 public class COPSMsgParserTest {
13 public void testCombineTwoBytesToOne() {
16 byte byte2 = (byte)15;
17 byte combined = (byte)((byte1) * 16 + (byte2));
19 System.out.println("Combined value =" + combined + " Hex value = " + Integer.toHexString(combined) );
24 public void testCombineAndSplitForAllValidCombinations() {
25 for (byte byte1 = 0; byte1 < 16; byte1++ ) {
26 for (byte byte2 = 0; byte2 < 16; byte2++ ) {
27 int combined = COPSMsgParser.combineNibbles(byte1, byte2);
28 System.out.println("byte1 = " + byte1 + " byte2 = " + byte2 + " Combined value =" + combined + " Hex value = " + Integer.toHexString(combined) );
30 Assert.assertTrue(combined >= 0 && combined < 256);
31 byte[] nibbles = COPSMsgParser.splitByteToNibbles((byte) combined);
33 Assert.assertEquals("Nibble 1 value = " + nibbles[0], byte1, nibbles[0]);
34 Assert.assertEquals("Nibble 2 value = " + nibbles[1], byte2, nibbles[1]);
40 public void testBytesToShortMin() {
41 final byte byte1 = (byte)0;
42 final byte byte2 = (byte)0;
43 final short val = COPSMsgParser.bytesToShort(byte1, byte2);
44 final byte[] outBytes = COPSMsgParser.shortToBytes(val);
45 Assert.assertEquals(byte1, outBytes[0]);
46 Assert.assertEquals(byte2, outBytes[1]);
47 Assert.assertEquals(0, val);
51 public void testBytesToShortMax() {
52 final byte byte1 = (byte)255;
53 final byte byte2 = (byte)255;
54 final short val = COPSMsgParser.bytesToShort(byte1, byte2);
55 final byte[] outBytes = COPSMsgParser.shortToBytes(val);
56 Assert.assertEquals(byte1, outBytes[0]);
57 Assert.assertEquals(byte2, outBytes[1]);
61 public void testBytesToShortPCMMClientType() {
62 final byte[] outBytes = COPSMsgParser.shortToBytes(IPCMMClient.CLIENT_TYPE);
63 final short val = COPSMsgParser.bytesToShort(outBytes[0], outBytes[1]);
64 Assert.assertEquals(IPCMMClient.CLIENT_TYPE, val);
67 @Test(expected = IllegalArgumentException.class)
68 public void testCombineByte1TooBig() {
69 COPSMsgParser.combineNibbles((byte)16, (byte)0);
72 @Test(expected = IllegalArgumentException.class)
73 public void testCombineByte2TooBig() {
74 COPSMsgParser.combineNibbles((byte)0, (byte)16);
77 @Test(expected = IllegalArgumentException.class)
78 public void testCombineByte1TooSmall() {
79 COPSMsgParser.combineNibbles((byte)-1, (byte)0);
82 @Test(expected = IllegalArgumentException.class)
83 public void testCombineByte2TooSmall() {
84 COPSMsgParser.combineNibbles((byte)0, (byte)-1);
87 // TODO - determine if tests for marshalling & un should be done with this class or implicitly via the COPSMsg objects