KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestXSLT


1 /*
2  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
6  *
7  * $Id: TestXSLT.java,v 1.1 2003/07/07 10:30:29 per_nyfelt Exp $
8  */

9
10 package test.dom4j;
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15 import org.dom4j.Document;
16 import org.dom4j.io.DocumentResult;
17 import org.dom4j.io.DocumentSource;
18 import org.dom4j.io.SAXReader;
19
20 import javax.xml.transform.Source JavaDoc;
21 import javax.xml.transform.Transformer JavaDoc;
22 import javax.xml.transform.TransformerFactory JavaDoc;
23 import javax.xml.transform.stream.StreamSource JavaDoc;
24 import java.util.List JavaDoc;
25
26 /** Tests that XSLT works correctly
27   *
28   * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
29   * @version $Revision: 1.1 $
30   */

31 public class TestXSLT extends AbstractTestCase {
32
33     public static void main( String JavaDoc[] args ) {
34         TestRunner.run( suite() );
35     }
36
37     public static Test suite() {
38         return new TestSuite( TestXSLT.class );
39     }
40
41     public TestXSLT(String JavaDoc name) {
42         super(name);
43     }
44
45     // Test case(s)
46
//-------------------------------------------------------------------------
47
public void testTransform() throws Exception JavaDoc {
48         Document transformedDoc = transform( "xml/nitf/ashtml.xsl" );
49
50         //log( transformedDoc.asXML() );
51

52         assertTrue( "Transformed Document is not null", transformedDoc != null );
53
54         List JavaDoc h1List = transformedDoc.selectNodes( "/html//h1" );
55
56         assertTrue( "At least one <h1>", h1List.size() > 0 );
57
58         List JavaDoc pList = transformedDoc.selectNodes( "//p" );
59
60         assertTrue( "At least one <p>", pList.size() > 0 );
61     }
62
63     // Implementation methods
64
//-------------------------------------------------------------------------
65
protected void setUp() throws Exception JavaDoc {
66         SAXReader reader = new SAXReader();
67         document = reader.read( "xml/nitf/sample.xml" );
68     }
69
70     protected Document transform(String JavaDoc xsl) throws Exception JavaDoc {
71         assertTrue( "Document is not null", document != null );
72
73         // load the transformer
74
TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
75         Transformer JavaDoc transformer = factory.newTransformer(
76             new StreamSource JavaDoc( xsl )
77         );
78
79         // now lets create the TrAX source and result
80
// objects and do the transformation
81
Source source = new DocumentSource( document );
82         DocumentResult result = new DocumentResult();
83         transformer.transform( source, result );
84
85         return result.getDocument();
86     }
87
88 }
89
90
91
92
93 /*
94  * Redistribution and use of this software and associated documentation
95  * ("Software"), with or without modification, are permitted provided
96  * that the following conditions are met:
97  *
98  * 1. Redistributions of source code must retain copyright
99  * statements and notices. Redistributions must also contain a
100  * copy of this document.
101  *
102  * 2. Redistributions in binary form must reproduce the
103  * above copyright notice, this list of conditions and the
104  * following disclaimer in the documentation and/or other
105  * materials provided with the distribution.
106  *
107  * 3. The name "DOM4J" must not be used to endorse or promote
108  * products derived from this Software without prior written
109  * permission of MetaStuff, Ltd. For written permission,
110  * please contact dom4j-info@metastuff.com.
111  *
112  * 4. Products derived from this Software may not be called "DOM4J"
113  * nor may "DOM4J" appear in their names without prior written
114  * permission of MetaStuff, Ltd. DOM4J is a registered
115  * trademark of MetaStuff, Ltd.
116  *
117  * 5. Due credit should be given to the DOM4J Project
118  * (http://dom4j.org/).
119  *
120  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
121  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
122  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
123  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
124  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
125  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
126  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
127  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
128  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
129  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
130  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
131  * OF THE POSSIBILITY OF SUCH DAMAGE.
132  *
133  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
134  *
135  * $Id: TestXSLT.java,v 1.1 2003/07/07 10:30:29 per_nyfelt Exp $
136  */

137
Popular Tags