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.ElementSpec;
023: import junit.framework.TestCase;
024:
025: public class DefaultStyledDocument_ElementSpecTest extends TestCase {
026: /**
027: * ElementSpec created with ElementSpec(AttributeSet, short) constructor.
028: */
029: private ElementSpec spec1;
030:
031: /**
032: * ElementSpec created with ElementSpec(AttributeSet, short, int)
033: * constructor.
034: */
035: private ElementSpec spec2;
036:
037: /**
038: * ElementSpec created with
039: * ElementSpec(AttributeSet, short, char[], int, int) constructor.
040: */
041: private ElementSpec spec3;
042:
043: private static final AttributeSet attrs1;
044:
045: private static final AttributeSet attrs2;
046:
047: private static final AttributeSet attrs3;
048:
049: private char[] text;
050: static {
051: MutableAttributeSet mas;
052: mas = new SimpleAttributeSet();
053: StyleConstants.setBold(mas, true);
054: attrs1 = mas.copyAttributes();
055: mas = new SimpleAttributeSet();
056: StyleConstants.setItalic(mas, true);
057: attrs2 = mas.copyAttributes();
058: mas = new SimpleAttributeSet(attrs1);
059: mas.addAttributes(attrs2);
060: attrs3 = mas.copyAttributes();
061: }
062:
063: @Override
064: protected void setUp() throws Exception {
065: super .setUp();
066: spec1 = new ElementSpec(attrs1, ElementSpec.StartTagType);
067: spec2 = new ElementSpec(attrs2, ElementSpec.ContentType, 10);
068: spec3 = new ElementSpec(attrs3, ElementSpec.EndTagType,
069: text = "sample text".toCharArray(), 2, 4);
070: }
071:
072: /*
073: * Constructors are tested in all the getters. The only side effect of
074: * constructor is setting fields which are accessed through getters.
075: *
076: * ElementSpec(AttributeSet, short)
077: * ElementSpec(AttributeSet, short, char[], int, int)
078: * ElementSpec(AttributeSet, short, int)
079: */
080: /*
081: public void testElementSpecAttributeSetshort() {
082: }
083: public void testElementSpecAttributeSetshortcharArrayintint() {
084: }
085: public void testElementSpecAttributeSetshortint() {
086: }
087: */
088: public void testGetArray() {
089: assertNull(spec1.getArray());
090: assertNull(spec2.getArray());
091: assertSame(text, spec3.getArray());
092: }
093:
094: public void testGetAttributes() {
095: assertSame(attrs1, spec1.getAttributes());
096: assertSame(attrs2, spec2.getAttributes());
097: assertSame(attrs3, spec3.getAttributes());
098: }
099:
100: public void testGetDirection() {
101: assertEquals(ElementSpec.OriginateDirection, spec1
102: .getDirection());
103: assertEquals(ElementSpec.OriginateDirection, spec2
104: .getDirection());
105: assertEquals(ElementSpec.OriginateDirection, spec3
106: .getDirection());
107: }
108:
109: public void testGetLength() {
110: assertEquals(0, spec1.getLength());
111: assertEquals(10, spec2.getLength());
112: assertEquals(4, spec3.getLength());
113: }
114:
115: public void testGetOffset() {
116: assertEquals(0, spec1.getOffset());
117: assertEquals(0, spec2.getOffset());
118: assertEquals(2, spec3.getOffset());
119: }
120:
121: public void testGetType() {
122: assertEquals(ElementSpec.StartTagType, spec1.getType());
123: assertEquals(ElementSpec.ContentType, spec2.getType());
124: assertEquals(ElementSpec.EndTagType, spec3.getType());
125: }
126:
127: public void testSetDirection() {
128: assertEquals(ElementSpec.OriginateDirection, spec1
129: .getDirection());
130: spec1.setDirection(ElementSpec.JoinNextDirection);
131: assertEquals(ElementSpec.JoinNextDirection, spec1
132: .getDirection());
133: }
134:
135: public void testSetType() {
136: assertEquals(ElementSpec.StartTagType, spec1.getType());
137: spec1.setType(ElementSpec.EndTagType);
138: assertEquals(ElementSpec.EndTagType, spec1.getType());
139: }
140:
141: /*
142: * String toString()
143: */
144: public void testToString() {
145: assertEquals("StartTag:Originate:0", spec1.toString());
146: assertEquals("Content:Originate:10", spec2.toString());
147: assertEquals("EndTag:Originate:4", spec3.toString());
148: }
149:
150: /**
151: * Checks how directions represented in string.
152: */
153: public void testToStringDirection() {
154: final short[] direction = new short[] {
155: ElementSpec.OriginateDirection,
156: ElementSpec.JoinFractureDirection,
157: ElementSpec.JoinNextDirection,
158: ElementSpec.JoinPreviousDirection };
159: final String[] text = new String[] { "Originate", "Fracture",
160: "JoinNext", "JoinPrevious" };
161: for (int i = 0; i < direction.length; i++) {
162: spec1.setDirection(direction[i]);
163: assertEquals("@ " + i, text[i],
164: spec1.toString().split(":")[1]);
165: }
166: }
167:
168: public void testToStringIvalidDirection() {
169: spec1.setDirection((short) 25);
170: assertEquals((short) 25, spec1.getDirection());
171: assertEquals("StartTag:??:0", spec1.toString());
172: }
173:
174: public void testToStringInvalidType() throws Exception {
175: spec1.setType((short) 25);
176: assertEquals((short) 25, spec1.getType());
177: assertEquals("??:Originate:0", spec1.toString());
178: }
179: }
|