1 package org.openflow.codec.protocol.instruction;
3 import java.util.ArrayList;
6 import junit.framework.TestCase;
8 import org.openflow.codec.io.DataBuffers;
9 import org.openflow.codec.io.IDataBuffer;
10 import org.openflow.codec.protocol.action.OFPAction;
11 import org.openflow.codec.protocol.action.OFPActionCopyTimeToLiveIn;
12 import org.openflow.codec.protocol.action.OFPActionOutput;
13 import org.openflow.codec.protocol.factory.OFPBasicFactoryImpl;
14 import org.openflow.codec.protocol.factory.OFPActionFactory;
15 import org.openflow.codec.protocol.instruction.OFPInstructionApplyActions;
16 import org.openflow.codec.protocol.instruction.OFPInstructionClearActions;
17 import org.openflow.codec.protocol.instruction.OFPInstructionType;
18 import org.openflow.codec.protocol.instruction.OFPInstructionWriteActions;
21 * test class to verify instruction structure
26 public class OFPInstructionActionsTest extends TestCase {
28 private OFPActionFactory factory = new OFPBasicFactoryImpl();
30 private IDataBuffer buffer = DataBuffers.allocate(1024);
32 protected void tearDown() throws Exception {
36 public void testOFInstructionApplyActions() {
37 OFPInstructionApplyActions instruction = new OFPInstructionApplyActions();
38 assertTrue(instruction.type.equals(OFPInstructionType.APPLY_ACTIONS));
42 public void testOFInstructionWriteActions() {
43 OFPInstructionWriteActions instruction = new OFPInstructionWriteActions();
44 assertTrue(instruction.type.equals(OFPInstructionType.WRITE_ACTIONS));
48 public void testOFInstructionClearActions() {
49 OFPInstructionClearActions instruction = new OFPInstructionClearActions();
50 assertTrue(instruction.type.equals(OFPInstructionType.CLEAR_ACTIONS));
54 public void testClone() throws CloneNotSupportedException {
55 OFPInstructionApplyActions instruction = getOFInstructionApplyActions();
56 OFPInstructionApplyActions instructionCloned = (OFPInstructionApplyActions) instruction.clone();
57 TestCase.assertEquals(instruction, instructionCloned);
59 OFPAction action = new OFPActionCopyTimeToLiveIn();
60 List<OFPAction> actionList = new ArrayList<OFPAction>();
61 actionList.add(action);
62 instructionCloned.setActions(actionList);
63 instructionCloned.setActionFactory(factory);
64 TestCase.assertNotSame(instruction, instructionCloned);
66 instruction = (OFPInstructionApplyActions) instructionCloned.clone();
67 TestCase.assertEquals(instruction, instructionCloned);
70 public void testReadWriteSuccess() {
71 OFPInstructionApplyActions instruction = getOFInstructionApplyActions();
73 instruction.writeTo(buffer);
76 OFPInstructionApplyActions instructionTemp = new OFPInstructionApplyActions();
77 instructionTemp.setActionFactory(factory);
78 instructionTemp.readFrom(buffer);
79 TestCase.assertEquals(instruction, instructionTemp);
83 public void testReadWriteFailed() {
84 OFPInstructionApplyActions instruction = getOFInstructionApplyActions();
86 instruction.writeTo(buffer);
89 OFPInstructionApplyActions instructionTemp = new OFPInstructionApplyActions();
90 boolean result = false;
92 instructionTemp.readFrom(buffer);
93 } catch (RuntimeException ex) {
96 TestCase.assertTrue(result);
100 public void testToString() {
101 OFPInstructionApplyActions instruction = getOFInstructionApplyActions();
103 instruction.writeTo(buffer);
106 OFPInstructionApplyActions instructionTemp = new OFPInstructionApplyActions();
107 instructionTemp.setActionFactory(factory);
108 instructionTemp.readFrom(buffer);
109 TestCase.assertEquals(instruction.toString(), instructionTemp.toString());
112 public void testEqualHashcode() {
114 OFPInstructionApplyActions instruction = getOFInstructionApplyActions();
116 instruction.writeTo(buffer);
119 OFPInstructionApplyActions instructionTemp = new OFPInstructionApplyActions();
120 instructionTemp.setActionFactory(factory);
121 instructionTemp.readFrom(buffer);
123 TestCase.assertTrue(instruction.equals(instructionTemp));
124 TestCase.assertEquals(instruction.hashCode(), instructionTemp.hashCode());
127 private OFPInstructionApplyActions getOFInstructionApplyActions() {
128 OFPInstructionApplyActions instruction = new OFPInstructionApplyActions();
129 OFPAction action = new OFPActionOutput();
130 List<OFPAction> actionList = new ArrayList<OFPAction>();
131 actionList.add(action);
132 instruction.setActions(actionList);
133 instruction.setActionFactory(factory);