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;
|
---|
23 |
|
---|
24 | import javax.xml.parsers.DocumentBuilder;
|
---|
25 | import javax.xml.parsers.DocumentBuilderFactory;
|
---|
26 | import java.io.File;
|
---|
27 | import java.io.FileNotFoundException;
|
---|
28 | import java.net.URL;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Abtract testcase for XSLTLiaison.
|
---|
32 | * Override createLiaison for each XSLTLiaison.
|
---|
33 | *
|
---|
34 | * <a href="[email protected]">Stephane Bailliez</a>
|
---|
35 | */
|
---|
36 | public abstract class AbstractXSLTLiaisonTest extends TestCase {
|
---|
37 |
|
---|
38 | protected XSLTLiaison liaison;
|
---|
39 |
|
---|
40 | protected AbstractXSLTLiaisonTest(String name){
|
---|
41 | super(name);
|
---|
42 | }
|
---|
43 |
|
---|
44 | protected void setUp() throws Exception {
|
---|
45 | liaison = createLiaison();
|
---|
46 | }
|
---|
47 |
|
---|
48 | // to override
|
---|
49 | protected abstract XSLTLiaison createLiaison() throws Exception ;
|
---|
50 |
|
---|
51 | /** load the file from the caller classloader that loaded this class */
|
---|
52 | protected File getFile(String name) throws FileNotFoundException {
|
---|
53 | URL url = getClass().getResource(name);
|
---|
54 | if (url == null){
|
---|
55 | throw new FileNotFoundException("Unable to load '" + name + "' from classpath");
|
---|
56 | }
|
---|
57 | return new File(url.getFile());
|
---|
58 | }
|
---|
59 |
|
---|
60 | /** keep it simple stupid */
|
---|
61 | public void testTransform() throws Exception {
|
---|
62 | File xsl = getFile("/taskdefs/optional/xsltliaison-in.xsl");
|
---|
63 | liaison.setStylesheet(xsl);
|
---|
64 | liaison.addParam("param", "value");
|
---|
65 | File in = getFile("/taskdefs/optional/xsltliaison-in.xml");
|
---|
66 | File out = new File("xsltliaison.tmp");
|
---|
67 | try {
|
---|
68 | liaison.transform(in, out);
|
---|
69 | } finally {
|
---|
70 | out.delete();
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | public void testEncoding() throws Exception {
|
---|
75 | File xsl = getFile("/taskdefs/optional/xsltliaison-encoding-in.xsl");
|
---|
76 | liaison.setStylesheet(xsl);
|
---|
77 | File in = getFile("/taskdefs/optional/xsltliaison-encoding-in.xml");
|
---|
78 | File out = new File("xsltliaison-encoding.tmp");
|
---|
79 | try {
|
---|
80 | liaison.transform(in, out);
|
---|
81 | Document 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 parseXML(File file) throws Exception {
|
---|
91 | DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
|
---|
92 | DocumentBuilder dbuilder = dbfactory.newDocumentBuilder();
|
---|
93 | return dbuilder.parse(file);
|
---|
94 | }
|
---|
95 | }
|
---|