1 7 8 package org.netbeans.modules.xml.xdm.nodes; 9 10 import junit.framework.*; 11 import org.netbeans.modules.xml.xdm.Util; 12 import org.netbeans.modules.xml.xdm.XDMModel; 13 14 18 public class AttributeTest extends TestCase { 19 20 public AttributeTest(String testName) { 21 super(testName); 22 } 23 24 protected void setUp() throws Exception { 25 xmlModel = Util.loadXDMModel("nodes/xdm.xml"); 26 um = new javax.swing.undo.UndoManager (); 27 um.setLimit(10); 28 xmlModel.addUndoableEditListener(um); 29 xmlModel.sync(); 30 attr = (Attribute)xmlModel.getDocument().getChildNodes().item(0). 31 getChildNodes().item(1).getAttributes().item(0); 32 } 33 34 public static Test suite() { 35 TestSuite suite = new TestSuite(AttributeTest.class); 36 37 return suite; 38 } 39 40 43 public void testGetNodeType() { 44 45 short expResult = org.w3c.dom.Node.ATTRIBUTE_NODE; 46 short result = attr.getNodeType(); 47 assertEquals("getNodeType must return ATTRIBUTE_NODE",expResult, result); 48 } 49 50 53 public void testGetNodeName() { 54 55 String expResult = "ssn"; 56 String result = attr.getNodeName(); 57 assertEquals(expResult, result); 58 } 59 60 63 public void testGetNodeValue() { 64 65 String expResult = "xx-xx-xxxx"; 66 String result = attr.getNodeValue(); 67 assertEquals(expResult, result); 68 } 69 70 73 public void testGetOwnerElement() { 74 75 Element expResult = (Element)xmlModel.getDocument().getChildNodes().item(0). 76 getChildNodes().item(1); 77 78 org.w3c.dom.Element result = attr.getOwnerElement(); 79 assertEquals(expResult, result); 80 } 81 82 85 public void testGetLocalName() { 86 87 String expResult = "ssn"; 88 String result = attr.getLocalName(); 89 assertEquals(expResult, result); 90 91 Attribute instance = new Attribute("xs:attribute"); 92 expResult = "attribute"; 93 result = instance.getLocalName(); 94 assertEquals(expResult, result); 95 } 96 97 100 public void testGetPrefix() { 101 102 assertNull(attr.getPrefix()); 103 104 Attribute instance = new Attribute("xs:attribute"); 105 String expResult = "xs"; 106 String result = instance.getPrefix(); 107 assertEquals(expResult, result); 108 } 109 110 113 public void testSetPrefix() { 114 Attribute oldAttr = attr; 115 String oldPrefix = attr.getPrefix(); 116 String newPrefix = "xs"; 117 try { 118 attr.setPrefix(newPrefix); 119 assertTrue("setPrefix must throw exception for attribute node in tree",false); 120 } catch (Exception e) { 121 assertTrue(true); 122 } 123 Attribute newAttr = (Attribute)attr.clone(true,false,false); 124 try { 125 newAttr.setPrefix(newPrefix); 126 assertTrue(true); 127 } catch (Exception e) { 128 assertTrue("setPrefix must not throw exception for attribute node not in tree",false); 129 } 130 xmlModel.modify(attr,newAttr); 131 attr = (Attribute)xmlModel.getDocument().getChildNodes().item(0). 132 getChildNodes().item(1).getAttributes().item(0); 133 assertEquals(newPrefix,attr.getPrefix()); 134 135 um.undo(); 137 attr = (Attribute)xmlModel.getDocument().getChildNodes().item(0). 138 getChildNodes().item(1).getAttributes().item(0); 139 assertSame(oldAttr,attr); 140 assertNull(attr.getPrefix()); 141 um.redo(); 142 143 attr = (Attribute)xmlModel.getDocument().getChildNodes().item(0). 145 getChildNodes().item(1).getAttributes().item(0); 146 oldAttr = attr; 147 newAttr = (Attribute)attr.clone(true,false,false); 148 try { 149 newAttr.setPrefix(""); 150 assertTrue(true); 151 } catch (Exception e) { 152 assertTrue("setPrefix must not throw exception for attribute node not in tree",false); 153 } 154 155 um.discardAllEdits(); 157 xmlModel.modify(attr,newAttr); 158 attr = (Attribute)xmlModel.getDocument().getChildNodes().item(0). 159 getChildNodes().item(1).getAttributes().item(0); 160 assertNull(attr.getPrefix()); 161 162 um.undo(); 164 attr = (Attribute)xmlModel.getDocument().getChildNodes().item(0). 165 getChildNodes().item(1).getAttributes().item(0); 166 assertSame(oldAttr,attr); 167 assertEquals(newPrefix,attr.getPrefix()); 168 } 169 170 173 public void testGetName() { 174 175 String expResult = "ssn"; 176 String result = attr.getName(); 177 assertEquals(expResult, result); 178 179 Attribute instance = new Attribute("xs:attribute"); 180 expResult = "xs:attribute"; 181 result = instance.getName(); 182 assertEquals(expResult, result); 183 } 184 185 188 public void testSetName() { 189 190 Attribute oldAttr = attr; 191 String oldName = attr.getName(); 192 String newName = "ssn1"; 193 try { 194 attr.setName(newName); 195 assertTrue("setName must throw exception for attribute node in tree",false); 196 } catch (Exception e) { 197 assertTrue(true); 198 } 199 Attribute newAttr = (Attribute)attr.clone(true,false,false); 200 try { 201 newAttr.setName(newName); 202 assertTrue(true); 203 } catch (Exception e) { 204 assertTrue("setName must not throw exception for attribute node not in tree",false); 205 } 206 xmlModel.modify(attr,newAttr); 207 attr = (Attribute)xmlModel.getDocument().getChildNodes().item(0). 208 getChildNodes().item(1).getAttributes().item(0); 209 assertEquals(newName,attr.getName()); 210 211 um.undo(); 213 attr = (Attribute)xmlModel.getDocument().getChildNodes().item(0). 214 getChildNodes().item(1).getAttributes().item(0); 215 assertSame(oldAttr,attr); 216 assertEquals(oldName,attr.getName()); 217 } 218 219 222 public void testGetValue() { 223 224 String expResult = "xx-xx-xxxx"; 225 String result = attr.getValue(); 226 assertEquals(expResult, result); 227 } 228 229 232 public void testSetValue() { 233 234 Attribute oldAttr = attr; 235 String oldValue = oldAttr.getValue(); 236 String newValue = "123-45-6789"; 237 try { 238 attr.setValue(newValue); 239 assertTrue("setValue must throw exception for attribute node in tree",false); 240 } catch (Exception e) { 241 assertTrue(true); 242 } 243 Attribute newAttr = (Attribute)attr.clone(true,false,false); 244 try { 245 newAttr.setValue(newValue); 246 assertTrue(true); 247 } catch (Exception e) { 248 assertTrue("setValue must not throw exception for attribute node not in tree",false); 249 } 250 xmlModel.modify(attr,newAttr); 251 attr = (Attribute)xmlModel.getDocument().getChildNodes().item(0). 252 getChildNodes().item(1).getAttributes().item(0); 253 assertEquals(newValue,attr.getValue()); 254 um.undo(); 256 attr = (Attribute)xmlModel.getDocument().getChildNodes().item(0). 257 getChildNodes().item(1).getAttributes().item(0); 258 assertSame(oldAttr,attr); 259 assertEquals(oldValue,attr.getValue()); 260 } 261 262 private XDMModel xmlModel; 263 private Attribute attr; 264 private javax.swing.undo.UndoManager um; 265 } 266 | Popular Tags |