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.StringPool;
018    import com.liferay.portal.kernel.util.StringUtil;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.kernel.xml.Attribute;
021    import com.liferay.portal.kernel.xml.Element;
022    
023    import java.util.HashMap;
024    import java.util.HashSet;
025    import java.util.List;
026    import java.util.Map;
027    import java.util.Set;
028    import java.util.regex.Matcher;
029    import java.util.regex.Pattern;
030    
031    import org.apache.tools.ant.DirectoryScanner;
032    
033    /**
034     * @author Michael Hashimoto
035     */
036    public class SeleniumBuilderContext {
037    
038            public SeleniumBuilderContext(String baseDir) throws Exception {
039                    this(baseDir, "com/liferay/portalweb/portal/util/liferayselenium/");
040            }
041    
042            public SeleniumBuilderContext(String baseDir, String liferaySeleniumDir)
043                    throws Exception {
044    
045                    _baseDir = baseDir;
046    
047                    _seleniumBuilderFileUtil = new SeleniumBuilderFileUtil(_baseDir);
048    
049                    DirectoryScanner directoryScanner = new DirectoryScanner();
050    
051                    directoryScanner.setBasedir(_baseDir);
052                    directoryScanner.setIncludes(
053                            new String[] {
054                                    "**\\*.action", "**\\*.function", "**\\*.macro", "**\\*.path",
055                                    "**\\*.testcase", "**\\*.testsuite"
056                            });
057    
058                    directoryScanner.scan();
059    
060                    String[] fileNames = directoryScanner.getIncludedFiles();
061    
062                    for (String fileName : fileNames) {
063                            addFile(fileName);
064                    }
065    
066                    String[] seleniumFileNames = {
067                            liferaySeleniumDir + "LiferaySelenium.java",
068                            liferaySeleniumDir + "SeleniumWrapper.java"
069                    };
070    
071                    for (String seleniumFileName : seleniumFileNames) {
072                            String content = _seleniumBuilderFileUtil.getNormalizedContent(
073                                    seleniumFileName);
074    
075                            Pattern pattern = Pattern.compile(
076                                    "public [a-z]* [A-Za-z0-9_]*\\(.*?\\)");
077    
078                            Matcher matcher = pattern.matcher(content);
079    
080                            while (matcher.find()) {
081                                    String methodSignature = matcher.group();
082    
083                                    int x = methodSignature.indexOf(" ", 7);
084                                    int y = methodSignature.indexOf("(");
085    
086                                    String seleniumCommandName = methodSignature.substring(
087                                            x + 1, y);
088    
089                                    int count = 0;
090    
091                                    int z = methodSignature.indexOf(")");
092    
093                                    String parameters = methodSignature.substring(y + 1, z);
094    
095                                    if (!parameters.equals("")) {
096                                            count = StringUtil.count(parameters, ",") + 1;
097                                    }
098    
099                                    _seleniumParameterCounts.put(seleniumCommandName, count);
100                            }
101                    }
102    
103                    _seleniumParameterCounts.put("open", 1);
104            }
105    
106            public void addFile(String fileName) throws Exception {
107                    fileName = _normalizeFileName(fileName);
108    
109                    if (fileName.endsWith(".action")) {
110                            String actionName = _getName(fileName);
111    
112                            if (_actionFileNames.containsKey(actionName)) {
113                                    _seleniumBuilderFileUtil.throwValidationException(
114                                            1008, fileName, actionName);
115                            }
116    
117                            _actionFileNames.put(actionName, fileName);
118    
119                            _actionNames.add(actionName);
120    
121                            _actionRootElements.put(actionName, _getRootElement(fileName));
122                    }
123                    else if (fileName.endsWith(".function")) {
124                            String functionName = _getName(fileName);
125    
126                            _functionClassNames.put(functionName, _getClassName(fileName));
127    
128                            _functionFileNames.put(functionName, fileName);
129    
130                            _functionJavaFileNames.put(
131                                    functionName, _getJavaFileName(fileName));
132    
133                            Element rootElement = _getRootElement(fileName);
134    
135                            _functionLocatorCounts.put(
136                                    functionName, _getLocatorCount(rootElement));
137    
138                            if (_functionNames.contains(functionName)) {
139                                    _seleniumBuilderFileUtil.throwValidationException(
140                                            1008, fileName, functionName);
141                            }
142    
143                            _functionNames.add(functionName);
144    
145                            _functionPackageNames.put(functionName, _getPackageName(fileName));
146    
147                            _functionReturnTypes.put(
148                                    functionName, _getReturnType(functionName));
149    
150                            _functionRootElements.put(functionName, rootElement);
151    
152                            _functionSimpleClassNames.put(
153                                    functionName, _getSimpleClassName(fileName));
154                    }
155                    else if (fileName.endsWith(".macro")) {
156                            String macroName = _getName(fileName);
157    
158                            _macroClassNames.put(macroName, _getClassName(fileName));
159    
160                            _macroFileNames.put(macroName, fileName);
161    
162                            _macroJavaFileNames.put(macroName, _getJavaFileName(fileName));
163    
164                            if (_macroNames.contains(macroName)) {
165                                    _seleniumBuilderFileUtil.throwValidationException(
166                                            1008, fileName, macroName);
167                            }
168    
169                            _macroNames.add(macroName);
170    
171                            _macroPackageNames.put(macroName, _getPackageName(fileName));
172    
173                            _macroSimpleClassNames.put(
174                                    macroName, _getSimpleClassName(fileName));
175    
176                            _macroRootElements.put(macroName, _getRootElement(fileName));
177                    }
178                    else if (fileName.endsWith(".path")) {
179                            String pathName = _getName(fileName);
180    
181                            _actionClassNames.put(pathName, _getClassName(fileName, "Action"));
182    
183                            _actionJavaFileNames.put(
184                                    pathName, _getJavaFileName(fileName, "Action"));
185    
186                            _actionNames.add(pathName);
187    
188                            _actionPackageNames.put(pathName, _getPackageName(fileName));
189    
190                            _actionSimpleClassNames.put(
191                                    pathName, _getSimpleClassName(fileName, "Action"));
192    
193                            _pathClassNames.put(pathName, _getClassName(fileName));
194    
195                            _pathFileNames.put(pathName, fileName);
196    
197                            _pathJavaFileNames.put(pathName, _getJavaFileName(fileName));
198    
199                            if (_pathNames.contains(pathName)) {
200                                    _seleniumBuilderFileUtil.throwValidationException(
201                                            1008, fileName, pathName);
202                            }
203    
204                            _pathNames.add(pathName);
205    
206                            _pathPackageNames.put(pathName, _getPackageName(fileName));
207    
208                            _pathRootElements.put(pathName, _getRootElement(fileName));
209    
210                            _pathSimpleClassNames.put(pathName, _getSimpleClassName(fileName));
211                    }
212                    else if (fileName.endsWith(".testcase")) {
213                            String testCaseName = _getName(fileName);
214    
215                            _testCaseClassNames.put(testCaseName, _getClassName(fileName));
216    
217                            Element rootElement = _getRootElement(fileName);
218    
219                            _testCaseCommandNames.put(
220                                    testCaseName, _getTestCaseCommandNames(rootElement));
221    
222                            _testCaseFileNames.put(testCaseName, fileName);
223    
224                            _testCaseHTMLFileNames.put(
225                                    testCaseName, _getHTMLFileName(fileName));
226    
227                            _testCaseJavaFileNames.put(
228                                    testCaseName, _getJavaFileName(fileName));
229    
230                            if (_testCaseNames.contains(testCaseName)) {
231                                    _seleniumBuilderFileUtil.throwValidationException(
232                                            1008, fileName, testCaseName);
233                            }
234    
235                            _testCaseNames.add(testCaseName);
236    
237                            _testCasePackageNames.put(testCaseName, _getPackageName(fileName));
238    
239                            _testCaseRootElements.put(testCaseName, rootElement);
240    
241                            _testCaseSimpleClassNames.put(
242                                    testCaseName, _getSimpleClassName(fileName));
243                    }
244                    else if (fileName.endsWith(".testsuite")) {
245                            String testSuiteName = _getName(fileName);
246    
247                            _testSuiteClassNames.put(testSuiteName, _getClassName(fileName));
248    
249                            _testSuiteFileNames.put(testSuiteName, fileName);
250    
251                            _testSuiteHTMLFileNames.put(
252                                    testSuiteName, _getHTMLFileName(fileName));
253    
254                            _testSuiteJavaFileNames.put(
255                                    testSuiteName, _getJavaFileName(fileName));
256    
257                            if (_testSuiteNames.contains(testSuiteName)) {
258                                    _seleniumBuilderFileUtil.throwValidationException(
259                                            1008, fileName, testSuiteName);
260                            }
261    
262                            _testSuiteNames.add(testSuiteName);
263    
264                            _testSuitePackageNames.put(
265                                    testSuiteName, _getPackageName(fileName));
266    
267                            _testSuiteRootElements.put(
268                                    testSuiteName, _getRootElement(fileName));
269    
270                            _testSuiteSimpleClassNames.put(
271                                    testSuiteName, _getSimpleClassName(fileName));
272                    }
273                    else {
274                            throw new IllegalArgumentException("Invalid file " + fileName);
275                    }
276            }
277    
278            public String getActionClassName(String actionName) {
279                    return _actionClassNames.get(actionName);
280            }
281    
282            public String getActionFileName(String actionName) {
283                    return _actionFileNames.get(actionName);
284            }
285    
286            public String getActionJavaFileName(String actionName) {
287                    return _actionJavaFileNames.get(actionName);
288            }
289    
290            public Set<String> getActionNames() {
291                    return _actionNames;
292            }
293    
294            public String getActionPackageName(String actionName) {
295                    return _actionPackageNames.get(actionName);
296            }
297    
298            public Element getActionRootElement(String actionName) {
299                    return _actionRootElements.get(actionName);
300            }
301    
302            public String getActionSimpleClassName(String actionName) {
303                    return _actionSimpleClassNames.get(actionName);
304            }
305    
306            public String getBaseDir() {
307                    return _baseDir;
308            }
309    
310            public String getFunctionClassName(String functionName) {
311                    return _functionClassNames.get(functionName);
312            }
313    
314            public String getFunctionFileName(String functionName) {
315                    return _functionFileNames.get(functionName);
316            }
317    
318            public String getFunctionJavaFileName(String functionName) {
319                    return _functionJavaFileNames.get(functionName);
320            }
321    
322            public int getFunctionLocatorCount(String functionName) {
323                    return _functionLocatorCounts.get(functionName);
324            }
325    
326            public Set<String> getFunctionNames() {
327                    return _functionNames;
328            }
329    
330            public String getFunctionPackageName(String functionName) {
331                    return _functionPackageNames.get(functionName);
332            }
333    
334            public String getFunctionReturnType(String functionName) {
335                    return _functionReturnTypes.get(functionName);
336            }
337    
338            public Element getFunctionRootElement(String functionName) {
339                    return _functionRootElements.get(functionName);
340            }
341    
342            public String getFunctionSimpleClassName(String functionName) {
343                    return _functionSimpleClassNames.get(functionName);
344            }
345    
346            public String getMacroClassName(String macroName) {
347                    return _macroClassNames.get(macroName);
348            }
349    
350            public String getMacroFileName(String macroName) {
351                    return _macroFileNames.get(macroName);
352            }
353    
354            public String getMacroJavaFileName(String macroName) {
355                    return _macroJavaFileNames.get(macroName);
356            }
357    
358            public Set<String> getMacroNames() {
359                    return _macroNames;
360            }
361    
362            public String getMacroPackageName(String macroName) {
363                    return _macroPackageNames.get(macroName);
364            }
365    
366            public Element getMacroRootElement(String macroName) {
367                    return _macroRootElements.get(macroName);
368            }
369    
370            public String getMacroSimpleClassName(String macroName) {
371                    return _macroSimpleClassNames.get(macroName);
372            }
373    
374            public String getPath(Element rootElement, String locatorKey) {
375                    String pathName = "";
376    
377                    Element bodyElement = rootElement.element("body");
378    
379                    Element tableElement = bodyElement.element("table");
380    
381                    Element tbodyElement = tableElement.element("tbody");
382    
383                    List<Element> trElements = tbodyElement.elements();
384    
385                    for (Element trElement : trElements) {
386                            List<Element> tdElements = trElement.elements("td");
387    
388                            Element pathLocatorElement = tdElements.get(1);
389    
390                            Element pathLocatorKeyElement = tdElements.get(0);
391    
392                            String pathLocatorKey = pathLocatorKeyElement.getText();
393    
394                            if (pathLocatorKey.equals(locatorKey)) {
395                                    return pathLocatorElement.getText();
396                            }
397    
398                            if (pathLocatorKey.equals("EXTEND_ACTION_PATH")) {
399                                    pathName = pathLocatorElement.getText();
400                            }
401                    }
402    
403                    if (Validator.isNotNull(pathName)) {
404                            Element pathRootElement = getPathRootElement(pathName);
405    
406                            return getPath(pathRootElement, locatorKey);
407                    }
408    
409                    return locatorKey;
410            }
411    
412            public String getPathClassName(String pathName) {
413                    return _pathClassNames.get(pathName);
414            }
415    
416            public String getPathFileName(String pathName) {
417                    return _pathFileNames.get(pathName);
418            }
419    
420            public String getPathJavaFileName(String pathName) {
421                    return _pathJavaFileNames.get(pathName);
422            }
423    
424            public Set<String> getPathLocatorKeys(Element rootElement) {
425                    Set<String> pathLocatorKeys = new HashSet<String>();
426    
427                    Element bodyElement = rootElement.element("body");
428    
429                    Element tableElement = bodyElement.element("table");
430    
431                    Element tbodyElement = tableElement.element("tbody");
432    
433                    List<Element> trElements = tbodyElement.elements();
434    
435                    for (Element trElement : trElements) {
436                            List<Element> tdElements = trElement.elements("td");
437    
438                            Element pathLocatorKeyElement = tdElements.get(0);
439    
440                            String pathLocatorKey = pathLocatorKeyElement.getText();
441    
442                            if (pathLocatorKey.equals("EXTEND_ACTION_PATH")) {
443                                    Element pathLocatorElement = tdElements.get(1);
444    
445                                    String pathName = pathLocatorElement.getText();
446    
447                                    Element pathRootElement = getPathRootElement(pathName);
448    
449                                    pathLocatorKeys.addAll(getPathLocatorKeys(pathRootElement));
450                            }
451                            else {
452                                    pathLocatorKeys.add(pathLocatorKey);
453                            }
454                    }
455    
456                    return pathLocatorKeys;
457            }
458    
459            public Set<String> getPathNames() {
460                    return _pathNames;
461            }
462    
463            public String getPathPackageName(String pathName) {
464                    return _pathPackageNames.get(pathName);
465            }
466    
467            public Element getPathRootElement(String pathName) {
468                    return _pathRootElements.get(pathName);
469            }
470    
471            public String getPathSimpleClassName(String pathName) {
472                    return _pathSimpleClassNames.get(pathName);
473            }
474    
475            public int getSeleniumParameterCount(String seleniumCommandName) {
476                    return _seleniumParameterCounts.get(seleniumCommandName);
477            }
478    
479            public String getTestCaseClassName(String testCaseName) {
480                    return _testCaseClassNames.get(testCaseName);
481            }
482    
483            public String getTestCaseFileName(String testCaseName) {
484                    return _testCaseFileNames.get(testCaseName);
485            }
486    
487            public String getTestCaseHTMLFileName(String testCaseName) {
488                    return _testCaseHTMLFileNames.get(testCaseName);
489            }
490    
491            public String getTestCaseJavaFileName(String testCaseName) {
492                    return _testCaseJavaFileNames.get(testCaseName);
493            }
494    
495            public Set<String> getTestCaseNames() {
496                    return _testCaseNames;
497            }
498    
499            public String getTestCasePackageName(String testCaseName) {
500                    return _testCasePackageNames.get(testCaseName);
501            }
502    
503            public Element getTestCaseRootElement(String testCaseName) {
504                    return _testCaseRootElements.get(testCaseName);
505            }
506    
507            public String getTestCaseSimpleClassName(String testCaseName) {
508                    return _testCaseSimpleClassNames.get(testCaseName);
509            }
510    
511            public String getTestSuiteClassName(String testSuiteName) {
512                    return _testSuiteClassNames.get(testSuiteName);
513            }
514    
515            public String getTestSuiteFileName(String testSuiteName) {
516                    return _testSuiteFileNames.get(testSuiteName);
517            }
518    
519            public String getTestSuiteHTMLFileName(String testSuiteName) {
520                    return _testSuiteHTMLFileNames.get(testSuiteName);
521            }
522    
523            public String getTestSuiteJavaFileName(String testSuiteName) {
524                    return _testSuiteJavaFileNames.get(testSuiteName);
525            }
526    
527            public Set<String> getTestSuiteNames() {
528                    return _testSuiteNames;
529            }
530    
531            public String getTestSuitePackageName(String testSuiteName) {
532                    return _testSuitePackageNames.get(testSuiteName);
533            }
534    
535            public Element getTestSuiteRootElement(String testSuiteName) {
536                    return _testSuiteRootElements.get(testSuiteName);
537            }
538    
539            public String getTestSuiteSimpleClassName(String testCaseName) {
540                    return _testSuiteSimpleClassNames.get(testCaseName);
541            }
542    
543            public void validateActionElements(String actionName) {
544                    String actionFileName = getActionFileName(actionName);
545    
546                    Element rootElement = getActionRootElement(actionName);
547    
548                    if (rootElement == null) {
549                            return;
550                    }
551    
552                    if (!_pathNames.contains(actionName)) {
553                            _seleniumBuilderFileUtil.throwValidationException(
554                                    2002, actionFileName, actionName);
555                    }
556    
557                    List<Element> caseElements =
558                            _seleniumBuilderFileUtil.getAllChildElements(rootElement, "case");
559    
560                    for (Element caseElement : caseElements) {
561                            _validateLocatorKeyElement(actionFileName, actionName, caseElement);
562                    }
563    
564                    List<Element> commandElements =
565                            _seleniumBuilderFileUtil.getAllChildElements(
566                                    rootElement, "command");
567    
568                    Set<String> commandElementNames = new HashSet<String>();
569    
570                    for (Element commandElement : commandElements) {
571                            String commandName = commandElement.attributeValue("name");
572    
573                            if (commandElementNames.contains(commandName)) {
574                                    _seleniumBuilderFileUtil.throwValidationException(
575                                            1009, actionFileName, commandElement, commandName);
576                            }
577    
578                            commandElementNames.add(commandName);
579    
580                            if (!_isFunctionName(commandName)) {
581                                    _seleniumBuilderFileUtil.throwValidationException(
582                                            2001, actionFileName, commandElement, commandName);
583                            }
584                    }
585    
586                    List<Element> executeElements =
587                            _seleniumBuilderFileUtil.getAllChildElements(
588                                    rootElement, "execute");
589    
590                    for (Element executeElement : executeElements) {
591                            _validateFunctionElement(actionFileName, executeElement);
592                    }
593            }
594    
595            public void validateElements(String fileName) {
596                    String name = _getName(fileName);
597    
598                    if (fileName.endsWith(".action")) {
599                            validateActionElements(name);
600                    }
601                    else if (fileName.endsWith(".function")) {
602                            validateFunctionElements(name);
603                    }
604                    else if (fileName.endsWith(".macro")) {
605                            validateMacroElements(name);
606                    }
607                    else if (fileName.endsWith(".testcase")) {
608                            validateTestCaseElements(name);
609                    }
610                    else if (fileName.endsWith(".testsuite")) {
611                            validateTestSuiteElements(name);
612                    }
613            }
614    
615            public void validateFunctionElements(String functionName) {
616                    Element rootElement = getFunctionRootElement(functionName);
617    
618                    if (rootElement == null) {
619                            return;
620                    }
621    
622                    String functionFileName = getFunctionFileName(functionName);
623    
624                    List<Element> commandElements =
625                            _seleniumBuilderFileUtil.getAllChildElements(
626                                    rootElement, "command");
627    
628                    Set<String> commandElementNames = new HashSet<String>();
629    
630                    for (Element commandElement : commandElements) {
631                            String commandName = commandElement.attributeValue("name");
632    
633                            if (commandElementNames.contains(commandName)) {
634                                    _seleniumBuilderFileUtil.throwValidationException(
635                                            1009, functionFileName, commandElement, commandName);
636                            }
637                            else {
638                                    commandElementNames.add(commandName);
639                            }
640                    }
641    
642                    List<Element> conditionAndExecuteElements =
643                            _seleniumBuilderFileUtil.getAllChildElements(
644                                    rootElement, "condition");
645    
646                    conditionAndExecuteElements.addAll(
647                            _seleniumBuilderFileUtil.getAllChildElements(
648                                    rootElement, "execute"));
649    
650                    for (Element conditionAndExecuteElement : conditionAndExecuteElements) {
651                            String function = conditionAndExecuteElement.attributeValue(
652                                    "function");
653                            String selenium = conditionAndExecuteElement.attributeValue(
654                                    "selenium");
655    
656                            if (function != null) {
657                                    _validateFunctionElement(
658                                            functionFileName, conditionAndExecuteElement);
659                            }
660                            else if (selenium != null) {
661                                    _validateSeleniumElement(
662                                            functionFileName, conditionAndExecuteElement);
663                            }
664                    }
665            }
666    
667            public void validateMacroElements(String macroName) {
668                    Element rootElement = getMacroRootElement(macroName);
669    
670                    if (rootElement == null) {
671                            return;
672                    }
673    
674                    String macroFileName = getMacroFileName(macroName);
675    
676                    validateVarElements(rootElement, macroFileName);
677    
678                    List<Element> commandElements =
679                            _seleniumBuilderFileUtil.getAllChildElements(
680                                    rootElement, "command");
681    
682                    Set<String> commandElementNames = new HashSet<String>();
683    
684                    for (Element commandElement : commandElements) {
685                            String commandName = commandElement.attributeValue("name");
686    
687                            if (commandElementNames.contains(commandName)) {
688                                    _seleniumBuilderFileUtil.throwValidationException(
689                                            1009, macroFileName, commandElement, commandName);
690                            }
691                            else {
692                                    commandElementNames.add(commandName);
693                            }
694                    }
695    
696                    List<Element> conditionAndExecuteElements =
697                            _seleniumBuilderFileUtil.getAllChildElements(
698                                    rootElement, "condition");
699    
700                    conditionAndExecuteElements.addAll(
701                            _seleniumBuilderFileUtil.getAllChildElements(
702                                    rootElement, "execute"));
703    
704                    for (Element conditionAndExecuteElement : conditionAndExecuteElements) {
705                            String action = conditionAndExecuteElement.attributeValue("action");
706                            String macro = conditionAndExecuteElement.attributeValue("macro");
707    
708                            if (action != null) {
709                                    _validateActionElement(
710                                            macroFileName, conditionAndExecuteElement);
711                            }
712                            else if (macro != null) {
713                                    _validateMacroElement(
714                                            macroFileName, conditionAndExecuteElement);
715                            }
716                    }
717            }
718    
719            public void validateTestCaseElements(String testCaseName) {
720                    Element rootElement = getTestCaseRootElement(testCaseName);
721    
722                    if (rootElement == null) {
723                            return;
724                    }
725    
726                    String testCaseFileName = getTestCaseFileName(testCaseName);
727    
728                    validateVarElements(rootElement, testCaseFileName);
729    
730                    List<Element> commandElements =
731                            _seleniumBuilderFileUtil.getAllChildElements(
732                                    rootElement, "command");
733    
734                    Set<String> commandElementNames = new HashSet<String>();
735    
736                    for (Element commandElement : commandElements) {
737                            String commandName = commandElement.attributeValue("name");
738    
739                            if (commandElementNames.contains(commandName)) {
740                                    _seleniumBuilderFileUtil.throwValidationException(
741                                            1009, testCaseFileName, commandElement, commandName);
742                            }
743                            else {
744                                    commandElementNames.add(commandName);
745                            }
746                    }
747    
748                    List<Element> executeElements =
749                            _seleniumBuilderFileUtil.getAllChildElements(
750                                    rootElement, "execute");
751    
752                    for (Element executeElement : executeElements) {
753                            String action = executeElement.attributeValue("action");
754                            String macro = executeElement.attributeValue("macro");
755    
756                            if (action != null) {
757                                    _validateActionElement(testCaseFileName, executeElement);
758                            }
759                            else if (macro != null) {
760                                    _validateMacroElement(testCaseFileName, executeElement);
761                            }
762                    }
763            }
764    
765            public void validateTestSuiteElements(String testSuiteName) {
766                    Element rootElement = getTestSuiteRootElement(testSuiteName);
767    
768                    List<Element> executeElements =
769                            _seleniumBuilderFileUtil.getAllChildElements(
770                                    rootElement, "execute");
771    
772                    String testSuiteFileName = getTestSuiteFileName(testSuiteName);
773    
774                    for (Element executeElement : executeElements) {
775                            String testCase = executeElement.attributeValue("test-case");
776                            String testCaseCommand = executeElement.attributeValue(
777                                    "test-case-command");
778                            String testSuite = executeElement.attributeValue("test-suite");
779    
780                            if (testCase != null) {
781                                    _validateTestCaseElement(testSuiteFileName, executeElement);
782                            }
783                            else if (testCaseCommand != null) {
784                                    _validateTestCaseCommandElement(
785                                            testSuiteFileName, executeElement);
786                            }
787                            else if (testSuite != null) {
788                                    _validateTestSuiteElement(testSuiteFileName, executeElement);
789                            }
790                    }
791            }
792    
793            public void validateVarElements(Element rootElement, String fileName) {
794                    List<Element> varElements =
795                            _seleniumBuilderFileUtil.getAllChildElements(rootElement, "var");
796    
797                    for (Element varElement : varElements) {
798                            String varLocatorKey = varElement.attributeValue("locator-key");
799                            String varPath = varElement.attributeValue("path");
800    
801                            if (Validator.isNotNull(varLocatorKey) &&
802                                    Validator.isNotNull(varPath)) {
803    
804                                    if (!_pathRootElements.containsKey(varPath)) {
805                                            _seleniumBuilderFileUtil.throwValidationException(
806                                                    1014, fileName, varElement, varPath);
807                                    }
808    
809                                    if (!_isValidLocatorKey(varPath, null, varLocatorKey)) {
810                                            _seleniumBuilderFileUtil.throwValidationException(
811                                                    1010, fileName, varElement, varLocatorKey);
812                                    }
813                            }
814                    }
815            }
816    
817            private String _getClassName(String fileName) {
818                    return _seleniumBuilderFileUtil.getClassName(fileName);
819            }
820    
821            private String _getClassName(String fileName, String classSuffix) {
822                    return _seleniumBuilderFileUtil.getClassName(fileName, classSuffix);
823            }
824    
825            private String _getHTMLFileName(String fileName) {
826                    return _seleniumBuilderFileUtil.getHTMLFileName(fileName);
827            }
828    
829            private String _getJavaFileName(String fileName) {
830                    return _seleniumBuilderFileUtil.getJavaFileName(fileName);
831            }
832    
833            private String _getJavaFileName(String fileName, String classSuffix) {
834                    return _seleniumBuilderFileUtil.getJavaFileName(fileName, classSuffix);
835            }
836    
837            private int _getLocatorCount(Element rootElement) throws Exception {
838                    return _seleniumBuilderFileUtil.getLocatorCount(rootElement);
839            }
840    
841            private String _getName(String fileName) {
842                    return _seleniumBuilderFileUtil.getName(fileName);
843            }
844    
845            private String _getPackageName(String fileName) {
846                    return _seleniumBuilderFileUtil.getPackageName(fileName);
847            }
848    
849            private String _getReturnType(String name) throws Exception {
850                    return _seleniumBuilderFileUtil.getReturnType(name);
851            }
852    
853            private Element _getRootElement(String fileName) throws Exception {
854                    return _seleniumBuilderFileUtil.getRootElement(fileName);
855            }
856    
857            private String _getSimpleClassName(String fileName) {
858                    return _seleniumBuilderFileUtil.getSimpleClassName(fileName);
859            }
860    
861            private String _getSimpleClassName(String fileName, String classSuffix) {
862                    return _seleniumBuilderFileUtil.getSimpleClassName(
863                            fileName, classSuffix);
864            }
865    
866            private Set<String> _getTestCaseCommandNames(Element rootElement) {
867                    List<Element> commandElements =
868                            _seleniumBuilderFileUtil.getAllChildElements(
869                                    rootElement, "command");
870    
871                    Set<String> commandNames = new HashSet<String>();
872    
873                    for (Element commandElement : commandElements) {
874                            commandNames.add(commandElement.attributeValue("name"));
875                    }
876    
877                    return commandNames;
878            }
879    
880            private boolean _isActionName(String name) {
881                    for (String actionName : _actionNames) {
882                            if (actionName.equals(name)) {
883                                    return true;
884                            }
885                    }
886    
887                    return false;
888            }
889    
890            private boolean _isFunctionCommand(String name, String command) {
891                    if (!_isFunctionName(name)) {
892                            return false;
893                    }
894    
895                    Element rootElement = getFunctionRootElement(name);
896    
897                    List<Element> commandElements =
898                            _seleniumBuilderFileUtil.getAllChildElements(
899                                    rootElement, "command");
900    
901                    for (Element commandElement : commandElements) {
902                            String commandName = commandElement.attributeValue("name");
903    
904                            if (commandName.equals(command)) {
905                                    return true;
906                            }
907                    }
908    
909                    return false;
910            }
911    
912            private boolean _isFunctionName(String name) {
913                    for (String functionName : _functionNames) {
914                            if (functionName.equals(StringUtil.upperCaseFirstLetter(name))) {
915                                    return true;
916                            }
917                    }
918    
919                    return false;
920            }
921    
922            private boolean _isMacroCommand(String name, String command) {
923                    if (!_isMacroName(name)) {
924                            return false;
925                    }
926    
927                    Element rootElement = getMacroRootElement(name);
928    
929                    List<Element> commandElements =
930                            _seleniumBuilderFileUtil.getAllChildElements(
931                                    rootElement, "command");
932    
933                    for (Element commandElement : commandElements) {
934                            String commandName = commandElement.attributeValue("name");
935    
936                            if (commandName.equals(command)) {
937                                    return true;
938                            }
939                    }
940    
941                    return false;
942            }
943    
944            private boolean _isMacroName(String name) {
945                    for (String macroName : _macroNames) {
946                            if (macroName.equals(name)) {
947                                    return true;
948                            }
949                    }
950    
951                    return false;
952            }
953    
954            private boolean _isSeleniumCommand(String command) {
955                    if (_seleniumParameterCounts.containsKey(command)) {
956                            return true;
957                    }
958    
959                    return false;
960            }
961    
962            private boolean _isTestCaseCommand(
963                    String testCaseName, String testCaseCommand) {
964    
965                    Set<String> commands = _testCaseCommandNames.get(testCaseName);
966    
967                    return commands.contains(testCaseCommand);
968            }
969    
970            private boolean _isTestCaseName(String name) {
971                    for (String testCaseName : _testCaseNames) {
972                            if (testCaseName.equals(name)) {
973                                    return true;
974                            }
975                    }
976    
977                    return false;
978            }
979    
980            private boolean _isTestSuiteName(String name) {
981                    for (String testSuiteName : _testSuiteNames) {
982                            if (testSuiteName.equals(name)) {
983                                    return true;
984                            }
985                    }
986    
987                    return false;
988            }
989    
990            private boolean _isValidLocatorKey(
991                    String actionName, String caseComparator, String locatorKey) {
992    
993                    Element pathRootElement = getPathRootElement(actionName);
994    
995                    Set<String> pathLocatorKeys = getPathLocatorKeys(pathRootElement);
996    
997                    String[] partialKeys = {};
998    
999                    if (locatorKey.contains("${") && locatorKey.contains("}")) {
1000                            caseComparator = "partial";
1001    
1002                            partialKeys = locatorKey.split("\\$\\{[^}]*?\\}");
1003                    }
1004    
1005                    for (String pathLocatorKey : pathLocatorKeys) {
1006                            if (caseComparator == null) {
1007                                    if (pathLocatorKey.equals(locatorKey)) {
1008                                            return true;
1009                                    }
1010                            }
1011                            else {
1012                                    if (caseComparator.equals("contains") &&
1013                                            pathLocatorKey.contains(locatorKey)) {
1014    
1015                                            return true;
1016                                    }
1017                                    else if (caseComparator.equals("endsWith") &&
1018                                                     pathLocatorKey.endsWith(locatorKey)) {
1019    
1020                                            return true;
1021                                    }
1022                                    else if (caseComparator.equals("partial")) {
1023                                            boolean containsAll = true;
1024    
1025                                            for (String s : partialKeys) {
1026                                                    if (!pathLocatorKey.contains(s)) {
1027                                                            containsAll = false;
1028                                                    }
1029                                            }
1030    
1031                                            if (containsAll) {
1032                                                    return true;
1033                                            }
1034                                    }
1035                                    else if (caseComparator.equals("startsWith") &&
1036                                                     pathLocatorKey.startsWith(locatorKey)) {
1037    
1038                                            return true;
1039                                    }
1040                            }
1041                    }
1042    
1043                    return false;
1044            }
1045    
1046            private String _normalizeFileName(String fileName) {
1047                    return _seleniumBuilderFileUtil.normalizeFileName(fileName);
1048            }
1049    
1050            private void _validateActionElement(String fileName, Element element) {
1051                    String action = element.attributeValue("action");
1052    
1053                    int x = action.indexOf(StringPool.POUND);
1054    
1055                    if (x == -1) {
1056                            _seleniumBuilderFileUtil.throwValidationException(
1057                                    1006, fileName, element, "action");
1058                    }
1059    
1060                    String actionName = action.substring(0, x);
1061    
1062                    if (!_isActionName(actionName)) {
1063                            _seleniumBuilderFileUtil.throwValidationException(
1064                                    1011, fileName, element, "action", actionName);
1065                    }
1066    
1067                    String actionCommand = action.substring(x + 1);
1068    
1069                    if (!_isFunctionName(actionCommand)) {
1070                            _seleniumBuilderFileUtil.throwValidationException(
1071                                    1012, fileName, element, "action", actionCommand);
1072                    }
1073    
1074                    _validateLocatorKeyElement(fileName, actionName, element);
1075            }
1076    
1077            private void _validateFunctionElement(String fileName, Element element) {
1078                    String function = element.attributeValue("function");
1079    
1080                    int x = function.indexOf(StringPool.POUND);
1081    
1082                    if (x == -1) {
1083                            _seleniumBuilderFileUtil.throwValidationException(
1084                                    1006, fileName, element, "function");
1085                    }
1086    
1087                    String functionName = function.substring(0, x);
1088    
1089                    if (!_isFunctionName(functionName)) {
1090                            _seleniumBuilderFileUtil.throwValidationException(
1091                                    1011, fileName, element, "function", functionName);
1092                    }
1093    
1094                    String functionCommand = function.substring(x + 1);
1095    
1096                    if (!_isFunctionCommand(functionName, functionCommand)) {
1097                            _seleniumBuilderFileUtil.throwValidationException(
1098                                    1012, fileName, element, "function", functionCommand);
1099                    }
1100            }
1101    
1102            private void _validateLocatorKeyElement(
1103                    String fileName, String actionName, Element element) {
1104    
1105                    String comparator = element.attributeValue("comparator");
1106    
1107                    List<Attribute> attributes = element.attributes();
1108    
1109                    for (Attribute attribute : attributes) {
1110                            String attributeName = attribute.getName();
1111    
1112                            if (attributeName.startsWith("locator-key")) {
1113                                    String attributeValue = attribute.getValue();
1114    
1115                                    if (!_isValidLocatorKey(
1116                                                    actionName, comparator, attributeValue)) {
1117    
1118                                            _seleniumBuilderFileUtil.throwValidationException(
1119                                                    1010, fileName, element, attributeValue);
1120                                    }
1121                            }
1122                    }
1123            }
1124    
1125            private void _validateMacroElement(String fileName, Element element) {
1126                    String macro = element.attributeValue("macro");
1127    
1128                    int x = macro.indexOf(StringPool.POUND);
1129    
1130                    if (x == -1) {
1131                            _seleniumBuilderFileUtil.throwValidationException(
1132                                    1006, fileName, element, "macro");
1133                    }
1134    
1135                    String macroName = macro.substring(0, x);
1136    
1137                    if (!_isMacroName(macroName)) {
1138                            _seleniumBuilderFileUtil.throwValidationException(
1139                                    1011, fileName, element, "macro", macroName);
1140                    }
1141    
1142                    String macroCommand = macro.substring(x + 1);
1143    
1144                    if (!_isMacroCommand(macroName, macroCommand)) {
1145                            _seleniumBuilderFileUtil.throwValidationException(
1146                                    1012, fileName, element, "macro", macroCommand);
1147                    }
1148            }
1149    
1150            private void _validateSeleniumElement(String fileName, Element element) {
1151                    String selenium = element.attributeValue("selenium");
1152    
1153                    if (!_isSeleniumCommand(selenium)) {
1154                            _seleniumBuilderFileUtil.throwValidationException(
1155                                    1012, fileName, element, "selenium", selenium);
1156                    }
1157            }
1158    
1159            private void _validateTestCaseCommandElement(
1160                    String fileName, Element element) {
1161    
1162                    String testCaseCommand = element.attributeValue("test-case-command");
1163    
1164                    int x = testCaseCommand.lastIndexOf("#");
1165    
1166                    String testCaseName = testCaseCommand.substring(0, x);
1167    
1168                    if (!_isTestCaseName(testCaseName)) {
1169                            _seleniumBuilderFileUtil.throwValidationException(
1170                                    1016, fileName, element, "test-case-command", testCaseName);
1171                    }
1172    
1173                    String testCaseCommandName = testCaseCommand.substring(x + 1);
1174    
1175                    if (!_isTestCaseCommand(testCaseName, testCaseCommandName)) {
1176                            _seleniumBuilderFileUtil.throwValidationException(
1177                                    1016, fileName, element, "test-case-command",
1178                                    testCaseCommandName);
1179                    }
1180            }
1181    
1182            private void _validateTestCaseElement(String fileName, Element element) {
1183                    String testCase = element.attributeValue("test-case");
1184    
1185                    if (!_isTestCaseName(testCase)) {
1186                            _seleniumBuilderFileUtil.throwValidationException(
1187                                    1011, fileName, element, "test-case", testCase);
1188                    }
1189            }
1190    
1191            private void _validateTestSuiteElement(String fileName, Element element) {
1192                    String testSuite = element.attributeValue("test-suite");
1193    
1194                    if (!_isTestSuiteName(testSuite)) {
1195                            _seleniumBuilderFileUtil.throwValidationException(
1196                                    1011, fileName, element, "test-suite", testSuite);
1197                    }
1198            }
1199    
1200            private Map<String, String> _actionClassNames =
1201                    new HashMap<String, String>();
1202            private Map<String, String> _actionFileNames =
1203                    new HashMap<String, String>();
1204            private Map<String, String> _actionJavaFileNames =
1205                    new HashMap<String, String>();
1206            private Set<String> _actionNames = new HashSet<String>();
1207            private Map<String, String> _actionPackageNames =
1208                    new HashMap<String, String>();
1209            private Map<String, Element> _actionRootElements =
1210                    new HashMap<String, Element>();
1211            private Map<String, String> _actionSimpleClassNames =
1212                    new HashMap<String, String>();
1213            private String _baseDir;
1214            private Map<String, String> _functionClassNames =
1215                    new HashMap<String, String>();
1216            private Map<String, String> _functionFileNames =
1217                    new HashMap<String, String>();
1218            private Map<String, String> _functionJavaFileNames =
1219                    new HashMap<String, String>();
1220            private Map<String, Integer> _functionLocatorCounts =
1221                    new HashMap<String, Integer>();
1222            private Set<String> _functionNames = new HashSet<String>();
1223            private Map<String, String> _functionPackageNames =
1224                    new HashMap<String, String>();
1225            private Map<String, String> _functionReturnTypes =
1226                    new HashMap<String, String>();
1227            private Map<String, Element> _functionRootElements =
1228                    new HashMap<String, Element>();
1229            private Map<String, String> _functionSimpleClassNames =
1230                    new HashMap<String, String>();
1231            private Map<String, String> _macroClassNames =
1232                    new HashMap<String, String>();
1233            private Map<String, String> _macroFileNames = new HashMap<String, String>();
1234            private Map<String, String> _macroJavaFileNames =
1235                    new HashMap<String, String>();
1236            private Set<String> _macroNames = new HashSet<String>();
1237            private Map<String, String> _macroPackageNames =
1238                    new HashMap<String, String>();
1239            private Map<String, Element> _macroRootElements =
1240                    new HashMap<String, Element>();
1241            private Map<String, String> _macroSimpleClassNames =
1242                    new HashMap<String, String>();
1243            private Map<String, String> _pathClassNames = new HashMap<String, String>();
1244            private Map<String, String> _pathFileNames = new HashMap<String, String>();
1245            private Map<String, String> _pathJavaFileNames =
1246                    new HashMap<String, String>();
1247            private Set<String> _pathNames = new HashSet<String>();
1248            private Map<String, String> _pathPackageNames =
1249                    new HashMap<String, String>();
1250            private Map<String, Element> _pathRootElements =
1251                    new HashMap<String, Element>();
1252            private Map<String, String> _pathSimpleClassNames =
1253                    new HashMap<String, String>();
1254            private SeleniumBuilderFileUtil _seleniumBuilderFileUtil;
1255            private Map<String, Integer> _seleniumParameterCounts =
1256                    new HashMap<String, Integer>();
1257            private Map<String, String> _testCaseClassNames =
1258                    new HashMap<String, String>();
1259            private Map<String, Set<String>> _testCaseCommandNames =
1260                    new HashMap<String, Set<String>>();
1261            private Map<String, String> _testCaseFileNames =
1262                    new HashMap<String, String>();
1263            private Map<String, String> _testCaseHTMLFileNames =
1264                    new HashMap<String, String>();
1265            private Map<String, String> _testCaseJavaFileNames =
1266                    new HashMap<String, String>();
1267            private Set<String> _testCaseNames = new HashSet<String>();
1268            private Map<String, String> _testCasePackageNames =
1269                    new HashMap<String, String>();
1270            private Map<String, Element> _testCaseRootElements =
1271                    new HashMap<String, Element>();
1272            private Map<String, String> _testCaseSimpleClassNames =
1273                    new HashMap<String, String>();
1274            private Map<String, String> _testSuiteClassNames =
1275                    new HashMap<String, String>();
1276            private Map<String, String> _testSuiteFileNames =
1277                    new HashMap<String, String>();
1278            private Map<String, String> _testSuiteHTMLFileNames =
1279                    new HashMap<String, String>();
1280            private Map<String, String> _testSuiteJavaFileNames =
1281                    new HashMap<String, String>();
1282            private Set<String> _testSuiteNames = new HashSet<String>();
1283            private Map<String, String> _testSuitePackageNames =
1284                    new HashMap<String, String>();
1285            private Map<String, Element> _testSuiteRootElements =
1286                    new HashMap<String, Element>();
1287            private Map<String, String> _testSuiteSimpleClassNames =
1288                    new HashMap<String, String>();
1289    
1290    }