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 javax.microedition.sip;
028:
029: import javax.microedition.io.Connector; //import javax.microedition.sip.SipConnectionNotifier;
030: import javax.microedition.sip.SipServerConnection;
031: import javax.microedition.sip.SipClientConnection;
032:
033: //import gov.nist.siplite.message.*;
034: import com.sun.midp.i3test.TestCase;
035: import gov.nist.siplite.header.*;
036:
037: /**
038: * Test for support of short forms of headers names.
039: */
040: public class TestShortHeaderNames extends SipHeaderBaseTest {
041:
042: /** Compact forms of headers names */
043: private static String[] shortNames = { "i", "m", "e", "l", "c",
044: "f", "s", "k", "t", "v", "o", "u", "a" };
045:
046: /** Full headers names */
047: private static String[] fullNames = { Header.CALL_ID,
048: Header.CONTACT, Header.CONTENT_ENCODING,
049: Header.CONTENT_LENGTH, Header.CONTENT_TYPE, Header.FROM,
050: Header.SUBJECT, Header.SUPPORTED, Header.TO, Header.VIA,
051: Header.EVENT, Header.ALLOW_EVENTS, Header.ACCEPT_CONTACT };
052:
053: /** Headers values to set */
054: private static String[] headerValues = {
055: "f81d4fae-7dec-11d0-a765-00a0c91e6bf6@biloxi.com",
056: "\"Mr. Watson\" <sip:watson@worcester.bell-telephone.com>",
057: "gzip", "12345", "application/sdp",
058: "\"A. G. Bell\" <sip:agb@bell-telephone.com>",
059: "Test of short header's name", "100rel",
060: "<sip:+12125551212@server.phone2net.com>",
061: "SIP/2.0/UDP erlang.bell-telephone.com:5060",
062: "presence.test", "presence.test", "*" };
063:
064: /**
065: * Opens a new SIP client connection and initializes Init request.
066: * @return newly opened SipClientConnection
067: */
068: private SipClientConnection openClientConnection() {
069: // Open SipClientConnection.
070: SipClientConnection scc = null;
071:
072: try {
073: scc = (SipClientConnection) Connector
074: .open("sip:sippy.tester@localhost:5090");
075: } catch (Exception ex) {
076: fail("Exception during Connector.open(): " + ex);
077: }
078:
079: assertNotNull("scc is null", scc);
080:
081: try {
082: // Put the connection into the appropriate state
083: // to call setHeader().
084: scc.initRequest("INVITE", null);
085: } catch (Exception ex) {
086: fail("Exception during scc.initRequest(): " + ex);
087: }
088:
089: return scc;
090: }
091:
092: /**
093: * Tests get/set/removeHeader() methods of SipConnection.
094: * @param sc SIP commection to test
095: */
096: private void testGetSetHeader(SipConnection sc) {
097: assertNotNull("sc is null", sc);
098:
099: try {
100: String[] headerNamesToAdd, headerNamesToRemove;
101:
102: for (int j = 0; j < 2; j++) {
103: if (j == 0) {
104: // Trying to add a header using its short name
105: // and then trying to remove it using its full name.
106: headerNamesToAdd = shortNames;
107: headerNamesToRemove = fullNames;
108: } else {
109: // Use full header's name to add the header,
110: // then use its short name to remove the header.
111: headerNamesToAdd = fullNames;
112: headerNamesToRemove = shortNames;
113: }
114:
115: // Test set/getHeader().
116: for (int i = 0; i < headerNamesToAdd.length; i++) {
117: sc.setHeader(headerNamesToAdd[i], headerValues[i]);
118:
119: // Check that getHeader() returns correct values
120: // both for short and long form of header's name.
121: String val = sc.getHeader(headerNamesToAdd[i]);
122: assertTrue("Invalid '" + headerNamesToAdd[i]
123: + "' header's value: '" + val + "'",
124: headerValues[i].equalsIgnoreCase(val));
125:
126: String name = sc.getHeader(headerNamesToRemove[i]);
127: assertTrue("Invalid '" + headerNamesToRemove[i]
128: + "' header's value: '" + val + "'",
129: headerValues[i].equalsIgnoreCase(val));
130:
131: // Remove the header and check that getHeader()
132: // will return null.
133: sc.removeHeader(headerNamesToRemove[i]);
134:
135: val = sc.getHeader(headerNamesToAdd[i]);
136: assertNull("getHeader(\"" + headerNamesToAdd[i]
137: + "\") returned non-null value!", val);
138:
139: val = sc.getHeader(headerNamesToRemove[i]);
140: assertNull("getHeader(\"" + headerNamesToRemove[i]
141: + "\") returned non-null value!", val);
142: } // end for i
143: } // end for j
144: } catch (Exception ex) {
145: fail("Exception during set/getHeader(): " + ex);
146: }
147: }
148:
149: /**
150: * Body of the test 1: creating SipHeader with short header's name test.
151: */
152: void Test1() {
153: SipHeader sh;
154:
155: for (int i = 0; i < shortNames.length; i++) {
156: sh = createSipHeader(shortNames[i], headerValues[i]);
157:
158: if (sh == null) {
159: return;
160: }
161:
162: assertEquals("Invalid header name: " + sh.getName(),
163: shortNames[i], sh.getName());
164:
165: assertEquals("Invalid header value!", headerValues[i], sh
166: .getValue());
167: } // end for
168: }
169:
170: /**
171: * Body of the test 2: SipClientConnection methods test.
172: */
173: void Test2() {
174: SipClientConnection scc = openClientConnection();
175: testGetSetHeader(scc);
176:
177: try {
178: if (scc != null) {
179: scc.close();
180: }
181: } catch (Exception ex) {
182: }
183: }
184:
185: /**
186: * Body of the test 3: SipServerConnection methods test.
187: */
188: void Test3() {
189: SipServerConnection ssc = null;
190: SipClientConnection scc = openClientConnection();
191:
192: // Open SipServerConnection.
193: try {
194: SipConnectionNotifier scn = (SipConnectionNotifier) Connector
195: .open("sip:5090");
196: scc.send();
197:
198: ssc = scn.acceptAndOpen();
199: ssc.initResponse(200);
200: } catch (Exception ex) {
201: fail("Exception during ssc open: " + ex);
202: }
203:
204: testGetSetHeader(ssc);
205: }
206:
207: /**
208: * Run the tests.
209: */
210: public void runTests() {
211: declare("Creating SipHeader with short header's name test");
212: Test1();
213:
214: declare("SipClientConnection methods test");
215: Test2();
216:
217: // Disabled until SipServerConnection.get/setHeader() will be modified.
218:
219: // declare("SipServerConnection methods test");
220: // Test3();
221: }
222: }
|