001: package test.tck.factory;
002:
003: // import gov.nist.javax.sip.header.*;
004: import java.lang.reflect.*;
005:
006: import javax.sip.address.SipURI;
007: import javax.sip.address.URI;
008: import javax.sip.header.*;
009:
010: import org.apache.log4j.Logger; // import gov.nist.core.*;
011: import java.util.*;
012: import java.text.*;
013:
014: import test.tck.*;
015:
016: /**
017: * Generate header test cases based on RI. This assumes the correctness
018: * of the TCK and generates a set of tests for a reference implementation
019: * based on the TCK. It does this by introspection.
020: */
021:
022: public class HeaderFactoryTest extends FactoryTestHarness {
023:
024: private static Logger logger = Logger
025: .getLogger(HeaderFactoryTest.class);
026:
027: // Header definitions for valid headers.
028:
029: /*
030: * JvB: This is using gov.nist specific classes that are not in the RI
031: * and is not being used at the moment
032: *
033: *
034: private static void testParametersHeader(
035: Header referenceHeader,
036: Header headerToTest) {
037: Iterator it = ((ParametersHeader) referenceHeader).getParameterNames();
038: //logger.info(referenceHeader.getClass());
039: while (it.hasNext()) {
040: String name = (String) it.next();
041: String value = ((Parameters) headerToTest).getParameter(name);
042: String referenceValue =
043: ((Parameters) referenceHeader).getParameter(name);
044: NameValue nv =
045: ((ParametersHeader) referenceHeader).getNameValue(name);
046: Object val = nv.getValue();
047: if (val != null && val.getClass().equals(String.class)) {
048: if (nv.isValueQuoted()) {
049: assertTrue(value.equals(referenceValue));
050: } else {
051: assertTrue(value.equalsIgnoreCase((String) referenceValue));
052: }
053: } else if (val != null) {
054: assertTrue(value.equals(referenceValue));
055: } else {
056: assertTrue(value == null);
057: }
058: }
059: }
060: */
061:
062: private void testGetMethods(Header refHeader, Header headerToTest)
063: throws IllegalAccessException, InvocationTargetException {
064: try {
065: Class implementationClass;
066: String name = refHeader.getName();
067:
068: implementationClass = refHeader.getClass();
069:
070: Class[] implementedInterfaces = implementationClass
071: .getInterfaces();
072: int j = 0;
073: for (j = 0; j < implementedInterfaces.length; j++) {
074: if (Header.class
075: .isAssignableFrom(implementedInterfaces[j]))
076: break;
077: }
078: if (j == implementedInterfaces.length) {
079: logger.fatal("Hmm... could not find it"
080: + refHeader.getClass());
081: throw new TckInternalError("Header not implemented");
082: }
083:
084: String jainClassName = implementedInterfaces[j].getName();
085:
086: checkImplementsInterface(headerToTest.getClass(),
087: implementedInterfaces[j]);
088:
089: // Test the get methods of the interface.
090: Method methods[] = implementedInterfaces[j]
091: .getDeclaredMethods();
092: for (int i = 0; i < methods.length; i++) {
093: String methodName = methods[i].getName();
094: if ((!methodName.startsWith("get"))
095: || methodName.equals("getParameter"))
096: continue;
097: Class returnType = methods[i].getReturnType();
098: Object refType = null;
099: try {
100: refType = methods[i].invoke(refHeader,
101: (Object[]) null);
102: } catch (InvocationTargetException ex1) {
103: ex1.getCause().printStackTrace();
104: throw new TckInternalError("Invocation failure "
105: + methodName);
106: }
107: String ftype = returnType.toString();
108: if (returnType.isPrimitive()) {
109: Object testValue = methods[i].invoke(headerToTest,
110: (Object[]) null);
111: assertTrue(testValue.equals(refType));
112: } else {
113: // Non primitive.
114: Object testValue = methods[i].invoke(headerToTest,
115: (Object[]) null);
116: if (refType != null) {
117:
118: if (refType instanceof Calendar) {
119: // TODO: compare how? equals fails
120: } else if (refType instanceof Iterator) {
121: // TODO
122: } else {
123: assertEquals("Method:" + methods[i],
124: refType, testValue);
125: }
126: } else {
127: assertNull(testValue);
128: }
129: }
130: }
131: if (refHeader instanceof Parameters) {
132: Parameters p1 = (Parameters) refHeader;
133: Parameters p2 = (Parameters) headerToTest;
134:
135: for (Iterator it = ((Parameters) refHeader)
136: .getParameterNames(); it.hasNext();) {
137: String pname = (String) it.next();
138:
139: // too strict: equalsIgnoreCase is better
140: assertEquals(p1.getParameter(pname), p2
141: .getParameter(pname));
142: }
143: }
144:
145: /*
146: if (!refHeader.toString().trim().replaceAll( "[\\n\\t\\r ]", "" ).equalsIgnoreCase( headerToTest.toString().trim().replaceAll( "[\\n\\t\\r ]", "" ))) {
147: System.err.println( "\n\n\n##### Difference #####\n" + refHeader + "!=" + headerToTest );
148: }
149: */
150:
151: } catch (InvocationTargetException ex) {
152: ex.getCause().printStackTrace();
153: throw new TiUnexpectedError(ex.getMessage());
154: } catch (Exception ex) {
155:
156: ex.printStackTrace();
157: throw new TiUnexpectedError(ex.getMessage());
158: } finally {
159: logTestCompleted("testGetMethods(refHeader,headerToTest)");
160: }
161:
162: }
163:
164: public void testHeaderFactory() {
165:
166: try {
167: for (int i = 0; i < headers.length; i++) {
168: StringBuffer value = new StringBuffer(headers[i]);
169: StringBuffer name = new StringBuffer();
170: StringBuffer body = new StringBuffer();
171: int j = 0;
172: for (j = 0; j < value.length(); j++) {
173:
174: if (value.charAt(j) != ':') {
175: name.append(value.charAt(j));
176: } else {
177: break;
178: }
179: }
180:
181: j++;
182:
183: for (; j < value.length(); j++) {
184: body.append(value.charAt(j));
185: }
186:
187: Header riHeader = null;
188: Header tiHeader = null;
189: try {
190:
191: riHeader = riHeaderFactory.createHeader(name
192: .toString().trim(), body.toString().trim());
193:
194: } catch (ParseException ex) {
195: ex.printStackTrace();
196: throw new TckInternalError(ex.getMessage());
197: }
198:
199: Header headerToTest = tiHeaderFactory.createHeader(name
200: .toString().trim(), body.toString().trim());
201: assertTrue(headerToTest != null);
202:
203: logger.info("Testing header: " + name + " = " + body);
204:
205: testGetMethods(riHeader, headerToTest);
206: }
207: for (int i = 0; i < multiHeaders.length; i++) {
208: StringBuffer value = new StringBuffer(multiHeaders[i]);
209: List riHeaders = null;
210: List tiHeaders = null;
211: try {
212:
213: riHeaders = riHeaderFactory
214: .createHeaders(multiHeaders[i]);
215:
216: } catch (ParseException ex) {
217: throw new TckInternalError(ex.getMessage());
218: }
219:
220: tiHeaders = tiHeaderFactory
221: .createHeaders(multiHeaders[i]);
222: assertTrue(tiHeaders != null);
223: assertTrue(tiHeaders.size() == riHeaders.size());
224: ListIterator li = riHeaders.listIterator();
225: ListIterator li1 = tiHeaders.listIterator();
226: while (li.hasNext()) {
227: Header riHeader = (Header) li.next();
228: Header headerToTest = (Header) li1.next();
229: testGetMethods(riHeader, headerToTest);
230: }
231:
232: }
233: } catch (Exception ex) {
234: ex.printStackTrace();
235: throw new TiUnexpectedError(ex.getMessage());
236: } finally {
237: logTestCompleted("testHeaderFactory()");
238: }
239:
240: }
241:
242: /**
243: * This test checks the Date format, which is quite strict for SIP
244: */
245: public void testDate() {
246: try {
247: Calendar date = Calendar.getInstance(TimeZone
248: .getTimeZone("GMT"));
249: date.setTimeInMillis(0);
250: DateHeader dh = tiHeaderFactory.createDateHeader(date);
251: assertEquals("Date: Thu, 01 Jan 1970 00:00:00 GMT", dh
252: .toString().trim());
253: } finally {
254: logTestCompleted("testDate()");
255: }
256: }
257:
258: /**
259: * This tests that header parameters are properly assigned to the header, not the URI,
260: * when there are no angle brackets
261: */
262: public void testHeaderParams() {
263: try {
264: Header h = tiHeaderFactory.createHeader("m",
265: "sip:User1@127.0.0.1:1234;param1");
266: System.err.println(h);
267: assertTrue(h instanceof ContactHeader);
268: ContactHeader c = (ContactHeader) h;
269: URI u = c.getAddress().getURI();
270: assertTrue(u.isSipURI());
271: assertNull("URI must have no params", ((SipURI) u)
272: .getParameter("param1"));
273: assertNotNull(
274: "Parameter 'param1' must be assigned to the header",
275: c.getParameter("param1"));
276: } catch (ParseException e) {
277: e.printStackTrace();
278: fail(e.getMessage());
279: } finally {
280: logTestCompleted("testHeaderParams()");
281: }
282: }
283:
284: public HeaderFactoryTest() {
285: super ("HeaderFactoryTest");
286: }
287:
288: public static void main(String[] args) {
289: junit.textui.TestRunner.run(HeaderFactoryTest.class);
290: }
291:
292: }
|