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 TestCOPSServer server;
26 public void setup() throws Exception {
27 server = new TestCOPSServer();
29 outSocket = new Socket(InetAddress.getLocalHost(), server.getPort());
33 public void tearDown() throws Exception {
38 @Test(expected = IllegalArgumentException.class)
39 public void version0() {
40 new COPSSyncStateMsg(0, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE, null, null);
43 @Test(expected = IllegalArgumentException.class)
44 public void nullFlag() {
45 new COPSSyncStateMsg(1, null, IPCMMClient.CLIENT_TYPE, null, null);
48 @Test(expected = IllegalArgumentException.class)
49 public void nullHeader() {
50 final COPSHeader hdr = null;
51 new COPSSyncStateMsg(hdr, null, null);
54 @Test(expected = IllegalArgumentException.class)
55 public void invalidHeader() {
56 final COPSHeader hdr = new COPSHeader(1, Flag.UNSOLICITED, OPCode.NA, IPCMMClient.CLIENT_TYPE);
57 new COPSSyncStateMsg(hdr, null, null);
61 public void validMinimal() {
62 final COPSSyncStateMsg msg = new COPSSyncStateMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE, null, null);
64 Assert.assertEquals(1, msg.getHeader().getPcmmVersion());
65 Assert.assertEquals(Flag.SOLICITED, msg.getHeader().getFlag());
66 Assert.assertEquals(IPCMMClient.CLIENT_TYPE, msg.getHeader().getClientType());
67 Assert.assertNull(msg.getClientHandle());
68 Assert.assertNull(msg.getIntegrity());
72 public void validAll() throws Exception {
73 final COPSSyncStateMsg msg = new COPSSyncStateMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
74 new COPSHandle(new COPSData()),
77 Assert.assertEquals(1, msg.getHeader().getPcmmVersion());
78 Assert.assertEquals(Flag.SOLICITED, msg.getHeader().getFlag());
79 Assert.assertEquals(IPCMMClient.CLIENT_TYPE, msg.getHeader().getClientType());
80 Assert.assertEquals(new COPSHandle(new COPSData()), msg.getClientHandle());
81 Assert.assertEquals(new COPSIntegrity(), msg.getIntegrity());
85 * This test is responsible for creating a COPSSyncStateMsg object without any nulls or empty collections
86 * and then is dumped to an OutputStream.
87 * @throws Exception - Test should fail if any exception is thrown
90 public void testDumpAll() throws Exception {
91 final COPSSyncStateMsg msg = new COPSSyncStateMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
92 new COPSHandle(new COPSData()),
95 final ByteArrayOutputStream os = new ByteArrayOutputStream();
98 final String out = new String(os.toByteArray());
99 System.out.println(out);
100 final String[] lines = out.split("\n");
101 Assert.assertEquals(15, lines.length);
103 // Only checking COPSMsg elements as the COPSObjectMsg elements have already been validated in their own tests
104 Assert.assertEquals("**MSG HEADER**", lines[0]);
105 Assert.assertEquals("Version: 1", lines[1]);
106 Assert.assertEquals("Flags: SOLICITED", lines[2]);
107 Assert.assertEquals("OpCode: SSQ", lines[3]);
108 Assert.assertEquals("Client-type: -32758", lines[4]);
112 * This test is responsible for creating a COPSSyncStateMsg object with the minimal necessary attributes to make
113 * it valid. It is then streamed over a socket (unmarshalled) then reassembled (marshalled).
114 * @throws Exception - Test should fail if any exception is thrown
117 public void testWriteMinimal() throws Exception {
118 final COPSSyncStateMsg msg = new COPSSyncStateMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE, null, null);
120 msg.writeData(outSocket);
122 final long start = System.currentTimeMillis();
123 while (server.copsMsgs.size() < 1) {
125 if (System.currentTimeMillis() - start > 2000) break;
128 Assert.assertEquals(1, server.copsMsgs.size());
129 Assert.assertEquals(msg, server.copsMsgs.get(0));
133 * This test is responsible for creating a COPSSyncStateMsg object without any nulls or empty collections
134 * and then is streamed over a socket (unmarshalled) then reassembled (marshalled)
135 * @throws Exception - Test should fail if any exception is thrown
138 public void testWriteAll() throws Exception {
139 final COPSSyncStateMsg msg = new COPSSyncStateMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
140 new COPSHandle(new COPSData()),
141 new COPSIntegrity(8, 9, new COPSData("12345")));
143 msg.writeData(outSocket);
145 final long start = System.currentTimeMillis();
146 while (server.copsMsgs.size() < 1) {
148 if (System.currentTimeMillis() - start > 2000) break;
151 Assert.assertEquals(1, server.copsMsgs.size());
152 Assert.assertEquals(msg, server.copsMsgs.get(0));