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: import junit.framework.TestCase;
025:
026: /**
027: * Tests for addAttribute method.
028: *
029: */
030: public class StyleContext_AddAttrTest extends TestCase {
031: private StyleContext sc;
032:
033: /**
034: * Add two same attributes into two sets using different order.
035: * The cache should return the same objects.
036: */
037: public void testAddAttributeCacheDiffOrder() {
038: AttributeSet as1 = sc.addAttribute(sc.getEmptySet(),
039: StyleConstants.Bold, Boolean.TRUE);
040: AttributeSet as2 = sc.addAttribute(as1, StyleConstants.Italic,
041: Boolean.FALSE);
042: AttributeSet as12 = sc.addAttribute(sc.getEmptySet(),
043: StyleConstants.Italic, Boolean.FALSE);
044: AttributeSet as22 = sc.addAttribute(as12, StyleConstants.Bold,
045: Boolean.TRUE);
046: assertSame(as2, as22);
047: assertEquals(2, as2.getAttributeCount());
048: }
049:
050: /**
051: * Add two same attributes into two sets using the same order.
052: * The cache should return the same objects.
053: */
054: public void testAddAttributeCacheSameOrder() {
055: AttributeSet as11 = sc.addAttribute(sc.getEmptySet(),
056: StyleConstants.Bold, Boolean.TRUE);
057: AttributeSet as21 = sc.addAttribute(as11,
058: StyleConstants.Italic, Boolean.FALSE);
059: AttributeSet as12 = sc.addAttribute(sc.getEmptySet(),
060: StyleConstants.Bold, Boolean.TRUE);
061: AttributeSet as22 = sc.addAttribute(as12,
062: StyleConstants.Italic, Boolean.FALSE);
063: assertSame(as11, as12);
064: assertSame(as21, as22);
065: assertEquals(1, as11.getAttributeCount());
066: assertEquals(2, as21.getAttributeCount());
067: }
068:
069: /**
070: * Add two different key/value pair to an empty set.
071: * Check the return instances are different.
072: */
073: public void testAddAttributeDiff() {
074: AttributeSet as1 = sc.addAttribute(sc.getEmptySet(),
075: StyleConstants.Bold, Boolean.TRUE);
076: AttributeSet as2 = sc.addAttribute(sc.getEmptySet(),
077: StyleConstants.Bold, Boolean.FALSE);
078: assertNotSame(as1, as2);
079: }
080:
081: /**
082: * Add nine attributes into a set.
083: */
084: public void testAddAttributeNine() {
085: AttributeSet as = StyleContextTest.addAttribute(9);
086: assertEquals(9, as.getAttributeCount());
087: assertTrue(as instanceof SmallAttributeSet);
088: }
089:
090: /**
091: * Add nine attributes into a set, and try to add the tenth one
092: * which is already in the set.
093: */
094: public void testAddAttributeNineSameValue() {
095: AttributeSet as = StyleContextTest.addAttribute(9);
096: as = sc.addAttribute(as, StyleConstants.Bold, Boolean.TRUE);
097: assertEquals(9, as.getAttributeCount());
098: if (!BasicSwingTestCase.isHarmony()) {
099: assertTrue(as instanceof SimpleAttributeSet);
100: } else {
101: assertTrue(as instanceof SmallAttributeSet);
102: }
103: }
104:
105: /**
106: * Add the same key/value pair to an empty set.
107: * Check the returned instances are the same.
108: */
109: public void testAddAttributeSame() {
110: AttributeSet as1 = StyleContextTest.addAttribute(null, 1);
111: AttributeSet as2 = StyleContextTest.addAttribute(null, 1);
112: assertSame(as1, as2);
113: }
114:
115: /**
116: * Add the same attribute value but with different objects.
117: * Check the returned sets are the same.
118: */
119: public void testAddAttributeSimilar() {
120: AttributeSet as1 = sc.addAttribute(sc.getEmptySet(),
121: StyleConstants.FontSize, new Integer(10));
122: AttributeSet as2 = sc.addAttribute(sc.getEmptySet(),
123: StyleConstants.FontSize, new Integer(10));
124: assertSame(as1, as2);
125: }
126:
127: /**
128: * Add the value into an attribute set twice using two
129: * different objects.
130: */
131: public void testAddAttributeSimilarTwice() {
132: AttributeSet as1 = sc.addAttribute(sc.getEmptySet(),
133: StyleConstants.FontSize, new Integer(10));
134: AttributeSet as2 = sc.addAttribute(as1,
135: StyleConstants.FontSize, new Integer(10));
136: assertSame(as1, as2);
137: assertEquals(1, as1.getAttributeCount());
138: }
139:
140: /**
141: * Add ten attributes into a set.
142: */
143: public void testAddAttributeTen() {
144: AttributeSet as = StyleContextTest.addAttribute(10);
145: assertEquals(10, as.getAttributeCount());
146: assertTrue(as instanceof SimpleAttributeSet);
147: }
148:
149: /**
150: * Add three attributes into a set.
151: */
152: public void testAddAttributeThree() {
153: AttributeSet as = StyleContextTest.addAttribute(3);
154: assertEquals(3, as.getAttributeCount());
155: assertTrue(as instanceof SmallAttributeSet);
156: }
157:
158: /**
159: * Add four attributes into a set, but one of them is used twice,
160: * so the returned attribute set should contain only three ones.
161: */
162: public void testAddAttributeThreeSameKey() {
163: AttributeSet as = StyleContextTest.addAttribute(3);
164: as = sc.addAttribute(as, StyleConstants.Bold, Boolean.FALSE);
165: assertEquals(3, as.getAttributeCount());
166: assertTrue(as instanceof SmallAttributeSet);
167: }
168:
169: /**
170: * Add two attributes into a set.
171: */
172: public void testAddAttributeTwo() {
173: AttributeSet as = StyleContextTest.addAttribute(2);
174: assertEquals(2, as.getAttributeCount());
175: assertTrue(as instanceof SmallAttributeSet);
176: }
177:
178: /**
179: * Create an attribute set of two elements. Then create the same
180: * set using MutableAttributeSet implementor.
181: */
182: public void testAddMutable() {
183: AttributeSet as = StyleContextTest.addAttribute(3);
184: SimpleAttributeSet sas = new SimpleAttributeSet();
185: int i;
186: for (i = 0; i < 4; i += 2) {
187: sas.addAttribute(StyleContextTest.attr[i],
188: StyleContextTest.attr[i + 1]);
189: }
190: AttributeSet set = sc.addAttribute(sas,
191: StyleContextTest.attr[i], StyleContextTest.attr[i + 1]);
192: assertEquals(3, set.getAttributeCount());
193: assertSame(as, set);
194: }
195:
196: @Override
197: protected void setUp() {
198: sc = StyleContextTest.sc = new StyleContext();
199: }
200: }
|