001    /**
002     * Copyright (c) 2000-2012 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.xml.Element;
018    
019    import java.util.HashMap;
020    import java.util.HashSet;
021    import java.util.Map;
022    import java.util.Set;
023    
024    import org.apache.tools.ant.DirectoryScanner;
025    
026    /**
027     * @author Michael Hashimoto
028     */
029    public class SeleniumBuilderContext {
030    
031            public SeleniumBuilderContext(String baseDir) throws Exception {
032                    _baseDir = baseDir;
033    
034                    _seleniumBuilderFileUtil = new SeleniumBuilderFileUtil(_baseDir);
035    
036                    DirectoryScanner directoryScanner = new DirectoryScanner();
037    
038                    directoryScanner.setBasedir(_baseDir);
039                    directoryScanner.setIncludes(
040                            new String[] {
041                                    "**\\*.action", "**\\*.function", "**\\*.macro", "**\\*.path",
042                                    "**\\*.testcase", "**\\*.testsuite"
043                            });
044    
045                    directoryScanner.scan();
046    
047                    String[] fileNames = directoryScanner.getIncludedFiles();
048    
049                    for (String fileName : fileNames) {
050                            fileName = _normalizeFileName(fileName);
051    
052                            if (fileName.endsWith(".action")) {
053                                    String actionName = _getName(fileName);
054    
055                                    _actionFileNames.put(actionName, fileName);
056    
057                                    if (_actionNames.contains(actionName)) {
058                                            throw new Exception(
059                                                    "Duplicate name " + actionName + " at " + fileName);
060                                    }
061    
062                                    _actionNames.add(actionName);
063    
064                                    _actionRootElements.put(fileName, _getRootElement(fileName));
065                            }
066                            else if (fileName.endsWith(".function")) {
067                                    _functionClassNames.add(_getClassName(fileName));
068    
069                                    String functionName = _getName(fileName);
070    
071                                    _functionFileNames.put(functionName, fileName);
072    
073                                    if (_functionNames.contains(functionName)) {
074                                            throw new Exception(
075                                                    "Duplicate name " + functionName + " at " + fileName);
076                                    }
077    
078                                    _functionNames.add(functionName);
079    
080                                    _functionRootElements.put(fileName, _getRootElement(fileName));
081                            }
082                            else if (fileName.endsWith(".macro")) {
083                                    _macroClassNames.add(_getClassName(fileName));
084    
085                                    String macroName = _getName(fileName);
086    
087                                    _macroFileNames.put(macroName, fileName);
088    
089                                    if (_macroNames.contains(macroName)) {
090                                            throw new Exception(
091                                                    "Duplicate name " + macroName + " at " + fileName);
092                                    }
093    
094                                    _macroNames.add(macroName);
095    
096                                    _macroRootElements.put(fileName, _getRootElement(fileName));
097                            }
098                            else if (fileName.endsWith(".path")) {
099                                    _actionClassNames.add(_getClassName(fileName, "Action"));
100    
101                                    _pathClassNames.add(_getClassName(fileName));
102    
103                                    String pathName = _getName(fileName);
104    
105                                    _pathFileNames.put(pathName, fileName);
106    
107                                    if (_pathNames.contains(pathName)) {
108                                            throw new Exception(
109                                                    "Duplicate name " + pathName + " at " + fileName);
110                                    }
111    
112                                    _pathNames.add(pathName);
113    
114                                    _pathRootElements.put(fileName, _getRootElement(fileName));
115                            }
116                            else if (fileName.endsWith(".testcase")) {
117                                    _testCaseClassNames.add(_getClassName(fileName));
118    
119                                    String testCaseName = _getName(fileName);
120    
121                                    _testCaseFileNames.put(testCaseName, fileName);
122    
123                                    if (_testCaseNames.contains(testCaseName)) {
124                                            throw new Exception(
125                                                    "Duplicate name " + testCaseName + " at " + fileName);
126                                    }
127    
128                                    _testCaseNames.add(testCaseName);
129    
130                                    _testCaseRootElements.put(fileName, _getRootElement(fileName));
131                            }
132                            else if (fileName.endsWith(".testsuite")) {
133                                    _testSuiteClassNames.add(_getClassName(fileName));
134    
135                                    String testSuiteName = _getName(fileName);
136    
137                                    _testSuiteFileNames.put(testSuiteName, fileName);
138    
139                                    if (_testSuiteNames.contains(testSuiteName)) {
140                                            throw new Exception(
141                                                    "Duplicate name " + testSuiteName + " at " + fileName);
142                                    }
143    
144                                    _testSuiteNames.add(testSuiteName);
145    
146                                    _testSuiteRootElements.put(fileName, _getRootElement(fileName));
147                            }
148                            else {
149                                    throw new IllegalArgumentException("Invalid file " + fileName);
150                            }
151                    }
152            }
153    
154            public Set<String> getActionClassNames() {
155                    return _actionClassNames;
156            }
157    
158            public Map<String, String> getActionFileNames() {
159                    return _actionFileNames;
160            }
161    
162            public Set<String> getActionNames() {
163                    return _actionNames;
164            }
165    
166            public Element getActionRootElement(String fileName) {
167                    return _actionRootElements.get(fileName);
168            }
169    
170            public Map<String, Element> getActionRootElements() {
171                    return _actionRootElements;
172            }
173    
174            public String getBaseDir() {
175                    return _baseDir;
176            }
177    
178            public Set<String> getFunctionClassNames() {
179                    return _functionClassNames;
180            }
181    
182            public Map<String, String> getFunctionFileNames() {
183                    return _functionFileNames;
184            }
185    
186            public Set<String> getFunctionNames() {
187                    return _functionNames;
188            }
189    
190            public Element getFunctionRootElement(String fileName) {
191                    return _functionRootElements.get(fileName);
192            }
193    
194            public Map<String, Element> getFunctionRootElements() {
195                    return _functionRootElements;
196            }
197    
198            public Set<String> getMacroClassNames() {
199                    return _macroClassNames;
200            }
201    
202            public Map<String, String> getMacroFileNames() {
203                    return _macroFileNames;
204            }
205    
206            public Set<String> getMacroNames() {
207                    return _macroNames;
208            }
209    
210            public Element getMacroRootElement(String fileName) {
211                    return _macroRootElements.get(fileName);
212            }
213    
214            public Map<String, Element> getMacroRootElements() {
215                    return _macroRootElements;
216            }
217    
218            public Set<String> getPathClassNames() {
219                    return _pathClassNames;
220            }
221    
222            public Map<String, String> getPathFileNames() {
223                    return _pathFileNames;
224            }
225    
226            public Set<String> getPathNames() {
227                    return _pathNames;
228            }
229    
230            public Element getPathRootElement(String fileName) {
231                    return _pathRootElements.get(fileName);
232            }
233    
234            public Map<String, Element> getPathRootElements() {
235                    return _pathRootElements;
236            }
237    
238            public Set<String> getTestCaseClassNames() {
239                    return _testCaseClassNames;
240            }
241    
242            public Map<String, String> getTestCaseFileNames() {
243                    return _testCaseFileNames;
244            }
245    
246            public Set<String> getTestCaseNames() {
247                    return _testCaseNames;
248            }
249    
250            public Element getTestCaseRootElement(String fileName) {
251                    return _testCaseRootElements.get(fileName);
252            }
253    
254            public Map<String, Element> getTestCaseRootElements() {
255                    return _testCaseRootElements;
256            }
257    
258            public Set<String> getTestSuiteClassNames() {
259                    return _testSuiteClassNames;
260            }
261    
262            public Map<String, String> getTestSuiteFileNames() {
263                    return _testSuiteFileNames;
264            }
265    
266            public Set<String> getTestSuiteNames() {
267                    return _testSuiteNames;
268            }
269    
270            public Element getTestSuiteRootElement(String fileName) {
271                    return _testSuiteRootElements.get(fileName);
272            }
273    
274            public Map<String, Element> getTestSuiteRootElements() {
275                    return _testSuiteRootElements;
276            }
277    
278            private String _getClassName(String fileName) {
279                    return _seleniumBuilderFileUtil.getClassName(fileName);
280            }
281    
282            private String _getClassName(String fileName, String classSuffix) {
283                    return _seleniumBuilderFileUtil.getClassName(fileName, classSuffix);
284            }
285    
286            private String _getName(String fileName) {
287                    return _seleniumBuilderFileUtil.getName(fileName);
288            }
289    
290            private Element _getRootElement(String fileName) throws Exception {
291                    return _seleniumBuilderFileUtil.getRootElement(fileName);
292            }
293    
294            private String _normalizeFileName(String fileName) {
295                    return _seleniumBuilderFileUtil.normalizeFileName(fileName);
296            }
297    
298            private Set<String> _actionClassNames = new HashSet<String>();
299            private Map<String, String> _actionFileNames =
300                    new HashMap<String, String>();
301            private Set<String> _actionNames = new HashSet<String>();
302            private Map<String, Element> _actionRootElements =
303                    new HashMap<String, Element>();
304            private String _baseDir;
305            private Set<String> _functionClassNames = new HashSet<String>();
306            private Map<String, String> _functionFileNames =
307                    new HashMap<String, String>();
308            private Set<String> _functionNames = new HashSet<String>();
309            private Map<String, Element> _functionRootElements =
310                    new HashMap<String, Element>();
311            private Set<String> _macroClassNames = new HashSet<String>();
312            private Map<String, String> _macroFileNames = new HashMap<String, String>();
313            private Set<String> _macroNames = new HashSet<String>();
314            private Map<String, Element> _macroRootElements =
315                    new HashMap<String, Element>();
316            private Set<String> _pathClassNames = new HashSet<String>();
317            private Map<String, String> _pathFileNames = new HashMap<String, String>();
318            private Set<String> _pathNames = new HashSet<String>();
319            private Map<String, Element> _pathRootElements =
320                    new HashMap<String, Element>();
321            private SeleniumBuilderFileUtil _seleniumBuilderFileUtil;
322            private Set<String> _testCaseClassNames = new HashSet<String>();
323            private Map<String, String> _testCaseFileNames =
324                    new HashMap<String, String>();
325            private Set<String> _testCaseNames = new HashSet<String>();
326            private Map<String, Element> _testCaseRootElements =
327                    new HashMap<String, Element>();
328            private Set<String> _testSuiteClassNames = new HashSet<String>();
329            private Map<String, String> _testSuiteFileNames =
330                    new HashMap<String, String>();
331            private Set<String> _testSuiteNames = new HashSet<String>();
332            private Map<String, Element> _testSuiteRootElements =
333                    new HashMap<String, Element>();
334    
335    }