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 javax.microedition.content.ActionNameMap;
030:
031: import com.sun.midp.i3test.TestCase;
032:
033: import java.io.ByteArrayOutputStream;
034: import java.io.ByteArrayInputStream;
035: import java.io.DataOutputStream;
036: import java.io.DataInputStream;
037: import java.io.IOException;
038:
039: /**
040: * Test RegistryStore functionality.
041: * Test write and read of ContentHandlerImpl.
042: */
043: public class TestRegReadWrite extends TestCase {
044:
045: /** Constant application ID for testing. */
046: private static final int suiteId = 60000;
047:
048: /** The name of the GraphicalInstaller. */
049: private static final String name = "Test Suite";
050:
051: /** Test ID. */
052: private static final String ID = "TestID";
053:
054: /** The class of the GraphicalInstaller. */
055: private static final String classname = "com.sun.content.test.CHStaticMIDlet";
056:
057: /** The types registered. */
058: private String[] types = { "app/chs1", "app/chs2" };
059: /** The suffixes registered. */
060: private String[] suffixes = { ".chs1", ".chs2" };
061: /** The actions registered. */
062: private String[] actions = { "edit", "save", "send" };
063: /** The english action names registered. */
064: private String[] enActionNames = { "en-edit", "en-save", "en-send" };
065: /** The french action names registered. */
066: private String[] frActionNames = { "fr_CH-edit", "fr_CH-save",
067: "fr_CH-send" };
068:
069: /** The action name maps to verify. */
070: private String[][] actionmaps = { enActionNames, frActionNames };
071: /** The locale supported. */
072: private String[] actionlocales = { "en", "fr_CH" };
073:
074: /** The restricted access. */
075: private String[] accessRestricted = { "me", "justme" };
076:
077: /** A zero length array of strings. */
078: private String[] ZERO_STRINGS = new String[0];
079:
080: /** Empty ActionNameMap to return when needed. */
081: private final static ActionNameMap[] ZERO_ACTIONNAMES = new ActionNameMap[0];
082:
083: /**
084: * Run the tests.
085: */
086: public void runTests() {
087: test001();
088: }
089:
090: /**
091: * Test that the built-in suite is registered.
092: */
093: void test001() {
094: boolean b;
095: ContentHandlerImpl[] chArr;
096: ContentHandlerImpl chTst;
097: String[] arr;
098: String caller = accessRestricted[0];
099: String badStr = "_";
100:
101: declare("Verify the write and read of RegistryStore");
102:
103: ContentHandlerImpl ch = new ContentHandlerImpl(ZERO_STRINGS,
104: ZERO_STRINGS, ZERO_STRINGS, ZERO_ACTIONNAMES, null,
105: ZERO_STRINGS, "");
106:
107: assertNotNull("Verify handler created", ch);
108:
109: b = RegistryStore.register(ch);
110: assertFalse("Verify empty handler is not registered", b);
111:
112: ActionNameMap[] actionnames = new ActionNameMap[2];
113: actionnames[0] = new ActionNameMap(actions, enActionNames,
114: "en_US");
115: actionnames[1] = new ActionNameMap(actions, frActionNames,
116: "fr_CH");
117:
118: ch = new ContentHandlerImpl(types, suffixes, actions,
119: actionnames, ID, accessRestricted, "authority");
120:
121: ch.storageId = suiteId;
122: ch.classname = classname;
123: ch.appname = name;
124:
125: // static boolean register(ContentHandlerImpl contentHandler) {
126: b = RegistryStore.register(ch);
127: assertTrue("Verify right handler is registered", b);
128:
129: /**
130: * @param searchBy:
131: * <code>BY_TYPE</code>,
132: * <code>BY_SUFFIX</code>,
133: * <code>BY_ACTION</code>,
134: * <code>BY_ID</code>,
135: * <code>BY_ID_EXACT</code>
136: * <code>BY_SUITE</code>
137: */
138: // static String[] findHandler(String callerId, int searchBy, String value)
139: chArr = RegistryStore.findHandler(caller,
140: RegistryStore.FIELD_TYPES, ch.getType(0));
141: if (chArr != null && chArr.length == 1) {
142: assertEquals("Verify handler ID after search by type",
143: ch.ID, chArr[0].ID);
144: } else {
145: fail("Verify search handler by type");
146: }
147:
148: chArr = RegistryStore.findHandler(caller,
149: RegistryStore.FIELD_TYPES, badStr);
150: assertTrue("Verify empty search results by type", chArr == null
151: || chArr.length == 0);
152:
153: // search by partial ID
154: chArr = RegistryStore.findHandler(caller,
155: RegistryStore.FIELD_ID, ch.ID + ch.ID.substring(0, 3));
156: if (chArr != null && chArr.length == 1) {
157: assertEquals("Verify handler ID after partial search",
158: ch.ID, chArr[0].ID);
159: } else {
160: fail("Verify handler ID after search by cut ID");
161: }
162:
163: // search by exact ID
164: chTst = RegistryStore.getHandler(caller, ch.ID,
165: RegistryStore.SEARCH_EXACT);
166: if (chTst != null) {
167: assertEquals("Verify handler search by ID exact", ch.ID,
168: chTst.ID);
169: } else {
170: fail("Verify handler search by ID exact");
171: }
172:
173: // get values
174: arr = RegistryStore
175: .getValues(caller, RegistryStore.FIELD_TYPES);
176: assertTrue("Verify getValues by type", arr != null
177: && arr.length >= 2);
178:
179: // testId
180: b = testId(ch.ID);
181: assertFalse("Verify test equal ID", b);
182:
183: b = testId(ch.ID.substring(0, 3));
184: assertFalse("Verify test prefixed ID", b);
185:
186: b = testId(ch.ID + "qqq");
187: assertFalse("Verify test prefixing ID", b);
188:
189: b = testId("qqq" + ch.ID);
190: assertTrue("Verify test good ID", b);
191:
192: // load handler
193: ContentHandlerImpl ch2 = RegistryStore.getHandler(null, ch.ID,
194: RegistryStore.SEARCH_EXACT);
195: assertNotNull("Verify loadHandler by ID", ch2);
196:
197: // version and appname are not storable
198: if (ch2 != null) {
199: ch2.appname = ch.appname;
200: ch2.version = ch.version;
201: assertEquals("Verify loadHandler by ID", ch, ch2);
202: }
203:
204: // unregister
205: b = RegistryStore.unregister(ch.ID);
206: assertTrue(
207: "Verify right handler is unregistered "
208: + "/CHECK: if JSR 211 database has the correct format!/",
209: b);
210: }
211:
212: private boolean testId(String ID) {
213: ContentHandlerImpl[] arr = RegistryStore.findHandler(null,
214: RegistryStore.FIELD_ID, ID);
215: return arr == null || arr.length == 0;
216: }
217:
218: /**
219: * Compare two Content Handlers.
220: * @param msg describing the test
221: * @param expected the expected ContentHandler
222: * @param actual the actual ContentHandler
223: */
224: void assertEquals(String msg, ContentHandlerImpl expected,
225: ContentHandlerImpl actual) {
226: assertEquals("Verify storageId", expected.storageId,
227: actual.storageId);
228: assertEquals("Verify classname", expected.classname,
229: actual.classname);
230: assertEquals("Verify ID", expected.ID, actual.ID);
231: // Verify the dynamic flag
232: assertTrue(
233: "Verify dynamic flag",
234: expected.registrationMethod == actual.registrationMethod);
235: }
236:
237: /**
238: * Verify string arrays; asserting members are equals.
239: * @param msg describing the test
240: * @param expected the expected array of strings.
241: * @param actual the actual array of strings.
242: */
243: void assertEquals(String msg, String[] expected, String[] actual) {
244: assertEquals(msg + " length", expected.length, actual.length);
245: int len = expected.length;
246: if (len > actual.length) {
247: len = actual.length;
248: }
249: for (int i = 0; i < len; i++) {
250: assertEquals(msg + "[" + i + "]", expected[i], actual[i]);
251: }
252:
253: // Report errors for any extra actuals
254: for (int i = len; i < actual.length; i++) {
255: assertEquals(msg + "[" + i + "]", null, actual[i]);
256: }
257:
258: // Report errors for any extra expected
259: for (int i = len; i < expected.length; i++) {
260: assertEquals(msg + "[" + i + "]", actual[i], null);
261: }
262: }
263:
264: /**
265: * Verify that two ActionNameMaps are equal.
266: * @param msg the message to print if a compare fails
267: * @param expected the expected map
268: * @param actual the actual map
269: */
270: void assertEquals(String msg, ActionNameMap expected,
271: ActionNameMap actual) {
272: assertEquals("Verify locale", expected.getLocale(), actual
273: .getLocale());
274: int size = expected.size();
275: assertEquals(msg + " size ", size, actual.size());
276: if (size == actual.size()) {
277: for (int i = 0; i < size; i++) {
278: assertEquals(msg + " action ", expected.getAction(i),
279: actual.getAction(i));
280: assertEquals(msg + " action name", expected
281: .getActionName(i), actual.getActionName(i));
282: }
283: }
284: }
285:
286: /**
287: * Make a contenthandlerImpl with everything set.
288: *
289: * @return content handler object
290: */
291: ContentHandlerImpl makeFull() {
292:
293: ActionNameMap[] actionnames = new ActionNameMap[2];
294: actionnames[0] = new ActionNameMap(actions, enActionNames,
295: "en_US");
296: actionnames[1] = new ActionNameMap(actions, frActionNames,
297: "fr_CH");
298:
299: ContentHandlerImpl ch = new ContentHandlerImpl(types, suffixes,
300: actions, actionnames, "ID-1", accessRestricted,
301: "authority");
302: ch.storageId = 61000;
303: ch.appname = "Application Name";
304: ch.version = "1.1.1";
305: ch.classname = "classname";
306: ch.registrationMethod = ContentHandlerImpl.REGISTERED_STATIC;
307: return ch;
308: }
309:
310: }
|