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.links;
028:
029: import com.sun.cldc.isolate.Isolate;
030: import com.sun.midp.i3test.TestCase;
031:
032: /**
033: * Tests basic operations on the LinkMessage class.
034: */
035: public class TestLinkMessage extends TestCase {
036:
037: /**
038: * Checks the creation of a data message with subrange arguments to
039: * determine whether it throws IndexOutOfBoundsException. Returns a
040: * boolean indicating whether IOOBE was thrown.
041: */
042: boolean checkRange(byte[] data, int offset, int length) {
043: boolean thrown = false;
044:
045: try {
046: LinkMessage.newDataMessage(data, offset, length);
047: } catch (IndexOutOfBoundsException ioobe) {
048: thrown = true;
049: }
050:
051: return thrown;
052: }
053:
054: /**
055: * Tests a LinkMessage containing a basic byte array (that is, not a
056: * subrange).
057: */
058: void testData() {
059: byte[] data = Utils.extractBytes("this is a test string");
060: LinkMessage lm = LinkMessage.newDataMessage(data);
061:
062: assertTrue("containsData must return true", lm.containsData());
063: assertFalse("containsLink must return false", lm.containsLink());
064: assertFalse("containsString must return false", lm
065: .containsString());
066: assertSame("extract must return same object", data, lm
067: .extract());
068:
069: boolean thrown;
070: byte[] retData = null;
071:
072: thrown = false;
073: try {
074: retData = lm.extractData();
075: } catch (IllegalStateException ise) {
076: thrown = true;
077: }
078: assertFalse("extractData must not throw exception", thrown);
079: assertTrue("extractData must return equal data", Utils
080: .bytesEqual(data, retData));
081:
082: thrown = false;
083: Object obj;
084: try {
085: obj = lm.extractLink();
086: } catch (IllegalStateException ise) {
087: thrown = true;
088: }
089: assertTrue("extractLink must throw exception", thrown);
090:
091: thrown = false;
092: try {
093: obj = lm.extractString();
094: } catch (IllegalStateException ise) {
095: thrown = true;
096: }
097: assertTrue("extractString must throw exception", thrown);
098: }
099:
100: /**
101: * Tests a LinkMessage that contains a byte array subrange.
102: */
103: void testDataSubrange() {
104: byte[] data = Utils.extractBytes("this is test data fodder");
105:
106: assertTrue("offset -7 must throw ioobe", checkRange(data, -7,
107: 10));
108: assertTrue("offset 9853 must throw ioobe", checkRange(data,
109: 9853, 3));
110: assertTrue("length 2387 must throw ioobe", checkRange(data, 0,
111: 2387));
112: assertTrue("length -2 must throw ioobe",
113: checkRange(data, 5, -2));
114: assertTrue("20, 20 must throw ioobe", checkRange(data, 20, 20));
115:
116: assertFalse("0,0 ok", checkRange(data, 0, 0));
117: assertFalse("0,len ok", checkRange(data, 0, data.length));
118: assertFalse("len,0 ok", checkRange(data, data.length, 0));
119: assertFalse("3,6 ok", checkRange(data, 3, 6));
120:
121: LinkMessage lm;
122: byte[] retData;
123:
124: lm = LinkMessage.newDataMessage(data, 0, data.length);
125: retData = lm.extractData();
126: assertTrue("full data", Utils.bytesEqual(data, retData));
127:
128: byte[] nullData = new byte[0];
129:
130: lm = LinkMessage.newDataMessage(data, 0, 0);
131: retData = lm.extractData();
132: assertTrue("zero null", Utils.bytesEqual(nullData, retData));
133:
134: lm = LinkMessage.newDataMessage(data, data.length, 0);
135: retData = lm.extractData();
136: assertTrue("end null", Utils.bytesEqual(nullData, retData));
137:
138: lm = LinkMessage.newDataMessage(data, 5, 9);
139: retData = lm.extractData();
140: assertTrue("subrange", Utils.bytesEqual(data, 5, 9, retData));
141: }
142:
143: /**
144: * Tests a LinkMessage containing a Link.
145: */
146: void testLink() {
147: Isolate isolate = Isolate.currentIsolate();
148: Link link = Link.newLink(isolate, isolate);
149: LinkMessage lm = LinkMessage.newLinkMessage(link);
150:
151: assertFalse("containsData must return false", lm.containsData());
152: assertTrue("containsLink must return true", lm.containsLink());
153: assertFalse("containsString must return false", lm
154: .containsString());
155: assertSame("extract must return same object", link, lm
156: .extract());
157:
158: boolean thrown;
159: Object obj;
160:
161: thrown = false;
162: try {
163: obj = lm.extractData();
164: } catch (IllegalStateException ise) {
165: thrown = true;
166: }
167: assertTrue("extractData must throw exception", thrown);
168:
169: thrown = false;
170: Link retLink = null;
171: try {
172: retLink = lm.extractLink();
173: } catch (IllegalStateException ise) {
174: thrown = true;
175: }
176: assertFalse("extractLink must not throw exception", thrown);
177: assertSame("extractLink must return same link", link, retLink);
178:
179: thrown = false;
180: try {
181: obj = lm.extractString();
182: } catch (IllegalStateException ise) {
183: thrown = true;
184: }
185: assertTrue("extractString must throw exception", thrown);
186: }
187:
188: /**
189: * Tests a LinkMessage containing a String.
190: */
191: void testString() {
192: String msg = "this is a string message";
193: LinkMessage lm = LinkMessage.newStringMessage(msg);
194:
195: assertFalse("containsData must return false", lm.containsData());
196: assertFalse("containsLink must return false", lm.containsLink());
197: assertTrue("containsString must return true", lm
198: .containsString());
199: assertSame("extract must return same object", msg, lm.extract());
200:
201: boolean thrown;
202: Object obj;
203:
204: thrown = false;
205: try {
206: obj = lm.extractData();
207: } catch (IllegalStateException ise) {
208: thrown = true;
209: }
210: assertTrue("extractData must throw exception", thrown);
211:
212: thrown = false;
213: try {
214: obj = lm.extractLink();
215: } catch (IllegalStateException ise) {
216: thrown = true;
217: }
218: assertTrue("extractLink must throw exception", thrown);
219:
220: thrown = false;
221: String str = null;
222: try {
223: str = lm.extractString();
224: } catch (IllegalStateException ise) {
225: thrown = true;
226: }
227: assertFalse("extractString must not thrown exception", thrown);
228: assertTrue("strings must match", msg.equals(str));
229: }
230:
231: /**
232: * Runs all tests.
233: */
234: public void runTests() {
235: declare("data");
236: testData();
237: declare("data subrange");
238: testDataSubrange();
239: declare("link");
240: testLink();
241: declare("string");
242: testString();
243: }
244:
245: }
|