KickJava   Java API By Example, From Geeks To Geeks.

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


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 appendChild method.
31  *
32  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
33  * @version $Id: AppendChildTest.java,v 1.4 2004/08/18 07:16:39 vhardy Exp $
34  */

35 public class AppendChildTest 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 ERROR_EXCEPTION_NOT_THROWN
40         = "error.exception.not.thrown";
41
42     public static String JavaDoc ENTRY_KEY_ID
43         = "entry.key.id";
44
45     protected String JavaDoc testFileName;
46     protected String JavaDoc rootTag;
47     protected String JavaDoc targetId;
48
49     public AppendChildTest(String JavaDoc file,
50                            String JavaDoc root,
51                            String JavaDoc id) {
52         testFileName = file;
53         rootTag = root;
54         targetId = id;
55     }
56     
57     public TestReport runImpl() throws Exception JavaDoc {
58         String JavaDoc parser =
59             XMLResourceDescriptor.getXMLParserClassName();
60
61         DocumentFactory df
62             = new SAXDocumentFactory
63             (GenericDOMImplementation.getDOMImplementation(), parser);
64
65         File f = (new File(testFileName));
66         URL url = f.toURL();
67         Document doc = df.createDocument(null,
68                                          rootTag,
69                                          url.toString(),
70                                          url.openStream());
71
72         
73         Element e = doc.getElementById(targetId);
74
75         if (e == null){
76             DefaultTestReport report = new DefaultTestReport(this);
77             report.setErrorCode(ERROR_GET_ELEMENT_BY_ID_FAILED);
78             report.addDescriptionEntry(ENTRY_KEY_ID,
79                                        targetId);
80             report.setPassed(false);
81             return report;
82         }
83
84         Document otherDocument = df.createDocument(null,
85                                                    rootTag,
86                                                    url.toString(),
87                                                    url.openStream());
88
89         DocumentFragment docFrag = otherDocument.createDocumentFragment();
90         try {
91             docFrag.appendChild(doc.getDocumentElement());
92         } catch (DOMException ex) {
93             return reportSuccess();
94         }
95
96         DefaultTestReport report = new DefaultTestReport(this);
97         report.setErrorCode(ERROR_EXCEPTION_NOT_THROWN);
98         report.setPassed(false);
99         return report;
100     }
101 }
102
Popular Tags