1 package org.umu.cops.stack;
3 import org.junit.After;
4 import org.junit.Assert;
5 import org.junit.Before;
7 import org.pcmm.rcd.IPCMMClient;
8 import org.umu.cops.stack.COPSHeader.Flag;
9 import org.umu.cops.stack.COPSHeader.OPCode;
11 import java.io.ByteArrayOutputStream;
12 import java.net.InetAddress;
13 import java.net.Socket;
16 * Tests for the first constructor of the COPSSyncStateMsg class.
17 * Should any of these tests be inaccurate it is due to the fact that they have been written after COPSSyncStateMsg had been
18 * released and my assumptions may be incorrect.
20 public class COPSSyncStateMsgTest {
22 private final static int testPort = 7777;
23 TestCOPSServer server;
27 public void setup() throws Exception {
28 server = new TestCOPSServer(testPort);
30 outSocket = new Socket(InetAddress.getLocalHost(), testPort);
34 public void tearDown() throws Exception {
39 @Test(expected = IllegalArgumentException.class)
40 public void version0() {
41 new COPSSyncStateMsg(0, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE, null, null);
44 @Test(expected = IllegalArgumentException.class)
45 public void nullFlag() {
46 new COPSSyncStateMsg(1, null, IPCMMClient.CLIENT_TYPE, null, null);
49 @Test(expected = IllegalArgumentException.class)
50 public void nullHeader() {
51 final COPSHeader hdr = null;
52 new COPSSyncStateMsg(hdr, null, null);
55 @Test(expected = IllegalArgumentException.class)
56 public void invalidHeader() {
57 final COPSHeader hdr = new COPSHeader(1, Flag.UNSOLICITED, OPCode.NA, IPCMMClient.CLIENT_TYPE);
58 new COPSSyncStateMsg(hdr, null, null);
62 public void validMinimal() {
63 final COPSSyncStateMsg msg = new COPSSyncStateMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE, null, null);
65 Assert.assertEquals(1, msg.getHeader().getPcmmVersion());
66 Assert.assertEquals(Flag.SOLICITED, msg.getHeader().getFlag());
67 Assert.assertEquals(IPCMMClient.CLIENT_TYPE, msg.getHeader().getClientType());
68 Assert.assertNull(msg.getClientHandle());
69 Assert.assertNull(msg.getIntegrity());
73 public void validAll() throws Exception {
74 final COPSSyncStateMsg msg = new COPSSyncStateMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
75 new COPSHandle(new COPSData()),
78 Assert.assertEquals(1, msg.getHeader().getPcmmVersion());
79 Assert.assertEquals(Flag.SOLICITED, msg.getHeader().getFlag());
80 Assert.assertEquals(IPCMMClient.CLIENT_TYPE, msg.getHeader().getClientType());
81 Assert.assertEquals(new COPSHandle(new COPSData()), msg.getClientHandle());
82 Assert.assertEquals(new COPSIntegrity(), msg.getIntegrity());
86 * This test is responsible for creating a COPSSyncStateMsg object without any nulls or empty collections
87 * and then is dumped to an OutputStream.
88 * @throws Exception - Test should fail if any exception is thrown
91 public void testDumpAll() throws Exception {
92 final COPSSyncStateMsg msg = new COPSSyncStateMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
93 new COPSHandle(new COPSData()),
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(15, lines.length);
104 // Only checking COPSMsg elements as the COPSObjectMsg elements have already been validated in their own tests
105 Assert.assertEquals("**MSG HEADER**", lines[0]);
106 Assert.assertEquals("Version: 1", lines[1]);
107 Assert.assertEquals("Flags: SOLICITED", lines[2]);
108 Assert.assertEquals("OpCode: SSQ", lines[3]);
109 Assert.assertEquals("Client-type: -32758", lines[4]);
113 * This test is responsible for creating a COPSSyncStateMsg object with the minimal necessary attributes to make
114 * it valid. It is then streamed over a socket (unmarshalled) then reassembled (marshalled).
115 * @throws Exception - Test should fail if any exception is thrown
118 public void testWriteMinimal() throws Exception {
119 final COPSSyncStateMsg msg = new COPSSyncStateMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE, null, null);
121 msg.writeData(outSocket);
123 final long start = System.currentTimeMillis();
124 while (server.copsMsgs.size() < 1) {
126 if (System.currentTimeMillis() - start > 2000) break;
129 Assert.assertEquals(1, server.copsMsgs.size());
130 Assert.assertEquals(msg, server.copsMsgs.get(0));
134 * This test is responsible for creating a COPSSyncStateMsg object without any nulls or empty collections
135 * and then is streamed over a socket (unmarshalled) then reassembled (marshalled)
136 * @throws Exception - Test should fail if any exception is thrown
139 public void testWriteAll() throws Exception {
140 final COPSSyncStateMsg msg = new COPSSyncStateMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
141 new COPSHandle(new COPSData()),
142 new COPSIntegrity(8, 9, new COPSData("12345")));
144 msg.writeData(outSocket);
146 final long start = System.currentTimeMillis();
147 while (server.copsMsgs.size() < 1) {
149 if (System.currentTimeMillis() - start > 2000) break;
152 Assert.assertEquals(1, server.copsMsgs.size());
153 Assert.assertEquals(msg, server.copsMsgs.get(0));