Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * Conditions Of Use
  *
  * This software was developed by employees of the National Institute of
  * Standards and Technology (NIST), and others.
  * This software is has been contributed to the public domain.
  * As a result, a formal license is not needed to use the software.
  *
  * This software is provided "AS IS."
 * NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
 * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
 * AND DATA ACCURACY.  NIST does not warrant or make any representations
 * regarding the use of the software or the results thereof, including but
 * not limited to the correctness, accuracy, reliability or usefulness of
 * the software.
 *
 *
 */
 package test.tck.msgflow;
 
 import  junit.framework.*;
 
 import javax.sip.*;
 import java.util.*;
 import test.tck.*;

Title: TCK

Description: JAIN SIP 1.2 Technology Compatibility Kit

Author(s):
Emil Ivov Network Research Team, Louis Pasteur University, Strasbourg, France. This code is in the public domain.
Version:
1.0
 
 
 public class DialogTest extends MessageFlowHarness {
    
The Dialog to test
 
     private Dialog dialog = null;
    
The request that was sent by the T
 
     private Request tiInvite = null;
    
The initial invite request at the RI side
 
     private Request riInvite = null;
     private ClientTransaction cliTran = null;
     private Response ringing = null;
 
 
     private String riToTag// JvB: to-tag set by RI
 
 
     public DialogTest(String name) {
         super(name);
     }
 
     //==================== add a dialog to the fixture ========
     
Calls MessageFlowHarness.setUp() and creates a dialog afterwards.

Throws:
Exception if anything goes wrong
 
     public void setUp() throws Exception {
         try {
             super.setUp();
              = createTiInviteRequest(nullnullnull);
             //Send an invite request
             try {
                 .collectRequestEvent();
                  = .getNewClientTransaction();
                 .sendRequest();
             } catch (TooManyListenersException e) {
                 throw new TckInternalError(
                     "Failed to register a listener with the RI",
                     e);
             } catch (SipException e) {
                 throw new TiUnexpectedError(
                     "Failed to send initial invite request",
                     e);
             }
             //Wait for the invite to arrive
             waitForMessage();
             RequestEvent inviteReqEvt =
                 .extractCollectedRequestEvent();
             if (inviteReqEvt == null || inviteReqEvt.getRequest() == null)
                 throw new TiUnexpectedError("The TI did not send the initial invite request");
              = inviteReqEvt.getRequest();
             //get the dialog
              = .getDialog();
             //Send ringing to initialise dialog
             //start listening for the response
             try {
                 .collectResponseEvent();
             } catch (TooManyListenersException e) {
                 throw new TiUnexpectedError(
                     "Failed to register a SipListener with the TI.",
                     e);
             }
             try {
                  =
                    .createResponse(
                        .,
                        inviteReqEvt.getRequest());
                ((ToHeader.getHeader(.)).setTag(
                     = Integer.toString(hashCode()));
                // BUG report from Ben Evans:
                // set contact header on dialog-creating response
                .setHeader(createRiContact());
                .sendResponse();
            } catch (Exception e) {
                throw new TckInternalError(
                    "Failed to create and send a RINGING response",
                    e);
            }
            waitForMessage();
            ResponseEvent ringingRespEvt =
                .extractCollectedResponseEvent();
            if (ringingRespEvt == null || ringingRespEvt.getResponse() == null)
                throw new TiUnexpectedError("The TI did not dispatch a RINGING response.");
        } catch (Throwable exc) {
            exc.printStackTrace();
            fail(exc.getClass().getName() + ": " + exc.getMessage());
        }
        assertTrue(new Exception().getStackTrace()[0].toString(), true);
    }
    //==================== tests ==============================
    
Test whether dialog fields are properly set
    public void testDialogProperties() {
        try {
            //CallId
            assertEquals(
                "The Dialog did not have the right Call ID.",
                ((CallIdHeader.getHeader(.))
                    .getCallId(),
                .getCallId().getCallId());
            //Tran
            /* Deprecated
             * assertSame(
                "The Dialog.getTransaction did not return the right transaction.",
                cliTran,
                dialog.getFirstTransaction());
            */
            //LocalParty
            assertEquals(
                "Dialog.getLocalParty() returned a bad address.",
                ((FromHeader.getHeader(.)).getAddress(),
                .getLocalParty());
            //SeqNum
            assertTrue(
                "Dialog.getLocalSequenceNumber() returned a bad value.",
                1 == .getLocalSeqNumber());
            //LocalTag
            assertEquals(
                "Dialog.getLocalTag() returned a bad tag",
                ((FromHeader.getHeader(.)).getTag(),
                .getLocalTag());
            //RemoteParty
            assertEquals(
                "Dialog.getRemoteParty() returned a bad address.",
                ((ToHeader.getHeader(.)).getAddress(),
                .getRemoteParty());
            //RemoteTag
                assertEquals(
                "Dialog.getRemoteTag() returned a bad tag",
            ((ToHeader.getHeader(.)).getTag(),
                .getRemoteTag());
            //is server
            assertFalse(
                "Dialog.isServer returned true for a client side dialog",
                .isServer());
        } catch (Throwable exc) {
            exc.printStackTrace();
            fail(exc.getClass().getName() + ": " + exc.getMessage());
        }
        assertTrue(new Exception().getStackTrace()[0].toString(), true);
    }

    
Create a BYE request and check whether major fields are properly set
    public void testCreateRequest() {
        try {
          // JvB: First abort the INVITE, this should trigger a TransactionTerminatedEvent
          .terminate();
            Request bye = null;
            try {
                bye = .createRequest(.);
            } catch (SipException ex) {
                ex.printStackTrace();
                fail("A dialog failed to create a BYE request.");
            }
            //check method
            assertEquals(
                "Dialog.createRequest() returned a request with a bad method.",
                .,
                bye.getMethod());
            //check CSeq number
            assertEquals(
                "Dialog.createRequest() returned a request with a bad sequence number.",
                .getLocalSeqNumber() + 1,
                ((CSeqHeaderbye.getHeader(.))
                    .getSeqNumber());
            //Check From
            FromHeader byeFrom = (FromHeaderbye.getHeader(.);
            assertEquals(
                "Dialog.createRequest() returned a request with a bad From header.",
                .getLocalParty(),
                byeFrom.getAddress());
            //Check From tags
            assertEquals(
                "Dialog.createRequest() returned a request with a bad From tag.",
                .getLocalTag(),
                byeFrom.getTag());
            //Check To
            ToHeader byeTo = (ToHeaderbye.getHeader(.);
            assertEquals(
                "Dialog.createRequest() returned a request with a bad To header.",
                .getRemoteParty(),
                byeTo.getAddress());
            //Check To tags
            assertEquals(
                "Dialog.createRequest() returned a request with a bad To tag.",
                .getRemoteTag(),
                byeTo.getTag());
            ClientTransaction ct = super..getNewClientTransaction(bye);
      // JvB: set the SipListener before sending the BYE
            try {
            } catchTooManyListenersException ex) {
                throw new TckInternalError("failed to regiser a listener iwth the TI"ex);
            }
            .sendRequest(ct);
            assertEquals("Dialog mismatch "ct.getDialog(), );
            waitForMessage();
            waitForTimeout();
            // Should see a DTE here also for early Dialog
            assertNotNull("No DTE received for early Dialog terminated via BYE"dte);
        } catch (Throwable exc) {
            exc.printStackTrace();
            fail(exc.getClass().getName() + ": " + exc.getMessage());
        }
        assertTrue(new Exception().getStackTrace()[0].toString(), true);
    }

    
Steer the dialog to a CONFIRMED state and try sending an ack An invite has been sent by the TI in the setUp method so we take it from that point on.
    public void testSendAck() {
        this.doTestSendAck(false);
    }

    
Regression test for broken clients that send ACK to 2xx with same branch as INVITE public void testSendAckWithSameBranch() { this.doTestSendAck(true); }
    private void doTestSendAckboolean sameBranch ) {
    	
    	..println("doTestSendAck " + sameBranch);
        try {
           
            //We will now send an OK response
            //start listening for the response
            try {
                .collectResponseEvent();
            } catch (TooManyListenersException e) {
                throw new TckInternalError(
                    "Failed to register a SipListener with the RI.",
                    e);
            }
            Response ok = null;
            try {
                ok = .createResponse(.);
                ok.addHeader(
                    createRiInviteRequest(nullnullnull).getHeader(
                        .));
                ToHeader okToHeader = (ToHeader)ok.getHeader(.);
                okToHeader.setTag );// needs same tag as 180 ringing!
                // riSipProvider.sendResponse(ok);
                // Need to explicitly create the dialog on RI side
                ServerTransaction riST = .getNewServerTransaction );
                riST.getDialog();
                riST.sendResponseok );
            } catch (Exception e) {
                throw new TckInternalError(
                    "Failed to create and send an OK response",
                    e);
            }
            waitForMessage();
            ResponseEvent okRespEvt =
                .extractCollectedResponseEvent();
            if (okRespEvt == null || okRespEvt.getResponse() == null)
                throw new TiUnexpectedError("The TI did not dispatch an OK response.");
            
            String okBranch = 
                ((ViaHeaderokRespEvt.getResponse().getHeader(.)).getBranch();
            
            // After 2xx, dialog should be in CONFIRMED state. Needed to send ACK
            assertEquals..getState() );
            //Send the ack
            //Setup the ack listener
            try {
                .collectRequestEvent();
            } catch (TooManyListenersException ex) {
                throw new TckInternalError(
                    "Failed to register a SipListener with the RI",
                    ex);
            }
            Request ack = null;
            try {
                CSeqHeader cseq = (CSeqHeaderokRespEvt.getResponse().getHeader(.);
                ack = .createAck(cseq.getSeqNumber());
                //System.out.println( "Created ACK:" + ack );
                //System.out.println( "original INVITE:" + riInvite );
                // This is wrong according to RFC3261, but some clients do this...
                if (sameBranch) {
                    ViaHeader via = (ViaHeaderack.getHeader("Via");
                    via.setBranch( ((ViaHeader).getHeader("Via")).getBranch() );
                } 
            } catch (SipException ex) {
                throw new TiUnexpectedError(
                    "Failed to create an ACK request.",
                    ex);
            }
            try {
                .sendAck(ack);
            } catch (Throwable ex) {
                ex.printStackTrace();
                fail("SipException; Failed to send an ACK request using Dialog.sendAck()");
            }
            waitForMessage();
            
           
            // Did the RI get the ACK? If the dialog is not found, the ACK is filtered!
            RequestEvent ackEvt = .extractCollectedRequestEvent();
            assertNotNull(
                "No requestEvent sent by Dialog.sendAck() was received by the RI",
                ackEvt);
            assertNotNull(
                "The request sent by Dialog.sendAck() was not received by the RI",
                ackEvt.getRequest());
            if ( !sameBranch ) {
                String ackBranchId = ((ViaHeaderackEvt.getRequest().getHeader(.)).getBranch();
                super.assertNotSame("ACK branch ID must differ from INVITE OK branch ID"ackBranchId,
                        okBranch);
            }
        } catch (Throwable exc) {
            exc.printStackTrace();
            fail(exc.getClass().getName() + ": " + exc.getMessage());
        }
        assertTrue(new Exception().getStackTrace()[0].toString(), true);
    }
    public void testSendRequest() {
        try {
            Request reInvite = null;
            ClientTransaction reInviteTran = null;
            //Create
            try {
                reInvite = .createRequest(.);
                reInviteTran = .getNewClientTransaction(reInvite);
            } catch (Exception ex) {
                throw new TiUnexpectedError(
                    "Failed to create a CANCEL request with Dialog.createRequest()",
                    ex);
            }
            //Listen
            try {
                .collectRequestEvent();
            } catch (TooManyListenersException ex) {
                throw new TckInternalError(
                    "Failed to register a SipListener with the RI",
                    ex);
            }
            //Send
            try {
                .sendRequest(reInviteTran);
            } catch (SipException ex) {
                ex.printStackTrace();
                fail("Failed to send a cancel request using Dialog.sendRequest()");
            }
            waitForMessage();
            //Did they get it?
            RequestEvent cancelEvt =
                .extractCollectedRequestEvent();
            assertNotNull("The RI did not receive the sent request"cancelEvt);
            assertNotNull(
                "The RI did not receive the sent request",
                cancelEvt.getRequest());
        } catch (Throwable exc) {
            exc.printStackTrace();
            fail(exc.getClass().getName() + ": " + exc.getMessage());
        }
        assertTrue(new Exception().getStackTrace()[0].toString(), true);
    }
    //==================== end of tests
    //====== STATIC JUNIT ==========
    public static Test suite() {
        return new TestSuite(DialogTest.class);
    }
New to GrepCode? Check out our FAQ X