001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2002 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "WSIF" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 2001, 2002, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package util;
059:
060: import java.io.File;
061: import java.io.FileNotFoundException;
062: import java.io.FileReader;
063: import java.io.FileWriter;
064: import java.io.FilenameFilter;
065: import java.io.IOException;
066: import java.util.Enumeration;
067: import java.util.Stack;
068: import java.util.Vector;
069:
070: import junit.framework.Test;
071: import junit.framework.TestCase;
072: import junit.framework.TestSuite;
073: import junit.textui.TestRunner;
074:
075: /**
076: * A JUnit test to check our files for the dreaded ^M character. The test displays a list of
077: * all the WSIF files it finds that contain the character.
078: *
079: * This test is configured by the "fix" component setting in the wsif.test.properties file.
080: * If fix is set to on, this test will attempt to remove any ^M chars it finds in the WSIF files.
081: * It is recommended that fix=on if only set if you are sure that the ^M characters are present
082: * in the server copy of the file.
083: *
084: * @author Owen Burroughs <owenb@apache.org>
085: */
086: public class FileCharacterTest extends TestCase {
087:
088: static Stack dirStack = new Stack();
089: static Vector files = new Vector();
090: static Vector badFiles = new Vector();
091:
092: public FileCharacterTest(String name) {
093: super (name);
094: }
095:
096: public static void main(String[] args) {
097: TestRunner.run(suite());
098: }
099:
100: public static Test suite() {
101: return new TestSuite(FileCharacterTest.class);
102: }
103:
104: public void testFiles() {
105: File path = null;
106: int index;
107: try {
108: String temp = TestUtilities.getWsdlPath("");
109: path = new File(temp);
110:
111: //check that path exists
112: if (!path.exists())
113: throw new FileNotFoundException();
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
123: + "' is not a file or directory!");
124: System.exit(1);
125: }
126:
127: //if path is unreadable, report error and exit
128: if (!path.canRead()) {
129: System.out.println("FCT: " + path
130: + ": Permission denied");
131: System.exit(1);
132: }
133: }
134: //catch non-existent file error
135: catch (FileNotFoundException e) {
136: System.out.println("\npath '" + path + "' does not exist!");
137: System.exit(1);
138: }
139:
140: findFiles(path);
141: while (!dirStack.empty()) {
142: dirStack.pop();
143: }
144: int total = files.size();
145: Enumeration e = files.elements();
146: while (e.hasMoreElements()) {
147: File f = (File) e.nextElement();
148: //System.out.println(f);
149: checkFile(f);
150: }
151:
152: int bad = badFiles.size();
153: if (bad > 0) {
154: System.out.println("\nOut of the " + total
155: + " WSIF files under " + path + ",\nthe following "
156: + bad + " files contain ^M characters:\n");
157: Enumeration e2 = badFiles.elements();
158: while (e2.hasMoreElements()) {
159: File f = (File) e2.nextElement();
160: System.out.println(f);
161: }
162:
163: if (TestUtilities.areWeTesting("fix")) {
164: System.out.println("\nNow to fix them :-)\n");
165: Enumeration e3 = badFiles.elements();
166: while (e3.hasMoreElements()) {
167: File f = (File) e3.nextElement();
168: fixFile(f);
169: }
170: }
171: } else {
172: System.out.println("All files under " + path
173: + " are OK :-)");
174: }
175: }
176:
177: private void findFiles(File path) {
178: try {
179: //obtain canonical file (condenses cycles e.g. from links)
180: File canonPath = path.getCanonicalFile();
181: FileCharacterTest.dirStack.push(canonPath.toString());
182: } catch (IOException e) {
183: //trivially handle exception and carry on
184: System.out.println("Canonical problem");
185: }
186: //search directory for file names that satisfy FilenameFilter
187: //select a subset of the directory's filenames
188: File[] temp = path.listFiles(new FilenameFilter() {
189:
190: public boolean accept(File afile, String name) {
191: //create a new file object for each sub-object
192: File newFile = new File(afile, name);
193:
194: //ignore if file object is unreadable
195: if (!newFile.canRead()) {
196: //report omission
197: System.out.println(newFile + ": Permission denied");
198: return false;
199: }
200:
201: //ignore files in a bin directory. We are only interested in source
202: if (newFile.getPath().indexOf("\\bin\\") != -1) {
203: return false;
204: }
205:
206: //ignore class files!!!!
207: if (name.toUpperCase().endsWith(".CLASS")) {
208: return false;
209: }
210:
211: //ignore ear files!!!!
212: if (name.toUpperCase().endsWith(".EAR")) {
213: return false;
214: }
215:
216: //ignore jar files!!!!
217: if (name.toUpperCase().endsWith(".JAR")) {
218: return false;
219: }
220:
221: //ignore zip files!!!!
222: if (name.toUpperCase().endsWith(".ZIP")) {
223: return false;
224: }
225:
226: //ignore war files!!!!
227: if (name.toUpperCase().endsWith(".WAR")) {
228: return false;
229: }
230:
231: //ignore eclipse files!!!!
232: if (name.toUpperCase().startsWith(".VCM")
233: || name.toUpperCase().startsWith(".CLASSPATH")) {
234: return false;
235: }
236:
237: //ignore if file object is hidden
238: if (newFile.isHidden())
239: return false;
240: return !(dirStack.contains(newFile.toString()));
241: }
242: });
243: for (int a = 0; a < temp.length; a++) {
244: if (temp[a].isDirectory()) {
245: findFiles(temp[a]);
246: //sub-tree traversed - pop object from stack
247: dirStack.pop();
248: } else {
249: files.add(temp[a]);
250: }
251: }
252: }
253:
254: private void checkFile(File file) {
255: FileReader fr = null;
256: try {
257: fr = new FileReader(file);
258: char[] chunk = new char[500];
259: int ok = fr.read(chunk);
260: while (ok != -1) {
261: String s = new String(chunk);
262: if (s.indexOf("\r") != -1) {
263: badFiles.add(file);
264: break;
265: }
266: ok = fr.read(chunk);
267: }
268: fr.close();
269: } catch (Exception e) {
270: System.out.println("Unable to check file " + file + " : "
271: + e);
272: assertTrue(false);
273: }
274: }
275:
276: private void fixFile(File file) {
277: FileReader fr = null;
278: StringBuffer sb = new StringBuffer((int) file.length());
279: try {
280: fr = new FileReader(file);
281: char[] chunk = new char[(int) file.length()];
282: int ok = fr.read(chunk);
283: while (ok != -1) {
284: String s = new String(chunk);
285: //s = s.replace('\r', ' ');
286: sb.append(s);
287: ok = fr.read(chunk);
288: }
289: fr.close();
290:
291: // Not amazingly efficient but the for the best end result
292: // iterate through all chars and when we find a ^M, delete it.
293: // The alternative replace methods leave us with a ' ' char
294: // at the end of lines which mucks up properties files!!
295: for (int x = 0; x < sb.length(); x++) {
296: if (sb.charAt(x) == '\r') {
297: sb.deleteCharAt(x);
298: if (x > 0)
299: x--;
300: }
301: }
302:
303: } catch (Exception e) {
304: System.out.println("Unable to read file " + file + " : "
305: + e);
306: assertTrue(false);
307: }
308: try {
309: FileWriter fw = new FileWriter(file);
310: fw.write(sb.toString().trim());
311: fw.flush();
312: fw.close();
313: System.out.println("Fixed file " + file);
314: } catch (Exception e) {
315: System.out.println("Unable to write file " + file + " : "
316: + e);
317: assertTrue(false);
318: }
319: }
320: }
|