KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > dom > CloneElementTest


1 /*
2
3    Copyright 2002-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.dom;
19
20 import java.io.File JavaDoc;
21 import java.net.URL JavaDoc;
22
23 import org.apache.batik.dom.util.SAXDocumentFactory;
24 import org.apache.batik.test.AbstractTest;
25 import org.apache.batik.test.DefaultTestReport;
26 import org.apache.batik.test.TestReport;
27 import org.apache.batik.util.XMLResourceDescriptor;
28 import org.w3c.dom.Document JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30 import org.w3c.dom.NamedNodeMap JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32
33
34 /**
35  * This class tests the non-deep cloneNode method for elements.
36  *
37  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
38  * @version $Id: CloneElementTest.java,v 1.5 2004/08/18 07:16:39 vhardy Exp $
39  */

40 public class CloneElementTest extends AbstractTest {
41     protected String JavaDoc testFileName;
42     protected String JavaDoc rootTag;
43     protected String JavaDoc targetId;
44
45     public CloneElementTest(String JavaDoc file, String JavaDoc root, String JavaDoc id) {
46         testFileName = file;
47         rootTag = root;
48         targetId = id;
49     }
50     
51     public TestReport runImpl() throws Exception JavaDoc {
52         String JavaDoc parser =
53             XMLResourceDescriptor.getXMLParserClassName();
54
55         SAXDocumentFactory df =
56             new SAXDocumentFactory
57             (GenericDOMImplementation.getDOMImplementation(), parser);
58
59         File JavaDoc f = (new File JavaDoc(testFileName));
60         URL JavaDoc url = f.toURL();
61         Document JavaDoc doc = df.createDocument(null,
62                                          rootTag,
63                                          url.toString(),
64                                          url.openStream());
65         
66         Element JavaDoc e = doc.getElementById(targetId);
67
68         if (e == null){
69             DefaultTestReport report = new DefaultTestReport(this);
70             report.setErrorCode("error.get.element.by.id.failed");
71             report.addDescriptionEntry("entry.key.id", targetId);
72             report.setPassed(false);
73             return report;
74         }
75
76         Element JavaDoc celt = (Element JavaDoc)e.cloneNode(false);
77
78         NamedNodeMap JavaDoc attrs = e.getAttributes();
79
80         for (int i = 0; i < attrs.getLength(); i++) {
81             Node JavaDoc attr = attrs.item(i);
82             String JavaDoc ns = attr.getNamespaceURI();
83             String JavaDoc name = (ns == null)
84                 ? attr.getNodeName()
85                 : attr.getLocalName();
86             String JavaDoc val = attr.getNodeValue();
87             String JavaDoc val2 = celt.getAttributeNS(ns, name);
88             if (!val.equals(val2)) {
89                 DefaultTestReport report = new DefaultTestReport(this);
90                 report.setErrorCode("error.attr.comparison.failed");
91                 report.addDescriptionEntry("entry.attr.name", name);
92                 report.addDescriptionEntry("entry.attr.value1", val);
93                 report.addDescriptionEntry("entry.attr.value2", val2);
94                 report.setPassed(false);
95                 return report;
96             }
97         }
98
99         return reportSuccess();
100     }
101 }
102
Popular Tags