KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > editor > settings > AttributesUtilitiesTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.editor.settings;
21
22 import java.awt.Color JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.swing.text.AttributeSet JavaDoc;
27 import javax.swing.text.SimpleAttributeSet JavaDoc;
28 import org.netbeans.junit.NbTestCase;
29
30 /**
31  *
32  * @author Vita Stejskal
33  */

34 public class AttributesUtilitiesTest extends NbTestCase {
35     
36     /** Creates a new instance of AttributesUtilitiesTest */
37     public AttributesUtilitiesTest(String JavaDoc name) {
38         super(name);
39     }
40     
41     public void testCreateImmutablePairs() {
42         Object JavaDoc [] pairs = new Object JavaDoc [] {
43             "key-1", "value-1",
44             new HashMap JavaDoc(), new ArrayList JavaDoc(),
45             new Integer JavaDoc(123456), new Character JavaDoc('.')
46         };
47         
48         AttributeSet JavaDoc as = AttributesUtilities.createImmutable(pairs);
49         
50         assertEquals("Wrong size of attribute set", pairs.length / 2, as.getAttributeCount());
51         for(int i = 0; i < pairs.length / 2; i++) {
52             Object JavaDoc value = as.getAttribute(pairs[2 * i]);
53             assertSame("Wrong attribute value", pairs[2 * i + 1], value);
54         }
55     }
56
57     public void testCreateImmutableSets() {
58         final Object JavaDoc SHARED_ATTR_KEY = "SHARED-ATTRIBUTE";
59         final Object JavaDoc SHARED_ATTR_FIRST_VALUE = new Color JavaDoc(0);
60         
61         SimpleAttributeSet JavaDoc setA = new SimpleAttributeSet JavaDoc();
62         SimpleAttributeSet JavaDoc setB = new SimpleAttributeSet JavaDoc();
63         SimpleAttributeSet JavaDoc setC = new SimpleAttributeSet JavaDoc();
64         
65         setA.addAttribute(new ArrayList JavaDoc(2), new Long JavaDoc(1234));
66         setA.addAttribute(new HashMap JavaDoc(2), "AAA");
67         setA.addAttribute(new Object JavaDoc(), SimpleAttributeSet.EMPTY);
68         setA.addAttribute(new Double JavaDoc("1.1"), setA);
69         setA.addAttribute(SHARED_ATTR_KEY, SHARED_ATTR_FIRST_VALUE);
70
71         setB.addAttribute("set-2/attr-1", "set-2/attr-1/value-1");
72         setB.addAttribute(new Integer JavaDoc(2), new Character JavaDoc(','));
73
74         setC.addAttribute("set-3/attr-1", "set-3/attr-1/value-1");
75         setC.addAttribute(SHARED_ATTR_KEY, "set-3/attr-2/value-2");
76         setC.addAttribute("set-3/attr-3", "set-3/attr-3/value-3");
77
78         AttributeSet JavaDoc as = AttributesUtilities.createImmutable(setA, setB, setC);
79
80         // Check the initial contents
81
assertEquals("Wrong number of attributes",
82             setA.getAttributeCount() + setB.getAttributeCount() + setC.getAttributeCount() - 1,
83             as.getAttributeCount());
84         
85         assertTrue("Doesn't contain setA attributes", as.containsAttributes(setA));
86         assertTrue("Doesn't contain setB attributes", as.containsAttributes(setB));
87         
88         assertTrue("Wrong setC attr-1", as.containsAttribute("set-3/attr-1", "set-3/attr-1/value-1"));
89         assertTrue("Wrong setC attr-3", as.containsAttribute("set-3/attr-3", "set-3/attr-3/value-3"));
90         assertTrue("Wrong SHARED-ATTRIBUTE", as.containsAttribute(SHARED_ATTR_KEY, SHARED_ATTR_FIRST_VALUE));
91         
92         // Check that mutations are not propagated
93
setA.removeAttribute(SHARED_ATTR_KEY);
94         setC.removeAttribute(SHARED_ATTR_KEY);
95         assertFalse("setA still contains shared attribute", setA.isDefined(SHARED_ATTR_KEY));
96         assertFalse("setC still contains shared attribute", setC.isDefined(SHARED_ATTR_KEY));
97         assertTrue("AS should not be modified", as.isDefined(SHARED_ATTR_KEY));
98         assertTrue("AS should not be modified", as.containsAttribute(SHARED_ATTR_KEY, SHARED_ATTR_FIRST_VALUE));
99     }
100     
101     public void testCreateComposite() {
102         final Object JavaDoc SHARED_ATTR_KEY = "SHARED-ATTRIBUTE";
103         final Object JavaDoc SHARED_ATTR_LAST_VALUE = new Color JavaDoc(0);
104         
105         SimpleAttributeSet JavaDoc setA = new SimpleAttributeSet JavaDoc();
106         SimpleAttributeSet JavaDoc setB = new SimpleAttributeSet JavaDoc();
107         SimpleAttributeSet JavaDoc setC = new SimpleAttributeSet JavaDoc();
108         
109         setA.addAttribute("set-1/attr-1", "set-1/attr-1/value-1");
110         setA.addAttribute(SHARED_ATTR_KEY, "set-1/attr-2/value-2");
111         setA.addAttribute("set-1/attr-3", "set-1/attr-3/value-3");
112
113         setB.addAttribute("set-2/attr-1", "set-2/attr-1/value-1");
114         setB.addAttribute(new Integer JavaDoc(2), new Character JavaDoc(','));
115
116         setC.addAttribute(new ArrayList JavaDoc(2), new Long JavaDoc(1234));
117         setC.addAttribute(new HashMap JavaDoc(2), "AAA");
118         setC.addAttribute(new Object JavaDoc(), SimpleAttributeSet.EMPTY);
119         setC.addAttribute(new Double JavaDoc("1.1"), setA);
120         setC.addAttribute(SHARED_ATTR_KEY, SHARED_ATTR_LAST_VALUE);
121         
122         AttributeSet JavaDoc as = AttributesUtilities.createComposite(setC, setB, setA);
123
124         // Check the initial contents
125
assertEquals("Wrong number of attributes",
126             setA.getAttributeCount() + setB.getAttributeCount() + setC.getAttributeCount() - 1,
127             as.getAttributeCount());
128         
129         assertTrue("Doesn't contain setC attributes", as.containsAttributes(setC));
130         assertTrue("Doesn't contain setB attributes", as.containsAttributes(setB));
131         
132         assertTrue("Wrong setA attr-1", as.containsAttribute("set-1/attr-1", "set-1/attr-1/value-1"));
133         assertTrue("Wrong setA attr-3", as.containsAttribute("set-1/attr-3", "set-1/attr-3/value-3"));
134         assertTrue("Wrong SHARED-ATTRIBUTE", as.containsAttribute(SHARED_ATTR_KEY, SHARED_ATTR_LAST_VALUE));
135         
136         // Check that mutations __are__ propagated
137
setA.removeAttribute(SHARED_ATTR_KEY);
138         setC.removeAttribute(SHARED_ATTR_KEY);
139         assertFalse("setA still contains shared attribute", setA.isDefined(SHARED_ATTR_KEY));
140         assertFalse("setC still contains shared attribute", setC.isDefined(SHARED_ATTR_KEY));
141         assertFalse("AS still contains shared attribute", as.isDefined(SHARED_ATTR_KEY));
142         
143         String JavaDoc newSharedValue = "HA!";
144         setA.addAttribute(SHARED_ATTR_KEY, newSharedValue);
145         assertTrue("AS doesn't show the added attribute", as.containsAttribute(SHARED_ATTR_KEY, newSharedValue));
146         
147         // Check the clone is the same
148
AttributeSet JavaDoc clone = as.copyAttributes();
149         assertTrue("as.copyAttributes() != as", clone.isEqual(as));
150         assertTrue("as != as.copyAttributes()", as.isEqual(clone));
151     }
152     
153     public void testDismantle() {
154         AttributeSet JavaDoc as1 = AttributesUtilities.createImmutable("attr1", "attr1-value1");
155         AttributeSet JavaDoc as2 = as1.copyAttributes(); // Proxy
156
AttributeSet JavaDoc as3 = AttributesUtilities.createImmutable("attr2", "attr2-value2");
157         AttributeSet JavaDoc as4 = AttributesUtilities.createComposite(as1, as3); // Composite
158
AttributeSet JavaDoc as5 = AttributesUtilities.createComposite(as2, as4); // Composite
159

160         List JavaDoc<AttributeSet JavaDoc> allSets = (List JavaDoc<AttributeSet JavaDoc>) as5.getAttribute("dismantled-structure");
161         assertEquals("Wrong number of sets in the structure", 3, allSets.size());
162         assertSame("Wrong 1. set in the structure", as1, allSets.get(0));
163         assertSame("Wrong 2. set in the structure", as1, allSets.get(1));
164         assertSame("Wrong 3. set in the structure", as3, allSets.get(2));
165     }
166 }
167
Popular Tags