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.util.List;
023: import javax.swing.BasicSwingTestCase;
024:
025: /**
026: * Tests some internal functionality of GapContent but not its methods.
027: *
028: */
029: public class GapContent_InternalTest extends BasicSwingTestCase {
030: protected GapContent content;
031:
032: @Override
033: protected void setUp() throws Exception {
034: content = new GapContent(30);
035: content.insertString(0, "This is a test string.");
036: }
037:
038: public void testBufferExpansion() throws BadLocationException {
039: if (!isHarmony()) {
040: return;
041: }
042: content = new GapContent();
043: int size = content.getArrayLength();
044: char c = '0';
045: while (size == content.getArrayLength()) {
046: content.insertString(content.getGapStart(), new String(
047: new char[] { c }));
048: c++;
049: }
050: assertEquals("012345678\n", content.getString(0, content
051: .length()));
052: }
053:
054: public void testInsertPosition() throws BadLocationException {
055: if (!isHarmony()) {
056: return;
057: }
058: content.shiftGap(5);
059: content.createPosition(10);
060: content.createPosition(4);
061: content.createPosition(5);
062: content.createPosition(15);
063: content.createPosition(2);
064: content.createPosition(23);
065: int[] positions = { 2, 4, 12, 17, 22, 30 };
066: int[] offsets = { 2, 4, 5, 10, 15, 23 };
067: List<?> posList = getPositionList(content);
068: for (int i = 0; i < posList.size(); i++) {
069: Position p = (Position) posList.get(i);
070: assertEquals("Indexes are different @ " + i, positions[i],
071: getIndex(p));
072: assertEquals("Offsets are different @ " + i, offsets[i], p
073: .getOffset());
074: }
075: }
076:
077: /**
078: * Test that positionList is sorted.
079: * @throws BadLocationException
080: */
081: public void testPositionSort() throws BadLocationException {
082: if (!isHarmony()) {
083: return;
084: }
085: GapContent g = new GapContent();
086: g.insertString(0, "123\n");
087: int[] pos = { 0, 0, 0, 5, 5, 4 };
088: for (int i = 0; i < pos.length; i++) {
089: g.createPosition(pos[i]);
090: }
091: List<?> posList = getPositionList(g);
092: // Check all the positions are sorted
093: Position dmL;
094: Position dmR;
095: for (int i = 0; i < posList.size() - 1; i++) {
096: dmL = (Position) posList.get(i);
097: dmR = (Position) posList.get(i + 1);
098: assertFalse(getIndex(dmL) > getIndex(dmR));
099: }
100: }
101:
102: public void testPositionGC() throws BadLocationException {
103: if (!isHarmony()) {
104: return;
105: }
106: final Position[] pos = new Position[5];
107: for (int i = 0; i < 10; i++) {
108: Position p = content.createPosition(i * 2);
109: if (i < 5) {
110: pos[i] = p;
111: }
112: }
113: assertEquals(10, getPositionList(content).size());
114: for (int i = 0; i < 3; i++) {
115: System.gc();
116: }
117: // if this code is omitted the saved objects are
118: // considered to be unreachable, so the garbage
119: // collector deletes refences to them
120: for (int i = 0; i < pos.length; i++) {
121: pos[i].getOffset();
122: }
123: // Create another position object. This will implicitly
124: // remove all unreachable positions
125: content.createPosition(20);
126: assertEquals(6, getPositionList(content).size());
127: }
128:
129: private static List<?> getPositionList(final GapContent content) {
130: return GapContentTest.getPositionList(content);
131: }
132:
133: private static int getIndex(final Position pos) {
134: return GapContentTest.getIndex(pos);
135: }
136: }
|