1 package org.apache.dvsl;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.StringReader;
23 import java.io.StringWriter;
24
25 import junit.framework.TestCase;
26
27 import org.dom4j.Document;
28 import org.dom4j.io.SAXReader;
29
30 /**
31 * Simple testcase to ensure things are basically working.
32 * This tests both a serialized document as well as a 'live'
33 * dom4j one.
34 *
35 * @author <a href="mailto:geirm@apache.org>Geir Magnusson Jr.</a>
36 */
37 public class TransformTest
38 extends TestCase
39 {
40 /*
41 * use simple in-memory style and input strings
42 */
43
44 private String dvslstyle = "#match(\"element\")Hello from element! $node.value()#end";
45 private String input = "<?xml version=\"1.0\"?><document><element>Foo</element></document>";
46
47 public TransformTest( String name )
48 {
49 super(name);
50 }
51
52
53 public void testSelection()
54 {
55 try
56 {
57 doit();
58 }
59 catch( Exception e )
60 {
61 fail( e.getMessage() );
62 }
63 }
64
65 public void doit()
66 throws Exception
67 {
68 /*
69 * make a dvsl
70 */
71
72 DVSL dvsl = new DVSL();
73
74 /*
75 * register the stylesheet
76 */
77
78 dvsl.setStylesheet( new StringReader(dvslstyle) );
79
80 /*
81 * render the document as a Reader
82 */
83
84 StringWriter sw = new StringWriter();
85
86 dvsl.transform( new StringReader( input ), sw );
87
88 if( !sw.toString().equals("Hello from element! Foo"))
89 fail( "Result of first test is wrong : " + sw.toString() );
90
91 /*
92 * now test if we can pass it a Document
93 */
94
95 SAXReader sr = new SAXReader();
96
97 Document document = sr.read( new StringReader(input ) );
98
99 sw = new StringWriter();
100
101 dvsl.transform( document, sw );
102
103 if( !sw.toString().equals("Hello from element! Foo"))
104 fail( "Result of second test is wrong : " + sw.toString() );
105 }
106 }