001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.tools;
022:
023: import com.liferay.portal.kernel.util.CharPool;
024: import com.liferay.portal.kernel.util.StringMaker;
025: import com.liferay.portal.kernel.util.StringPool;
026: import com.liferay.portal.kernel.util.StringUtil;
027: import com.liferay.portal.kernel.util.UnicodeFormatter;
028: import com.liferay.portal.tools.servicebuilder.ServiceBuilder;
029: import com.liferay.portal.util.InitUtil;
030: import com.liferay.util.FileUtil;
031:
032: import java.io.File;
033:
034: import org.apache.tools.ant.DirectoryScanner;
035:
036: /**
037: * <a href="SeleneseToJavaBuilder.java.html"><b><i>View Source</i></b></a>
038: *
039: * @author Brian Wing Shun Chan
040: *
041: */
042: public class SeleneseToJavaBuilder {
043:
044: static {
045: InitUtil.init();
046: }
047:
048: public static void main(String[] args) throws Exception {
049: if (args.length == 1) {
050: new SeleneseToJavaBuilder(args[0]);
051: } else {
052: throw new IllegalArgumentException();
053: }
054: }
055:
056: public SeleneseToJavaBuilder(String basedir) throws Exception {
057: DirectoryScanner ds = new DirectoryScanner();
058:
059: ds.setBasedir(basedir);
060: ds.setIncludes(new String[] { "**\\*.html" });
061:
062: ds.scan();
063:
064: String[] files = ds.getIncludedFiles();
065:
066: for (int i = 0; i < files.length; i++) {
067:
068: // I would have preferred to use XlateHtmlSeleneseToJava, but it
069: // is horribly out of sync with Selenium IDE and generates incorrect
070: // code.
071:
072: /*File file = new File(basedir + "/" + files[i]);
073:
074: String input = StringUtil.replace(file.toString(), "\\", "/");
075:
076: XlateHtmlSeleneseToJava.main(
077: new String[] {
078: "test", "-silent", input
079: }
080: );*/
081:
082: translate(basedir, files[i]);
083: }
084: }
085:
086: protected String fixParam(String param) {
087: StringMaker sm = new StringMaker();
088:
089: char[] array = param.toCharArray();
090:
091: for (int i = 0; i < array.length; ++i) {
092: char c = array[i];
093:
094: if (c == CharPool.BACK_SLASH) {
095: sm.append("\\\\");
096: } else if (c == CharPool.QUOTE) {
097: sm.append("\\\"");
098: } else if (Character.isWhitespace(c)) {
099: sm.append(c);
100: } else if ((c < 0x0020) || (c > 0x007e)) {
101: sm.append("\\u");
102: sm.append(UnicodeFormatter.charToHex(c));
103: } else {
104: sm.append(c);
105: }
106: }
107:
108: return StringUtil.replace(sm.toString(), _FIX_PARAM_OLDSUB,
109: _FIX_PARAM_NEWSUB);
110: }
111:
112: protected String[] getParams(String step) throws Exception {
113: String[] params = new String[3];
114:
115: int x = 0;
116: int y = 0;
117:
118: for (int i = 0; i < 3; i++) {
119: x = step.indexOf("<td>", x) + 4;
120: y = step.indexOf("\n", x);
121: y = step.lastIndexOf("</td>", y);
122:
123: params[i] = step.substring(x, y);
124: }
125:
126: return params;
127: }
128:
129: protected void translate(String basedir, String file)
130: throws Exception {
131: file = StringUtil.replace(file, StringPool.BACK_SLASH,
132: StringPool.SLASH);
133:
134: int x = file.lastIndexOf(StringPool.SLASH);
135: int y = file.indexOf(StringPool.PERIOD);
136:
137: String testPackagePath = StringUtil.replace(file
138: .substring(0, x), StringPool.SLASH, StringPool.PERIOD);
139: String testName = file.substring(x + 1, y);
140: String testMethodName = "test"
141: + testName.substring(0, testName.length() - 4);
142: String testFileName = basedir + "/" + file.substring(0, y)
143: + ".java";
144:
145: StringMaker sm = new StringMaker();
146:
147: sm.append("package " + testPackagePath + ";\n\n");
148:
149: sm
150: .append("import com.liferay.portal.kernel.util.StringPool;\n");
151: sm
152: .append("import com.liferay.portalweb.portal.BaseTestCase;\n\n");
153:
154: sm.append("public class " + testName
155: + " extends BaseTestCase {");
156:
157: sm.append("public void " + testMethodName
158: + "() throws Exception {");
159:
160: String xml = FileUtil.read(new File(basedir + "/" + file),
161: StringPool.UTF8, false);
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
198: .replace(param2, "?", "[\\\\s\\\\S]");
199:
200: sm
201: .append("assertTrue(selenium.getConfirmation().matches(\"^");
202: sm.append(param2);
203: sm.append("$\"));");
204: } else if (param1.equals("click")
205: || param1.equals("mouseDown")
206: || param1.equals("mouseUp")
207: || param1.equals("open")
208: || param1.equals("selectFrame")
209: || param1.equals("selectWindow")) {
210:
211: sm.append("selenium.");
212: sm.append(param1);
213: sm.append("(\"");
214: sm.append(param2);
215: sm.append("\");");
216: } else if (param1.equals("clickAndWait")) {
217: sm.append("selenium.click(\"");
218: sm.append(param2);
219: sm.append("\");");
220: sm.append("selenium.waitForPageToLoad(\"30000\");");
221: } else if (param1.equals("close")) {
222: sm.append("selenium.");
223: sm.append(param1);
224: sm.append("();");
225: } else if (param1.equals("pause")) {
226: sm.append("Thread.sleep(");
227: sm.append(param2);
228: sm.append(");");
229: } else if (param1.equals("select") || param1.equals("type")
230: || param1.equals("typeKeys")
231: || param1.equals("waitForPopUp")) {
232:
233: sm.append("selenium.");
234: sm.append(param1);
235: sm.append("(\"");
236: sm.append(param2);
237: sm.append("\", \"");
238: sm.append(param3);
239: sm.append("\");");
240: } else if (param1.equals("selectAndWait")) {
241: sm.append("selenium.select(\"");
242: sm.append(param1);
243: sm.append("\", \"");
244: sm.append(param2);
245: sm.append("\");");
246: sm.append("selenium.waitForPageToLoad(\"30000\");");
247: } else if (param1.equals("verifyTextPresent")
248: || param1.equals("verifyTextNotPresent")) {
249:
250: if (param1.equals("verifyTextPresent")) {
251: sm.append("verifyTrue");
252: } else if (param1.equals("verifyTextNotPresent")) {
253: sm.append("verifyFalse");
254: }
255:
256: sm.append("(selenium.isTextPresent(\"");
257: sm.append(param2);
258: sm.append("\"));");
259: } else if (param1.equals("verifyTitle")) {
260: sm.append("verifyEquals(\"");
261: sm.append(param2);
262: sm.append("\", selenium.getTitle());");
263: } else if (param1.equals("waitForElementPresent")
264: || param1.equals("waitForTextPresent")) {
265:
266: sm.append("for (int second = 0;; second++) {");
267: sm.append("if (second >= 60) {");
268: sm.append("fail(\"timeout\");");
269: sm.append("}");
270:
271: sm.append("try {");
272: sm.append("if (selenium.");
273:
274: if (param1.equals("waitForElementPresent")) {
275: sm.append("isElementPresent");
276: } else if (param1.equals("waitForTextPresent")) {
277: sm.append("isTextPresent");
278: }
279:
280: sm.append("(\"");
281: sm.append(param2);
282: sm.append("\")) {");
283: sm.append("break;");
284: sm.append("}");
285: sm.append("}");
286: sm.append("catch (Exception e) {");
287: sm.append("}");
288:
289: sm.append("Thread.sleep(1000);");
290: sm.append("}");
291: } else if (param1.equals("waitForTable")) {
292: sm.append("for (int second = 0;; second++) {");
293: sm.append("if (second >= 60) {");
294: sm.append("fail(\"timeout\");");
295: sm.append("}");
296:
297: sm.append("try {");
298: sm
299: .append("if (StringPool.BLANK.equals(selenium.getTable(\"");
300: sm.append(param2);
301: sm.append("\"))) {");
302: sm.append("break;");
303: sm.append("}");
304: sm.append("}");
305: sm.append("catch (Exception e) {");
306: sm.append("}");
307:
308: sm.append("Thread.sleep(1000);");
309: sm.append("}");
310: } else {
311: System.out.println(param1 + " was not translated");
312: }
313: }
314:
315: sm.append("}");
316: sm.append("}");
317:
318: String content = sm.toString();
319:
320: ServiceBuilder.writeFile(new File(testFileName), content);
321: }
322:
323: private static final String[] _FIX_PARAM_OLDSUB = new String[] {
324: "\\\\n", "<br />" };
325: private static final String[] _FIX_PARAM_NEWSUB = new String[] {
326: "\\n", "\\n" };
327:
328: }
|