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.ArrayList;
023: import java.util.List;
024: import junit.framework.TestCase;
025:
026: /**
027: * Tests DefaultStyledDocument class. This test checks that
028: * ElementBuffer calls AbstractDocument.create{Branch,Leaf}Element methods
029: * to actually create elements.
030: *
031: * Methods to modify document structure are: insertString, remove,
032: * set{Character,Paragraph}Attributes, setLogicalStyle.
033: *
034: */
035: public class DefaultStyledDocument_CreateElementTest extends TestCase {
036: private static class LeafChild {
037: public final Element parent;
038:
039: public final AttributeSet attrs;
040:
041: public final int start;
042:
043: public final int end;
044:
045: public final Element child;
046:
047: public LeafChild(Element parent, AttributeSet attrs, int start,
048: int end, Element child) {
049: this .parent = parent;
050: this .attrs = attrs;
051: this .end = end;
052: this .start = start;
053: this .child = child;
054: }
055:
056: public void assertExpected(Element parent, AttributeSet attrs,
057: int start, int end, Element child) {
058: assertSame(parent, this .parent);
059: assertEquals(attrs, new SimpleAttributeSet(this .attrs));
060: assertEquals(start, this .start);
061: assertEquals(end, this .end);
062: assertSame(child, this .child);
063: }
064: }
065:
066: private static class BranchChild {
067: public final Element parent;
068:
069: public final AttributeSet attrs;
070:
071: public final Element child;
072:
073: public BranchChild(Element parent, AttributeSet attrs,
074: Element child) {
075: this .parent = parent;
076: this .attrs = attrs;
077: this .child = child;
078: }
079:
080: public void assertExpected(Element parent, AttributeSet attrs,
081: Element child) {
082: assertSame(parent, this .parent);
083: assertEquals(attrs, new SimpleAttributeSet(this .attrs));
084: assertSame(child, this .child);
085: }
086: }
087:
088: private DefaultStyledDocument doc;
089:
090: private Element root;
091:
092: private List<BranchChild> branches;
093:
094: private List<LeafChild> leaves;
095:
096: @Override
097: protected void setUp() throws Exception {
098: super .setUp();
099: branches = new ArrayList<BranchChild>();
100: leaves = new ArrayList<LeafChild>();
101: doc = new DefaultStyledDocument() {
102: private static final long serialVersionUID = 1L;
103:
104: @Override
105: protected Element createBranchElement(Element parent,
106: AttributeSet as) {
107: Element child = super .createBranchElement(parent, as);
108: branches.add(new BranchChild(parent, as, child));
109: return child;
110: }
111:
112: @Override
113: protected Element createLeafElement(Element parent,
114: AttributeSet as, int start, int end) {
115: Element child = super .createLeafElement(parent, as,
116: start, end);
117: leaves
118: .add(new LeafChild(parent, as, start, end,
119: child));
120: return child;
121: }
122: };
123: root = doc.getDefaultRootElement();
124: doc.insertString(0, "012abc\ntwo\nthree", null);
125: branches.clear();
126: leaves.clear();
127: }
128:
129: /*
130: * DefaultStyledDocument.setCharacterAttributes()
131: */
132: public void testSetCharacterAttributes() {
133: MutableAttributeSet attrs = new SimpleAttributeSet();
134: StyleConstants.setBold(attrs, true);
135: doc.setCharacterAttributes(2, 2, attrs, false);
136: assertEquals(0, branches.size());
137: assertEquals(3, leaves.size());
138: final Element parent = root.getElement(0);
139: leaves.get(0).assertExpected(parent, SimpleAttributeSet.EMPTY,
140: 0, 2, parent.getElement(0));
141: assertEquals(SimpleAttributeSet.EMPTY, parent.getElement(0)
142: .getAttributes());
143: leaves.get(1).assertExpected(parent, SimpleAttributeSet.EMPTY,
144: 2, 4, parent.getElement(1));
145: assertEquals(attrs, parent.getElement(1).getAttributes());
146: leaves.get(2).assertExpected(parent, SimpleAttributeSet.EMPTY,
147: 4, 7, parent.getElement(2));
148: assertEquals(SimpleAttributeSet.EMPTY, parent.getElement(2)
149: .getAttributes());
150: }
151:
152: /*
153: * DefaultStyledDocument.setLogicalStyle()
154: */
155: public void testSetLogicalStyle() {
156: Style style = doc.addStyle("aStyle", doc
157: .getStyle(StyleContext.DEFAULT_STYLE));
158: doc.setLogicalStyle(3, style);
159: assertEquals(0, branches.size());
160: assertEquals(0, leaves.size());
161: }
162:
163: /*
164: * DefaultStyledDocument.setParagraphAttributes()
165: */
166: public void testSetParagraphAttributes() {
167: MutableAttributeSet attrs = new SimpleAttributeSet();
168: StyleConstants.setBold(attrs, true);
169: doc.setParagraphAttributes(3, 6, attrs, false);
170: assertEquals(0, branches.size());
171: assertEquals(0, leaves.size());
172: }
173:
174: /*
175: * AbstractDocument.insertString(int, String, AttributeSet)
176: */
177: public void testInsertString() throws BadLocationException {
178: doc.insertString(7, "^^^\n", null);
179: assertEquals(1, branches.size());
180: assertEquals(2, leaves.size());
181: MutableAttributeSet logStyle = new SimpleAttributeSet();
182: logStyle.setResolveParent(doc
183: .getStyle(StyleContext.DEFAULT_STYLE));
184: branches.get(0).assertExpected(root, logStyle,
185: root.getElement(1));
186: leaves.get(0).assertExpected(root.getElement(0),
187: SimpleAttributeSet.EMPTY, 0, 7,
188: root.getElement(0).getElement(0));
189: leaves.get(1).assertExpected(root.getElement(1),
190: SimpleAttributeSet.EMPTY, 7, 11,
191: root.getElement(1).getElement(0));
192: }
193:
194: /*
195: * AbstractDocument.remove(int, int)
196: */
197: public void testRemove() throws BadLocationException {
198: doc.remove(3, 4);
199: assertEquals(1, branches.size());
200: assertEquals(2, leaves.size());
201: MutableAttributeSet logStyle = new SimpleAttributeSet();
202: logStyle.setResolveParent(doc
203: .getStyle(StyleContext.DEFAULT_STYLE));
204: branches.get(0).assertExpected(root, logStyle,
205: root.getElement(0));
206: leaves.get(0).assertExpected(root.getElement(0),
207: SimpleAttributeSet.EMPTY, 0, 7,
208: root.getElement(0).getElement(0));
209: leaves.get(1).assertExpected(root.getElement(0),
210: SimpleAttributeSet.EMPTY, 7, 11,
211: root.getElement(0).getElement(1));
212: }
213: }
|