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 Alexander T. Simbirtsev
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import javax.swing.BasicSwingTestCase;
023:
024: public class ElementIteratorTest extends BasicSwingTestCase {
025: protected ElementIterator iterator;
026:
027: protected AbstractDocument doc;
028:
029: protected Element root;
030:
031: protected Element branch1;
032:
033: protected Element branch2;
034:
035: protected Element leaf1;
036:
037: protected Element leaf2;
038:
039: protected Element leaf3;
040:
041: protected Element leaf4;
042:
043: protected Element leaf5;
044:
045: protected Element leaf6;
046:
047: @Override
048: protected void setUp() throws Exception {
049: super .setUp();
050: doc = new DefaultStyledDocument();
051: MutableAttributeSet attr1 = new SimpleAttributeSet();
052: MutableAttributeSet attr2 = new SimpleAttributeSet();
053: StyleConstants.setBold(attr1, true);
054: StyleConstants.setItalic(attr2, true);
055: doc.insertString(0, "00", attr1);
056: doc.insertString(2, "XX", attr2);
057: doc.insertString(4, "\n00", attr1);
058: doc.insertString(7, "XX", attr2);
059: root = doc.getRootElements()[0];
060: branch1 = root.getElement(0);
061: leaf1 = branch1.getElement(0);
062: leaf2 = branch1.getElement(1);
063: leaf3 = branch1.getElement(2);
064: branch2 = root.getElement(1);
065: leaf4 = branch2.getElement(0);
066: leaf5 = branch2.getElement(1);
067: leaf6 = branch2.getElement(2);
068: iterator = new ElementIterator(doc);
069: }
070:
071: @Override
072: protected void tearDown() throws Exception {
073: super .tearDown();
074: }
075:
076: /*
077: * Test method for 'javax.swing.text.ElementIterator.ElementIterator(Document)'
078: */
079: public void testElementIteratorDocument() {
080: assertSame(root, iterator.current());
081: assertNull(iterator.previous());
082: iterator.next();
083: assertSame(root, iterator.first());
084: assertNull(iterator.previous());
085: assertSame(root, iterator.current());
086:
087: try { // Regression test for HARMONY-1811
088: new ElementIterator((Document) null);
089: fail("NullPointerException should have been thrown");
090: } catch (NullPointerException e) {
091: // Expected
092: }
093: }
094:
095: /*
096: * Test method for 'javax.swing.text.ElementIterator.ElementIterator(Element)'
097: */
098: public void testElementIteratorElement() {
099: iterator = new ElementIterator(branch1);
100: assertSame(branch1, iterator.current());
101: assertSame(leaf1, iterator.next());
102: assertSame(leaf2, iterator.next());
103: assertSame(leaf3, iterator.next());
104: assertNull(iterator.next());
105: assertSame(branch1, iterator.first());
106: iterator = new ElementIterator(leaf1);
107: assertSame(leaf1, iterator.current());
108: assertNull(iterator.next());
109: assertNull(iterator.previous());
110: iterator = new ElementIterator(leaf1);
111: assertSame(leaf1, iterator.next());
112: assertNull(iterator.current());
113: assertNull(iterator.next());
114: assertNull(iterator.previous());
115: }
116:
117: /*
118: * Test method for 'javax.swing.text.ElementIterator.first()'
119: */
120: public void testFirst() {
121: Element first = iterator.current();
122: assertNotSame(first, iterator.next());
123: assertNotSame(first, iterator.next());
124: assertNotSame(first, iterator.next());
125: assertSame(first, iterator.first());
126: }
127:
128: /*
129: * Test method for 'javax.swing.text.ElementIterator.current()'
130: */
131: public void testCurrent() throws BadLocationException {
132: assertSame(doc.getRootElements()[0], iterator.current());
133: assertSame(iterator.current(), iterator.current());
134: iterator.next();
135: assertSame(iterator.current(), iterator.current());
136: }
137:
138: /*
139: * Test method for 'javax.swing.text.ElementIterator.next()'
140: */
141: public void testNext() {
142: assertSame(root, iterator.next());
143: assertSame(branch1, iterator.next());
144: assertSame(leaf1, iterator.next());
145: assertSame(leaf2, iterator.next());
146: assertSame(leaf3, iterator.next());
147: assertSame(branch2, iterator.next());
148: assertSame(leaf4, iterator.next());
149: assertSame(leaf5, iterator.next());
150: assertSame(leaf6, iterator.next());
151: assertNull(iterator.next());
152: }
153:
154: /*
155: * Test method for 'javax.swing.text.ElementIterator.previous()'
156: */
157: public void testPrevious() {
158: assertNull(iterator.previous());
159: assertSame(root, iterator.next());
160: assertNull(iterator.previous());
161: assertSame(branch1, iterator.next());
162: assertSame(root, iterator.previous());
163: assertSame(leaf1, iterator.next());
164: assertSame(branch1, iterator.previous());
165: assertSame(leaf2, iterator.next());
166: assertSame(leaf1, iterator.previous());
167: assertSame(leaf3, iterator.next());
168: assertSame(leaf2, iterator.previous());
169: assertSame(branch2, iterator.next());
170: assertSame(leaf3, iterator.previous());
171: assertSame(leaf4, iterator.next());
172: assertSame(branch2, iterator.previous());
173: assertSame(leaf5, iterator.next());
174: assertSame(leaf4, iterator.previous());
175: assertSame(leaf6, iterator.next());
176: assertSame(leaf5, iterator.previous());
177: assertNull(iterator.next());
178: assertNull(iterator.previous());
179: }
180:
181: /*
182: * Test method for 'javax.swing.text.ElementIterator.depth()'
183: */
184: public void testDepth() throws BadLocationException {
185: assertEquals(0, iterator.depth());
186: iterator.current();
187: assertEquals(1, iterator.depth());
188: iterator.next();
189: assertEquals(2, iterator.depth());
190: iterator.next();
191: assertEquals(3, iterator.depth());
192: iterator.next();
193: assertEquals(3, iterator.depth());
194: iterator.next();
195: assertEquals(3, iterator.depth());
196: iterator.next();
197: assertEquals(2, iterator.depth());
198: iterator.next();
199: assertEquals(3, iterator.depth());
200: iterator.next();
201: assertEquals(3, iterator.depth());
202: iterator.next();
203: assertEquals(3, iterator.depth());
204: iterator.next();
205: assertEquals(0, iterator.depth());
206: iterator.first();
207: assertEquals(1, iterator.depth());
208: iterator = new ElementIterator(doc.getBidiRootElement());
209: assertEquals(0, iterator.depth());
210: iterator.next();
211: assertEquals(1, iterator.depth());
212: iterator.next();
213: assertEquals(2, iterator.depth());
214: iterator.next();
215: assertEquals(0, iterator.depth());
216: }
217:
218: /*
219: * Test method for 'javax.swing.text.ElementIterator.clone()'
220: */
221: public void testClone() {
222: iterator = new ElementIterator(branch2);
223: iterator.current();
224: iterator.next();
225: ElementIterator cloned = (ElementIterator) iterator.clone();
226: assertSame(leaf4, cloned.current());
227: assertSame(branch2, cloned.previous());
228: assertSame(leaf5, cloned.next());
229: assertSame(leaf6, cloned.next());
230: assertNull(cloned.next());
231: assertSame(leaf4, iterator.current());
232: assertSame(branch2, cloned.first());
233: }
234: }
|