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.Enumeration;
023: import javax.swing.text.AbstractDocument.AbstractElement;
024: import javax.swing.text.AbstractDocument.BranchElement;
025: import junit.framework.TestCase;
026:
027: public class AbstractDocument_AbstractElementRTest extends TestCase {
028: private static class AbstractElementImpl extends AbstractElement {
029: private static final long serialVersionUID = 1L;
030:
031: public static final int START_OFFSET = -101;
032:
033: public static final int END_OFFSET = -10;
034:
035: public AbstractElementImpl(final AbstractDocument doc,
036: final Element parent, final AttributeSet attrs) {
037: doc.super (parent, attrs);
038: }
039:
040: @Override
041: public boolean getAllowsChildren() {
042: return false;
043: }
044:
045: @Override
046: public boolean isLeaf() {
047: return true;
048: }
049:
050: @SuppressWarnings("unchecked")
051: @Override
052: public Enumeration<?> children() {
053: return null;
054: }
055:
056: @Override
057: public Element getElement(int index) {
058: return null;
059: }
060:
061: @Override
062: public int getElementCount() {
063: return 0;
064: }
065:
066: @Override
067: public int getElementIndex(int offset) {
068: return 0;
069: }
070:
071: @Override
072: public int getStartOffset() {
073: return START_OFFSET;
074: }
075:
076: @Override
077: public int getEndOffset() {
078: return END_OFFSET;
079: }
080: }
081:
082: private AbstractDocument doc;
083:
084: private Element root;
085:
086: private Element element;
087:
088: private MutableAttributeSet attrs;
089:
090: public void testGetNameAbstract() throws Exception {
091: final String name = "Unbekannte";
092: attrs.addAttribute(AbstractDocument.ElementNameAttribute, name);
093: element = new AbstractElementImpl(doc, root, attrs);
094: assertSame(name, element.getName());
095: assertSame(root, element.getParentElement());
096: attrs.removeAttribute(AbstractDocument.ElementNameAttribute);
097: assertEquals(0, attrs.getAttributeCount());
098: element = new AbstractElementImpl(doc, root, attrs);
099: assertNull(element.getName());
100: assertSame(root, element.getParentElement());
101: }
102:
103: public void testGetNameLeaf() throws Exception {
104: final String name = "Blatt";
105: attrs.addAttribute(AbstractDocument.ElementNameAttribute, name);
106: element = doc.new LeafElement(root, attrs, 15, 23);
107: assertSame(name, element.getName());
108: assertEquals(15, element.getStartOffset());
109: assertEquals(23, element.getEndOffset());
110: assertSame(root, element.getParentElement());
111: }
112:
113: public void testGetNameBranch() throws Exception {
114: final String name = "Ast";
115: attrs.addAttribute(AbstractDocument.ElementNameAttribute, name);
116: element = doc.new BranchElement(root, attrs);
117: assertSame(name, element.getName());
118: assertSame(root, element.getParentElement());
119: }
120:
121: public void testGetNameNonString() throws Exception {
122: attrs.addAttribute(AbstractDocument.ElementNameAttribute,
123: new Integer(10101));
124: element = new AbstractElementImpl(doc, root, attrs);
125: try {
126: element.getName();
127: fail("ClassCastException is expected");
128: } catch (ClassCastException e) {
129: }
130: }
131:
132: public void testGetNameParent() throws Exception {
133: final String parentName = "parentName";
134: attrs.addAttribute(AbstractDocument.ElementNameAttribute,
135: parentName);
136: BranchElement parent = doc.new BranchElement(null, attrs);
137: AbstractElement element = new AbstractElementImpl(doc, parent,
138: null);
139: assertTrue(parent
140: .isDefined(AbstractDocument.ElementNameAttribute));
141: assertEquals(parentName, parent.getName());
142: assertFalse(element
143: .isDefined(AbstractDocument.ElementNameAttribute));
144: assertNull(element.getName());
145: }
146:
147: @Override
148: protected void setUp() throws Exception {
149: super .setUp();
150: doc = new PlainDocument();
151: root = doc.getDefaultRootElement();
152: attrs = new SimpleAttributeSet();
153: doc.writeLock();
154: }
155:
156: @Override
157: protected void tearDown() throws Exception {
158: super.tearDown();
159: doc.writeUnlock();
160: }
161: }
|