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.BranchElement;
024: import javax.swing.text.AbstractDocument.LeafElement;
025: import javax.swing.text.AbstractDocumentTest.DisAbstractedDocument;
026:
027: /**
028: * Tests AbstractDocument.LeafElement class.
029: *
030: */
031: public class AbstractDocument_LeafElementTest extends
032: BasicSwingTestCase {
033: protected AbstractDocument doc;
034:
035: protected LeafElement leaf1;
036:
037: protected LeafElement leaf2;
038:
039: protected AttributeSet[] as;
040:
041: @Override
042: protected void setUp() throws Exception {
043: super .setUp();
044: StyleContextTest.sc = StyleContext.getDefaultStyleContext();
045: as = new AttributeSet[] { StyleContextTest.addAttribute(1),
046: StyleContextTest.addAttribute(null, 2, 2),
047: StyleContextTest.addAttribute(null, 5, 2) };
048: doc = new DisAbstractedDocument(new GapContent());
049: doc.insertString(0, "0123456789", as[0]);
050: doc.writeLock();
051: BranchElement branch = doc.new BranchElement(null, as[1]);
052: leaf1 = doc.new LeafElement(null, as[2], 0, 3);
053: leaf2 = doc.new LeafElement(branch, as[2], 5, 8);
054: doc.writeUnlock();
055: }
056:
057: public void testGetElement() {
058: assertNull(leaf1.getElement(-1));
059: assertNull(leaf1.getElement(0));
060: assertNull(leaf1.getElement(1));
061: assertNull(leaf1.getElement(2));
062: assertNull(leaf1.getElement(20));
063: }
064:
065: public void testChildren() {
066: assertNull(leaf1.children());
067: assertNull(leaf2.children());
068: }
069:
070: public void testGetName() {
071: assertEquals("content", leaf1.getName());
072: assertEquals("content", leaf2.getName());
073: assertSame(AbstractDocument.ContentElementName, leaf1.getName());
074: }
075:
076: public void testGetElementIndex() {
077: assertEquals(-1, leaf1.getElementIndex(-1));
078: assertEquals(-1, leaf1.getElementIndex(0));
079: assertEquals(-1, leaf1.getElementIndex(1));
080: assertEquals(-1, leaf1.getElementIndex(2));
081: assertEquals(-1, leaf1.getElementIndex(20));
082: }
083:
084: public void testIsLeaf() {
085: assertTrue(leaf1.isLeaf());
086: }
087:
088: public void testGetAllowsChildren() {
089: assertFalse(leaf2.getAllowsChildren());
090: }
091:
092: public void testGetStartOffset() throws BadLocationException {
093: assertEquals(0, leaf1.getStartOffset());
094: assertEquals(5, leaf2.getStartOffset());
095: doc.insertString(2, "insert", as[2]);
096: assertEquals(0, leaf1.getStartOffset());
097: assertEquals(11, leaf2.getStartOffset());
098: }
099:
100: public void testGetEndOffset() throws BadLocationException {
101: assertEquals(3, leaf1.getEndOffset());
102: assertEquals(8, leaf2.getEndOffset());
103: doc.insertString(4, "insert", as[2]);
104: assertEquals(3, leaf1.getEndOffset());
105: assertEquals(14, leaf2.getEndOffset());
106: }
107:
108: public void testGetElementCount() {
109: assertEquals(0, leaf1.getElementCount());
110: }
111:
112: public void testLeafElement() {
113: doc.writeLock();
114: AbstractDocument.LeafElement leaf = doc.new LeafElement(leaf1,
115: as[2], 3, 9);
116: assertSame(leaf1, leaf.getParent());
117: assertSame(leaf1, leaf.getParentElement());
118: assertSame(leaf, leaf.getAttributes());
119: assertEquals(as[2].getAttributeCount(), leaf
120: .getAttributeCount());
121: assertEquals(3, leaf.getStartOffset());
122: assertEquals(9, leaf.getEndOffset());
123: int[] start = { -1, 3, 3, 3 }; // start offset
124: int[] expStart = { 0, 3, 3, 3 }; // expectations for start offset
125: int[] end = { 9, -1, 1, 20 }; // end offset
126: int[] expEnd = { 9, 0, 1, 20 }; // expectations for end offset
127: int[] intEnd = { 9, 3, 3, 20 }; // expectations for our end offset
128: for (int i = 0; i < start.length; i++) {
129: leaf = doc.new LeafElement(null, null, start[i], end[i]);
130: assertEquals("Start (" + i + ")", expStart[i], leaf
131: .getStartOffset());
132: assertEquals("End (" + i + ")", BasicSwingTestCase
133: .isHarmony() ? intEnd[i] : expEnd[i], leaf
134: .getEndOffset());
135: }
136: doc.writeUnlock();
137: }
138:
139: public void testToString() {
140: assertEquals("LeafElement(content) 0,3\n", leaf1.toString());
141: assertEquals("LeafElement(content) 5,8\n", leaf2.toString());
142: }
143: }
|