1 57 58 package util; 59 60 import java.io.File ; 61 import java.io.FileNotFoundException ; 62 import java.io.FileReader ; 63 import java.io.FileWriter ; 64 import java.io.FilenameFilter ; 65 import java.io.IOException ; 66 import java.util.Enumeration ; 67 import java.util.Stack ; 68 import java.util.Vector ; 69 70 import junit.framework.Test; 71 import junit.framework.TestCase; 72 import junit.framework.TestSuite; 73 import junit.textui.TestRunner; 74 75 86 public class FileCharacterTest extends TestCase { 87 88 static Stack dirStack = new Stack (); 89 static Vector files = new Vector (); 90 static Vector badFiles = new Vector (); 91 92 public FileCharacterTest(String name) { 93 super(name); 94 } 95 96 public static void main(String [] 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 path = null; 106 int index; 107 try { 108 String temp = TestUtilities.getWsdlPath(""); 109 path = new File (temp); 110 111 if (!path.exists()) 113 throw new FileNotFoundException (); 114 115 if (!path.isDirectory()) 117 if (path.isFile()) 119 path = path.getParentFile(); 120 else { 122 System.out.println("\npath '" + path + "' is not a file or directory!"); 123 System.exit(1); 124 } 125 126 if (!path.canRead()) { 128 System.out.println("FCT: " + path + ": Permission denied"); 129 System.exit(1); 130 } 131 } 132 catch (FileNotFoundException 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 e = files.elements(); 144 while (e.hasMoreElements()) { 145 File f = (File ) e.nextElement(); 146 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 e2 = badFiles.elements(); 161 while (e2.hasMoreElements()) { 162 File f = (File ) 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 e3 = badFiles.elements(); 169 while (e3.hasMoreElements()) { 170 File f = (File ) 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 path) { 180 try { 181 File canonPath = path.getCanonicalFile(); 183 FileCharacterTest.dirStack.push(canonPath.toString()); 184 } catch (IOException e) { 185 System.out.println("Canonical problem"); 187 } 188 File [] temp = path.listFiles(new FilenameFilter () { 191 192 public boolean accept(File afile, String name) { 193 File newFile = new File (afile, name); 195 196 if (!newFile.canRead()) { 198 System.out.println(newFile + ": Permission denied"); 200 return false; 201 } 202 203 if (newFile.getPath().indexOf("\\bin\\") != -1) { 205 return false; 206 } 207 208 if (name.toUpperCase().endsWith(".CLASS")) { 210 return false; 211 } 212 213 if (name.toUpperCase().endsWith(".EAR")) { 215 return false; 216 } 217 218 if (name.toUpperCase().endsWith(".JAR")) { 220 return false; 221 } 222 223 if (name.toUpperCase().endsWith(".ZIP")) { 225 return false; 226 } 227 228 if (name.toUpperCase().endsWith(".WAR")) { 230 return false; 231 } 232 233 if (name.toUpperCase().startsWith(".VCM") 235 || name.toUpperCase().startsWith(".CLASSPATH")) { 236 return false; 237 } 238 239 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 dirStack.pop(); 250 } else { 251 files.add(temp[a]); 252 } 253 } 254 } 255 256 private void checkFile(File file) { 257 FileReader fr = null; 258 try { 259 fr = new FileReader (file); 260 char[] chunk = new char[500]; 261 int ok = fr.read(chunk); 262 while (ok != -1) { 263 String s = new String (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 e) { 272 System.out.println("Unable to check file " + file + " : " + e); 273 assertTrue(false); 274 } 275 } 276 277 private void fixFile(File file) { 278 FileReader fr = null; 279 StringBuffer sb = new StringBuffer ((int) file.length()); 280 try { 281 fr = new FileReader (file); 282 char[] chunk = new char[(int) file.length()]; 283 int ok = fr.read(chunk); 284 while (ok != -1) { 285 String s = new String (chunk); 286 sb.append(s); 288 ok = fr.read(chunk); 289 } 290 fr.close(); 291 292 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 e) { 305 System.out.println("Unable to read file " + file + " : " + 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 + " : " + e); 316 assertTrue(false); 317 } 318 } 319 } 320 | Popular Tags |