1
22
23 package com.liferay.portal.tools;
24
25 import com.liferay.portal.kernel.util.CharPool;
26 import com.liferay.portal.kernel.util.StringMaker;
27 import com.liferay.portal.kernel.util.StringPool;
28 import com.liferay.portal.kernel.util.StringUtil;
29 import com.liferay.portal.kernel.util.UnicodeFormatter;
30 import com.liferay.portal.tools.servicebuilder.ServiceBuilder;
31 import com.liferay.portal.util.InitUtil;
32 import com.liferay.util.FileUtil;
33
34 import java.io.File;
35
36 import org.apache.tools.ant.DirectoryScanner;
37
38
44 public class SeleneseToJavaBuilder {
45
46 static {
47 InitUtil.init();
48 }
49
50 public static void main(String[] args) throws Exception {
51 if (args.length == 1) {
52 new SeleneseToJavaBuilder(args[0]);
53 }
54 else {
55 throw new IllegalArgumentException();
56 }
57 }
58
59 public SeleneseToJavaBuilder(String basedir) throws Exception {
60 DirectoryScanner ds = new DirectoryScanner();
61
62 ds.setBasedir(basedir);
63 ds.setIncludes(new String[] {"**\\*.html"});
64
65 ds.scan();
66
67 String[] files = ds.getIncludedFiles();
68
69 for (int i = 0; i < files.length; i++) {
70
71
75
84
85 translate(basedir, files[i]);
86 }
87 }
88
89 protected String fixParam(String param) {
90 StringMaker sm = new StringMaker();
91
92 char[] array = param.toCharArray();
93
94 for (int i = 0; i < array.length; ++i) {
95 char c = array[i];
96
97 if (c == CharPool.BACK_SLASH) {
98 sm.append("\\\\");
99 }
100 else if (c == CharPool.QUOTE) {
101 sm.append("\\\"");
102 }
103 else if (Character.isWhitespace(c)) {
104 sm.append(c);
105 }
106 else if ((c < 0x0020) || (c > 0x007e)) {
107 sm.append("\\u");
108 sm.append(UnicodeFormatter.charToHex(c));
109 }
110 else {
111 sm.append(c);
112 }
113 }
114
115 return StringUtil.replace(
116 sm.toString(), _FIX_PARAM_OLD_SUBS, _FIX_PARAM_NEW_SUBS);
117 }
118
119 protected String[] getParams(String step) throws Exception {
120 String[] params = new String[3];
121
122 int x = 0;
123 int y = 0;
124
125 for (int i = 0; i < 3; i++) {
126 x = step.indexOf("<td>", x) + 4;
127 y = step.indexOf("\n", x);
128 y = step.lastIndexOf("</td>", y);
129
130 params[i] = step.substring(x, y);
131 }
132
133 return params;
134 }
135
136 protected void translate(String basedir, String file) throws Exception {
137 file = StringUtil.replace(
138 file, StringPool.BACK_SLASH, StringPool.SLASH);
139
140 int x = file.lastIndexOf(StringPool.SLASH);
141 int y = file.indexOf(StringPool.PERIOD);
142
143 String testPackagePath = StringUtil.replace(
144 file.substring(0, x), StringPool.SLASH, StringPool.PERIOD);
145 String testName = file.substring(x + 1, y);
146 String testMethodName =
147 "test" + testName.substring(0, testName.length() - 4);
148 String testFileName = basedir + "/" + file.substring(0, y) + ".java";
149
150 StringMaker sm = new StringMaker();
151
152 sm.append("package " + testPackagePath + ";\n\n");
153
154 sm.append("import com.liferay.portal.kernel.util.StringPool;\n");
155 sm.append("import com.liferay.portalweb.portal.BaseTestCase;\n\n");
156
157 sm.append("public class " + testName + " extends BaseTestCase {");
158
159 sm.append("public void " + testMethodName + "() throws Exception {");
160
161 String xml = FileUtil.read(basedir + "/" + file);
162
163 if ((xml.indexOf("<title>" + testName + "</title>") == -1) ||
164 (xml.indexOf("colspan=\"3\">" + testName + "</td>") == -1)) {
165
166 System.out.println(testName + " has an invalid test name");
167 }
168
169 x = xml.indexOf("<tbody>");
170 y = xml.indexOf("</tbody>");
171
172 xml = xml.substring(x, y + 8);
173
174 x = 0;
175 y = 0;
176
177 while (true) {
178 x = xml.indexOf("<tr>", x);
179 y = xml.indexOf("\n</tr>", x);
180
181 if ((x == -1) || (y == -1)) {
182 break;
183 }
184
185 x += 6;
186 y++;
187
188 String step = xml.substring(x, y);
189
190 String[] params = getParams(step);
191
192 String param1 = params[0];
193 String param2 = fixParam(params[1]);
194 String param3 = fixParam(params[2]);
195
196 if (param1.equals("assertConfirmation")) {
197 param2 = StringUtil.replace(param2, "?", "[\\\\s\\\\S]");
198
199 sm.append("assertTrue(selenium.getConfirmation().matches(\"^");
200 sm.append(param2);
201 sm.append("$\"));");
202 }
203 else if (param1.equals("click") || param1.equals("mouseDown") ||
204 param1.equals("mouseUp") || param1.equals("open") ||
205 param1.equals("selectFrame") ||
206 param1.equals("selectWindow")) {
207
208 sm.append("selenium.");
209 sm.append(param1);
210 sm.append("(\"");
211 sm.append(param2);
212 sm.append("\");");
213 }
214 else if (param1.equals("clickAndWait")) {
215 sm.append("selenium.click(\"");
216 sm.append(param2);
217 sm.append("\");");
218 sm.append("selenium.waitForPageToLoad(\"30000\");");
219 }
220 else if (param1.equals("close")) {
221 sm.append("selenium.");
222 sm.append(param1);
223 sm.append("();");
224 }
225 else if (param1.equals("pause")) {
226 sm.append("Thread.sleep(");
227 sm.append(param2);
228 sm.append(");");
229 }
230 else if (param1.equals("select") || param1.equals("type") ||
231 param1.equals("typeKeys") ||
232 param1.equals("waitForPopUp")) {
233
234 sm.append("selenium.");
235 sm.append(param1);
236 sm.append("(\"");
237 sm.append(param2);
238 sm.append("\", \"");
239 sm.append(param3);
240 sm.append("\");");
241 }
242 else if (param1.equals("selectAndWait")) {
243 sm.append("selenium.select(\"");
244 sm.append(param2);
245 sm.append("\", \"");
246 sm.append(param3);
247 sm.append("\");");
248 sm.append("selenium.waitForPageToLoad(\"30000\");");
249 }
250 else if (param1.equals("verifyTextPresent") ||
251 param1.equals("verifyTextNotPresent")) {
252
253 if (param1.equals("verifyTextPresent")) {
254 sm.append("verifyTrue");
255 }
256 else if (param1.equals("verifyTextNotPresent")) {
257 sm.append("verifyFalse");
258 }
259
260 sm.append("(selenium.isTextPresent(\"");
261 sm.append(param2);
262 sm.append("\"));");
263 }
264 else if (param1.equals("verifyTitle")) {
265 sm.append("verifyEquals(\"");
266 sm.append(param2);
267 sm.append("\", selenium.getTitle());");
268 }
269 else if (param1.equals("waitForElementNotPresent") ||
270 param1.equals("waitForElementPresent") ||
271 param1.equals("waitForTextNotPresent") ||
272 param1.equals("waitForTextPresent")) {
273
274 sm.append("for (int second = 0;; second++) {");
275 sm.append("if (second >= 60) {");
276 sm.append("fail(\"timeout\");");
277 sm.append("}");
278
279 sm.append("try {");
280 sm.append("if (");
281
282 if (param1.equals("waitForElementNotPresent") ||
283 param1.equals("waitForTextNotPresent")) {
284
285 sm.append("!");
286 }
287
288 sm.append("selenium.");
289
290 if (param1.equals("waitForElementNotPresent") ||
291 param1.equals("waitForElementPresent")) {
292
293 sm.append("isElementPresent");
294 }
295 else if (param1.equals("waitForTextNotPresent") ||
296 param1.equals("waitForTextPresent")) {
297
298 sm.append("isTextPresent");
299 }
300
301 sm.append("(\"");
302 sm.append(param2);
303 sm.append("\")) {");
304 sm.append("break;");
305 sm.append("}");
306 sm.append("}");
307 sm.append("catch (Exception e) {");
308 sm.append("}");
309
310 sm.append("Thread.sleep(1000);");
311 sm.append("}");
312 }
313 else if (param1.equals("waitForTable")) {
314 sm.append("for (int second = 0;; second++) {");
315 sm.append("if (second >= 60) {");
316 sm.append("fail(\"timeout\");");
317 sm.append("}");
318
319 sm.append("try {");
320 sm.append("if (StringPool.BLANK.equals(selenium.getTable(\"");
321 sm.append(param2);
322 sm.append("\"))) {");
323 sm.append("break;");
324 sm.append("}");
325 sm.append("}");
326 sm.append("catch (Exception e) {");
327 sm.append("}");
328
329 sm.append("Thread.sleep(1000);");
330 sm.append("}");
331 }
332 else {
333 System.out.println(param1 + " was not translated");
334 }
335 }
336
337 sm.append("}");
338 sm.append("}");
339
340 String content = sm.toString();
341
342 ServiceBuilder.writeFile(new File(testFileName), content);
343 }
344
345 private static final String[] _FIX_PARAM_OLD_SUBS = new String[] {
346 "\\\\n", "<br />"
347 };
348
349 private static final String[] _FIX_PARAM_NEW_SUBS = new String[] {
350 "\\n", "\\n"
351 };
352
353 }