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.BasicSwingTestCase;
023:
024: public class SimpleAttributeSetTest extends MutableAttributeSetTest {
025: public SimpleAttributeSetTest() {
026: super ();
027: }
028:
029: public SimpleAttributeSetTest(final String name) {
030: super (name);
031: }
032:
033: /**
034: * The instance of SimpleAttributeSet.
035: */
036: protected SimpleAttributeSet sas;
037:
038: /*
039: * @see MutableAttributeSetTest#setUp()
040: */
041: @Override
042: protected void setUp() throws Exception {
043: super .setUp();
044: sas = (SimpleAttributeSet) as;
045: }
046:
047: public void testHashCode() {
048: SimpleAttributeSet newSet = (SimpleAttributeSet) sas.clone();
049: assertTrue("Hash codes must be equal for equal objects", newSet
050: .hashCode() == sas.hashCode());
051: newSet.setResolveParent(SimpleAttributeSet.EMPTY);
052: assertFalse(
053: "Hash codes should not be equal for not equal objects",
054: newSet.hashCode() == sas.hashCode());
055: sas.setResolveParent(new SimpleAttributeSet());
056: assertTrue("Hash codes must be equal for equal objects", newSet
057: .hashCode() == sas.hashCode());
058: SimpleAttributeSet parent = new SimpleAttributeSet();
059: parent.addAttribute("key", "value");
060: newSet.setResolveParent(parent);
061: sas.setResolveParent(parent);
062: assertTrue("Hash codes must be equal for equal objects", newSet
063: .hashCode() == sas.hashCode());
064: sas.removeAttribute(keys[1]);
065: assertFalse(
066: "Hash codes should not be equal for not equal objects",
067: newSet.hashCode() == sas.hashCode());
068: }
069:
070: public void testClone() {
071: SimpleAttributeSet parent = new SimpleAttributeSet();
072: parent.addAttribute("key", "value");
073: sas.setResolveParent(parent);
074: Object clone = sas.clone();
075: assertSame(sas.getClass(), clone.getClass());
076: assertNotSame(sas, clone);
077: assertEquals(sas, clone);
078: assertEquals(sas.getResolveParent(),
079: ((SimpleAttributeSet) clone).getResolveParent());
080: sas.removeAttribute(keys[1]);
081: assertFalse(sas.equals(clone));
082: }
083:
084: public void testEquals() {
085: SimpleAttributeSet newSet = (SimpleAttributeSet) sas.clone();
086: assertEquals(sas, newSet);
087: newSet.setResolveParent(SimpleAttributeSet.EMPTY);
088: assertFalse(sas.equals(newSet));
089: sas.setResolveParent(new SimpleAttributeSet());
090: assertEquals(sas, newSet);
091: SimpleAttributeSet parent = new SimpleAttributeSet();
092: parent.addAttribute("key", "value");
093: newSet.setResolveParent(new SimpleAttributeSet(parent));
094: sas.setResolveParent(new SimpleAttributeSet(parent));
095: assertEquals(sas, newSet);
096: sas.removeAttribute(keys[1]);
097: assertFalse(sas.equals(newSet));
098: StyleContext.SmallAttributeSet eq = StyleContext
099: .getDefaultStyleContext().new SmallAttributeSet(
100: new Object[] { keys[0], values[0], keys[2], values[2],
101: AttributeSet.ResolveAttribute, parent });
102: assertEquals(sas, eq);
103: }
104:
105: public void testToString() {
106: assertFalse(sas.toString().equals(""));
107: }
108:
109: /*
110: * Class under test for void SimpleAttributeSet()
111: */
112: public void testSimpleAttributeSet() {
113: sas = new SimpleAttributeSet();
114: assertEquals(SimpleAttributeSet.EMPTY, sas);
115: }
116:
117: public void testIsEmpty() {
118: assertFalse(sas.isEmpty());
119: SimpleAttributeSet clone = (SimpleAttributeSet) sas.clone();
120: sas.removeAttributes(clone);
121: assertTrue(sas.isEmpty());
122: sas.setResolveParent(clone);
123: assertFalse(sas.isEmpty());
124: }
125:
126: /*
127: * SimpleAttributeSet(AttributeSet)
128: */
129: public void testSimpleAttributeSetAttributeSet() {
130: SimpleAttributeSet parent = new SimpleAttributeSet();
131: parent.addAttribute("key", "value");
132: sas.setResolveParent(parent);
133: SimpleAttributeSet initSet = (SimpleAttributeSet) sas.clone();
134: sas = new SimpleAttributeSet(initSet);
135: assertEquals(initSet, sas);
136: assertEquals(initSet.getResolveParent(), sas.getResolveParent());
137: }
138:
139: public void testSerializable() throws Exception {
140: sas.setResolveParent(SimpleAttributeSet.EMPTY);
141: SimpleAttributeSet readSet = (SimpleAttributeSet) BasicSwingTestCase
142: .serializeObject(sas);
143: assertEquals(sas, readSet);
144: }
145: }
|