KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > AbstractXSLTLiaisonTest


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

19
20 import junit.framework.TestCase;
21 import org.apache.tools.ant.taskdefs.XSLTLiaison;
22 import org.w3c.dom.Document JavaDoc;
23
24 import javax.xml.parsers.DocumentBuilder JavaDoc;
25 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileNotFoundException JavaDoc;
28 import java.net.URL JavaDoc;
29
30 /**
31  * Abtract testcase for XSLTLiaison.
32  * Override createLiaison for each XSLTLiaison.
33  *
34  * <a HREF="sbailliez@apache.org">Stephane Bailliez</a>
35  */

36 public abstract class AbstractXSLTLiaisonTest extends TestCase {
37
38     protected XSLTLiaison liaison;
39
40     protected AbstractXSLTLiaisonTest(String JavaDoc name){
41         super(name);
42     }
43
44     protected void setUp() throws Exception JavaDoc {
45         liaison = createLiaison();
46     }
47
48     // to override
49
protected abstract XSLTLiaison createLiaison() throws Exception JavaDoc ;
50
51     /** load the file from the caller classloader that loaded this class */
52     protected File JavaDoc getFile(String JavaDoc name) throws FileNotFoundException JavaDoc {
53         URL JavaDoc url = getClass().getResource(name);
54         if (url == null){
55           throw new FileNotFoundException JavaDoc("Unable to load '" + name + "' from classpath");
56         }
57         return new File JavaDoc(url.getFile());
58     }
59
60     /** keep it simple stupid */
61     public void testTransform() throws Exception JavaDoc {
62         File JavaDoc xsl = getFile("/taskdefs/optional/xsltliaison-in.xsl");
63         liaison.setStylesheet(xsl);
64         liaison.addParam("param", "value");
65         File JavaDoc in = getFile("/taskdefs/optional/xsltliaison-in.xml");
66         File JavaDoc out = new File JavaDoc("xsltliaison.tmp");
67         try {
68             liaison.transform(in, out);
69         } finally {
70             out.delete();
71         }
72     }
73
74     public void testEncoding() throws Exception JavaDoc {
75         File JavaDoc xsl = getFile("/taskdefs/optional/xsltliaison-encoding-in.xsl");
76         liaison.setStylesheet(xsl);
77         File JavaDoc in = getFile("/taskdefs/optional/xsltliaison-encoding-in.xml");
78         File JavaDoc out = new File JavaDoc("xsltliaison-encoding.tmp");
79         try {
80             liaison.transform(in, out);
81             Document JavaDoc doc = parseXML(out);
82             assertEquals("root",doc.getDocumentElement().getNodeName());
83             assertEquals("message",doc.getDocumentElement().getFirstChild().getNodeName());
84             assertEquals("\u00E9\u00E0\u00E8\u00EF\u00F9",doc.getDocumentElement().getFirstChild().getFirstChild().getNodeValue());
85         } finally {
86             out.delete();
87         }
88     }
89
90     public Document JavaDoc parseXML(File JavaDoc file) throws Exception JavaDoc {
91         DocumentBuilderFactory JavaDoc dbfactory = DocumentBuilderFactory.newInstance();
92         DocumentBuilder JavaDoc dbuilder = dbfactory.newDocumentBuilder();
93         return dbuilder.parse(file);
94     }
95 }
96
Popular Tags