KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > util > FileCharacterTest


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package util;
59
60 import java.io.File JavaDoc;
61 import java.io.FileNotFoundException JavaDoc;
62 import java.io.FileReader JavaDoc;
63 import java.io.FileWriter JavaDoc;
64 import java.io.FilenameFilter JavaDoc;
65 import java.io.IOException JavaDoc;
66 import java.util.Enumeration JavaDoc;
67 import java.util.Stack JavaDoc;
68 import java.util.Vector JavaDoc;
69
70 import junit.framework.Test;
71 import junit.framework.TestCase;
72 import junit.framework.TestSuite;
73 import junit.textui.TestRunner;
74
75 /**
76  * A JUnit test to check our files for the dreaded ^M character. The test displays a list of
77  * all the WSIF files it finds that contain the character.
78  *
79  * This test is configured by the "fix" component setting in the wsif.test.properties file.
80  * If fix is set to on, this test will attempt to remove any ^M chars it finds in the WSIF files.
81  * It is recommended that fix=on if only set if you are sure that the ^M characters are present
82  * in the server copy of the file.
83  *
84  * @author Owen Burroughs <owenb@apache.org>
85  */

86 public class FileCharacterTest extends TestCase {
87
88     static Stack JavaDoc dirStack = new Stack JavaDoc();
89     static Vector JavaDoc files = new Vector JavaDoc();
90     static Vector JavaDoc badFiles = new Vector JavaDoc();
91
92     public FileCharacterTest(String JavaDoc name) {
93         super(name);
94     }
95
96     public static void main(String JavaDoc[] args) {
97         TestRunner.run(suite());
98     }
99
100     public static Test suite() {
101         return new TestSuite(FileCharacterTest.class);
102     }
103
104     public void testFiles() {
105         File JavaDoc path = null;
106         int index;
107         try {
108             String JavaDoc temp = TestUtilities.getWsdlPath("");
109             path = new File JavaDoc(temp);
110
111             //check that path exists
112
if (!path.exists())
113                 throw new FileNotFoundException JavaDoc();
114
115             //if path is not a directory, check that it is a file
116
if (!path.isDirectory())
117                 //if path is a file, get parent directory (ignore file name)
118
if (path.isFile())
119                     path = path.getParentFile();
120             //else report error and exit
121
else {
122                 System.out.println("\npath '" + path + "' is not a file or directory!");
123                 System.exit(1);
124             }
125
126             //if path is unreadable, report error and exit
127
if (!path.canRead()) {
128                 System.out.println("FCT: " + path + ": Permission denied");
129                 System.exit(1);
130             }
131         }
132         //catch non-existent file error
133
catch (FileNotFoundException JavaDoc e) {
134             System.out.println("\npath '" + path + "' does not exist!");
135             System.exit(1);
136         }
137
138         findFiles(path);
139         while (!dirStack.empty()) {
140             dirStack.pop();
141         }
142         int total = files.size();
143         Enumeration JavaDoc e = files.elements();
144         while (e.hasMoreElements()) {
145             File JavaDoc f = (File JavaDoc) e.nextElement();
146             //System.out.println(f);
147
checkFile(f);
148         }
149
150         int bad = badFiles.size();
151         if (bad > 0) {
152             System.out.println(
153                 "\nOut of the "
154                     + total
155                     + " WSIF files under "
156                     + path
157                     + ",\nthe following "
158                     + bad
159                     + " files contain ^M characters:\n");
160             Enumeration JavaDoc e2 = badFiles.elements();
161             while (e2.hasMoreElements()) {
162                 File JavaDoc f = (File JavaDoc) e2.nextElement();
163                 System.out.println(f);
164             }
165
166             if (TestUtilities.areWeTesting("fix")) {
167                 System.out.println("\nNow to fix them :-)\n");
168                 Enumeration JavaDoc e3 = badFiles.elements();
169                 while (e3.hasMoreElements()) {
170                     File JavaDoc f = (File JavaDoc) e3.nextElement();
171                     fixFile(f);
172                 }
173             }
174         } else {
175             System.out.println("All files under " + path + " are OK :-)");
176         }
177     }
178
179     private void findFiles(File JavaDoc path) {
180         try {
181             //obtain canonical file (condenses cycles e.g. from links)
182
File JavaDoc canonPath = path.getCanonicalFile();
183             FileCharacterTest.dirStack.push(canonPath.toString());
184         } catch (IOException JavaDoc e) {
185             //trivially handle exception and carry on
186
System.out.println("Canonical problem");
187         }
188         //search directory for file names that satisfy FilenameFilter
189
//select a subset of the directory's filenames
190
File JavaDoc[] temp = path.listFiles(new FilenameFilter JavaDoc() {
191         
192             public boolean accept(File JavaDoc afile, String JavaDoc name) {
193                 //create a new file object for each sub-object
194
File JavaDoc newFile = new File JavaDoc(afile, name);
195
196                 //ignore if file object is unreadable
197
if (!newFile.canRead()) {
198                     //report omission
199
System.out.println(newFile + ": Permission denied");
200                     return false;
201                 }
202
203                 //ignore files in a bin directory. We are only interested in source
204
if (newFile.getPath().indexOf("\\bin\\") != -1) {
205                     return false;
206                 }
207
208                 //ignore class files!!!!
209
if (name.toUpperCase().endsWith(".CLASS")) {
210                     return false;
211                 }
212
213                 //ignore ear files!!!!
214
if (name.toUpperCase().endsWith(".EAR")) {
215                     return false;
216                 }
217
218                 //ignore jar files!!!!
219
if (name.toUpperCase().endsWith(".JAR")) {
220                     return false;
221                 }
222
223                 //ignore zip files!!!!
224
if (name.toUpperCase().endsWith(".ZIP")) {
225                     return false;
226                 }
227
228                 //ignore war files!!!!
229
if (name.toUpperCase().endsWith(".WAR")) {
230                     return false;
231                 }
232
233                 //ignore eclipse files!!!!
234
if (name.toUpperCase().startsWith(".VCM")
235                     || name.toUpperCase().startsWith(".CLASSPATH")) {
236                     return false;
237                 }
238
239                 //ignore if file object is hidden
240
if (newFile.isHidden())
241                     return false;
242                 return !(dirStack.contains(newFile.toString()));
243             }
244         });
245         for (int a = 0; a < temp.length; a++) {
246             if (temp[a].isDirectory()) {
247                 findFiles(temp[a]);
248                 //sub-tree traversed - pop object from stack
249
dirStack.pop();
250             } else {
251                 files.add(temp[a]);
252             }
253         }
254     }
255
256     private void checkFile(File JavaDoc file) {
257         FileReader JavaDoc fr = null;
258         try {
259             fr = new FileReader JavaDoc(file);
260             char[] chunk = new char[500];
261             int ok = fr.read(chunk);
262             while (ok != -1) {
263                 String JavaDoc s = new String JavaDoc(chunk);
264                 if (s.indexOf("\r") != -1) {
265                     badFiles.add(file);
266                     break;
267                 }
268                 ok = fr.read(chunk);
269             }
270             fr.close();
271         } catch (Exception JavaDoc e) {
272             System.out.println("Unable to check file " + file + " : " + e);
273             assertTrue(false);
274         }
275     }
276
277     private void fixFile(File JavaDoc file) {
278         FileReader JavaDoc fr = null;
279         StringBuffer JavaDoc sb = new StringBuffer JavaDoc((int) file.length());
280         try {
281             fr = new FileReader JavaDoc(file);
282             char[] chunk = new char[(int) file.length()];
283             int ok = fr.read(chunk);
284             while (ok != -1) {
285                 String JavaDoc s = new String JavaDoc(chunk);
286                 //s = s.replace('\r', ' ');
287
sb.append(s);
288                 ok = fr.read(chunk);
289             }
290             fr.close();
291
292             // Not amazingly efficient but the for the best end result
293
// iterate through all chars and when we find a ^M, delete it.
294
// The alternative replace methods leave us with a ' ' char
295
// at the end of lines which mucks up properties files!!
296
for (int x = 0; x < sb.length(); x++) {
297                 if (sb.charAt(x) == '\r') {
298                     sb.deleteCharAt(x);
299                     if (x > 0)
300                         x--;
301                 }
302             }
303
304         } catch (Exception JavaDoc e) {
305             System.out.println("Unable to read file " + file + " : " + e);
306             assertTrue(false);
307         }
308         try {
309             FileWriter JavaDoc fw = new FileWriter JavaDoc(file);
310             fw.write(sb.toString().trim());
311             fw.flush();
312             fw.close();
313             System.out.println("Fixed file " + file);
314         } catch (Exception JavaDoc e) {
315             System.out.println("Unable to write file " + file + " : " + e);
316             assertTrue(false);
317         }
318     }
319 }
320
Popular Tags