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 java.lang.ref.Reference;
023: import java.lang.ref.ReferenceQueue;
024: import java.lang.ref.WeakReference;
025: import java.lang.reflect.Field;
026: import java.util.List;
027: import java.util.Vector;
028: import javax.swing.BasicSwingTestCase;
029: import javax.swing.undo.UndoableEdit;
030:
031: public class GapContentTest extends AbstractDocument_ContentTest {
032: protected AbstractDocument.Content content;
033:
034: @Override
035: protected void setUp() throws Exception {
036: super .setUp();
037: obj = content = new GapContent(30);
038: obj.insertString(0, "This is a test string.");
039: }
040:
041: public void testGetPositionsInRangeVector()
042: throws BadLocationException {
043: Vector<Object> v = new Vector<Object>();
044: v.add(new Object());
045: v.add(new Object());
046: content.createPosition(0);
047: content.createPosition(1);
048: content.createPosition(2);
049: ((GapContent) content).getPositionsInRange(v, 0, 3);
050: if (BasicSwingTestCase.isHarmony()) {
051: // Position at offset 0 WILL NOT be included
052: assertEquals(4, v.size());
053: } else {
054: // Position at offset 0 WILL be included
055: assertEquals(5, v.size());
056: }
057: }
058:
059: public void testGetPositionsInRange() throws BadLocationException {
060: Vector<Position> pos = new Vector<Position>();
061: for (int i = 0; i < content.length(); i += 2) {
062: Position p = content.createPosition(i);
063: if (i >= 3 && i <= 3 + 9) {
064: pos.add(p);
065: }
066: }
067: Vector<?> v = ((GapContent) content).getPositionsInRange(null,
068: 3, 9);
069: assertEquals(pos.size(), v.size());
070: int[] offsets = new int[v.size()];
071: for (int i = 0; i < pos.size(); i++) {
072: offsets[i] = pos.get(i).getOffset();
073: }
074: UndoableEdit ue = content.remove(0, 9);
075: ue.undo();
076: for (int i = 0; i < pos.size(); i++) {
077: assertEquals(offsets[i], pos.get(i).getOffset());
078: }
079: }
080:
081: public void testUpdatePositions() throws BadLocationException {
082: GapContent cont1 = new GapContent();
083: final Vector<Position> pos = new Vector<Position>();
084: final int posSize = 5;
085: final int buffsize = 10;
086: cont1 = new GapContent() {
087: private static final long serialVersionUID = 1L;
088:
089: @Override
090: public UndoableEdit remove(int where, int len)
091: throws BadLocationException {
092: UndoableEdit u = super .remove(where, len);
093: return u;
094: }
095:
096: @Override
097: public UndoableEdit insertString(int offset, String str)
098: throws BadLocationException {
099: UndoableEdit u = super .insertString(offset, str);
100: return u;
101: }
102:
103: @SuppressWarnings("unchecked")
104: @Override
105: protected void updateUndoPositions(Vector vector,
106: int offset, int len) {
107: super .updateUndoPositions(vector, 1, 2);
108: }
109: };
110: for (int i = 0; i < posSize; i++) {
111: pos.add(cont1.createPosition(i));
112: }
113: StringBuffer f = new StringBuffer();
114: for (int i = 0; i < buffsize; i++) {
115: f.append("a");
116: }
117: cont1.insertString(0, f.toString());
118: cont1.remove(0, 10).undo();
119: }
120:
121: /**
122: * Tests that the position at offset of offset + len is included in
123: * the returned vector.
124: */
125: public void testGetPositionsInRangeEnd()
126: throws BadLocationException {
127: content.createPosition(10);
128: Vector<?> v = ((GapContent) content).getPositionsInRange(null,
129: 0, 10);
130: assertEquals(1, v.size());
131: }
132:
133: public void testPositionGC() throws BadLocationException {
134: Vector<WeakReference<Position>> pos = new Vector<WeakReference<Position>>(
135: 10);
136: ReferenceQueue<Position> rq = new ReferenceQueue<Position>();
137: for (int i = 0; i < content.length(); i += 2) {
138: pos.add(new WeakReference<Position>(content
139: .createPosition(i), rq));
140: }
141: int count = 0;
142: int i;
143: for (i = 0; i < 100; i++) {
144: System.gc();
145: Reference<?> r;
146: if ((r = rq.poll()) != null) {
147: pos.remove(r);
148: count++;
149: if (pos.size() == 0) {
150: return;
151: }
152: }
153: }
154: fail("Not all Position objects are removed (" + pos.size()
155: + "/" + count + ").");
156: }
157:
158: private void isContentArraySame(final boolean expected) {
159: if (BasicSwingTestCase.isHarmony()) {
160: if (expected) {
161: assertSame(((GapContent) obj).getArray(), text.array);
162: } else {
163: assertNotSame(((GapContent) obj).getArray(), text.array);
164: }
165: }
166: }
167:
168: @Override
169: public void testGetCharsAfterGap() throws BadLocationException {
170: super .testGetCharsAfterGap();
171: isContentArraySame(true);
172: }
173:
174: public void testGetCharsAfterGapNoImplied()
175: throws BadLocationException {
176: // Move the gap
177: obj.insertString(10, "big ");
178: // Don't include the implied char
179: obj.getChars(19, 7, text);
180: assertEquals("string.", text.toString());
181: isContentArraySame(true);
182: }
183:
184: @Override
185: public void testGetCharsBeforeGap() throws BadLocationException {
186: super .testGetCharsBeforeGap();
187: isContentArraySame(true);
188: }
189:
190: @Override
191: public void testGetCharsFullLength() throws BadLocationException {
192: super .testGetCharsFullLength();
193: isContentArraySame(false);
194: }
195:
196: public void testGetCharsFullActualLength()
197: throws BadLocationException {
198: obj.getChars(0, obj.length() - 1, text);
199: assertEquals("This is a test string.", text.toString());
200: isContentArraySame(true);
201: }
202:
203: @Override
204: public void testGetCharsImpliedChar() throws BadLocationException {
205: super .testGetCharsImpliedChar();
206: isContentArraySame(false);
207: }
208:
209: public void testGetCharsImpliedCharPartial()
210: throws BadLocationException {
211: obj = content = new GapContent();
212: assertEquals(1, content.length());
213: text.setPartialReturn(false);
214: content.getChars(0, 1, text);
215: assertEquals("\n", text.toString());
216: assertEquals(((GapContent) content).getArrayLength(),
217: text.array.length);
218: text.setPartialReturn(true);
219: content.getChars(0, 1, text);
220: assertEquals("\n", text.toString());
221: assertEquals(((GapContent) content).getArrayLength(),
222: text.array.length);
223: }
224:
225: @Override
226: public void testGetCharsPartial() throws BadLocationException {
227: super .testGetCharsPartial();
228: isContentArraySame(true);
229: }
230:
231: @Override
232: public void testGetCharsWithGap() throws BadLocationException {
233: super .testGetCharsWithGap();
234: isContentArraySame(false);
235: }
236:
237: // Regression for HARMONY-2566
238: public void testGetCharsMaxInteger() {
239: try {
240: content.getChars(1, Integer.MAX_VALUE, null);
241: fail("BadLocationException is expected");
242: } catch (BadLocationException e) {
243: }
244: }
245:
246: public void testGetCharsNullSegment() throws BadLocationException {
247: try {
248: content.getChars(1, 1, null);
249: fail("NullPointerException is expected");
250: } catch (NullPointerException e) {
251: }
252: }
253:
254: public void testCreatePositionBeforeUndo()
255: throws BadLocationException {
256: UndoableEdit ue = content.remove(3, 8);
257: Position pos = content.createPosition(3);
258: assertEquals(3, pos.getOffset());
259: ue.undo();
260: assertEquals(11, pos.getOffset());
261: ue.redo();
262: assertEquals(3, pos.getOffset());
263: }
264:
265: public void testCreatePositionAfterUndone()
266: throws BadLocationException {
267: UndoableEdit ue = content.remove(3, 8);
268: ue.undo();
269: Position pos = content.createPosition(5);
270: assertEquals(5, pos.getOffset());
271: ue.redo();
272: assertEquals(3, pos.getOffset());
273: ue.undo();
274: assertEquals(5, pos.getOffset());
275: }
276:
277: public void testCreatePositionAfterInsert()
278: throws BadLocationException {
279: UndoableEdit ue = content.insertString(10, "big ");
280: Position pos = content.createPosition(12);
281: assertEquals(12, pos.getOffset());
282: ue.undo();
283: assertEquals(10, pos.getOffset());
284: ue.redo();
285: assertEquals(12, pos.getOffset());
286: }
287:
288: public static int getIndex(final Position pos) {
289: try {
290: Field f = pos.getClass().getDeclaredField("index");
291: f.setAccessible(true);
292: return f.getInt(pos);
293: } catch (IllegalAccessException e) {
294: fail(e.getMessage());
295: } catch (NoSuchFieldException e) {
296: fail(e.getMessage());
297: }
298: return -1000;
299: }
300:
301: public static List<?> getPositionList(final GapContent content) {
302: try {
303: Field f = content.getClass().getDeclaredField(
304: "gapContentPositions");
305: f.setAccessible(true);
306: ContentPositions gapContentPositions = (ContentPositions) f
307: .get(content);
308: f = gapContentPositions.getClass().getSuperclass()
309: .getDeclaredField("positionList");
310: f.setAccessible(true);
311: return (List<?>) (f.get(gapContentPositions));
312: } catch (IllegalAccessException e) {
313: fail(e.getMessage());
314: } catch (NoSuchFieldException e) {
315: fail(e.getMessage());
316: }
317: return null;
318: }
319: }
|