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