KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.w3c.dom.*;
21
22 import java.io.*;
23 import java.net.*;
24 import org.apache.batik.dom.util.*;
25 import org.apache.batik.util.*;
26
27 import org.apache.batik.test.*;
28
29 /**
30  * This class tests the hasChildNodes method.
31  *
32  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
33  * @version $Id: ReplaceChildTest.java,v 1.4 2004/08/18 07:16:40 vhardy Exp $
34  */

35 public class ReplaceChildTest extends AbstractTest {
36     public static String JavaDoc ERROR_GET_ELEMENT_BY_ID_FAILED
37         = "error.get.element.by.id.failed";
38
39     public static String JavaDoc ENTRY_KEY_ID
40         = "entry.key.id";
41
42     protected String JavaDoc testFileName;
43     protected String JavaDoc rootTag;
44     protected String JavaDoc targetId;
45
46     public ReplaceChildTest(String JavaDoc file,
47                              String JavaDoc root,
48                              String JavaDoc id) {
49         testFileName = file;
50         rootTag = root;
51         targetId = id;
52     }
53
54     public TestReport runImpl() throws Exception JavaDoc {
55         String JavaDoc parser =
56             XMLResourceDescriptor.getXMLParserClassName();
57
58         DocumentFactory df
59             = new SAXDocumentFactory
60             (GenericDOMImplementation.getDOMImplementation(), parser);
61
62         File f = (new File(testFileName));
63         URL url = f.toURL();
64         Document doc = df.createDocument(null,
65                                          rootTag,
66                                          url.toString(),
67                                          url.openStream());
68
69         
70         Element e = doc.getElementById(targetId);
71
72         if (e == null){
73             DefaultTestReport report = new DefaultTestReport(this);
74             report.setErrorCode(ERROR_GET_ELEMENT_BY_ID_FAILED);
75             report.addDescriptionEntry(ENTRY_KEY_ID,
76                                        targetId);
77             report.setPassed(false);
78             return report;
79         }
80
81         Element fc = null;
82         for (Node n = e.getFirstChild();
83              n != null;
84              n = n.getNextSibling()) {
85             if (n.getNodeType() == Node.ELEMENT_NODE) {
86                 fc = (Element)n;
87                 break;
88             }
89         }
90         Element ne = doc.createElementNS(null, "elt4");
91         e.replaceChild(ne, fc);
92
93         if (ne.getParentNode() != e || fc.getParentNode() != null) {
94             DefaultTestReport report = new DefaultTestReport(this);
95             report.setErrorCode(TestReport.ERROR_TEST_FAILED);
96             report.setPassed(false);
97             return report;
98         }
99         return reportSuccess();
100     }
101 }
102
Popular Tags