1   /***
2    Copyright (C) 2005 The Java Community
3   
4    This program is free software; you can redistribute it and/or modify  it under
5    the terms of the GNU General Public License as published by  the Free Software
6    Foundation; either version 2 of the License, or  (at your option) any later
7    version.
8   
9    This program is distributed in the hope that it will be useful,  but WITHOUT
10   ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS
11   FOR A PARTICULAR PURPOSE. See the  GNU General Public License for more details.
12  
13   You should have received a copy of the GNU General Public License  along with
14   this program; if not, write to the Free Software  Foundation, Inc., 59 Temple
15   Place, Suite 330, Boston, MA 02111-1307 USA.
16   */
17  package org.bejug.javacareers.view;
18  
19  import java.io.BufferedReader;
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.FileNotFoundException;
23  import java.io.IOException;
24  import java.io.InputStreamReader;
25  import java.util.ArrayList;
26  import java.util.Collections;
27  import java.util.List;
28  
29  import junit.framework.TestCase;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  /***
35   * Created by IntelliJ IDEA.
36   * User: bbr
37   * Date: 26-jul-2005
38   * Time: 10:51:14
39   * To change this template use File | Settings | File Templates.
40   */
41  
42  /***
43   * @author bbr (last modified by $Author: shally $)
44   * @version $Revision: 1.8 $ - $Date: 2005/12/21 11:38:43 $
45   */
46  public class JavaCareersFacesIDTests extends TestCase {
47      private static final Log LOG = LogFactory.getLog(JavaCareersFacesIDTests.class);
48  
49      private List componentTags;
50      private List components;
51  
52      private static final String JSF_EXTENSION = ".jsp";
53  
54  
55      /***
56       * 
57       */
58      public JavaCareersFacesIDTests() {
59          super();
60  
61          componentTags = new ArrayList();
62          componentTags.add("<h:panelGrid");
63          componentTags.add("<h:dataTable");
64          componentTags.add("<h:form");
65          componentTags.add("<h:panelGroup");
66          componentTags.add("<h:column");
67          componentTags.add("<h:outputText");
68          componentTags.add("<h:outputLink");
69          componentTags.add("<h:commandLink");
70          componentTags.add("<h:inputText");
71          componentTags.add("<h:inputArea");
72          componentTags.add("<h:message");
73          componentTags.add("<h:commandButton");
74          componentTags.add("<h:outputLabel");
75          componentTags.add("<f:subview");
76          LOG.info("Debug: Checking for elements: " + componentTags);
77          components = collectComponents();
78  
79  
80      }
81      /***
82       * 
83       */
84      public void setUp() {
85      }
86      /***
87       * 
88       * @return list of components
89       */
90      private List collectComponents() {
91          List componentlist = new ArrayList();
92          List files = getSourceFiles();
93          LOG.info("Debug: Found: " + files.size() + " files");
94          for (int i = 0; i < files.size(); i++) {
95              //for (int i = 0; i < 15; i++) {
96              File file = (File) files.get(i);
97              String fileString = readFile(file);
98              LOG.info("Debug: Read file: " + file);
99              if (fileString.indexOf("<title>") != -1) {
100                 LOG.info("Debug: This is a header page, not searching");
101                 continue;
102             }
103             for (int j = 0; j < componentTags.size(); j++) {
104                 String s = (String) componentTags.get(j);
105 
106                 int index = 0;
107                 int prevIndex = 0;
108                 LOG.info("Debug: getting ids for: " + s);
109 
110                 while (index > -1) {
111                     index = fileString.indexOf(s, prevIndex + 1);
112                     if (index > -1) {
113                         String id = null;
114                         int indexEnd = fileString.indexOf(">", index);
115                         int indexId = fileString.indexOf("id=", index);
116                         if (indexId != -1 && indexId < indexEnd) {
117                             int hyph = fileString.indexOf("\"", indexId);
118                             id = fileString.substring(hyph + 1, fileString.indexOf("\"", hyph + 1));
119                             LOG.info("Debug: ID is " + id);
120 
121 
122                         }
123                         FacesComponentHolder holder = new FacesComponentHolder(s, file.getName(), id);
124                         componentlist.add(holder);
125                         prevIndex = index;
126                     }
127 
128                 }
129 
130                 //LOG.info("Debug: element: "+ele.toString());
131 
132             }
133 
134 
135         }
136 
137 
138         return componentlist;
139     }
140 
141     /***
142      * 
143      * @param file to be read
144      * @return contents of the file
145      */
146     private String readFile(File file) {
147         StringBuffer fileString = null;
148         try {
149             BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
150 
151             fileString = new StringBuffer();
152             while (in.ready()) {
153                 fileString.append(in.readLine());
154             }
155 
156 
157             LOG.info("Debug: File: " + fileString);
158 
159         } catch (FileNotFoundException e) {
160             LOG.debug(e);
161         } catch (IOException e) {
162             LOG.debug(e);
163         }
164         return fileString.toString();
165     }
166 
167     /***
168      * 
169      * @return list of files
170      */
171     private List getSourceFiles() {
172         File pwd = new File("." + File.separator + "src");
173         String BASE_PATH = null;
174         try {
175             BASE_PATH = pwd.getCanonicalPath();
176         } catch (IOException e) {
177             LOG.debug(e);
178         }
179         LOG.info("Debug: Using path: " + BASE_PATH);
180         List files = new ArrayList();
181         recurseDirectory(BASE_PATH, files);
182 
183 
184         return files;
185 
186     }
187     
188     /***
189      * 
190      * @param path to be investigated
191      * @param files collected
192      */
193     private void recurseDirectory(final String path, List files) {
194         //LOG.info("Debug: Recursing "+path);
195         File f = new File(path);
196         if (f.isFile() && f.getName().endsWith(JSF_EXTENSION)) {
197             files.add(f);
198             LOG.info("Debug: Added file: " + f.getAbsolutePath());
199         } else if (f.isDirectory()) {
200             String dirpath = path.endsWith(File.separator) ? path : 
201             		         path + File.separator;
202             String[] list = f.list();
203             for (int a = 0; (list != null) && (a < list.length); a++) {
204                 recurseDirectory(dirpath + list[a], files);
205             }
206         }
207     }
208 
209     /***
210      * 
211      */
212     public void testDuplicateIDs() {
213         LOG.info("Debug: Checking for duplicates");
214         int count = 0;
215         int countNotNull = 0;
216         Collections.sort(components, FacesComponentHolder.getIdComparator());
217         for (int i = 0; i < components.size(); i++) {
218             FacesComponentHolder facesComponentHolder = (FacesComponentHolder) components.get(i);
219             int search = Collections.binarySearch(components, facesComponentHolder, FacesComponentHolder.getIdComparator());
220             if (search > 0 && search != i) {
221                 LOG.info("Debug: Found duplicate: " + facesComponentHolder);
222                 LOG.info("Debug: With: " + components.get(search));
223                 //LOG.info("Debug: ");
224                 if (facesComponentHolder.getId() != null) {
225                     countNotNull++;
226                 }
227                 count++;
228             }
229 
230 
231         }
232         LOG.info("Debug: Found " + count + " duplicate id's");
233         LOG.info("Debug: Whereof " + countNotNull + " duplicate non-null id's");
234 
235         assertEquals(0, count);
236 
237 
238         LOG.info("Debug: Done");
239 
240     }
241 
242     /***
243      * 
244      */
245     public void testMissingIDs() {
246         LOG.info("Debug: Checking for missing id's");
247         int count = 0;
248         for (int i = 0; i < components.size(); i++) {
249             FacesComponentHolder facesComponentHolder = (FacesComponentHolder) components.get(i);
250 
251             if (facesComponentHolder.getId() == null) {
252                 LOG.debug(facesComponentHolder);
253                 count++;
254 
255 
256             }
257 
258         }
259         LOG.info("Debug: Found " + count + " missing id's");
260         assertEquals(0, count);
261 
262         LOG.info("Debug: Done");
263 
264     }
265 
266 
267 }
268 
269 /***
270  * $Log: JavaCareersFacesIDTests.java,v $
271  * Revision 1.8  2005/12/21 11:38:43  shally
272  * *** empty log message ***
273  *
274  * Revision 1.7  2005/12/20 15:36:47  shally
275  * CheckStyle and PMD changes.
276  *
277  * Revision 1.6  2005/12/08 14:53:46  shally
278  * Opkuis voor checkstyle.
279  *
280  * Revision 1.5  2005/09/30 14:38:08  bavo_jcs
281  * Fixed URL
282  *
283  * Revision 1.4  2005/09/13 08:11:06  schauwvliege
284  * organize imports
285  *
286  * Revision 1.3  2005/08/03 10:36:41  bavo_jcs
287  * ID fix
288  *
289  * Revision 1.2  2005/08/01 09:50:09  bavo_jcs
290  * Fixed SPAN tag in title
291  *
292  * Revision 1.1  2005/07/26 14:22:04  bavo_jcs
293  * Faces Id tests
294  *
295  */