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. 38; BNF: p. 229
036: *
037: * CSeq = "CSeq" HCOLON 1*DIGIT LWS Method
038: *
039: * The CSeq header field serves as a way to identify and order
040: * transactions. It consists of a sequence number and a method. The
041: * method MUST match that of the request. For non-REGISTER requests
042: * outside of a dialog, the sequence number value is arbitrary. The
043: * sequence number value MUST be expressible as a 32-bit unsigned
044: * integer and MUST be less than 2**31. As long as it follows the above
045: * guidelines, a client may use any mechanism it would like to select
046: * CSeq header field values.
047: *
048: * Section 12.2.1.1 discusses construction of the CSeq for requests
049: * within a dialog.
050: *
051: * Example:
052: *
053: * CSeq: 4711 INVITE
054: *
055: */
056:
057: public class TestCSeqHeader extends SipHeaderBaseTest {
058:
059: /** A name of the header that will be tested */
060: private final String headerName = "CSeq";
061:
062: /**
063: * Body of the test 1.
064: *
065: * Test for CSeq header field: setName()/getName().
066: */
067: void Test1() {
068: // DEBUG: System.out.println("");
069: // DEBUG: System.out.println("*** Test1 started ***");
070:
071: SipHeader sh;
072:
073: // Test the constructor.
074: /*
075: try {
076: sh = new SipHeader(headerName, "Invalid INVITE");
077: fail("Constructor: IAE was not thrown!");
078: } catch (IllegalArgumentException iae) {
079: }
080: */
081:
082: sh = createSipHeader(headerName, "4711 INVITE");
083:
084: if (sh == null) {
085: return;
086: }
087:
088: // Testing getName()...
089: String ret_name = sh.getName();
090: assertTrue("Invalid header value: " + ret_name, ret_name
091: .equals(headerName));
092:
093: // Testing setName()...
094: try {
095: sh.setName(headerName);
096: } catch (java.lang.IllegalArgumentException e) {
097: fail("setName(" + headerName + ") failed (IAE): " + e);
098: } catch (Throwable e) {
099: fail("setName(" + headerName + ") failed: " + e);
100: }
101: }
102:
103: /**
104: * Body of the test 2.
105: *
106: * Test for CSeq header field: getValue()/getHeaderValue().
107: */
108: void Test2() {
109: String val;
110: String headerValue = "4711 INVITE";
111:
112: // DEBUG: System.out.println("");
113: // DEBUG: System.out.println("*** Test2 started ***");
114:
115: SipHeader sh = createSipHeader(headerName, headerValue);
116:
117: if (sh != null) {
118: val = sh.getValue();
119: assertTrue("getValue() returned invalid value: '" + val
120: + "'", val.equals(headerValue));
121:
122: val = sh.getHeaderValue();
123: assertTrue("(1) getHeaderValue() returned invalid "
124: + "value: '" + val + "'", val.equals(headerValue));
125:
126: // Test if the value can be changed.
127: headerValue = "12345 REGISTER";
128: sh.setValue(headerValue);
129:
130: val = sh.getHeaderValue();
131: assertTrue("(2) getHeaderValue() returned invalid "
132: + "value: '" + val + "'", val.equals(headerValue));
133: }
134: }
135:
136: /**
137: * Body of the test 4.
138: *
139: * Test for CSeq header field: getParameterNames()/getParameter().
140: */
141: void Test4() {
142: // DEBUG: System.out.println("");
143: // DEBUG: System.out.println("*** Test4 started ***");
144:
145: SipHeader sh = createSipHeader(headerName, "4711 INVITE");
146:
147: if (sh == null) {
148: return;
149: }
150:
151: // Testing getParameterNames()...
152: String[] paramList = sh.getParameterNames();
153:
154: if (paramList != null) {
155: fail("getParameterNames() should return null!");
156: }
157:
158: // Testing getParameter()...
159: String paramVal = sh.getParameter("ttl");
160: assertTrue("getParameter() returned '" + paramVal
161: + "' for the parameter 'ttl' that doesn't exist.",
162: paramVal == null);
163: }
164:
165: /**
166: * Body of the test 5.
167: *
168: * Test for CSeq header field: setParameter()/removeParameter().
169: */
170: void Test5() {
171: // DEBUG: System.out.println("");
172: // DEBUG: System.out.println("*** Test5 started ***");
173:
174: SipHeader sh = createSipHeader(headerName, "4711 INVITE");
175:
176: if (sh == null) {
177: return;
178: }
179:
180: // Testing setParameter()...
181: try {
182: sh.setParameter("test", "192.0.0.1");
183: } catch (Exception e) {
184: fail(e + " was thrown.");
185: }
186:
187: try {
188: sh.removeParameter("test");
189: } catch (Exception e) {
190: fail("removeParameter(): " + e + " was thrown!");
191: }
192:
193: assertTrue(true); // to avoid error message from the test framework
194: }
195:
196: /**
197: * Run the tests
198: */
199: public void runTests() {
200: declare("setName()/getName()");
201: Test1();
202:
203: declare("getValue()/getHeaderValue()");
204: Test2();
205:
206: declare("setValue()");
207: testSetValue(headerName, "4711 INVITE");
208:
209: declare("getParameterNames()/getParameter()");
210: Test4();
211:
212: declare("setParameter()/removeParameter()");
213: Test5();
214:
215: declare("toString()");
216: testToString(headerName, "4711 INVITE");
217: }
218: }
|