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