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 java.text.CharacterIterator;
020: import junit.framework.TestCase;
021:
022: public class SegmentTest extends TestCase {
023: private Segment s;
024:
025: private char[] arr = new char[] { 'a', 'b', 'c', 'd', 'e', 'f',
026: 'g', 'h', 'i', 'j', 'k' };
027:
028: @Override
029: protected void setUp() {
030: s = new Segment(arr, 2, 6);
031: }
032:
033: public void testClone() {
034: Segment clone = (Segment) s.clone();
035: assertEquals(s.array, clone.array);
036: assertEquals(s.count, clone.count);
037: assertEquals(s.offset, clone.offset);
038: assertEquals(s.getIndex(), clone.getIndex());
039: assertEquals(s.getClass(), clone.getClass());
040: assertEquals(s.isPartialReturn(), clone.isPartialReturn());
041: assertNotSame(s, clone);
042: }
043:
044: public void testToString() {
045: assertEquals("cdefgh", s.toString());
046: }
047:
048: public void testToStringEmpty() {
049: s = new Segment();
050: assertNull(s.array);
051: assertEquals("", s.toString());
052: }
053:
054: public void testCurrent() {
055: assertEquals(arr[0], s.current());
056: assertEquals(s.array[s.getIndex()], s.current());
057: s.setIndex(2);
058: assertEquals(s.array[2], s.current());
059: s.setIndex(s.getEndIndex());
060: assertEquals(CharacterIterator.DONE, s.current());
061: s = new Segment();
062: assertEquals(CharacterIterator.DONE, s.current());
063: }
064:
065: public void testFirst() {
066: assertEquals(arr[2], s.first());
067: assertEquals(s.array[s.getBeginIndex()], s.first());
068: assertEquals(s.getBeginIndex(), s.getIndex());
069: s = new Segment();
070: assertEquals(CharacterIterator.DONE, s.first());
071: assertEquals(s.getBeginIndex(), s.getIndex());
072: }
073:
074: public void testLast() {
075: assertEquals(arr[7], s.last());
076: assertEquals(s.array[s.getEndIndex() - 1], s.last());
077: assertEquals(s.getEndIndex() - 1, s.getIndex());
078: s = new Segment();
079: assertEquals(CharacterIterator.DONE, s.last());
080: assertEquals(s.getEndIndex(), s.getIndex());
081: s = new Segment(arr, 2, 0);
082: assertEquals(CharacterIterator.DONE, s.last());
083: assertEquals(s.getEndIndex(), s.getIndex());
084: }
085:
086: public void testNext() {
087: assertEquals(arr[1], s.next());
088: assertEquals(1, s.getIndex());
089: s.setIndex(4);
090: assertEquals(arr[5], s.next());
091: s.setIndex(s.getEndIndex());
092: assertEquals(CharacterIterator.DONE, s.next());
093: s.setIndex(s.getEndIndex() - 1);
094: assertEquals(CharacterIterator.DONE, s.next());
095: s = new Segment();
096: assertEquals(CharacterIterator.DONE, s.next());
097: }
098:
099: public void testPrevious() {
100: s.setIndex(2);
101: assertEquals(CharacterIterator.DONE, s.previous());
102: s.setIndex(5);
103: assertEquals(arr[4], s.previous());
104: s = new Segment();
105: assertEquals(CharacterIterator.DONE, s.previous());
106: }
107:
108: public void testGetBeginIndex() {
109: assertEquals(2, s.getBeginIndex());
110: s = new Segment();
111: assertEquals(0, s.getBeginIndex());
112: }
113:
114: public void testGetEndIndex() {
115: assertEquals(8, s.getEndIndex());
116: s = new Segment();
117: assertEquals(0, s.getEndIndex());
118: }
119:
120: public void testGetIndex() {
121: assertEquals(0, s.getIndex());
122: s.setIndex(5);
123: assertEquals(5, s.getIndex());
124: s = new Segment();
125: assertEquals(0, s.getIndex());
126: }
127:
128: /*
129: * void Segment()
130: */
131: public void testSegment() {
132: s = new Segment();
133: assertNull(s.array);
134: assertEquals(0, s.count);
135: assertEquals(0, s.offset);
136: }
137:
138: public void testIsPartialReturn() {
139: // Default state must be FALSE
140: assertFalse(s.isPartialReturn());
141: }
142:
143: public void testSetIndex() {
144: try {
145: s.setIndex(-1);
146: fail("IllegalArgumentException was expected");
147: } catch (IllegalArgumentException e) {
148: }
149: assertEquals(0, s.getIndex());
150: try {
151: s.setIndex(s.getEndIndex() + 1);
152: fail("IllegalArgumentException was expected");
153: } catch (IllegalArgumentException e) {
154: }
155: assertEquals(0, s.getIndex());
156: assertEquals(CharacterIterator.DONE, s
157: .setIndex(s.getEndIndex()));
158: assertEquals(s.getEndIndex(), s.getIndex());
159: assertEquals(arr[4], s.setIndex(4));
160: assertEquals(4, s.getIndex());
161: }
162:
163: public void testSetPartialReturn() {
164: s.setPartialReturn(true);
165: assertTrue(s.isPartialReturn());
166: }
167:
168: /*
169: * void Segment(char[], int, int)
170: */
171: public void testSegmentcharArrayintint() {
172: assertEquals(arr, s.array);
173: assertEquals(2, s.offset);
174: assertEquals(6, s.count);
175: }
176: }
|