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 com.sun.midp.i3test.TestCase;
030:
031: import javax.microedition.io.Connector;
032: import java.io.IOException;
033:
034: /**
035: * RFC3261, p. 167
036: * A Contact header field value provides a URI whose meaning depends on
037: * the type of request or response it is in.
038: *
039: * A Contact header field value can contain a display name, a URI with
040: * URI parameters, and header parameters.
041: *
042: * This document defines the Contact parameters "q" and "expires".
043: * These parameters are only used when the Contact is present in a
044: * REGISTER request or response, or in a 3xx response. Additional
045: * parameters may be defined in other specifications.
046: *
047: * When the header field value contains a display name, the URI
048: * including all URI parameters is enclosed in "<" and ">". If no "<"
049: * and ">" are present, all parameters after the URI are header
050: * parameters, not URI parameters. The display name can be tokens, or a
051: * quoted string, if a larger character set is desired.
052: *
053: * Even if the "display-name" is empty, the "name-addr" form MUST be
054: * used if the "addr-spec" contains a comma, semicolon, or question
055: * mark. There may or may not be LWS between the display-name and the
056: * "<".
057: *
058: * These rules for parsing a display name, URI and URI parameters, and
059: * header parameters also apply for the header fields To and From.
060: *
061: * The Contact header field has a role similar to the Location header
062: * field in HTTP. However, the HTTP header field only allows one
063: * address, unquoted. Since URIs can contain commas and semicolons
064: * as reserved characters, they can be mistaken for header or
065: * parameter delimiters, respectively.
066: *
067: * The compact form of the Contact header field is m (for "moved").
068: *
069: * Examples:
070: *
071: * Contact: "Mr. Watson" <sip:watson@worcester.bell-telephone.com>
072: * ;q=0.7; expires=3600,
073: * "Mr. Watson" <mailto:watson@bell-telephone.com> ;q=0.1
074: * m: <sips:bob@192.0.2.4>;expires=60
075: */
076:
077: public class TestContactHeader extends SipHeaderBaseTest {
078:
079: /** A name of the header that will be tested */
080: private final String headerName = "Contact";
081:
082: /**
083: * Body of the test 1.
084: *
085: * Test for Contact header field: setName()/getName().
086: */
087: void Test1() {
088: // DEBUG: System.out.println("");
089: // DEBUG: System.out.println("*** Test1 started ***");
090:
091: SipHeader sh = createSipHeader(headerName, "\"Mr. Watson\" "
092: + "<sip:watson@worcester.bell-telephone.com>");
093:
094: if (sh == null) {
095: return;
096: }
097:
098: // Testing getName()...
099: String ret_name = sh.getName();
100: assertTrue("Invalid header value: " + ret_name, ret_name
101: .equals(headerName));
102:
103: // Testing setName()...
104: try {
105: sh.setName(headerName);
106: } catch (java.lang.IllegalArgumentException e) {
107: fail("setName(" + headerName + ") failed (IAE): " + e);
108: } catch (Throwable e) {
109: fail("setName(" + headerName + ") failed: " + e);
110: }
111: }
112:
113: /**
114: * Body of the test 2.
115: *
116: * Test for Contact header field: getValue()/getHeaderValue().
117: */
118: void Test2() {
119: SipHeader sh;
120: String val;
121: String headerValue = "\"Mr. Watson\" "
122: + "<sip:watson@worcester.bell-telephone.com>";
123: String headerParam = headerValue;
124: String[] paramList = { "expires=3600", "q=0.7" };
125:
126: // DEBUG: System.out.println("");
127: // DEBUG: System.out.println("*** Test2 started ***");
128:
129: for (int i = 0; i < paramList.length + 1; i++) {
130: sh = createSipHeader(headerName, headerParam);
131:
132: if (sh != null) {
133: val = sh.getValue();
134: assertTrue(
135: "getValue() returned invalid parameter value: '"
136: + val + "'", val.equals(headerValue));
137:
138: val = sh.getHeaderValue();
139: assertTrue("getHeaderValue() returned invalid "
140: + "parameter value: '" + val + "'", val
141: .equals(headerParam));
142: }
143:
144: headerParam += ";";
145:
146: if (i < paramList.length) {
147: headerParam += paramList[i];
148: }
149: } // end for
150: }
151:
152: /**
153: * Body of the test 3.
154: *
155: * Test for Contact header field: setValue().
156: */
157: void Test3() {
158: // DEBUG: System.out.println("");
159: // DEBUG: System.out.println("*** Test3 started ***");
160:
161: SipHeader sh = createSipHeader(headerName, "\"Mr. Watson\" "
162: + "<sip:watson@worcester.bell-telephone.com>");
163:
164: if (sh == null) {
165: return;
166: }
167:
168: // Testing setValue()...
169: String headerValue = "<sips:bob@192.0.2.4>;expires=60";
170:
171: try {
172: // try to set a value with a parameter
173: sh.setValue(headerValue);
174: fail("IllegalArgumentException was not thrown!");
175: } catch (java.lang.IllegalArgumentException e) {
176: } catch (Throwable e) {
177: fail("" + e + " was caught");
178: }
179:
180: // try to set a value without parameters
181: headerValue = "<sips:bob@192.0.2.4>";
182: try {
183: sh.setValue(headerValue);
184: } catch (Throwable e) {
185: fail("" + e + " was caught");
186: }
187:
188: String realValue = sh.getValue();
189: assertTrue("setValue() set '" + realValue + "' instead of '"
190: + headerValue + "'.", realValue.equals(headerValue));
191:
192: // TODO: investigate this case!
193: /*
194: headerValue = "INVALID VALUE";
195: try {
196: sh.setValue(headerValue);
197: fail("IllegalArgumentException was not caught!");
198: } catch (java.lang.IllegalArgumentException e) {
199: } catch (Throwable e) {
200: fail("" + e + " was caught");
201: }
202: */
203: }
204:
205: /**
206: * Body of the test 4.
207: *
208: * Test for Contact header field: getParameterNames()/getParameter().
209: */
210: void Test4() {
211: // DEBUG: System.out.println("");
212: // DEBUG: System.out.println("*** Test4 started ***");
213:
214: SipHeader sh = createSipHeader(headerName,
215: "<sips:bob@192.0.2.4>;expires=60");
216:
217: if (sh == null) {
218: return;
219: }
220:
221: // Testing getParameterNames()...
222: String[] paramList = sh.getParameterNames();
223: if (paramList == null) {
224: fail("getParameterNames() returned null!");
225: } else {
226: assertTrue("getParameterNames() returned "
227: + paramList.length + " parameters instead of 1.",
228: paramList.length == 1);
229:
230: assertTrue("Invalid parameter name: " + paramList[0],
231: paramList[0].equals("expires"));
232: }
233:
234: // Testing getParameter()...
235: String paramVal = sh.getParameter("q");
236: assertTrue("getParameter() returned '" + paramVal
237: + "' for the parameter 'q' that doesn't exist.",
238: paramVal == null);
239:
240: paramVal = sh.getParameter("expires");
241: assertTrue("getParameter() returned '" + paramVal
242: + "' for 'expires' instead of 60.", paramVal
243: .equals("60"));
244: }
245:
246: /**
247: * Body of the test 5.
248: *
249: * Test for Contact header field: setParameter()/removeParameter().
250: */
251: void Test5() {
252: // DEBUG: System.out.println("");
253: // DEBUG: System.out.println("*** Test5 started ***");
254:
255: SipHeader sh = createSipHeader(headerName,
256: "<sips:bob@192.0.2.4>;expires=60");
257:
258: if (sh == null) {
259: return;
260: }
261:
262: // Testing setParameter()...
263: sh.setParameter("expires", "3600");
264:
265: String paramVal = sh.getParameter("expires");
266: assertTrue("getParameter() returned '" + paramVal
267: + "' instead of 3600.", paramVal.equals("3600"));
268:
269: sh.setParameter("q", "10"); // parameter doesn't exist
270:
271: paramVal = sh.getParameter("q");
272: assertTrue("getParameter() returned '" + paramVal
273: + "' instead of 10.", paramVal.equals("10"));
274: }
275:
276: /**
277: * Body of the test 6.
278: *
279: * Test for Contact header field: toString().
280: */
281: void Test6() {
282: // DEBUG: System.out.println("");
283: // DEBUG: System.out.println("*** Test6 started ***");
284:
285: String headerParam = "<sips:bob@192.0.2.4>;expires=60";
286: SipHeader sh = createSipHeader(headerName, headerParam);
287:
288: if (sh == null) {
289: return;
290: }
291:
292: assertTrue("Invalid string representation: '" + sh.toString()
293: + "'", sh.toString().equals(
294: headerName + ": " + headerParam));
295: }
296:
297: /**
298: * Run the tests
299: */
300: public void runTests() {
301: declare("setName()/getName()");
302: Test1();
303:
304: declare("getValue()/getHeaderValue()");
305: Test2();
306:
307: // declare("setValue()");
308: // Test3();
309:
310: declare("getParameterNames()/getParameter()");
311: Test4();
312:
313: declare("setParameter()/removeParameter()");
314: Test5();
315:
316: // declare("toString()");
317: // Test6();
318: }
319:
320: }
|