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_OLDSUB, _FIX_PARAM_NEWSUB);
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(
162 new File(basedir + "/" + file), StringPool.UTF8, false);
163
164 if ((xml.indexOf("<title>" + testName + "</title>") == -1) ||
165 (xml.indexOf("colspan=\"3\">" + testName + "</td>") == -1)) {
166
167 System.out.println(testName + " has an invalid test name");
168 }
169
170 x = xml.indexOf("<tbody>");
171 y = xml.indexOf("</tbody>");
172
173 xml = xml.substring(x, y + 8);
174
175 x = 0;
176 y = 0;
177
178 while (true) {
179 x = xml.indexOf("<tr>", x);
180 y = xml.indexOf("\n</tr>", x);
181
182 if ((x == -1) || (y == -1)) {
183 break;
184 }
185
186 x += 6;
187 y++;
188
189 String step = xml.substring(x, y);
190
191 String[] params = getParams(step);
192
193 String param1 = params[0];
194 String param2 = fixParam(params[1]);
195 String param3 = fixParam(params[2]);
196
197 if (param1.equals("assertConfirmation")) {
198 param2 = StringUtil.replace(param2, "?", "[\\\\s\\\\S]");
199
200 sm.append("assertTrue(selenium.getConfirmation().matches(\"^");
201 sm.append(param2);
202 sm.append("$\"));");
203 }
204 else if (param1.equals("click") || param1.equals("mouseDown") ||
205 param1.equals("mouseUp") || param1.equals("open") ||
206 param1.equals("selectFrame") ||
207 param1.equals("selectWindow")) {
208
209 sm.append("selenium.");
210 sm.append(param1);
211 sm.append("(\"");
212 sm.append(param2);
213 sm.append("\");");
214 }
215 else if (param1.equals("clickAndWait")) {
216 sm.append("selenium.click(\"");
217 sm.append(param2);
218 sm.append("\");");
219 sm.append("selenium.waitForPageToLoad(\"30000\");");
220 }
221 else if (param1.equals("close")) {
222 sm.append("selenium.");
223 sm.append(param1);
224 sm.append("();");
225 }
226 else if (param1.equals("pause")) {
227 sm.append("Thread.sleep(");
228 sm.append(param2);
229 sm.append(");");
230 }
231 else if (param1.equals("select") || param1.equals("type") ||
232 param1.equals("typeKeys") ||
233 param1.equals("waitForPopUp")) {
234
235 sm.append("selenium.");
236 sm.append(param1);
237 sm.append("(\"");
238 sm.append(param2);
239 sm.append("\", \"");
240 sm.append(param3);
241 sm.append("\");");
242 }
243 else if (param1.equals("selectAndWait")) {
244 sm.append("selenium.select(\"");
245 sm.append(param1);
246 sm.append("\", \"");
247 sm.append(param2);
248 sm.append("\");");
249 sm.append("selenium.waitForPageToLoad(\"30000\");");
250 }
251 else if (param1.equals("verifyTextPresent") ||
252 param1.equals("verifyTextNotPresent")) {
253
254 if (param1.equals("verifyTextPresent")) {
255 sm.append("verifyTrue");
256 }
257 else if (param1.equals("verifyTextNotPresent")) {
258 sm.append("verifyFalse");
259 }
260
261 sm.append("(selenium.isTextPresent(\"");
262 sm.append(param2);
263 sm.append("\"));");
264 }
265 else if (param1.equals("verifyTitle")) {
266 sm.append("verifyEquals(\"");
267 sm.append(param2);
268 sm.append("\", selenium.getTitle());");
269 }
270 else if (param1.equals("waitForElementPresent") ||
271 param1.equals("waitForTextPresent")) {
272
273 sm.append("for (int second = 0;; second++) {");
274 sm.append("if (second >= 60) {");
275 sm.append("fail(\"timeout\");");
276 sm.append("}");
277
278 sm.append("try {");
279 sm.append("if (selenium.");
280
281 if (param1.equals("waitForElementPresent")) {
282 sm.append("isElementPresent");
283 }
284 else if (param1.equals("waitForTextPresent")) {
285 sm.append("isTextPresent");
286 }
287
288 sm.append("(\"");
289 sm.append(param2);
290 sm.append("\")) {");
291 sm.append("break;");
292 sm.append("}");
293 sm.append("}");
294 sm.append("catch (Exception e) {");
295 sm.append("}");
296
297 sm.append("Thread.sleep(1000);");
298 sm.append("}");
299 }
300 else if (param1.equals("waitForTable")) {
301 sm.append("for (int second = 0;; second++) {");
302 sm.append("if (second >= 60) {");
303 sm.append("fail(\"timeout\");");
304 sm.append("}");
305
306 sm.append("try {");
307 sm.append("if (StringPool.BLANK.equals(selenium.getTable(\"");
308 sm.append(param2);
309 sm.append("\"))) {");
310 sm.append("break;");
311 sm.append("}");
312 sm.append("}");
313 sm.append("catch (Exception e) {");
314 sm.append("}");
315
316 sm.append("Thread.sleep(1000);");
317 sm.append("}");
318 }
319 else {
320 System.out.println(param1 + " was not translated");
321 }
322 }
323
324 sm.append("}");
325 sm.append("}");
326
327 String content = sm.toString();
328
329 ServiceBuilder.writeFile(new File(testFileName), content);
330 }
331
332 private static final String [] _FIX_PARAM_OLDSUB = new String[] {"\\\\n", "<br />"};
333 private static final String [] _FIX_PARAM_NEWSUB = new String[] {"\\n", "\\n"};
334
335 }