1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package org.codehaus.groovy.wiki;
47
48 import java.io.IOException;
49 import java.io.Writer;
50 import java.util.regex.Pattern;
51
52 import org.radeox.api.engine.RenderEngine;
53 import org.radeox.api.engine.context.RenderContext;
54
55 /***
56 * @author James Strachan
57 * @version $Revision: 1.5 $
58 */
59 public class TestCaseRenderEngine implements RenderEngine {
60 Pattern groovyCodePattern = Pattern.compile("//{code:groovy//}");
61 Pattern groovyShellPattern = Pattern.compile("//{code:groovysh//}");
62 Pattern codePattern = Pattern.compile("//{code//}");
63
64 public TestCaseRenderEngine() {
65 }
66
67 public String getName() {
68 return "TestCase";
69 }
70
71 public String render(String content, RenderContext context) {
72 String name = (String) context.get("name");
73 if (name == null) {
74 name = "UknownName.wiki";
75 }
76 int idx = name.lastIndexOf('.');
77 if (idx > 0) {
78 name = name.substring(0, idx);
79 }
80 name = name + "Test";
81
82
83 StringBuffer buf = new StringBuffer();
84
85 String[] parts = groovyCodePattern.split(content);
86
87 buf.append( "package wiki\nclass " + name + " extends GroovyTestCase {\n\n");
88 buf.append("/*\n");
89 buf.append(processShellScripts(parts[0]));
90
91 for (int count = 1; count < parts.length; count++ ) {
92 buf.append("*/ \n\n void testCase" + count + "() {\n");
93
94 buf.append(processShellScripts(removeCloseCode(parts[count])));
95 }
96
97 buf.append("\n*/\n\n");
98 buf.append("void testDummy() {\n// this is a dummy test case\n}\n\n}\n");
99
100 return buf.toString();
101 }
102
103 /***
104 * Splits the comment block extracting any scripts that need to be tested
105 * @param text
106 * @return
107 */
108 protected String processShellScripts(String text) {
109 StringBuffer buf = new StringBuffer();
110
111 String[] parts = groovyShellPattern.split(text);
112
113 buf.append(parts[0]);
114
115 for (int count = 1; count < parts.length; count++ ) {
116 buf.append("*/ \n\n void testScript" + count + "() {\n");
117 buf.append(" assertScript( <<<SCRIPT_EOF" + count + "\n");
118
119 String code = parts[count].replaceFirst("//{code//}", "\nSCRIPT_EOF" + count + " )\n} \n\n /*");
120
121
122 StringBuffer temp = new StringBuffer(code);
123 for (int idx = 0; true; idx++) {
124 idx = temp.indexOf("$", idx);
125 if (idx >= 0) {
126 String next = temp.substring(++idx, idx+1);
127 if (next.equals("{")) {
128
129
130
131
132
133 if( idx-2 >= 0 && !temp.substring(idx-2,idx-1).equals("//") )
134 {
135 temp.insert(idx-1, "//");
136 idx++;
137 }
138 }
139 }
140 else {
141 break;
142 }
143 }
144
145 buf.append(temp.toString());
146 }
147 return buf.toString();
148 }
149
150 protected String removeCloseCode(String text) {
151 return text.replaceFirst("//{code//}", "\n}\n\n /*");
152 }
153
154 public void render(Writer out, String content, RenderContext context) throws IOException {
155 out.write(render(content, context));
156 }
157 }