KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xdm > nodes > AttributeTest


1 /*
2  * AttributeTest.java
3  * JUnit based test
4  *
5  * Created on October 21, 2005, 2:21 PM
6  */

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 /**
15  *
16  * @author ajit
17  */

18 public class AttributeTest extends TestCase {
19     
20     public AttributeTest(String JavaDoc testName) {
21         super(testName);
22     }
23
24     protected void setUp() throws Exception JavaDoc {
25         xmlModel = Util.loadXDMModel("nodes/xdm.xml");
26         um = new javax.swing.undo.UndoManager JavaDoc();
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     /**
41      * Test of getNodeType method, of class org.netbeans.modules.xml.xdm.nodes.Attribute.
42      */

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     /**
51      * Test of getNodeName method, of class org.netbeans.modules.xml.xdm.nodes.Attribute.
52      */

53     public void testGetNodeName() {
54         
55         String JavaDoc expResult = "ssn";
56         String JavaDoc result = attr.getNodeName();
57         assertEquals(expResult, result);
58     }
59
60     /**
61      * Test of getNodeValue method, of class org.netbeans.modules.xml.xdm.nodes.Attribute.
62      */

63     public void testGetNodeValue() {
64         
65         String JavaDoc expResult = "xx-xx-xxxx";
66         String JavaDoc result = attr.getNodeValue();
67         assertEquals(expResult, result);
68     }
69
70     /**
71      * Test of getOwnerElement method, of class org.netbeans.modules.xml.xdm.nodes.Attribute.
72      */

73     public void testGetOwnerElement() {
74         
75         Element expResult = (Element)xmlModel.getDocument().getChildNodes().item(0).
76                 getChildNodes().item(1);
77         
78         org.w3c.dom.Element JavaDoc result = attr.getOwnerElement();
79         assertEquals(expResult, result);
80     }
81
82     /**
83      * Test of getLocalName method, of class org.netbeans.modules.xml.xdm.nodes.Attribute.
84      */

85     public void testGetLocalName() {
86         
87         String JavaDoc expResult = "ssn";
88         String JavaDoc 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     /**
98      * Test of getPrefix method, of class org.netbeans.modules.xml.xdm.nodes.Attribute.
99      */

100     public void testGetPrefix() {
101         
102         assertNull(attr.getPrefix());
103
104         Attribute instance = new Attribute("xs:attribute");
105         String JavaDoc expResult = "xs";
106         String JavaDoc result = instance.getPrefix();
107         assertEquals(expResult, result);
108     }
109
110     /**
111      * Test of setPrefix method, of class org.netbeans.modules.xml.xdm.nodes.Attribute.
112      */

113     public void testSetPrefix() {
114         Attribute oldAttr = attr;
115         String JavaDoc oldPrefix = attr.getPrefix();
116         String JavaDoc newPrefix = "xs";
117         try {
118             attr.setPrefix(newPrefix);
119             assertTrue("setPrefix must throw exception for attribute node in tree",false);
120         } catch (Exception JavaDoc 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 JavaDoc 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         //try undo and make sure original tree is not changed
136
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         // try to remove prefix
144
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 JavaDoc e) {
152             assertTrue("setPrefix must not throw exception for attribute node not in tree",false);
153         }
154         
155         // now that xdmmodelundoableedit can absorb, need to control absorption
156
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         //try undo and make sure previous tree is not changed
163
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     /**
171      * Test of getName method, of class org.netbeans.modules.xml.xdm.nodes.Attribute.
172      */

173     public void testGetName() {
174         
175         String JavaDoc expResult = "ssn";
176         String JavaDoc 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     /**
186      * Test of setName method, of class org.netbeans.modules.xml.xdm.nodes.Attribute.
187      */

188     public void testSetName() {
189         
190         Attribute oldAttr = attr;
191         String JavaDoc oldName = attr.getName();
192         String JavaDoc newName = "ssn1";
193         try {
194             attr.setName(newName);
195             assertTrue("setName must throw exception for attribute node in tree",false);
196         } catch (Exception JavaDoc 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 JavaDoc 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         //try undo and make sure old tree is not changed
212
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     /**
220      * Test of getValue method, of class org.netbeans.modules.xml.xdm.nodes.Attribute.
221      */

222     public void testGetValue() {
223         
224         String JavaDoc expResult = "xx-xx-xxxx";
225         String JavaDoc result = attr.getValue();
226         assertEquals(expResult, result);
227     }
228
229     /**
230      * Test of setValue method, of class org.netbeans.modules.xml.xdm.nodes.Attribute.
231      */

232     public void testSetValue() {
233         
234         Attribute oldAttr = attr;
235         String JavaDoc oldValue = oldAttr.getValue();
236         String JavaDoc 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 JavaDoc 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 JavaDoc 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         //try undo and make sure old tree is not changed
255
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 JavaDoc um;
265 }
266
Popular Tags