@Test
public void testHandleOutgoingMessage() throws Exception {

    OFMessage m = factory.buildEchoRequest().build();
    IOFSwitchBackend sw = createMock(IOFSwitchBackend.class);
    expect(sw.getId()).andReturn(DATAPATH_ID_0).anyTimes();
    // Add listeners
    IOFMessageListener test1 = createMock(IOFMessageListener.class);
    expect(test1.getName()).andReturn("test1").anyTimes();
    setupListenerOrdering(test1);
    IOFMessageListener test2 = createMock(IOFMessageListener.class);
    expect(test2.getName()).andReturn("test2").anyTimes();
    test2.isCallbackOrderingPostreq(OFType.ECHO_REQUEST, "test1");
    expectLastCall().andReturn(true).atLeastOnce();
    setupListenerOrdering(test2);
    IOFMessageListener test3 = createMock(IOFMessageListener.class);
    expect(test3.getName()).andReturn("test3").anyTimes();
    test3.isCallbackOrderingPostreq(OFType.ECHO_REQUEST, "test2");
    expectLastCall().andReturn(true).atLeastOnce();
    setupListenerOrdering(test3);
    // expected ordering is test3, test2, test1
    replay(test1, test2, test3);
    controller.addOFMessageListener(OFType.ECHO_REQUEST, test1);
    controller.addOFMessageListener(OFType.ECHO_REQUEST, test3);
    controller.addOFMessageListener(OFType.ECHO_REQUEST, test2);
    verify(test1);
    verify(test2);
    verify(test3);
    // Test inject with null switch and no message. Should not work.
    reset(test1, test2, test3);
    replay(test1, test2, test3, sw);
    try {
        controller.handleOutgoingMessage(null, pi);
        fail("handleOutgoindMessage should have thrown a NPE");
    } catch (NullPointerException e) {
        // expected
    }
    try {
        controller.handleOutgoingMessage(sw, null);
        fail("handleOutgoingMessage should have thrown a NPE");
    } catch (NullPointerException e) {
        // expected
    }
    verify(test1);
    verify(test2);
    verify(test3);
    verify(sw);
    // Test the handleOutgoingMessage
    reset(test1, test2, test3, sw);
    expect(sw.getId()).andReturn(DATAPATH_ID_0).anyTimes();
    expect(test2.receive(same(sw), same(m) , isA(FloodlightContext.class)))
            .andReturn(Command.STOP);
    expect(test3.receive(same(sw), same(m) , isA(FloodlightContext.class)))
            .andReturn(Command.CONTINUE);
    // test1 will not receive any message!
    replay(test1, test2, test3, sw);
    controller.handleOutgoingMessage(sw, m);
    verify(test1);
    verify(test2);
    verify(test3);
    verify(sw);
    // Test the handleOutgoingMessage with null context
    reset(test1, test2, test3, sw);
    expect(sw.getId()).andReturn(DATAPATH_ID_0).anyTimes();
    expect(test2.receive(same(sw), same(m) , isA(FloodlightContext.class)))
            .andReturn(Command.STOP);
    expect(test3.receive(same(sw), same(m) , isA(FloodlightContext.class)))
            .andReturn(Command.CONTINUE);
    // test1 will not receive any message!
    replay(test1, test2, test3, sw);
    controller.handleOutgoingMessage(sw, m);
    verify(test1);
    verify(test2);
    verify(test3);
    verify(sw);
    // Test for message without listeners
    reset(test1, test2, test3, sw);
    replay(test1, test2, test3, sw);
    m = factory.buildEchoReply().build();
    controller.handleOutgoingMessage(sw, m);
    verify(test1);
    verify(test2);
    verify(test3);
    verify(sw);
}