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: import javax.swing.text.StyleContext.SmallAttributeSet;
024:
025: public class StyleContext_SmallAttributeSetTest extends
026: AttributeSetTest {
027: private StyleContext sc;
028:
029: private SmallAttributeSet attrSet;
030:
031: @Override
032: protected void setUp() throws Exception {
033: super .setUp();
034: sc = new StyleContext();
035: attrSet = sc.new SmallAttributeSet(as);
036: as = attrSet;
037: asWithResolver = sc.new SmallAttributeSet(asWithResolver);
038: }
039:
040: public void testHashCode() {
041: SmallAttributeSet aSet = sc.new SmallAttributeSet(as);
042: assertNotSame(attrSet, aSet);
043: assertEquals("Hash codes must be equal for equal objects",
044: attrSet.hashCode(), aSet.hashCode());
045: aSet = sc.new SmallAttributeSet(new Object[0]);
046: assertEquals(0, aSet.hashCode());
047: // Hash codes for equal objects must be equal
048: // even if they are implemented by different classes
049: SimpleAttributeSet simpleAS = new SimpleAttributeSet(as);
050: assertEquals(simpleAS, attrSet);
051: if (BasicSwingTestCase.isHarmony()) {
052: assertEquals(attrSet.hashCode(), simpleAS.hashCode());
053: }
054: }
055:
056: public void testClone() {
057: Object clone = attrSet.clone();
058: assertSame(attrSet, clone);
059: }
060:
061: public void testEquals() {
062: AttributeSet newSet = sc.new SmallAttributeSet(new Object[] {
063: keys[0], values[0], keys[1], values[1], keys[2],
064: values[2] });
065: assertEquals(attrSet, newSet);
066: newSet = sc.new SmallAttributeSet(new Object[] { keys[0],
067: values[0], keys[1], values[1] });
068: assertFalse(attrSet.equals(newSet));
069: SimpleAttributeSet sas = new SimpleAttributeSet();
070: for (int i = 0; i < keys.length; i++) {
071: sas.addAttribute(keys[i], values[i]);
072: }
073: assertEquals(attrSet, sas);
074: assertFalse(attrSet.equals(null));
075: }
076:
077: public void testToString() {
078: assertAttributesSame("{key3=value3,key2=value2,key1=value1,}",
079: attrSet.toString());
080: AttributeSet parent = sc.new SmallAttributeSet(new Object[] {
081: "key", "value" });
082: attrSet = sc.new SmallAttributeSet(new Object[] { keys[0],
083: values[0], keys[1], values[1],
084: AttributeSet.ResolveAttribute, parent });
085: assertAttributesSame(
086: "{key1=value1,key2=value2,resolver=AttributeSet,}",
087: attrSet.toString());
088: }
089:
090: /*
091: * SmallAttributeSet(Object[])
092: */
093: public void testSmallAttributeSetObjectArray() {
094: Object[] array = new Object[] { keys[0], values[0], keys[1],
095: values[1], keys[2], values[2] };
096: attrSet = sc.new SmallAttributeSet(array);
097: assertEquals(keys.length, attrSet.getAttributeCount());
098: for (int i = 0; i < keys.length; i++) {
099: assertTrue(attrSet.containsAttribute(keys[i], values[i]));
100: }
101: // AttributeSet must not depend on the passed array
102: array[0] = "justKey";
103: if (BasicSwingTestCase.isHarmony()) {
104: assertFalse(attrSet.isDefined(array[0]));
105: }
106: array = new Object[] { "key", "value" };
107: assertEquals(3, attrSet.getAttributeCount());
108: // Empty array
109: array = new Object[0];
110: attrSet = sc.new SmallAttributeSet(array);
111: emptyTest(attrSet);
112: }
113:
114: public void emptyTest(final AttributeSet emptySet) {
115: assertNull(emptySet.getResolveParent());
116: assertFalse(emptySet.getAttributeNames().hasMoreElements());
117: assertNull(emptySet.getAttribute(keys[1]));
118: assertNull(emptySet.getAttribute(AttributeSet.ResolveAttribute));
119: assertEquals(emptySet, emptySet.copyAttributes());
120: emptySet.toString();
121: assertFalse(emptySet.isEqual(as));
122: assertFalse(emptySet.isDefined(keys[1]));
123: assertFalse(emptySet.equals(as));
124: assertFalse(emptySet.containsAttributes(as));
125: assertFalse(emptySet.containsAttribute("key", "value"));
126: assertEquals(0, emptySet.hashCode());
127: }
128:
129: /*
130: * SmallAttributeSet(AttributeSet)
131: */
132: public void testSmallAttributeSetStyleContextAttributeSet() {
133: assertEquals(keys.length, attrSet.getAttributeCount());
134: for (int i = 0; i < keys.length; i++) {
135: assertTrue(attrSet.containsAttribute(keys[i], values[i]));
136: }
137: // Empty attribute set
138: attrSet = sc.new SmallAttributeSet(SimpleAttributeSet.EMPTY);
139: emptyTest(attrSet);
140: }
141:
142: @Override
143: public void testGetResolveParent() {
144: assertNull(attrSet.getResolveParent());
145: AttributeSet parent = sc.new SmallAttributeSet(new Object[] {
146: "key", "value" });
147: Object[] array = new Object[] { keys[0], values[0], keys[1],
148: values[1], AttributeSet.ResolveAttribute, parent };
149: attrSet = sc.new SmallAttributeSet(array);
150: assertEquals(parent, attrSet.getResolveParent());
151: }
152:
153: private static void assertAttributesSame(final String expected,
154: final String actual) {
155: String[] splitExpected = StyleContextTest.trimEnds(expected)
156: .split(",");
157: String[] splitActual = StyleContextTest.trimEnds(actual).split(
158: ",");
159: assertTrue(StyleContextTest.assertAttributes(splitExpected,
160: splitActual));
161: }
162: }
|