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 java.util.NoSuchElementException;
024:
025: public class MutableAttributeSetTest extends AttributeSetTest {
026: public MutableAttributeSetTest() {
027: super ();
028: }
029:
030: public MutableAttributeSetTest(final String name) {
031: super (name);
032: }
033:
034: /**
035: * The interface MutableAttributeSet for test cases.
036: */
037: protected MutableAttributeSet mas;
038:
039: public static void main(final String[] args) {
040: junit.textui.TestRunner.run(MutableAttributeSetTest.class);
041: }
042:
043: @Override
044: protected void setUp() throws Exception {
045: super .setUp();
046: mas = (MutableAttributeSet) as;
047: }
048:
049: public void testRemoveAttribute() {
050: int count = mas.getAttributeCount();
051: mas.removeAttribute(keys[1]);
052: assertFalse(mas.isDefined(keys[1]));
053: mas.removeAttribute(values[1]);
054: assertEquals(keys.length - 1, mas.getAttributeCount());
055: assertEquals(count - 1, mas.getAttributeCount());
056: }
057:
058: public void testAddAttributes() {
059: MutableAttributeSet forAdd = new SimpleAttributeSet();
060: String key1 = "keyforAdd1", value1 = "valueforAdd1";
061: String key2 = "keyforAdd2", value2 = "valueforAdd2";
062: forAdd.addAttribute(key1, value1);
063: forAdd.addAttribute(key2, value2);
064: int count = mas.getAttributeCount();
065: mas.addAttributes(forAdd);
066: assertEquals(count + 2, mas.getAttributeCount());
067: assertEquals(value1, mas.getAttribute(key1));
068: assertEquals(value2, mas.getAttribute(key2));
069: }
070:
071: /*
072: * Class under test for void removeAttributes(javax.swing.text.AttributeSet)
073: */
074: public void testRemoveAttributesAttributeSetSame() {
075: MutableAttributeSet forRemove = new SimpleAttributeSet();
076: // The key/value pair are the same as those in mas
077: forRemove.addAttribute(keys[1], values[1]);
078: forRemove.addAttribute(keys[2], values[2]);
079: int count = mas.getAttributeCount();
080: mas.removeAttributes(forRemove);
081: // Attributes with keys { keys[1], keys[2] } should be removed
082: // from the set
083: assertFalse(mas.isDefined(keys[1]));
084: assertFalse(mas.isDefined(keys[2]));
085: assertEquals(count - 2, mas.getAttributeCount());
086: }
087:
088: /*
089: * Class under test for void removeAttributes(javax.swing.text.AttributeSet)
090: */
091: public void testRemoveAttributesAttributeSetDiff() {
092: MutableAttributeSet forRemove = new SimpleAttributeSet();
093: // The keys are the same but, the values are different
094: forRemove.addAttribute(keys[1], "one");
095: forRemove.addAttribute(keys[2], "two");
096: int count = mas.getAttributeCount();
097: mas.removeAttributes(forRemove);
098: // Attributes with keys { keys[1], keys[2] } should NOT be removed
099: // from the set as their values were different
100: assertTrue(mas.isDefined(keys[1]));
101: assertTrue(mas.isDefined(keys[2]));
102: assertEquals(count, mas.getAttributeCount());
103: }
104:
105: @SuppressWarnings("unchecked")
106: public void testRemoveAttributesEnumeration() {
107: int count = mas.getAttributeCount();
108: assertTrue(mas.isDefined(keys[0]));
109: assertTrue(mas.isDefined(keys[1]));
110: assertFalse(mas.isDefined(keyInResolver));
111: mas.removeAttributes(new Enumeration() {
112: private int count = 0;
113:
114: public boolean hasMoreElements() {
115: return count < 3;
116: }
117:
118: public Object nextElement() {
119: if (count < 2) {
120: return keys[count++];
121: } else if (count < 3) {
122: count = 3;
123: return keyInResolver;
124: } else {
125: throw new NoSuchElementException("No more elements");
126: }
127: }
128: });
129: assertEquals(count - 2, mas.getAttributeCount());
130: assertFalse(mas.isDefined(keys[0]));
131: assertFalse(mas.isDefined(keys[1]));
132: assertFalse(mas.isDefined(keyInResolver));
133: }
134:
135: public void testSetResolveParent() {
136: AttributeSet parent = mas.getResolveParent();
137: AttributeSet newParent = new SimpleAttributeSet();
138: mas.setResolveParent(newParent);
139: assertNotSame(parent, mas.getResolveParent());
140: assertSame(newParent, mas.getResolveParent());
141: }
142:
143: public void testAddAttribute() {
144: int count = mas.getAttributeCount();
145: String key = "key", value = "value";
146: mas.addAttribute(key, value);
147: assertTrue(mas.isDefined(key));
148: assertEquals(value, mas.getAttribute(key));
149: assertEquals(count + 1, mas.getAttributeCount());
150: }
151: }
|