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.text.DefaultStyledDocument.ElementBuffer;
023: import junit.framework.TestCase;
024:
025: /**
026: * Tests DefaultStyledDocument.ElementBuffer class. This test-case covers
027: * only simple methods of ElementBuffer class: ElementBuffer,
028: * clone(Element, Element), getRootElement.
029: *
030: */
031: public class DefaultStyledDocument_ElementBufferTest extends TestCase {
032: private DefaultStyledDocument doc;
033:
034: private ElementBuffer buf;
035:
036: @Override
037: protected void setUp() throws Exception {
038: super .setUp();
039: doc = new DefStyledDoc_Helpers.DefStyledDocWithLogging();
040: final Element root = doc.getDefaultRootElement();
041: buf = new DefStyledDoc_Helpers.ElementBufferWithLogging(doc,
042: root);
043: doc.buffer = buf;
044: }
045:
046: public void testElementBuffer() throws BadLocationException {
047: Element leaf = doc.new LeafElement(null, null, 0, 1);
048: buf = doc.new ElementBuffer(leaf);
049: assertSame(leaf, buf.getRootElement());
050: }
051:
052: /*
053: * Element clone(Element, Element)
054: *
055: * Clones a LeafElement.
056: */
057: public void testCloneLeaf() throws BadLocationException {
058: doc.writeLock();
059: try {
060: MutableAttributeSet attrs = new SimpleAttributeSet();
061: StyleConstants.setBold(attrs, true);
062: attrs.addAttribute(StyleConstants.Bold, Boolean.TRUE);
063: final AttributeSet bold = attrs.copyAttributes();
064: doc.insertString(0, "one\n", bold);
065: attrs = new SimpleAttributeSet();
066: StyleConstants.setAlignment(attrs, 0);
067: doc.insertString(doc.getLength(), "two\nthree", attrs);
068: final Element root = doc.getDefaultRootElement();
069: Element par1 = root.getElement(0);
070: Element par2 = root.getElement(1);
071: Element line = par1.getElement(0);
072: Element cloned = buf.clone(par2, line);
073: assertNotSame(line, cloned);
074: assertEquals(line.getStartOffset(), cloned.getStartOffset());
075: assertEquals(line.getEndOffset(), cloned.getEndOffset());
076: assertEquals(bold, cloned.getAttributes());
077: assertSame(par1, line.getParentElement());
078: assertSame(par2, cloned.getParentElement());
079: } finally {
080: doc.writeUnlock();
081: }
082: }
083:
084: /*
085: * Element clone(Element, Element)
086: *
087: * Clones a BranchElement.
088: */
089: public void testCloneBranch() throws BadLocationException {
090: doc.writeLock();
091: try {
092: MutableAttributeSet attrs = new SimpleAttributeSet();
093: StyleConstants.setBold(attrs, true);
094: final AttributeSet bold = attrs.copyAttributes();
095: doc.insertString(0, "one\n", bold);
096: attrs = new SimpleAttributeSet();
097: StyleConstants.setAlignment(attrs, 0);
098: doc.insertString(doc.getLength(), "two\nthree", attrs);
099: attrs = new SimpleAttributeSet();
100: StyleConstants.setFontSize(attrs, 25);
101: doc.setParagraphAttributes(0, 1, attrs, false);
102: final AttributeSet fontSize = attrs.copyAttributes();
103: final Element root = doc.getDefaultRootElement();
104: final Element par1 = root.getElement(0);
105: final Element par2 = root.getElement(1);
106: final Element cloned = buf.clone(par2, par1);
107: assertNotSame(par1, cloned);
108: assertEquals(par1.getStartOffset(), cloned.getStartOffset());
109: assertEquals(par1.getEndOffset(), cloned.getEndOffset());
110: final AttributeSet clonedAttrs = cloned.getAttributes();
111: assertTrue(clonedAttrs.containsAttributes(fontSize));
112: assertTrue(clonedAttrs
113: .isDefined(AttributeSet.ResolveAttribute));
114: assertSame(root, par1.getParentElement());
115: assertSame(par2, cloned.getParentElement());
116: assertSame(root, par2.getParentElement());
117: } finally {
118: doc.writeUnlock();
119: }
120: }
121:
122: public void testGetRootElement() {
123: Element docRoot = doc.getDefaultRootElement();
124: Element bufRoot = buf.getRootElement();
125: assertSame(docRoot, bufRoot);
126: bufRoot = doc.new BranchElement(null, null);
127: buf = doc.new ElementBuffer(bufRoot);
128: doc.buffer = buf;
129: // The previously fetched root element of the document is distinct
130: // from the newly created (no doubt).
131: assertNotSame(docRoot, bufRoot);
132: // But document returns the newly created element about which it didn't
133: // known when it was constructed itself. Thus the document doesn't
134: // store default root element but gets it from the associated
135: // ElementBuffer.
136: assertSame(bufRoot, doc.getDefaultRootElement());
137: }
138: }
|