KickJava   Java API By Example, From Geeks To Geeks.

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


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

16
17 package org.apache.batik.dom;
18
19 import org.w3c.dom.*;
20
21 import java.io.*;
22 import java.net.*;
23 import org.apache.batik.dom.util.*;
24 import org.apache.batik.util.*;
25
26 import org.apache.batik.test.*;
27
28 /**
29  * @author <a HREF="mailto:shillion@ilog.fr">Stephane Hillion</a>
30  * @version $Id: SetAttributeTest.java,v 1.5 2005/04/01 02:28:16 deweese Exp $
31  */

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