001: /*
002: * Conditions Of Use
003: *
004: * This software was developed by employees of the National Institute of
005: * Standards and Technology (NIST), and others.
006: * This software is has been contributed to the public domain.
007: * As a result, a formal license is not needed to use the software.
008: *
009: * This software is provided "AS IS."
010: * NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
011: * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
012: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
013: * AND DATA ACCURACY. NIST does not warrant or make any representations
014: * regarding the use of the software or the results thereof, including but
015: * not limited to the correctness, accuracy, reliability or usefulness of
016: * the software.
017: *
018: *
019: */
020: package test.tck.msgflow;
021:
022: import junit.framework.*;
023:
024: import javax.sip.*;
025: import javax.sip.message.*;
026: import javax.sip.header.*;
027: import java.util.*;
028: import java.text.*;
029: import test.tck.*;
030:
031: /**
032: * <p>Title: TCK</p>
033: * <p>Description: JAIN SIP 1.1 Technology Compatibility Kit</p>
034: * @author Emil Ivov
035: * Network Research Team, Louis Pasteur University, Strasbourg, France
036: * This code is in the public domain.
037: * @version 1.0
038: */
039:
040: public class ClientTransactionTest extends MessageFlowHarness {
041:
042: public ClientTransactionTest(String name) {
043: super (name);
044: }
045:
046: //==================== tests ==============================
047:
048: /**
049: * Creates an invite request using the Tested Implementation and then
050: * tests creating a cancel for the same invite request.
051: */
052: public void testCreateCancel() {
053: try {
054: Request invite = createTiInviteRequest(null, null, null);
055: ClientTransaction tran = null;
056: try {
057: tran = tiSipProvider.getNewClientTransaction(invite);
058: } catch (TransactionUnavailableException exc) {
059: throw new TiUnexpectedError(
060: "A TransactionUnavailableException was thrown while trying to "
061: + "create a new client transaction",
062: exc);
063: }
064:
065: // see if creating a dialog matters
066: tran.getDialog();
067:
068: Request cancel = null;
069: try {
070: cancel = tran.createCancel();
071: } catch (SipException ex) {
072: ex.printStackTrace();
073: fail("Failed to create cancel request!");
074: }
075: assertEquals(
076: "The created request did not have a CANCEL method.",
077: cancel.getMethod(), Request.CANCEL);
078: assertEquals(
079: "Request-URIs of the original and the cancel request do not match",
080: cancel.getRequestURI(), invite.getRequestURI());
081: assertEquals(
082: "Call-IDs of the original and the cancel request do not match",
083: cancel.getHeader(CallIdHeader.NAME), invite
084: .getHeader(CallIdHeader.NAME));
085: assertEquals(
086: "ToHeaders of the original and the cancel request do not match",
087: cancel.getHeader(ToHeader.NAME), invite
088: .getHeader(ToHeader.NAME));
089: assertTrue(
090: "The CSeqHeader's sequence number of the original and "
091: + "the cancel request do not match",
092: ((CSeqHeader) cancel.getHeader(CSeqHeader.NAME))
093: .getSeqNumber() == ((CSeqHeader) invite
094: .getHeader(CSeqHeader.NAME)).getSeqNumber());
095: assertEquals(
096: "The CSeqHeader's method of the cancel request was not CANCEL",
097: ((CSeqHeader) cancel.getHeader(CSeqHeader.NAME))
098: .getMethod(), Request.CANCEL);
099: assertTrue("There was no ViaHeader in the cancel request",
100: cancel.getHeaders(ViaHeader.NAME).hasNext());
101: Iterator cancelVias = cancel.getHeaders(ViaHeader.NAME);
102: ViaHeader cancelVia = ((ViaHeader) cancelVias.next());
103: ViaHeader inviteVia = ((ViaHeader) invite.getHeaders(
104: ViaHeader.NAME).next());
105: assertEquals(
106: "ViaHeaders of the original and the cancel request do not match!",
107: cancelVia, inviteVia);
108: assertFalse("Cancel request had more than one ViaHeader.",
109: cancelVias.hasNext());
110:
111: assertEquals("To tags must match", ((ToHeader) invite
112: .getHeader("to")).getTag(), ((ToHeader) cancel
113: .getHeader("to")).getTag());
114:
115: assertEquals("From tags must match", ((FromHeader) invite
116: .getHeader("from")).getTag(), ((FromHeader) cancel
117: .getHeader("from")).getTag());
118:
119: assertEquals("Max-Forwards must match", invite
120: .getHeader(MaxForwardsHeader.NAME), cancel
121: .getHeader(MaxForwardsHeader.NAME));
122:
123: } catch (Throwable exc) {
124: exc.printStackTrace();
125: fail(exc.getClass().getName() + ": " + exc.getMessage());
126: }
127:
128: assertTrue(new Exception().getStackTrace()[0].toString(), true);
129:
130: }
131:
132: /**
133: * Tests sending a request from a ClientTransaction.
134: */
135: public void testSendRequest() {
136: try {
137: Request invite = createTiInviteRequest(null, null, null);
138: RequestEvent receivedRequestEvent = null;
139: ClientTransaction tran = null;
140: try {
141: tran = tiSipProvider.getNewClientTransaction(invite);
142: eventCollector.collectRequestEvent(riSipProvider);
143: tran.sendRequest();
144: waitForMessage();
145: receivedRequestEvent = eventCollector
146: .extractCollectedRequestEvent();
147: assertNotNull(
148: "The sent request was not received by the RI!",
149: receivedRequestEvent);
150: assertNotNull(
151: "The sent request was not received by the RI!",
152: receivedRequestEvent.getRequest());
153: } catch (TransactionUnavailableException exc) {
154: throw new TiUnexpectedError(
155: "A TransactionUnavailableException was thrown while trying to "
156: + "create a new client transaction",
157: exc);
158: } catch (SipException exc) {
159: exc.printStackTrace();
160: fail("The SipException was thrown while trying to send the request.");
161: } catch (TooManyListenersException exc) {
162: throw new TckInternalError(
163: "A TooManyListenersException was thrown while trying "
164: + "to add a SipListener to an RI SipProvider",
165: exc);
166: }
167: } catch (Throwable exc) {
168: exc.printStackTrace();
169: fail(exc.getClass().getName() + ": " + exc.getMessage());
170: }
171:
172: assertTrue(new Exception().getStackTrace()[0].toString(), true);
173:
174: }
175:
176: //mranga: removed testCreateAck -- should not test for deprecated functionality
177: // in the TCK.
178:
179: //==================== end of tests
180:
181: //====== STATIC JUNIT ==========
182: public static Test suite() {
183: return new TestSuite(ClientTransactionTest.class);
184: }
185:
186: }
|