Merge "Copyright"
[packetcable.git] / packetcable-driver / src / test / java / org / umu / cops / stack / COPSMsgParserTest.java
1 package org.umu.cops.stack;
2
3 import org.junit.Assert;
4 import org.junit.Test;
5 import org.pcmm.rcd.IPCMMClient;
6
7 /**
8  * Tests the public static COPSMsgParser methods
9  */
10 public class COPSMsgParserTest {
11
12     @Test
13     public void testCombineTwoBytesToOne() {
14
15         byte byte1 = (byte)2;
16         byte byte2 = (byte)15;
17         byte combined = (byte)((byte1) * 16 + (byte2));
18
19         System.out.println("Combined value =" + combined + " Hex value = " + Integer.toHexString(combined) );
20
21     }
22
23     @Test
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) );
29
30                 Assert.assertTrue(combined >= 0 && combined < 256);
31                 byte[] nibbles = COPSMsgParser.splitByteToNibbles((byte) combined);
32
33                 Assert.assertEquals("Nibble 1 value = " + nibbles[0], byte1, nibbles[0]);
34                 Assert.assertEquals("Nibble 2 value = " + nibbles[1], byte2, nibbles[1]);
35             }
36         }
37     }
38
39     @Test
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);
48     }
49
50     @Test
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]);
58     }
59
60     @Test
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);
65     }
66
67     @Test(expected = IllegalArgumentException.class)
68     public void testCombineByte1TooBig() {
69         COPSMsgParser.combineNibbles((byte)16, (byte)0);
70     }
71
72     @Test(expected = IllegalArgumentException.class)
73     public void testCombineByte2TooBig() {
74         COPSMsgParser.combineNibbles((byte)0, (byte)16);
75     }
76
77     @Test(expected = IllegalArgumentException.class)
78     public void testCombineByte1TooSmall() {
79         COPSMsgParser.combineNibbles((byte)-1, (byte)0);
80     }
81
82     @Test(expected = IllegalArgumentException.class)
83     public void testCombineByte2TooSmall() {
84         COPSMsgParser.combineNibbles((byte)0, (byte)-1);
85     }
86
87     // TODO - determine if tests for marshalling & un should be done with this class or implicitly via the COPSMsg objects
88
89 }