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 junit.framework.TestCase;
023:
024: /**
025: * Tests for getStaticAttribute, getStaticAttributeKey, readAttributes,
026: * readAttributeSet, registerStaticAttributeKey, writeAttributes,
027: * writeAttributeSet methods.
028: *
029: */
030: public class StyleContext_StaticAttrTest extends TestCase {
031: /**
032: * Object that is used with registerStaticAttributeKey.
033: */
034: private static class SampleAttribute {
035: String name = "sampleAttribute";
036:
037: @Override
038: public boolean equals(final Object obj) {
039: if (obj instanceof SampleAttribute) {
040: return name == ((SampleAttribute) obj).name;
041: }
042: return false;
043: }
044:
045: @Override
046: public int hashCode() {
047: return name.hashCode();
048: }
049:
050: @Override
051: public String toString() {
052: return name;
053: }
054: }
055:
056: /**
057: * Array of objects that are registered as static attribute keys.
058: */
059: private static final Object keys[] = { StyleConstants.Bold,
060: StyleConstants.Italic, StyleConstants.Background,
061: StyleConstants.Underline, StyleConstants.Alignment };
062:
063: /**
064: * Array of strings that static attribute keys get registered with.
065: */
066: private static final Object strings[] = {
067: "javax.swing.text.StyleConstants$FontConstants.bold",
068: "javax.swing.text.StyleConstants$FontConstants.italic",
069: "javax.swing.text.StyleConstants$ColorConstants.background",
070: "javax.swing.text.StyleConstants$CharacterConstants.underline",
071: "javax.swing.text.StyleConstants$ParagraphConstants.Alignment" };
072:
073: public void testGetStaticAttribute() {
074: for (int i = 0; i < keys.length; i++) {
075: assertSame("Iteration: " + i, keys[i], StyleContext
076: .getStaticAttribute(strings[i]));
077: }
078: }
079:
080: public void testGetStaticAttributeKey() {
081: for (int i = 0; i < keys.length; i++) {
082: assertEquals("Iteration: " + i, strings[i], StyleContext
083: .getStaticAttributeKey(keys[i]));
084: }
085: }
086:
087: public void testRegisterStaticAttributeKey() {
088: SampleAttribute key = new SampleAttribute();
089: String str = (String) StyleContext.getStaticAttributeKey(key);
090: // We haven't registered our object yet, so result should be null
091: assertNull(StyleContext.getStaticAttribute(str));
092: StyleContext.registerStaticAttributeKey(key);
093: // The method should return the same object with which we called
094: // register... method
095: assertSame(key, StyleContext.getStaticAttribute(str));
096: }
097: /*
098: The commented out methods are implicitly tested
099: with StyleContext_SerializableTest
100: public void testReadAttributes() {
101: // implementation simply calls testReadAttributeSet
102: }
103:
104: public void testWriteAttributes() {
105: // implementation simply calls testWriteAttributeSet
106: }
107:
108: public void testReadAttributeSet() {
109: }
110:
111: public void testWriteAttributeSet() {
112: }*/
113: }
|