1
2
3
4
5
6
7
8 package org.dom4j.rule;
9
10 import junit.textui.TestRunner;
11
12 import org.dom4j.AbstractTestCase;
13 import org.dom4j.Document;
14 import org.dom4j.DocumentHelper;
15 import org.dom4j.Node;
16 import org.dom4j.xpath.DefaultXPath;
17
18 /***
19 * A test harness to test the use of the Stylesheet and the XSLT rule engine.
20 *
21 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
22 * @version $Revision: 1.4 $
23 */
24 public class StylesheetTest extends AbstractTestCase {
25 protected String[] templates = {
26 "/",
27 "*",
28 "root",
29 "author",
30 "@name",
31 "root/author",
32 "author[@location='UK']",
33 "root/author[@location='UK']",
34 "root//author[@location='UK']"};
35
36 protected String[] templates2 = {"/", "title", "para", "*"};
37
38 protected Stylesheet stylesheet;
39
40 public static void main(String[] args) {
41 TestRunner.run(StylesheetTest.class);
42 }
43
44
45
46 public void testRules() throws Exception {
47 for (int i = 0, size = templates.length; i < size; i++) {
48 addTemplate(templates[i]);
49 }
50
51 log("");
52 log("........................................");
53 log("");
54 log("Running stylesheet");
55
56 stylesheet.run(document);
57
58 log("Finished");
59 }
60
61 public void testLittleDoc() throws Exception {
62 for (int i = 0, size = templates2.length; i < size; i++) {
63 addTemplate(templates2[i]);
64 }
65 Document doc = getDocument("/xml/test/littledoc.xml");
66
67 stylesheet = new Stylesheet();
68 stylesheet.setValueOfAction(new Action() {
69 public void run(Node node) {
70 log("Default ValueOf action on node: " + node);
71 log("........................................");
72 }
73 });
74
75 stylesheet.run(doc);
76 }
77
78 public void testFireRuleForNode() throws Exception {
79 final StringBuffer b = new StringBuffer();
80
81 final Stylesheet s = new Stylesheet();
82 Pattern pattern = DocumentHelper.createPattern("url");
83 Action action = new Action() {
84 public void run(Node node) throws Exception {
85 b.append("url");
86 s.applyTemplates(node);
87 }
88 };
89
90 Rule r = new Rule(pattern, action);
91 s.addRule(r);
92
93 s.applyTemplates(document, new DefaultXPath("root/author/url"));
94
95 assertEquals("Check url is processed twice", "urlurl", b.toString());
96 }
97
98
99
100 protected void setUp() throws Exception {
101 super.setUp();
102
103 stylesheet = new Stylesheet();
104 stylesheet.setValueOfAction(new Action() {
105 public void run(Node node) {
106 log("Default ValueOf action on node: " + node);
107 log("........................................");
108 }
109 });
110 }
111
112 protected void addTemplate(final String match) {
113 log("Adding template match: " + match);
114
115 Pattern pattern = DocumentHelper.createPattern(match);
116
117 log("Pattern: " + pattern);
118 log("........................................");
119
120 Action action = new Action() {
121 public void run(Node node) throws Exception {
122 log("Matched pattern: " + match);
123 log("Node: " + node.asXML());
124 log("........................................");
125
126
127 stylesheet.applyTemplates(node);
128 }
129 };
130
131 Rule rule = new Rule(pattern, action);
132 stylesheet.addRule(rule);
133 }
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171