Completed COPS Message refactoring. Was planning on one additional patch starting...
[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
6 /**
7  * Tests the public static COPSMsgParser methods
8  */
9 public class COPSMsgParserTest {
10
11     @Test
12     public void testCombineTwoBytesToOne() {
13
14         byte byte1 = (byte)2;
15         byte byte2 = (byte)15;
16         byte combined = (byte)((byte1) * 16 + (byte2));
17
18         System.out.println("Combined value =" + combined + " Hex value = " + Integer.toHexString(combined) );
19
20     }
21
22     @Test
23     public void testCombineAndSplitForAllValidCombinations() {
24         for (byte byte1 = 0; byte1 < 16; byte1++ ) {
25             for (byte byte2 = 0; byte2 < 16; byte2++ ) {
26                 int combined = COPSMsgParser.combineNibbles(byte1, byte2);
27                 System.out.println("byte1 = " + byte1 + " byte2 = " + byte2 + " Combined value =" + combined + " Hex value = " + Integer.toHexString(combined) );
28
29                 Assert.assertTrue(combined >= 0 && combined < 256);
30                 byte[] nibbles = COPSMsgParser.splitByteToNibbles((byte) combined);
31
32                 Assert.assertEquals("Nibble 1 value = " + nibbles[0], byte1, nibbles[0]);
33                 Assert.assertEquals("Nibble 2 value = " + nibbles[1], byte2, nibbles[1]);
34             }
35         }
36     }
37
38     @Test(expected = IllegalArgumentException.class)
39     public void testCombineByte1TooBig() {
40         COPSMsgParser.combineNibbles((byte)16, (byte)0);
41     }
42
43     @Test(expected = IllegalArgumentException.class)
44     public void testCombineByte2TooBig() {
45         COPSMsgParser.combineNibbles((byte)0, (byte)16);
46     }
47
48     @Test(expected = IllegalArgumentException.class)
49     public void testCombineByte1TooSmall() {
50         COPSMsgParser.combineNibbles((byte)-1, (byte)0);
51     }
52
53     @Test(expected = IllegalArgumentException.class)
54     public void testCombineByte2TooSmall() {
55         COPSMsgParser.combineNibbles((byte)0, (byte)-1);
56     }
57
58     // TODO - determine if tests for marshalling & un should be done with this class or implicitly via the COPSMsg objects
59
60 }