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: /**
018: * @author Alexey A. Ivanov
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import javax.swing.BasicSwingTestCase;
023: import javax.swing.text.AbstractDocument.Content;
024:
025: /**
026: * This class is to test methods of AbstractDocument.Content inteface.
027: *
028: */
029: public class AbstractDocument_ContentTest extends BasicSwingTestCase {
030: /**
031: * This is a shared object under which will be run. It must be initialized
032: * to contain a test string:
033: *
034: * "This is a test string."
035: * 0123456789012345678901
036: */
037: Content obj;
038:
039: /**
040: * Shared segment of text which is used in testGetCharsXXX methods.
041: */
042: Segment text;
043:
044: public AbstractDocument_ContentTest() {
045: super ();
046: }
047:
048: public AbstractDocument_ContentTest(final String name) {
049: super (name);
050: }
051:
052: @Override
053: protected void setUp() throws Exception {
054: super .setUp();
055: obj = new GapContent();
056: obj.insertString(0, "This is a test string.");
057: text = new Segment();
058: }
059:
060: /**
061: * Tests that Position IS NOT changed, after an insertion has occured
062: * after this Position.
063: */
064: public void testCreatePositionInsertAfter()
065: throws BadLocationException {
066: Position pos = obj.createPosition(8);
067: obj.insertString(10, "big ");
068: assertEquals(8, pos.getOffset());
069: }
070:
071: /**
072: * Tests that Position IS NOT changed, after an insertion has occured
073: * before this Position.
074: */
075: public void testCreatePositionInsertBefore()
076: throws BadLocationException {
077: Position pos = obj.createPosition(10);
078: obj.insertString(9, " big");
079: assertEquals(14, pos.getOffset());
080: }
081:
082: /**
083: * Tests that Position IS NOT changed, after an insertion has occured
084: * after this Position.
085: */
086: public void testCreatePositionRemoveAfter()
087: throws BadLocationException {
088: Position pos = obj.createPosition(8);
089: obj.remove(10, 5);
090: assertEquals(8, pos.getOffset());
091: }
092:
093: /**
094: * Tests that Position IS changed, after an insertion has occured
095: * before this Position.
096: */
097: public void testCreatePositionRemoveBefore()
098: throws BadLocationException {
099: Position pos = obj.createPosition(15);
100: obj.remove(10, 5);
101: assertEquals(10, pos.getOffset());
102: }
103:
104: public void testCreatePositionInvalid() {
105: try {
106: obj.createPosition(-1);
107: if (BasicSwingTestCase.isHarmony()) {
108: fail("BadLocationException should be thrown");
109: }
110: } catch (BadLocationException e) {
111: }
112: }
113:
114: public void testCreatePositionFarAway() throws BadLocationException {
115: Position pos = obj.createPosition(150);
116: obj.insertString(10, "far away ");
117: assertEquals(159, pos.getOffset());
118: }
119:
120: public void testGetCharsAfterGap() throws BadLocationException {
121: // Move the gap
122: obj.insertString(10, "big ");
123: obj.getChars(19, 8, text);
124: assertEquals("string.\n", text.toString());
125: }
126:
127: public void testGetCharsBeforeGap() throws BadLocationException {
128: // The gap is in the end of the buffer
129: obj.getChars(0, 4, text);
130: assertEquals("This", text.toString());
131: }
132:
133: public void testGetCharsInvalidPosition() {
134: try {
135: obj.getChars(30, 4, text);
136: fail("BadLocationException should be thrown.");
137: } catch (BadLocationException e) {
138: }
139: }
140:
141: public void testGetCharsInvalidLength() {
142: try {
143: obj.getChars(15, 15, text);
144: fail("BadLocationException should be thrown.");
145: } catch (BadLocationException e) {
146: }
147: }
148:
149: public void testGetCharsNegativeLength() {
150: try {
151: obj.getChars(0, -2, new Segment());
152: fail("BadLocationException must be thrown: negative length");
153: } catch (BadLocationException e) {
154: }
155: }
156:
157: public void testGetCharsPartial() throws BadLocationException {
158: // Move the gap
159: obj.insertString(10, "big ");
160: text.setPartialReturn(true);
161: obj.getChars(8, 10, text);
162: assertEquals("a big ", text.toString());
163: }
164:
165: public void testGetCharsWithGap() throws BadLocationException {
166: // Move the gap
167: obj.insertString(10, "big ");
168: obj.getChars(8, 10, text);
169: assertEquals("a big test", text.toString());
170: }
171:
172: public void testGetCharsFullLength() throws BadLocationException {
173: obj.getChars(0, obj.length(), text);
174: assertEquals("This is a test string.\n", text.toString());
175: }
176:
177: public void testGetCharsImpliedChar() throws BadLocationException {
178: obj.getChars(15, 8, text);
179: assertEquals("string.\n", text.toString());
180: }
181:
182: public void testGetStringAfterGap() throws BadLocationException {
183: obj.insertString(10, "big ");
184: String str = obj.getString(14, 4);
185: assertEquals("test", str);
186: }
187:
188: public void testGetStringBeforeGap() throws BadLocationException {
189: obj.insertString(15, "of ");
190: String str = obj.getString(8, 6);
191: assertEquals("a test", str);
192: }
193:
194: public void testGetStringInvalidLength() {
195: try {
196: obj.getString(15, 15);
197: fail("BadLocationException should be thrown.");
198: } catch (BadLocationException e) {
199: }
200: }
201:
202: public void testGetStringNegativeLength() {
203: try {
204: obj.getString(0, -2);
205: fail("BadLocationException must be thrown: negative length");
206: } catch (BadLocationException e) {
207: }
208: }
209:
210: public void testGetStringInvalidPosition() {
211: try {
212: obj.getString(30, 5);
213: fail("BadLocationException should be thrown.");
214: } catch (BadLocationException e) {
215: }
216: }
217:
218: public void testGetStringWithGap() throws BadLocationException {
219: obj.insertString(10, "big ");
220: String str = obj.getString(8, 10);
221: assertEquals("a big test", str);
222: }
223:
224: public void testGetStringFullLength() throws BadLocationException {
225: String str = obj.getString(0, obj.length());
226: assertEquals("This is a test string.\n", str);
227: }
228:
229: public void testGetStringImpliedChar() throws BadLocationException {
230: String str = obj.getString(15, 8);
231: assertEquals("string.\n", str);
232: }
233:
234: public void testInsertStringValid() throws BadLocationException {
235: obj.insertString(10, "big ");
236: assertEquals("a big test", obj.getString(8, 10));
237: }
238:
239: public void testInsertStringInvalid() {
240: try {
241: obj.insertString(30, "text");
242: fail("BadLocationException should be thrown.");
243: } catch (BadLocationException e) {
244: }
245: }
246:
247: public void testLength() {
248: // actual string length is 22,
249: // but there is implied character at the end of the buffer, so we add 1
250: assertEquals(23, obj.length());
251: }
252:
253: public void testRemoveValid() throws BadLocationException {
254: obj.remove(10, 5);
255: assertEquals("a string", obj.getString(8, 8));
256: }
257:
258: /**
259: * Removes a portion of content at its end. shiftGapStartDown method
260: * should be called.
261: */
262: public void testRemoveBackEnd() throws BadLocationException {
263: obj.remove(14, 8);
264: assertEquals("This is a test", obj.getString(0,
265: obj.length() - 1));
266: }
267:
268: /**
269: * Removes a portion of content in the middle. shiftGapStartDown method
270: * should be called.
271: */
272: public void testRemoveBack() throws BadLocationException {
273: // Shift the gap so that it's in the middle
274: obj.remove(8, 2);
275: // Test
276: obj.remove(0, 8);
277: assertEquals("test string.", obj.getString(0, obj.length() - 1));
278: }
279:
280: /**
281: * Tries to remove implicit character at the end of the content.
282: */
283: public void testRemoveImplicit() {
284: try {
285: obj.remove(22, 1);
286: fail("BadLocationException should be thrown");
287: } catch (BadLocationException e) {
288: }
289: }
290:
291: public void testRemoveInvalidLength() {
292: try {
293: obj.remove(15, 15);
294: fail("BadLocationException should be thrown.");
295: } catch (BadLocationException e) {
296: }
297: }
298:
299: public void testRemoveInvalidPosition() {
300: try {
301: obj.remove(30, 15);
302: fail("BadLocationException should be thrown.");
303: } catch (BadLocationException e) {
304: }
305: }
306: }
|