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. 178
036: *
037: * The To header field specifies the logical recipient of the request.
038: *
039: * The optional "display-name" is meant to be rendered by a human-user
040: * interface. The "tag" parameter serves as a general mechanism for
041: * dialog identification.
042: *
043: * See Section 19.3 for details of the "tag" parameter.
044: *
045: * Comparison of To header fields for equality is identical to
046: * comparison of From header fields. See Section 20.10 for the rules
047: * for parsing a display name, URI and URI parameters, and header field
048: * parameters.
049: *
050: * The compact form of the To header field is t.
051: *
052: * The following are examples of valid To header fields:
053: *
054: * To: The Operator <sip:operator@cs.columbia.edu>;tag=287447
055: * t: sip:+12125551212@server.phone2net.com
056: *
057: */
058:
059: public class TestToHeader extends SipHeaderBaseTest {
060:
061: /** A name of the header that will be tested */
062: private final String headerName = "To";
063:
064: /**
065: * Body of the test 1.
066: *
067: * Test for To header field: setName()/getName().
068: */
069: void Test1() {
070: // DEBUG: System.out.println("");
071: // DEBUG: System.out.println("*** Test1 started ***");
072:
073: // Testing the constructor...
074: // testConstructorNegative(headerName, "Invalid Value");
075:
076: SipHeader sh = createSipHeader(headerName, "The Operator"
077: + " <sip:operator@cs.columbia.edu>;tag=287447");
078:
079: if (sh == null) {
080: return;
081: }
082:
083: // Testing getName()...
084: String ret_name = sh.getName();
085: assertTrue("Invalid header value: " + ret_name, ret_name
086: .equals(headerName));
087:
088: // Testing setName()...
089: try {
090: sh.setName(headerName);
091: } catch (java.lang.IllegalArgumentException e) {
092: fail("setName(" + headerName + ") failed (IAE): " + e);
093: } catch (Throwable e) {
094: fail("setName(" + headerName + ") failed: " + e);
095: }
096: }
097:
098: /**
099: * Body of the test 2.
100: *
101: * Test for To header field: getValue()/getHeaderValue().
102: */
103: void Test2() {
104: SipHeader sh;
105: String val;
106: String headerValue1 = "The Operator <sip:operator@cs.columbia.edu>";
107: String headerValue2 = "<sip:+12125551212@server.phone2net.com>";
108: String headerParam1 = headerValue1;
109: String headerParam2 = headerValue2;
110: String[] paramList = { "tag=287447", "generic=sample_value" };
111:
112: // DEBUG: System.out.println("");
113: // DEBUG: System.out.println("*** Test2 started ***");
114:
115: for (int i = 0; i < paramList.length + 1; i++) {
116: sh = createSipHeader(headerName, headerParam1);
117:
118: if (sh != null) {
119: val = sh.getValue();
120: assertTrue(
121: "getValue() returned invalid parameter value: '"
122: + val + "'", val.equals(headerValue1));
123:
124: val = sh.getHeaderValue();
125: assertTrue("(1) getHeaderValue() returned invalid "
126: + "parameter value: '" + val + "'", val
127: .equals(headerParam1));
128:
129: // Test if the value can be changed.
130: sh.setValue(headerValue2);
131:
132: val = sh.getHeaderValue();
133: assertTrue("(2) getHeaderValue() returned invalid "
134: + "parameter value: '" + val + "'", val
135: .equals(headerParam2));
136: }
137:
138: headerParam1 += ";";
139: headerParam2 += ";";
140:
141: if (i < paramList.length) {
142: headerParam1 += paramList[i];
143: headerParam2 += paramList[i];
144: }
145: } // end for
146: }
147:
148: /**
149: * Body of the test 4.
150: *
151: * Test for To header field: getParameterNames()/getParameter().
152: */
153: void Test4() {
154: // DEBUG: System.out.println("");
155: // DEBUG: System.out.println("*** Test4 started ***");
156:
157: SipHeader sh = createSipHeader(headerName, "The Operator"
158: + " <sip:operator@cs.columbia.edu>;tag=287447");
159:
160: if (sh == null) {
161: return;
162: }
163:
164: // Testing getParameterNames()...
165: String[] paramList = sh.getParameterNames();
166: if (paramList == null) {
167: fail("getParameterNames() returned null!");
168: } else {
169: assertTrue("getParameterNames() returned "
170: + paramList.length + " parameters instead of 1.",
171: paramList.length == 1);
172:
173: assertTrue("Invalid parameter name: " + paramList[0],
174: paramList[0].equals("tag"));
175: }
176:
177: // Testing getParameter()...
178: String paramVal = sh.getParameter("q");
179: assertTrue("getParameter() returned '" + paramVal
180: + "' for the parameter 'q' that doesn't exist.",
181: paramVal == null);
182:
183: paramVal = sh.getParameter("tag");
184: assertTrue("getParameter() returned '" + paramVal
185: + "' for 'tag' instead of 287447.", paramVal
186: .equals("287447"));
187: }
188:
189: /**
190: * Body of the test 5.
191: *
192: * Test for To header field: setParameter()/removeParameter().
193: */
194: void Test5() {
195: // DEBUG: System.out.println("");
196: // DEBUG: System.out.println("*** Test5 started ***");
197:
198: SipHeader sh = createSipHeader(headerName, "The Operator"
199: + " <sip:operator@cs.columbia.edu>;tag=287447");
200:
201: if (sh == null) {
202: return;
203: }
204:
205: // Testing setParameter()...
206: sh.setParameter("tag", "12345");
207:
208: String paramVal = sh.getParameter("tag");
209: assertTrue("getParameter() returned '" + paramVal
210: + "' instead of 12345.", paramVal.equals("12345"));
211:
212: sh.setParameter("q", "10"); // parameter doesn't exist
213:
214: paramVal = sh.getParameter("q");
215: assertTrue("getParameter() returned '" + paramVal
216: + "' instead of 10.", paramVal.equals("10"));
217: }
218:
219: /**
220: * Run the tests
221: */
222: public void runTests() {
223: String headerParam = "The Operator "
224: + "<sip:operator@cs.columbia.edu>;tag=287447";
225:
226: declare("setName()/getName()");
227: Test1();
228:
229: declare("getValue()/getHeaderValue()");
230: Test2();
231:
232: declare("setValue()");
233: testSetValue(headerName, headerParam);
234:
235: declare("getParameterNames()/getParameter()");
236: Test4();
237:
238: declare("setParameter()/removeParameter()");
239: Test5();
240:
241: declare("toString()");
242: testToString(headerName, headerParam);
243: }
244: }
|