001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package javax.swing.text;
018:
019: import javax.swing.BasicSwingTestCase;
020:
021: import junit.framework.TestCase;
022:
023: public class GapContent_GapVectorTest extends TestCase {
024: private GapContent gv;
025:
026: @Override
027: protected void setUp() throws BadLocationException {
028: gv = new GapContent(20);
029: gv.insertString(0, "abcdefghij");
030: // 0123456789
031: }
032:
033: /**
034: * Tests constructor GapContent()
035: */
036: public void testGapContent() {
037: GapContent gv = new GapContent();
038: assertEquals(10, gv.getArrayLength());
039: assertNotNull(gv.getArray());
040: }
041:
042: /**
043: * Tests constructor GapContent(int)
044: */
045: public void testGapContentint() {
046: assertEquals(20, gv.getArrayLength());
047: assertNotNull(gv.getArray());
048: }
049:
050: public void testAllocateArray() {
051: final int length = gv.getArrayLength();
052: char[] array = (char[]) gv.allocateArray(150);
053: assertEquals(150, array.length);
054: assertEquals(length, gv.getArrayLength());
055: }
056:
057: public void testGetArray() {
058: assertTrue(gv.getArray() instanceof char[]);
059: }
060:
061: public void testGetArrayLength() {
062: assertEquals(((char[]) gv.getArray()).length, gv
063: .getArrayLength());
064: }
065:
066: public void testGetGapEnd() {
067: assertEquals(19, gv.getGapEnd());
068: }
069:
070: public void testGetGapStart() {
071: assertEquals(10, gv.getGapStart());
072: }
073:
074: public void testInsertStringValid() throws BadLocationException {
075: gv.insertString(0, "z");
076: assertEquals(1, gv.getGapStart());
077: }
078:
079: public void testInsertStringEnd() throws BadLocationException {
080: gv.insertString(11, "z");
081: assertEquals(12, gv.getGapStart());
082: assertEquals("abcdefghij\nz", gv.getString(0, gv.length()));
083: }
084:
085: public void testInsertStringInvalid() {
086: try {
087: gv.insertString(12, "z");
088: fail("BadLocationException must be thrown");
089: } catch (BadLocationException e) {
090: }
091: }
092:
093: public void testInsertStringNull() throws BadLocationException {
094: try {
095: gv.insertString(0, null);
096: fail("NullPointerException is expected");
097: } catch (NullPointerException e) {
098: }
099: assertEquals(10, gv.getGapStart());
100: assertEquals(19, gv.getGapEnd());
101: }
102:
103: public void testInsertStringEmpty() throws BadLocationException {
104: gv.insertString(0, "");
105: assertEquals(10, gv.getGapStart());
106: assertEquals(19, gv.getGapEnd());
107: }
108:
109: public void testInsertStringInvalidNull() {
110: try {
111: gv.insertString(-1, null);
112:
113: fail("NullPointerException or BadLocationException must be thrown");
114: } catch (NullPointerException e) {
115: if (!BasicSwingTestCase.isHarmony()) {
116: fail("Unexpected NullPointerException");
117: }
118: } catch (BadLocationException e) {
119: if (BasicSwingTestCase.isHarmony()) {
120: fail("Unexpected BadLocationException");
121: }
122: }
123: }
124:
125: public void testRemoveValid() throws BadLocationException {
126: gv.remove(1, 7);
127: assertEquals(1, gv.getGapStart());
128: assertEquals(17, gv.getGapEnd());
129: }
130:
131: public void testRemoveInvalidPosition() {
132: try {
133: gv.remove(10, 1);
134: fail("BadLocationException should be thrown");
135: } catch (BadLocationException e) {
136: }
137: }
138:
139: public void testRemoveInvalidSize() {
140: // Try to remove more items than the array actually holds
141: try {
142: gv.remove(7, 10);
143: fail("BadLocationException should be thrown");
144: } catch (BadLocationException e) {
145: }
146: }
147:
148: public void testRemoveEmpty() throws BadLocationException {
149: gv.remove(1, 0);
150:
151: assertEquals(10, gv.getGapStart());
152: assertEquals(19, gv.getGapEnd());
153: }
154:
155: public void testRemoveEmptyInvalid() {
156: try {
157: gv.remove(-1, 0);
158:
159: fail("BadLocationException should be thrown");
160: } catch (BadLocationException e) {
161: }
162:
163: assertEquals(10, gv.getGapStart());
164: assertEquals(19, gv.getGapEnd());
165: }
166:
167: public void testReplace() throws BadLocationException {
168: char[] charArray = { 'z', 'y', 'x', 'w', 'v' };
169: gv.replace(3, 5, charArray, 3);
170: char[] chars1 = { 'a', 'b', 'c', 'z', 'y', 'x' };
171: char[] chars2 = { 'i', 'j' };
172: char[] array = (char[]) gv.getArray();
173: for (int i = 0; i < gv.getGapStart(); i++) {
174: assertEquals("1 @ " + i, chars1[i], array[i]);
175: }
176: for (int i = gv.getGapEnd(), j = 0; j < chars2.length; i++, j++) {
177: assertEquals("2 @ " + i, chars2[j], array[i]);
178: }
179: }
180:
181: // HARMONY-1809
182: public void testReplaceInvalidRemovePosition() throws Exception {
183: gv.replace(-2, 2, null, 0);
184: if (BasicSwingTestCase.isHarmony()) {
185: // Harmony just ignores the operation
186: // because GapContent.removeItems throws BadLocationException;
187: assertEquals("abcdefghij\n", gv.getString(0, gv.length()));
188: } else {
189: assertEquals("cdefghij\n", gv.getString(0, gv.length()));
190: }
191: }
192:
193: // HARMONY-1809
194: public void testReplaceInvalidRemoveLength() throws Exception {
195: gv.replace(5, 6, null, 0);
196: if (BasicSwingTestCase.isHarmony()) {
197: // Harmony just ignores the operation
198: // because GapContent.removeItems throws BadLocationException;
199: assertEquals("abcdefghij\n", gv.getString(0, gv.length()));
200: } else {
201: assertEquals("abcde", gv.getString(0, gv.length()));
202: }
203: }
204:
205: // HARMONY-1809
206: public void testReplaceNullInsert() throws Exception {
207: gv.replace(0, 0, null, 0);
208: assertEquals(10, gv.getGapStart());
209: assertEquals(19, gv.getGapEnd());
210: assertEquals("abcdefghij\n", gv.getString(0, gv.length()));
211: }
212:
213: // HARMONY-1809
214: public void testReplaceInvalidInsertLength() throws Exception {
215: try {
216: gv.replace(0, 0, new char[] { '1' }, 2);
217: fail("ArrayIndexOutOfBounds is expected");
218: } catch (ArrayIndexOutOfBoundsException e) {
219: }
220:
221: if (BasicSwingTestCase.isHarmony()) {
222: assertEquals(0, gv.getGapStart());
223: assertEquals(9, gv.getGapEnd());
224: assertEquals("abcdefghij\n", gv.getString(0, gv.length()));
225: } else {
226: assertEquals(2, gv.getGapStart());
227: assertEquals(9, gv.getGapEnd());
228: assertEquals("ababcdefghij\n", gv.getString(0, gv.length()));
229: }
230: }
231:
232: // HARMONY-1809
233: public void testReplaceInvalidInsertLengthNegative()
234: throws Exception {
235: try {
236: gv.replace(0, 0, new char[] { '1' }, -1);
237: fail("ArrayIndexOutOfBounds is expected");
238: } catch (ArrayIndexOutOfBoundsException e) {
239: }
240:
241: if (BasicSwingTestCase.isHarmony()) {
242: assertEquals(0, gv.getGapStart());
243: assertEquals(9, gv.getGapEnd());
244: assertEquals("abcdefghij\n", gv.getString(0, gv.length()));
245: } else {
246: assertEquals(-1, gv.getGapStart());
247: assertEquals(9, gv.getGapEnd());
248: assertEquals("bcdefghij\n", gv.getString(0, gv.length()));
249: }
250: }
251:
252: public void testShiftEnd() {
253: gv.shiftGap(5);
254: gv.shiftEnd(20);
255: // Check the gap boundaries and its size
256: assertEquals(5, gv.getGapStart());
257: assertEquals(36, gv.getGapEnd());
258: assertEquals(31, gv.getGapEnd() - gv.getGapStart());
259: // Check that values are copied
260: assertEquals('e',
261: ((char[]) gv.getArray())[gv.getGapStart() - 1]);
262: assertEquals('f', ((char[]) gv.getArray())[gv.getGapEnd()]);
263: try {
264: gv.shiftEnd(1);
265: fail("ArrayIndexOutOfBoundsException is expected as the size of "
266: + "the array can't be decreased.");
267: } catch (ArrayIndexOutOfBoundsException e) {
268: }
269: }
270:
271: public void testShiftEndOverfull() throws BadLocationException {
272: gv.insertString(gv.getGapStart(), "0123456");
273: int size = gv.getArrayLength();
274: assertEquals(20, size);
275: char c = '7';
276: while (size == gv.getArrayLength()) {
277: gv.insertString(gv.getGapStart(), String.valueOf(c));
278: c++;
279: }
280: assertEquals(19, gv.getGapStart());
281: assertEquals(41, gv.getGapEnd());
282: assertEquals(42, gv.getArrayLength());
283: }
284:
285: public void testShiftGapToLeft() {
286: gv.shiftGap(5);
287: assertEquals(5, gv.getGapStart());
288: assertEquals(14, gv.getGapEnd());
289: }
290:
291: public void testShiftGapToRight() {
292: gv.shiftGap(2);
293: gv.shiftGap(7);
294: assertEquals(7, gv.getGapStart());
295: assertEquals(16, gv.getGapEnd());
296: char[] array = (char[]) gv.getArray();
297: assertEquals('g', array[gv.getGapStart() - 1]);
298: assertEquals('h', array[gv.getGapEnd()]);
299: }
300:
301: public void testShiftGapEndUp() {
302: gv.shiftGap(5);
303: gv.shiftGapEndUp(17);
304: assertEquals(17, gv.getGapEnd());
305: }
306:
307: public void testShiftGapStartDown() {
308: gv.shiftGap(5);
309: gv.shiftGapStartDown(3);
310: assertEquals(3, gv.getGapStart());
311: }
312: }
|