001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package gov.nist.microedition.sip;
028:
029: import javax.microedition.sip.*;
030: import javax.microedition.io.Connector;
031: import gov.nist.microedition.sip.SipConnectionNotifierImpl;
032: import gov.nist.microedition.sip.SipClientConnectionImpl;
033: import gov.nist.siplite.message.Request;
034: import gov.nist.siplite.header.*;
035: import gov.nist.siplite.address.*;
036: import gov.nist.core.*;
037: import com.sun.midp.i3test.TestCase;
038:
039: /**
040: * Tests for SetHeader method of SipClientConnection class.
041: *
042: */
043: public class TestSipClientSetHeader extends TestCase {
044:
045: /**
046: * Body of the test 1.
047: * Checks that on "send" method the "tag" parameter is added
048: * to "From" header and the "contact" header contains the
049: * correct port value.
050: *
051: * @param method string representation of the requests method
052: */
053: void Test1(String method) {
054: /** Client connection. */
055: SipClientConnection sc = null;
056: SipConnectionNotifier scn = null;
057: try {
058: // Open SIP client connection to the local SIP server
059: sc = (SipClientConnection) Connector
060: .open("sip:sippy.tester@localhost:5060");
061: } catch (Exception ex) {
062: assertNull("Exception during sc open", sc);
063: ex.printStackTrace();
064: }
065: assertNotNull("sc is null", sc);
066:
067: // State of client connection must be "Created"
068: assertEquals("State is not CREATED",
069: ((SipClientConnectionImpl) sc).getState(),
070: SipClientConnectionImpl.CREATED);
071:
072: // Move to INITIALIZED state
073: try {
074: sc.initRequest(method, null);
075: sc.setHeader("Content-Type", "text/plain");
076: } catch (Throwable e) {
077: fail("" + e + " was caused");
078: }
079:
080: assertEquals("State is not INITIALIZED",
081: ((SipClientConnectionImpl) sc).getState(),
082: SipClientConnectionImpl.INITIALIZED);
083:
084: try {
085: scn = (SipConnectionNotifier) Connector.open("sip:5090");
086: sc.setHeader("From", "sip:sippy.tester@localhost"); // no tag
087: sc.setHeader("Subject", "Hello...");
088: sc.setHeader("Contact", "sip:user@" + scn.getLocalAddress()
089: + ":" + scn.getLocalPort());
090: sc.send();
091: } catch (Throwable e) {
092: fail("" + e + " was caused");
093: }
094:
095: Request request = ((SipClientConnectionImpl) sc).getRequest();
096:
097: try {
098: // On "send" method "tag" parameter must be added
099: FromHeader fromHeader = request.getFromHeader();
100: assertNotNull("No \"From\" header in request", fromHeader);
101: assertNotNull("No \"Tag\" parameter in \"From\" header",
102: fromHeader.getTag());
103: // "Contact" header must be correct
104: ContactList contacts = request.getContactHeaders();
105: assertEquals("Wrong number of contacts", contacts.size(), 1);
106: // port number must be 5090
107: ContactHeader contact = (ContactHeader) contacts
108: .elementAt(0);
109: assertEquals("Wrong port number", contact.getHostPort()
110: .getPort(), 5090);
111: } catch (Throwable e) {
112: fail("" + e + " was caused");
113: }
114:
115: // close connection
116: try {
117: sc.close();
118: scn.close();
119: } catch (Throwable e) {
120: fail("" + e + " was caused");
121: }
122: }
123:
124: /**
125: * Body of the test 2.
126: *
127: * Check the transforming TEL URI to SIP URI.
128: * @param method string representation of the requests method
129: */
130: void Test2(String method) {
131: /** Client connection. */
132: SipClientConnection sc = null;
133: try {
134: // Open SIP client connection to the local SIP server
135: sc = (SipClientConnection) Connector
136: .open("sip:sippy.tester@localhost:5060");
137: } catch (Exception ex) {
138: assertNull("Exception during sc open", sc);
139: ex.printStackTrace();
140: }
141: assertNotNull("sc is null", sc);
142:
143: // State of client connection must be "Created"
144: assertEquals("State is not CREATED",
145: ((SipClientConnectionImpl) sc).getState(),
146: SipClientConnectionImpl.CREATED);
147:
148: // Move to INITIALIZED state
149: try {
150: sc.initRequest(method, null);
151: } catch (Throwable e) {
152: fail("" + e + " was caused");
153: }
154:
155: assertEquals("State is not INITIALIZED",
156: ((SipClientConnectionImpl) sc).getState(),
157: SipClientConnectionImpl.INITIALIZED);
158:
159: // Set header with the "tel" scheme
160: try {
161: sc.setHeader("From", "tel:+1234567;postd=9876;isub=3746");
162: assertTrue("OK", true);
163: } catch (Throwable e) {
164: fail("" + e + " was caused");
165: e.printStackTrace();
166: }
167:
168: Request request = ((SipClientConnectionImpl) sc).getRequest();
169:
170: try {
171: // On "send" method "tag" parameter must be added
172: FromHeader fromHeader = request.getFromHeader();
173: assertNotNull("No \"From\" header in request", fromHeader);
174: Address telAddress = fromHeader.getAddress();
175: TelURL telURL = (TelURL) (telAddress.getURI());
176: assertTrue("Wrong scheme", telURL.getScheme().equals("tel"));
177: assertTrue("Wrong isub", telURL.getIsdnSubAddress().equals(
178: "3746"));
179:
180: } catch (Throwable e) {
181: fail("" + e + " was caused");
182: e.printStackTrace();
183: }
184:
185: // Set header with the "tel" scheme with correct isdn-subaddress
186: // RFC 2806, 2.2
187: // isdn-subaddress = ";isub=" 1*phonedigit
188: // phonedigit = DIGIT / visual-separator
189: // visual-separator = "-" / "." / "(" / ")"
190: try {
191: sc.setHeader("From", "tel:+1234567;isub=01234567890-.()");
192: assertTrue("OK", true);
193: } catch (Throwable e) {
194: fail("" + e + " was caused");
195: e.printStackTrace();
196: }
197: // Set header with the "tel" scheme with wrong isdn-subaddress
198: try {
199: sc.setHeader("From", "tel:+1234567;isub=3A46");
200: System.err.println("Test: 1");
201: fail("IllegalArgumentException weren't be caused");
202: } catch (IllegalArgumentException e) {
203: assertTrue("OK", true);
204: } catch (Throwable e) {
205: fail("" + e + " was caused");
206: e.printStackTrace();
207: }
208:
209: // Set header with the "tel" scheme with correct post-dial
210: // RFC 2806, 2.2
211: // post-dial = ";postd=" 1*(phonedigit /
212: // dtmf-digit / pause-character)
213: // dtmf-digit = "*" / "#" / "A" / "B" / "C" / "D"
214: // pause-character = one-second-pause / wait-for-dial-tone
215: // one-second-pause = "p"
216: // wait-for-dial-tone = "w"
217: try {
218: sc.setHeader("From",
219: "tel:+1234567;postd=01234567890-.()pw*#ABCD");
220: assertTrue("OK", true);
221: } catch (Throwable e) {
222: fail("" + e + " was caused");
223: e.printStackTrace();
224: }
225:
226: // Set header with the "tel" scheme with wrong post-dial
227: try {
228: sc.setHeader("From", "tel:+1234567;postd=9e8w76;isub=3746");
229: System.err.println("Test: 2");
230: fail("IllegalArgumentException weren't be caused");
231: } catch (IllegalArgumentException e) {
232: assertTrue("OK", true);
233: } catch (Throwable e) {
234: fail("" + e + " was caused");
235: e.printStackTrace();
236: }
237:
238: // close connection
239: try {
240: sc.close();
241: } catch (Throwable e) {
242: fail("" + e + " was caused");
243: }
244:
245: }
246:
247: /**
248: * Tests execute
249: *
250: */
251: public void runTests() {
252: declare("Test1");
253: Test1("INVITE");
254: declare("Test2");
255: Test2("INVITE");
256: }
257:
258: }
|