KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Copyright 2002 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: HasChildNodesTest.java,v 1.3 2004/08/18 07:16:39 vhardy Exp $
34  */

35 public class HasChildNodesTest 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 HasChildNodesTest(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         while (e.hasChildNodes()) {
82             e.removeChild(e.getFirstChild());
83         }
84
85         return reportSuccess();
86     }
87 }
88
Popular Tags