1 package org.umu.cops.stack;
3 import org.junit.Assert;
5 import org.umu.cops.stack.COPSObjHeader.CNum;
6 import org.umu.cops.stack.COPSObjHeader.CType;
8 import java.io.ByteArrayOutputStream;
9 import java.net.InetAddress;
10 import java.net.UnknownHostException;
13 * Tests for the first constructor of the COPSIpv4LastPdpAddr class.
14 * Should any of these tests be inaccurate it is due to the fact that they have been written after COPSAcctTimer had been
15 * released and my assumptions may be incorrect.
17 public class COPSIpv4LastPdpAddrTest {
19 @Test(expected = IllegalArgumentException.class)
20 public void nullHost() throws Exception {
21 new COPSIpv4LastPdpAddr(null, 1234, (short)0);
24 @Test(expected = UnknownHostException.class)
25 public void invalidHost() throws Exception {
26 new COPSIpv4LastPdpAddr("foo", 1234, (short)0);
29 @Test(expected = IllegalArgumentException.class)
30 public void invalidPort() throws Exception {
31 new COPSIpv4LastPdpAddr("localhost", 0, (short)0);
35 public void validConstructor1() throws Exception {
36 final COPSIpv4LastPdpAddr lastAddr = new COPSIpv4LastPdpAddr("localhost", 1234, (short)0);
37 Assert.assertEquals(8, lastAddr.getDataLength());
38 Assert.assertEquals(new COPSObjHeader(CNum.LAST_PDP_ADDR, CType.DEF), lastAddr.getHeader());
39 Assert.assertEquals(1234, lastAddr.getTcpPort());
40 Assert.assertEquals(0, lastAddr.getReserved());
42 final ByteArrayOutputStream os = new ByteArrayOutputStream();
45 final String out = new String(os.toByteArray());
46 System.out.println(out);
47 final String[] lines = out.split("\n");
48 Assert.assertEquals(6, lines.length);
49 Assert.assertEquals("**Last PDP addr**", lines[0]);
50 Assert.assertEquals("C-num: LAST_PDP_ADDR", lines[1]);
51 Assert.assertEquals("C-type: DEF", lines[2]);
52 Assert.assertEquals("Ipv4LastPdpAddress", lines[3]);
53 Assert.assertTrue(lines[4].equals("Address: localhost") || lines[4].equals("Address: 127.0.0.1"));
54 Assert.assertEquals("Port: 1234", lines[5]);
57 @Test(expected = IllegalArgumentException.class)
58 public void nullObjHeader() throws Exception {
59 final byte[] addr = InetAddress.getByName("localhost").getAddress();
60 new COPSIpv4LastPdpAddr(null, addr, 1234, (short)0);
63 @Test(expected = IllegalArgumentException.class)
64 public void invalidCNum() throws Exception {
65 final byte[] addr = InetAddress.getByName("localhost").getAddress();
66 new COPSIpv4LastPdpAddr(new COPSObjHeader(CNum.ACCT_TIMER, CType.DEF), addr, 1234, (short)0);
69 @Test(expected = IllegalArgumentException.class)
70 public void invalidCType() throws Exception {
71 final byte[] addr = InetAddress.getByName("localhost").getAddress();
72 new COPSIpv4LastPdpAddr(new COPSObjHeader(CNum.LAST_PDP_ADDR, CType.STATELESS), addr, 1234, (short)0);
75 @Test(expected = IllegalArgumentException.class)
76 public void nullAddr() throws Exception {
77 new COPSIpv4LastPdpAddr(new COPSObjHeader(CNum.LAST_PDP_ADDR, CType.DEF), null, 1234, (short)0);
80 @Test(expected = IllegalArgumentException.class)
81 public void zeroPort() throws Exception {
82 final byte[] addr = InetAddress.getByName("localhost").getAddress();
83 new COPSIpv4LastPdpAddr(new COPSObjHeader(CNum.LAST_PDP_ADDR, CType.DEF), addr, 0, (short)0);
87 public void validConstructor2() throws Exception {
88 final byte[] addr = InetAddress.getByName("localhost").getAddress();
89 final COPSIpv4LastPdpAddr lastAddr = new COPSIpv4LastPdpAddr(new COPSObjHeader(CNum.LAST_PDP_ADDR, CType.DEF),
90 addr, 1234, (short)0);
91 Assert.assertEquals(8, lastAddr.getDataLength());
92 Assert.assertEquals(new COPSObjHeader(CNum.LAST_PDP_ADDR, CType.DEF), lastAddr.getHeader());
93 Assert.assertEquals(1234, lastAddr.getTcpPort());
94 Assert.assertEquals(0, lastAddr.getReserved());
96 final ByteArrayOutputStream os = new ByteArrayOutputStream();
99 final String out = new String(os.toByteArray());
100 System.out.println(out);
101 final String[] lines = out.split("\n");
102 Assert.assertEquals(6, lines.length);
103 Assert.assertEquals("**Last PDP addr**", lines[0]);
104 Assert.assertEquals("C-num: LAST_PDP_ADDR", lines[1]);
105 Assert.assertEquals("C-type: DEF", lines[2]);
106 Assert.assertEquals("Ipv4LastPdpAddress", lines[3]);
107 Assert.assertTrue(lines[4].equals("Address: localhost") || lines[4].equals("Address: 127.0.0.1"));
108 Assert.assertEquals("Port: 1234", lines[5]);
111 // The writeData() method will be tested implicitly via any of the COPSMsg tests