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 junit.framework.TestCase;
024:
025: public class AttributeSetTest extends TestCase {
026: /**
027: * The interface AttributeSet for test cases.
028: */
029: protected AttributeSet as;
030:
031: /**
032: * The flag indicates that setUp method should include resolveParent
033: * while initializing test object.
034: */
035: protected AttributeSet asWithResolver;
036:
037: public static void main(final String[] args) {
038: junit.textui.TestRunner.run(AttributeSetTest.class);
039: }
040:
041: /**
042: * This is array containing keys for test attribute set.
043: */
044: protected static final String[] keys = new String[] { "key1",
045: "key2", "key3" };
046:
047: /**
048: * This is array containing values for test attribute set.
049: */
050: protected static final String[] values = new String[] { "value1",
051: "value2", "value3" };
052:
053: /**
054: * The attribute key to place in resolver set.
055: */
056: protected static final String keyInResolver = "keyInResolver";
057:
058: /**
059: * The attribute value to place in resolver set.
060: */
061: protected static final String valueInResolver = "valueInResolver";
062:
063: /**
064: * The attribute set used as a resolve parent.
065: */
066: protected static AttributeSet resolverSet = new SimpleAttributeSet();
067: static {
068: // Add the attribute into the set
069: ((SimpleAttributeSet) resolverSet).addAttribute(keyInResolver,
070: valueInResolver);
071: }
072:
073: /**
074: * AttributeSet must contain keys&values.
075: */
076: @Override
077: protected void setUp() throws Exception {
078: MutableAttributeSet mas = new SimpleAttributeSet();
079: ;
080: for (int i = 0; i < keys.length; i++) {
081: mas.addAttribute(keys[i], values[i]);
082: }
083: as = mas;
084: asWithResolver = mas.copyAttributes();
085: ((MutableAttributeSet) asWithResolver)
086: .setResolveParent(resolverSet);
087: }
088:
089: public AttributeSetTest() {
090: super ();
091: }
092:
093: public AttributeSetTest(final String name) {
094: super (name);
095: }
096:
097: public void testContainsAttribute() {
098: assertTrue(as.containsAttribute(keys[1], values[1]));
099: assertFalse(as.containsAttribute(values[1], keys[1]));
100: assertFalse(as
101: .containsAttribute(keyInResolver, valueInResolver));
102: assertTrue(asWithResolver.containsAttribute(keyInResolver,
103: valueInResolver));
104: }
105:
106: public void testContainsAttributes() {
107: MutableAttributeSet attrs = new SimpleAttributeSet();
108: attrs.addAttribute(keys[0], values[0]);
109: attrs.addAttribute(keys[2], values[2]);
110: assertTrue(as.containsAttributes(attrs));
111: attrs.addAttribute(values[1], keys[1]);
112: assertFalse(as.containsAttributes(attrs));
113: assertFalse(as.containsAttributes(resolverSet));
114: assertTrue(asWithResolver.containsAttributes(resolverSet));
115: }
116:
117: public void testCopyAttributes() {
118: AttributeSet copy = as.copyAttributes();
119: //copyAttributes returns an attribute set
120: //that is guaranteed not to change over time
121: if (as instanceof MutableAttributeSet) {
122: assertNotSame(copy, as);
123: }
124: assertEquals(as.getAttributeCount(), copy.getAttributeCount());
125: assertTrue(as.isEqual(copy));
126: }
127:
128: public void testGetAttribute() {
129: assertNull(as.getAttribute("key"));
130: assertEquals(values[1], as.getAttribute(keys[1]));
131: MutableAttributeSet parent = new SimpleAttributeSet();
132: String key = "key", value = "value";
133: parent.addAttribute(key, value);
134: assertNull(as.getAttribute(value));
135: assertNull(valueInResolver, as.getAttribute(keyInResolver));
136: assertEquals(valueInResolver, asWithResolver
137: .getAttribute(keyInResolver));
138: }
139:
140: public void testGetAttributeCount() {
141: assertEquals(keys.length, as.getAttributeCount());
142: assertEquals(keys.length + 1, asWithResolver
143: .getAttributeCount());
144: }
145:
146: public void testGetAttributeNames() {
147: int count = 0;
148: for (Enumeration<?> e = as.getAttributeNames(); e
149: .hasMoreElements();) {
150: count++;
151: assertTrue(as.isDefined(e.nextElement()));
152: }
153: assertEquals(as.getAttributeCount(), count);
154: count = 0;
155: for (Enumeration<?> e = asWithResolver.getAttributeNames(); e
156: .hasMoreElements();) {
157: count++;
158: assertTrue(asWithResolver.isDefined(e.nextElement()));
159: }
160: assertEquals(asWithResolver.getAttributeCount(), count);
161: }
162:
163: public void testGetResolveParent() {
164: assertNull(as.getResolveParent());
165: assertNotNull(asWithResolver.getResolveParent());
166: }
167:
168: public void testIsDefined() {
169: assertTrue(as.isDefined(keys[1]));
170: assertFalse(as.isDefined(values[1]));
171: assertFalse(asWithResolver.isDefined(keyInResolver));
172: }
173:
174: public void testIsEqual() {
175: AttributeSet copy = as.copyAttributes();
176: assertTrue(as.isEqual(copy));
177: assertFalse(as.isEqual(SimpleAttributeSet.EMPTY));
178: if (copy instanceof MutableAttributeSet) {
179: MutableAttributeSet parent = new SimpleAttributeSet();
180: ((MutableAttributeSet) copy).setResolveParent(parent);
181: assertFalse(as.isEqual(copy));
182: String key = "key", value = "value";
183: parent.addAttribute(key, value);
184: ((MutableAttributeSet) copy).setResolveParent(parent);
185: assertFalse(as.isEqual(copy));
186: if (as instanceof MutableAttributeSet) {
187: ((MutableAttributeSet) as).setResolveParent(parent);
188: assertTrue(as.isEqual(copy));
189: MutableAttributeSet parent2 = new SimpleAttributeSet();
190: parent2.addAttribute(key, value);
191: ((MutableAttributeSet) as).setResolveParent(parent2);
192: assertTrue(as.isEqual(copy));
193: }
194: }
195: }
196:
197: public void testResolveAttribute() {
198: assertSame(AttributeSet.ResolveAttribute,
199: StyleConstants.ResolveAttribute);
200: }
201:
202: public void testNameAttribute() {
203: assertSame(AttributeSet.NameAttribute,
204: StyleConstants.NameAttribute);
205: }
206: }
|