1 19 20 package org.netbeans.api.editor.settings; 21 22 import java.awt.Color ; 23 import java.util.ArrayList ; 24 import java.util.HashMap ; 25 import java.util.List ; 26 import javax.swing.text.AttributeSet ; 27 import javax.swing.text.SimpleAttributeSet ; 28 import org.netbeans.junit.NbTestCase; 29 30 34 public class AttributesUtilitiesTest extends NbTestCase { 35 36 37 public AttributesUtilitiesTest(String name) { 38 super(name); 39 } 40 41 public void testCreateImmutablePairs() { 42 Object [] pairs = new Object [] { 43 "key-1", "value-1", 44 new HashMap (), new ArrayList (), 45 new Integer (123456), new Character ('.') 46 }; 47 48 AttributeSet 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 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 SHARED_ATTR_KEY = "SHARED-ATTRIBUTE"; 59 final Object SHARED_ATTR_FIRST_VALUE = new Color (0); 60 61 SimpleAttributeSet setA = new SimpleAttributeSet (); 62 SimpleAttributeSet setB = new SimpleAttributeSet (); 63 SimpleAttributeSet setC = new SimpleAttributeSet (); 64 65 setA.addAttribute(new ArrayList (2), new Long (1234)); 66 setA.addAttribute(new HashMap (2), "AAA"); 67 setA.addAttribute(new Object (), SimpleAttributeSet.EMPTY); 68 setA.addAttribute(new Double ("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 (2), new Character (',')); 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 as = AttributesUtilities.createImmutable(setA, setB, setC); 79 80 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 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 SHARED_ATTR_KEY = "SHARED-ATTRIBUTE"; 103 final Object SHARED_ATTR_LAST_VALUE = new Color (0); 104 105 SimpleAttributeSet setA = new SimpleAttributeSet (); 106 SimpleAttributeSet setB = new SimpleAttributeSet (); 107 SimpleAttributeSet setC = new SimpleAttributeSet (); 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 (2), new Character (',')); 115 116 setC.addAttribute(new ArrayList (2), new Long (1234)); 117 setC.addAttribute(new HashMap (2), "AAA"); 118 setC.addAttribute(new Object (), SimpleAttributeSet.EMPTY); 119 setC.addAttribute(new Double ("1.1"), setA); 120 setC.addAttribute(SHARED_ATTR_KEY, SHARED_ATTR_LAST_VALUE); 121 122 AttributeSet as = AttributesUtilities.createComposite(setC, setB, setA); 123 124 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 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 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 AttributeSet 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 as1 = AttributesUtilities.createImmutable("attr1", "attr1-value1"); 155 AttributeSet as2 = as1.copyAttributes(); AttributeSet as3 = AttributesUtilities.createImmutable("attr2", "attr2-value2"); 157 AttributeSet as4 = AttributesUtilities.createComposite(as1, as3); AttributeSet as5 = AttributesUtilities.createComposite(as2, as4); 160 List <AttributeSet > allSets = (List <AttributeSet >) 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 |