001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.tools.seleniumbuilder;
016    
017    import com.liferay.portal.kernel.util.SetUtil;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.kernel.xml.Element;
021    import com.liferay.portal.tools.ArgumentsUtil;
022    import com.liferay.portal.util.InitUtil;
023    
024    import java.util.List;
025    import java.util.Map;
026    import java.util.Set;
027    import java.util.TreeSet;
028    
029    /**
030     * @author Michael Hashimoto
031     */
032    public class SeleniumBuilder {
033    
034            public static void main(String[] args) throws Exception {
035                    InitUtil.initWithSpring();
036    
037                    new SeleniumBuilder(args);
038            }
039    
040            public SeleniumBuilder(String[] args) throws Exception {
041                    Map<String, String> arguments = ArgumentsUtil.parseArguments(args);
042    
043                    String baseDir = arguments.get("selenium.base.dir");
044    
045                    SeleniumBuilderContext seleniumBuilderContext =
046                            new SeleniumBuilderContext(baseDir);
047    
048                    Set<String> types = SetUtil.fromArray(
049                            StringUtil.split(arguments.get("selenium.types")));
050    
051                    if (types.contains("action")) {
052                            ActionConverter actionConverter = new ActionConverter(
053                                    seleniumBuilderContext);
054    
055                            Set<String> actionNames = seleniumBuilderContext.getActionNames();
056    
057                            for (String actionName : actionNames) {
058                                    seleniumBuilderContext.validateActionElements(actionName);
059    
060                                    actionConverter.convert(actionName);
061                            }
062                    }
063    
064                    if (types.contains("function")) {
065                            FunctionConverter functionConverter = new FunctionConverter(
066                                    seleniumBuilderContext);
067    
068                            Set<String> functionNames =
069                                    seleniumBuilderContext.getFunctionNames();
070    
071                            for (String functionName : functionNames) {
072                                    seleniumBuilderContext.validateFunctionElements(functionName);
073    
074                                    functionConverter.convert(functionName);
075                            }
076                    }
077    
078                    if (types.contains("macro")) {
079                            MacroConverter macroConverter = new MacroConverter(
080                                    seleniumBuilderContext);
081    
082                            Set<String> macroNames = seleniumBuilderContext.getMacroNames();
083    
084                            for (String macroName : macroNames) {
085                                    seleniumBuilderContext.validateMacroElements(macroName);
086    
087                                    macroConverter.convert(macroName);
088                            }
089                    }
090    
091                    if (types.contains("path")) {
092                            PathConverter pathConverter = new PathConverter(
093                                    seleniumBuilderContext);
094    
095                            Set<String> pathNames = seleniumBuilderContext.getPathNames();
096    
097                            for (String pathName : pathNames) {
098                                    pathConverter.convert(pathName);
099                            }
100                    }
101    
102                    if (types.contains("testcase")) {
103                            TestCaseConverter testCaseConverter = new TestCaseConverter(
104                                    seleniumBuilderContext);
105    
106                            Set<String> testCaseNames =
107                                    seleniumBuilderContext.getTestCaseNames();
108    
109                            for (String testCaseName : testCaseNames) {
110                                    seleniumBuilderContext.validateTestCaseElements(testCaseName);
111    
112                                    testCaseConverter.convert(testCaseName);
113                            }
114                    }
115    
116                    SeleniumBuilderFileUtil seleniumBuilderFileUtil =
117                            new SeleniumBuilderFileUtil(baseDir);
118    
119                    Set<String> testCaseMethodNames = new TreeSet<String>();
120                    int testCaseCount = 0;
121    
122                    Set<String> testCaseNames = seleniumBuilderContext.getTestCaseNames();
123    
124                    for (String testCaseName : testCaseNames) {
125                            Element rootElement = seleniumBuilderContext.getTestCaseRootElement(
126                                    testCaseName);
127    
128                            List<Element> commandElements =
129                                    seleniumBuilderFileUtil.getAllChildElements(
130                                            rootElement, "command");
131    
132                            for (Element commandElement : commandElements) {
133                                    testCaseMethodNames.add(
134                                            testCaseName + "TestCase#test" +
135                                                    commandElement.attributeValue("name"));
136                            }
137    
138                            testCaseCount += commandElements.size();
139                    }
140    
141                    String testCaseMethodNamesString = StringUtil.merge(
142                            testCaseMethodNames.toArray(
143                                    new String[testCaseMethodNames.size()]),
144                            StringPool.SPACE);
145    
146                    seleniumBuilderFileUtil.writeFile(
147                            "../../../test.case.method.names.properties",
148                            "TEST_CASE_METHOD_NAMES=" + testCaseMethodNamesString, false);
149    
150                    System.out.println("\nThere are " + testCaseCount + " test cases.");
151            }
152    
153    }