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 com.sun.midp.content;
028:
029: import com.sun.midp.i3test.TestCase;
030: import com.sun.midp.security.SecurityToken;
031: import com.sun.midp.midlet.MIDletStateHandler;
032: import com.sun.midp.midlet.MIDletSuite;
033:
034: /**
035: * Extension to the basic TestCase class to add ContentHandler
036: * specific assert methods.
037: */
038: public abstract class ExtendedTestCase extends TestCase {
039:
040: /** Classname of the MIDlet. */
041: String classname = "com.sun.midp.i3test.Framework";
042:
043: /**
044: * Initialize the registry for this application.
045: * @return the registry for the Framework class
046: */
047: RegistryImpl getRegistry() {
048: try {
049: SecurityToken token = getSecurityToken();
050: MIDletStateHandler mstate = MIDletStateHandler
051: .getMidletStateHandler();
052: MIDletSuite msuite = mstate.getMIDletSuite();
053: msuite.setTempProperty(token, "MIDlet-1", "CHAPI Tests,,"
054: + classname);
055:
056: return RegistryImpl.getRegistryImpl(classname, token);
057: } catch (SecurityException sx) {
058: fail("SecurityException: can't setTempProperty");
059: } catch (Throwable t) {
060: assertNull("i3test can't get registry for class "
061: + classname, t);
062: t.printStackTrace();
063: }
064: return null;
065: }
066:
067: /**
068: * Override assertEquals to allow null pointers.
069: * @param message message
070: * @param expected the expected object
071: * @param actual the actual object.
072: */
073: public void assertEquals(String message, Object expected,
074: Object actual) {
075: if (expected == actual) {
076: // The identity and the null == null
077: return;
078: }
079: if (expected != null) {
080: assertTrue(message, expected.equals(actual));
081: } else {
082: assertTrue(message, actual.equals(expected));
083: }
084: }
085:
086: /**
087: * Assert that all of the fields of the two InvocationImpls match
088: * exactly.
089: * @param msg the message to print for assert failures
090: * @param expected the InvocationImpl containing the expected values
091: * @param actual the InvocationImpl containing the actual values
092: */
093: public void assertEquals(String msg, InvocationImpl expected,
094: InvocationImpl actual) {
095: if (expected == actual) {
096: return;
097: }
098: assertNotNull(msg + " expected: ", actual);
099: if (actual == null) {
100: return;
101: }
102:
103: assertEquals("verify getID", expected.getID(), actual.getID());
104: assertEquals("verify getType", expected.getType(), actual
105: .getType());
106: assertEquals("verify getURL", expected.getURL(), actual
107: .getURL());
108: assertTrue("verify responseRequired", expected
109: .getResponseRequired() == actual.getResponseRequired());
110: assertEquals("verify action", expected.getAction(), actual
111: .getAction());
112: assertEquals("verify classname", expected.classname,
113: actual.classname);
114: assertEquals("verify invokingSuiteId",
115: expected.invokingSuiteId, actual.invokingSuiteId);
116: assertEquals("verify invokingClassname",
117: expected.invokingClassname, actual.invokingClassname);
118: assertEquals("verify tid", expected.tid, actual.tid);
119: assertEquals("verify invokingAuthority",
120: expected.invokingAuthority, actual.invokingAuthority);
121: assertEquals("verify invokingID", expected.invokingID,
122: actual.invokingID);
123: assertEquals("verify previousTid", expected.previousTid,
124: actual.previousTid);
125: assertEquals("verify arguments", expected.getArgs(), actual
126: .getArgs());
127: String[] args = expected.getArgs();
128: int argsLen = args != null ? args.length : 0;
129: assertEquals("verify argsLen", argsLen, actual.argsLen);
130: assertEquals("verify data", expected.data, actual.data);
131: }
132:
133: /**
134: * Compare two arrays of strings, must be the same length
135: * and contents to match.
136: * @param msg the message to print for assert failures
137: * @param s1 an array of strings
138: * @param s2 another array of strings
139: */
140: public void assertEquals(String msg, String[] s1, String[] s2) {
141: // Identical arrays are equals
142: if (s1 == s2) {
143: return;
144: }
145: // If either array is null then they don't match
146: if (s1 == null) {
147: fail("mismatched arg arrays; expected is null");
148: return;
149: }
150: if (s2 == null) {
151: fail("mismatched arg arrays; actual is null");
152: return;
153: }
154: // If the lengths are unequal then they are not equal
155: assertEquals("mismatched arg lengths", s1.length, s2.length);
156:
157: if (s1.length == s2.length) {
158: // Compare the strings in each array
159: for (int i = 0; i < s1.length; i++) {
160: // Two strings, non-null of the same length
161: assertEquals("arg " + i, s1[i], s2[i]);
162: }
163: }
164: }
165:
166: /**
167: * Compare two byte arrays; must be same length.
168: * @param msg the description of the array
169: * @param expected the expected data array
170: * @param actual the actual data array
171: */
172: public void assertEquals(String msg, byte[] expected, byte[] actual) {
173: if (expected == actual) {
174: return;
175: }
176: if (expected == null) {
177: fail("expected string array is null");
178: return;
179: }
180: if (actual == null) {
181: fail("actual string array is null");
182: return;
183: }
184: assertEquals(msg + " length", expected.length, actual.length);
185: int badbytes = 0;
186: if (expected.length == actual.length) {
187: for (int i = 0; i < expected.length; i++) {
188: if (expected[i] != actual[i]) {
189: if (0 == badbytes++) {
190: fail(msg + " data does not match");
191: }
192: assertEquals(" " + i + ": ", expected[i],
193: actual[i]);
194: }
195: }
196: }
197: }
198:
199: /**
200: * Sleep a bit.
201: * @param millis millseconds to sleep
202: */
203: public static void sleep(long millis) {
204: try {
205: Thread.sleep(millis);
206: } catch (InterruptedException ie) {
207: }
208: }
209: }
|