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 gov.nist.core.Separators;
032: import javax.microedition.io.Connector;
033: import java.io.IOException;
034:
035: /**
036: * This is a base class for all Test<HeaderName>Header classes.
037: */
038: public abstract class SipHeaderBaseTest extends TestCase {
039:
040: /**
041: * Creates SipHeader with the specified name and parameter.
042: *
043: * @param headerName header's name.
044: * @param headerParam header's parameter.
045: * @return SipHeader instance if successfully created, null otherwise.
046: */
047: protected SipHeader createSipHeader(String headerName,
048: String headerParam) {
049: SipHeader sh;
050:
051: try {
052: sh = new SipHeader(headerName, headerParam);
053: } catch (java.lang.IllegalArgumentException e) {
054: sh = null;
055: fail("IllegalArgumentException: " + e);
056: } catch (Exception e) {
057: sh = null;
058: fail(e + " was caught");
059: }
060:
061: return sh;
062: }
063:
064: /**
065: * Checks if an exception is thrown when attempting to create
066: * a header with invalid value.
067: *
068: * @param headerName header's name.
069: * @param invalidParam value that is invalid for this type of header.
070: * @return true if the test was passed, false otherwise.
071: */
072: protected boolean testConstructorNegative(String headerName,
073: String invalidParam) {
074: SipHeader sh;
075: boolean result;
076:
077: try {
078: sh = new SipHeader(headerName, invalidParam);
079: result = false;
080: fail("Constructor: IAE was not thrown!");
081: } catch (java.lang.IllegalArgumentException e) {
082: result = true;
083: } catch (Exception e) {
084: result = false;
085: fail(e + " was thrown instead of IAE.");
086: }
087:
088: return result;
089: }
090:
091: /**
092: * Test for toString() method of Header's subclasses.
093: *
094: * @param headerName name of the header.
095: * @param headerParam value and parameters of the header.
096: */
097: protected void testToString(String headerName, String headerParam) {
098: SipHeader sh = createSipHeader(headerName, headerParam);
099:
100: if (sh == null) {
101: return;
102: }
103:
104: String strHeader = sh.toString();
105:
106: // Skip CRLF
107: strHeader = removeNewLine(strHeader);
108:
109: assertTrue(
110: "Invalid string representation: '" + strHeader + "'",
111: equalsIgnoreSpaces(strHeader, headerName + ": "
112: + headerParam));
113: }
114:
115: /**
116: * Test for setValue() method of Header's subclasses.
117: *
118: * @param headerName name of the header.
119: * @param headerParam value and parameters of the header.
120: */
121: protected void testSetValue(String headerName, String headerParam) {
122: SipHeader sh = createSipHeader(headerName, headerParam);
123:
124: if (sh == null) {
125: return;
126: }
127:
128: // Here the one common case is tested:
129: // if setValue() argument does include header's parameters,
130: // IllegalArgumentException must be generated.
131: try {
132: sh.setValue("test;param1=val1");
133: fail("setValue(): IAE was not thrown!");
134: } catch (IllegalArgumentException iae) {
135: // ok: IAE was thrown
136: } catch (Exception e) {
137: fail("setValue(): " + e + " was thrown instead of IAE!");
138: }
139:
140: assertTrue(true);
141: }
142:
143: /**
144: * Cuts ending CRLF's if any.
145: *
146: * @param str string to process.
147: * @return the resulting string.
148: */
149: protected String removeNewLine(String str) {
150: int len = Separators.NEWLINE.length();
151:
152: // Remove CRLF
153: while (str.endsWith(Separators.NEWLINE)) {
154: str = str.substring(0, str.length() - len);
155: }
156:
157: return str;
158: }
159:
160: /**
161: * Remove spaces/tabs surrounding delimeters in the string.
162: *
163: * @param str string to process.
164: * @return the resulting string.
165: */
166: protected String removeSpaces(String str) {
167: String strResult = "";
168: boolean delimeterOn = false;
169: char ch;
170:
171: for (int i = 0; i < str.length(); i++) {
172: ch = str.charAt(i);
173:
174: if (delimeterOn) {
175: if ((ch == ' ') || (ch == '\t')) {
176: continue; // skip the space/tab
177: }
178:
179: delimeterOn = false;
180: }
181:
182: if (ch == ';' || ch == '/') {
183: strResult = strResult.trim();
184: delimeterOn = true;
185: }
186:
187: strResult += ch;
188: } // end for
189:
190: return strResult;
191: }
192:
193: /**
194: * Compares two strings ignoring the spaces and tabs around delimeters.
195: * Comparision is case-insensitive.
196: *
197: * @param str1 first string to compare.
198: * @param str2 second string to compare.
199: * @return true if the string are equal, false otherwise.
200: */
201: protected boolean equalsIgnoreSpaces(String str1, String str2) {
202: String s1 = removeSpaces(str1);
203: String s2 = removeSpaces(str2);
204:
205: // DEBUG: System.out.println(">>> s1 = '" + s1 + "'");
206: // DEBUG: System.out.println(">>> s2 = '" + s2 + "'");
207:
208: return s1.equalsIgnoreCase(s2);
209: }
210: }
|