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.lcdui;
028:
029: import com.sun.midp.i3test.*;
030:
031: public class TestOne extends TestCase {
032:
033: // utilities
034:
035: Item[] createManyItems(int n) {
036: Item ar[] = new Item[n];
037: for (int i = 0; i < n; i++) {
038: ar[i] = new StringItem(null, null);
039: }
040: return ar;
041: }
042:
043: // assertions
044:
045: void checkItems(Form f, int expectedNumOfItems) {
046: assertNotNull(f.items);
047: assertEquals(expectedNumOfItems, f.numOfItems);
048: assertTrue(f.items.length >= f.numOfItems);
049:
050: // check ownership of all items
051: for (int i = 0; i < f.numOfItems; i++) {
052: assertEquals(f, f.items[i].owner);
053: }
054:
055: // check that the tail of the array is all nulls
056: for (int i = f.numOfItems; i < f.items.length; i++) {
057: assertNull(f.items[i]);
058: }
059: }
060:
061: // the tests
062:
063: public void testAppend() {
064: Form f = new Form("testAppend");
065: f.append(new StringItem("one", null));
066: checkItems(f, 1);
067: }
068:
069: public void testAppendIndex() {
070: final int COUNT = 7;
071: Item ia[] = createManyItems(COUNT);
072: Item newItem = new StringItem("new", null);
073: Form f = new Form("testAppendIndex", ia);
074: int ix = f.append(newItem);
075: assertEquals(COUNT, ix);
076: checkItems(f, COUNT + 1);
077: }
078:
079: public void testConstruct0() {
080: Form f = new Form("testConstruct0");
081: checkItems(f, 0);
082: }
083:
084: public void testConstructN() {
085: final int COUNT = 35;
086: Item ia[] = createManyItems(COUNT);
087: Form f = new Form("testConstructN", ia);
088: checkItems(f, COUNT);
089: }
090:
091: public void testDelete0() {
092: final int COUNT = 18;
093:
094: Item ia[] = createManyItems(COUNT);
095: Form f = new Form("testDelete0", ia);
096: checkItems(f, COUNT);
097:
098: f.delete(0);
099: checkItems(f, COUNT - 1);
100: assertNull(ia[0].owner);
101:
102: // check that items after the deleted one are moved properly
103: for (int i = 0; i < COUNT - 1; i++) {
104: assertEquals(ia[i + 1], f.items[i]);
105: }
106: }
107:
108: public void testDeleteK() {
109: final int COUNT = 20;
110: final int DELITEM = 10;
111:
112: Item ia[] = createManyItems(COUNT);
113: Form f = new Form("testDeleteK", ia);
114: checkItems(f, COUNT);
115:
116: f.delete(DELITEM);
117: checkItems(f, COUNT - 1);
118: assertNull(ia[DELITEM].owner);
119:
120: // check that items before the deleted one are undisturbed
121: for (int i = 0; i < DELITEM; i++) {
122: assertEquals(ia[i], f.items[i]);
123: }
124:
125: // check that items after the deleted one are moved properly
126: for (int i = DELITEM; i < COUNT - 1; i++) {
127: assertEquals(ia[i + 1], f.items[i]);
128: }
129: }
130:
131: public void testDeleteN() {
132: final int COUNT = 15;
133:
134: Item ia[] = createManyItems(COUNT);
135: Form f = new Form("testDeleteN", ia);
136: checkItems(f, COUNT);
137:
138: f.delete(COUNT - 1);
139: checkItems(f, COUNT - 1);
140: assertNull(ia[COUNT - 1].owner);
141:
142: // check that items before the deleted one are undisturbed
143: for (int i = 0; i < COUNT - 1; i++) {
144: assertEquals(ia[i], f.items[i]);
145: }
146: }
147:
148: public void testDeleteAll() {
149: final int COUNT = 17;
150:
151: Item ia[] = createManyItems(COUNT);
152: Form f = new Form("testDeleteAll", ia);
153: checkItems(f, COUNT);
154:
155: f.deleteAll();
156: checkItems(f, 0);
157:
158: for (int i = 0; i < COUNT; i++) {
159: assertNull(ia[i].owner);
160: }
161: }
162:
163: public void testInsert0() {
164: final int COUNT = 13;
165:
166: Item ia[] = createManyItems(COUNT);
167: Form f = new Form("testInsert0", ia);
168: checkItems(f, COUNT);
169: Item newItem = new StringItem("new", null);
170: f.insert(0, newItem);
171:
172: checkItems(f, COUNT + 1);
173: assertEquals(newItem, f.items[0]);
174:
175: // check items after insertion are moved properly
176: for (int i = 0; i < COUNT; i++) {
177: assertEquals(ia[i], f.items[i + 1]);
178: }
179: }
180:
181: public void testInsertK() {
182: final int COUNT = 17;
183: final int INSIDX = 4;
184:
185: Item ia[] = createManyItems(COUNT);
186: Form f = new Form("testInsertK", ia);
187: checkItems(f, COUNT);
188: Item newItem = new StringItem("new", null);
189: f.insert(INSIDX, newItem);
190:
191: checkItems(f, COUNT + 1);
192: assertEquals(newItem, f.items[INSIDX]);
193:
194: // check items before insertion are undisturbed
195: for (int i = 0; i < INSIDX; i++) {
196: assertEquals(ia[i], f.items[i]);
197: }
198:
199: // check items after insertion are moved properly
200: for (int i = INSIDX; i < COUNT; i++) {
201: assertEquals(ia[i], f.items[i + 1]);
202: }
203: }
204:
205: public void testInsertN() {
206: final int COUNT = 23;
207:
208: Item ia[] = createManyItems(COUNT);
209: Form f = new Form("testInsertN", ia);
210: checkItems(f, COUNT);
211: Item newItem = new StringItem("new", null);
212: f.insert(COUNT, newItem);
213:
214: checkItems(f, COUNT + 1);
215: assertEquals(newItem, f.items[COUNT]);
216:
217: // check items before insertion are undisturbed
218: for (int i = 0; i < COUNT; i++) {
219: assertEquals(ia[i], f.items[i]);
220: }
221: }
222:
223: // main test driver
224:
225: public void runTests() {
226: declare("testAppend");
227: testAppend();
228: declare("testAppendIndex");
229: testAppendIndex();
230: declare("testConstruct0");
231: testConstruct0();
232: declare("testConstructN");
233: testConstructN();
234: declare("testDelete0");
235: testDelete0();
236: declare("testDeleteK");
237: testDeleteK();
238: declare("testDeleteN");
239: testDeleteN();
240: declare("testDeleteAll");
241: testDeleteAll();
242: declare("testInsert0");
243: testInsert0();
244: declare("testInsertK");
245: testInsertK();
246: declare("testInsertN");
247: testInsertN();
248: }
249: }
|