001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * AttributeTest.java
044: * JUnit based test
045: *
046: * Created on October 21, 2005, 2:21 PM
047: */
048:
049: package org.netbeans.modules.xml.xdm.nodes;
050:
051: import junit.framework.*;
052: import org.netbeans.modules.xml.xdm.Util;
053: import org.netbeans.modules.xml.xdm.XDMModel;
054:
055: /**
056: *
057: * @author ajit
058: */
059: public class AttributeTest extends TestCase {
060:
061: public AttributeTest(String testName) {
062: super (testName);
063: }
064:
065: protected void setUp() throws Exception {
066: xmlModel = Util.loadXDMModel("nodes/xdm.xml");
067: um = new javax.swing.undo.UndoManager();
068: um.setLimit(10);
069: xmlModel.addUndoableEditListener(um);
070: xmlModel.sync();
071: attr = (Attribute) xmlModel.getDocument().getChildNodes().item(
072: 0).getChildNodes().item(1).getAttributes().item(0);
073: }
074:
075: public static Test suite() {
076: TestSuite suite = new TestSuite(AttributeTest.class);
077:
078: return suite;
079: }
080:
081: /**
082: * Test of getNodeType method, of class org.netbeans.modules.xml.xdm.nodes.Attribute.
083: */
084: public void testGetNodeType() {
085:
086: short expResult = org.w3c.dom.Node.ATTRIBUTE_NODE;
087: short result = attr.getNodeType();
088: assertEquals("getNodeType must return ATTRIBUTE_NODE",
089: expResult, result);
090: }
091:
092: /**
093: * Test of getNodeName method, of class org.netbeans.modules.xml.xdm.nodes.Attribute.
094: */
095: public void testGetNodeName() {
096:
097: String expResult = "ssn";
098: String result = attr.getNodeName();
099: assertEquals(expResult, result);
100: }
101:
102: /**
103: * Test of getNodeValue method, of class org.netbeans.modules.xml.xdm.nodes.Attribute.
104: */
105: public void testGetNodeValue() {
106:
107: String expResult = "xx-xx-xxxx";
108: String result = attr.getNodeValue();
109: assertEquals(expResult, result);
110: }
111:
112: /**
113: * Test of getOwnerElement method, of class org.netbeans.modules.xml.xdm.nodes.Attribute.
114: */
115: public void testGetOwnerElement() {
116:
117: Element expResult = (Element) xmlModel.getDocument()
118: .getChildNodes().item(0).getChildNodes().item(1);
119:
120: org.w3c.dom.Element result = attr.getOwnerElement();
121: assertEquals(expResult, result);
122: }
123:
124: /**
125: * Test of getLocalName method, of class org.netbeans.modules.xml.xdm.nodes.Attribute.
126: */
127: public void testGetLocalName() {
128:
129: String expResult = "ssn";
130: String result = attr.getLocalName();
131: assertEquals(expResult, result);
132:
133: Attribute instance = new Attribute("xs:attribute");
134: expResult = "attribute";
135: result = instance.getLocalName();
136: assertEquals(expResult, result);
137: }
138:
139: /**
140: * Test of getPrefix method, of class org.netbeans.modules.xml.xdm.nodes.Attribute.
141: */
142: public void testGetPrefix() {
143:
144: assertNull(attr.getPrefix());
145:
146: Attribute instance = new Attribute("xs:attribute");
147: String expResult = "xs";
148: String result = instance.getPrefix();
149: assertEquals(expResult, result);
150: }
151:
152: /**
153: * Test of setPrefix method, of class org.netbeans.modules.xml.xdm.nodes.Attribute.
154: */
155: public void testSetPrefix() {
156: Attribute oldAttr = attr;
157: String oldPrefix = attr.getPrefix();
158: String newPrefix = "xs";
159: try {
160: attr.setPrefix(newPrefix);
161: assertTrue(
162: "setPrefix must throw exception for attribute node in tree",
163: false);
164: } catch (Exception e) {
165: assertTrue(true);
166: }
167: Attribute newAttr = (Attribute) attr.clone(true, false, false);
168: try {
169: newAttr.setPrefix(newPrefix);
170: assertTrue(true);
171: } catch (Exception e) {
172: assertTrue(
173: "setPrefix must not throw exception for attribute node not in tree",
174: false);
175: }
176: xmlModel.modify(attr, newAttr);
177: attr = (Attribute) xmlModel.getDocument().getChildNodes().item(
178: 0).getChildNodes().item(1).getAttributes().item(0);
179: assertEquals(newPrefix, attr.getPrefix());
180:
181: //try undo and make sure original tree is not changed
182: um.undo();
183: attr = (Attribute) xmlModel.getDocument().getChildNodes().item(
184: 0).getChildNodes().item(1).getAttributes().item(0);
185: assertSame(oldAttr, attr);
186: assertNull(attr.getPrefix());
187: um.redo();
188:
189: // try to remove prefix
190: attr = (Attribute) xmlModel.getDocument().getChildNodes().item(
191: 0).getChildNodes().item(1).getAttributes().item(0);
192: oldAttr = attr;
193: newAttr = (Attribute) attr.clone(true, false, false);
194: try {
195: newAttr.setPrefix("");
196: assertTrue(true);
197: } catch (Exception e) {
198: assertTrue(
199: "setPrefix must not throw exception for attribute node not in tree",
200: false);
201: }
202:
203: // now that xdmmodelundoableedit can absorb, need to control absorption
204: um.discardAllEdits();
205: xmlModel.modify(attr, newAttr);
206: attr = (Attribute) xmlModel.getDocument().getChildNodes().item(
207: 0).getChildNodes().item(1).getAttributes().item(0);
208: assertNull(attr.getPrefix());
209:
210: //try undo and make sure previous tree is not changed
211: um.undo();
212: attr = (Attribute) xmlModel.getDocument().getChildNodes().item(
213: 0).getChildNodes().item(1).getAttributes().item(0);
214: assertSame(oldAttr, attr);
215: assertEquals(newPrefix, attr.getPrefix());
216: }
217:
218: /**
219: * Test of getName method, of class org.netbeans.modules.xml.xdm.nodes.Attribute.
220: */
221: public void testGetName() {
222:
223: String expResult = "ssn";
224: String result = attr.getName();
225: assertEquals(expResult, result);
226:
227: Attribute instance = new Attribute("xs:attribute");
228: expResult = "xs:attribute";
229: result = instance.getName();
230: assertEquals(expResult, result);
231: }
232:
233: /**
234: * Test of setName method, of class org.netbeans.modules.xml.xdm.nodes.Attribute.
235: */
236: public void testSetName() {
237:
238: Attribute oldAttr = attr;
239: String oldName = attr.getName();
240: String newName = "ssn1";
241: try {
242: attr.setName(newName);
243: assertTrue(
244: "setName must throw exception for attribute node in tree",
245: false);
246: } catch (Exception e) {
247: assertTrue(true);
248: }
249: Attribute newAttr = (Attribute) attr.clone(true, false, false);
250: try {
251: newAttr.setName(newName);
252: assertTrue(true);
253: } catch (Exception e) {
254: assertTrue(
255: "setName must not throw exception for attribute node not in tree",
256: false);
257: }
258: xmlModel.modify(attr, newAttr);
259: attr = (Attribute) xmlModel.getDocument().getChildNodes().item(
260: 0).getChildNodes().item(1).getAttributes().item(0);
261: assertEquals(newName, attr.getName());
262:
263: //try undo and make sure old tree is not changed
264: um.undo();
265: attr = (Attribute) xmlModel.getDocument().getChildNodes().item(
266: 0).getChildNodes().item(1).getAttributes().item(0);
267: assertSame(oldAttr, attr);
268: assertEquals(oldName, attr.getName());
269: }
270:
271: /**
272: * Test of getValue method, of class org.netbeans.modules.xml.xdm.nodes.Attribute.
273: */
274: public void testGetValue() {
275:
276: String expResult = "xx-xx-xxxx";
277: String result = attr.getValue();
278: assertEquals(expResult, result);
279: }
280:
281: /**
282: * Test of setValue method, of class org.netbeans.modules.xml.xdm.nodes.Attribute.
283: */
284: public void testSetValue() {
285:
286: Attribute oldAttr = attr;
287: String oldValue = oldAttr.getValue();
288: String newValue = "123-45-6789";
289: try {
290: attr.setValue(newValue);
291: assertTrue(
292: "setValue must throw exception for attribute node in tree",
293: false);
294: } catch (Exception e) {
295: assertTrue(true);
296: }
297: Attribute newAttr = (Attribute) attr.clone(true, false, false);
298: try {
299: newAttr.setValue(newValue);
300: assertTrue(true);
301: } catch (Exception e) {
302: assertTrue(
303: "setValue must not throw exception for attribute node not in tree",
304: false);
305: }
306: xmlModel.modify(attr, newAttr);
307: attr = (Attribute) xmlModel.getDocument().getChildNodes().item(
308: 0).getChildNodes().item(1).getAttributes().item(0);
309: assertEquals(newValue, attr.getValue());
310: //try undo and make sure old tree is not changed
311: um.undo();
312: attr = (Attribute) xmlModel.getDocument().getChildNodes().item(
313: 0).getChildNodes().item(1).getAttributes().item(0);
314: assertSame(oldAttr, attr);
315: assertEquals(oldValue, attr.getValue());
316: }
317:
318: private XDMModel xmlModel;
319: private Attribute attr;
320: private javax.swing.undo.UndoManager um;
321: }
|