1 24 25 package org.objectweb.cjdbc.scenario.standalone.xml; 26 27 import java.io.BufferedReader ; 28 import java.io.File ; 29 import java.io.FileInputStream ; 30 import java.io.FileReader ; 31 import java.io.FilenameFilter ; 32 import java.io.IOException ; 33 import java.io.InputStreamReader ; 34 import java.util.ArrayList ; 35 import java.util.StringTokenizer ; 36 37 import org.objectweb.cjdbc.common.xml.XmlValidator; 38 import org.objectweb.cjdbc.scenario.templates.NoTemplate; 39 40 45 public class XmlParserTest extends NoTemplate 46 { 47 48 public static final String XML_FILE = "xmls.txt"; 49 50 public static String xmlFiles; 51 52 private int count = 0; 53 private int failed = 0; 54 55 58 public void setUp() 59 { 60 xmlFiles = getTextPath(XML_FILE); 61 } 62 63 private String getRelativePath(String pathFromTxt) 64 { 65 return getUserDir() + File.separator + pathFromTxt; 66 } 67 68 73 public void testXmlDocuments() throws IOException 74 { 75 File file = new File (xmlFiles); 76 BufferedReader d = new BufferedReader (new InputStreamReader ( 77 new FileInputStream (file))); 78 String line; 79 boolean allResultsOk = true; 80 while ((line = d.readLine()) != null) 81 { 82 if (line.startsWith("###") || line.trim().equals("")) 83 continue; 84 else 85 { 86 StringTokenizer st = new StringTokenizer (line, ";"); 87 String xmlFile = getRelativePath(st.nextToken()); 88 String dtdFile = getRelativePath(st.nextToken()); 89 boolean valid = new Boolean (st.nextToken()).booleanValue(); 90 91 File xfile = new File (xmlFile); 92 boolean value = false; 93 if (xfile.isDirectory()) 94 { 95 value = analyseDirectoryWithDtd(dtdFile, xfile); 96 } 97 else 98 { 99 value = analyseXmlAndDtd(dtdFile, xmlFile); 100 } 101 System.out.print("File '" + xmlFile + "': "); 102 System.out.println((valid == value) ? "ok" : "ko"); 103 allResultsOk = allResultsOk && (valid == value); 104 } 105 } 106 107 System.out.println("SUMMARY: " + count + " files tested, " + failed 108 + " failed."); 109 assertTrue(failed + " tests failed", allResultsOk); 110 } 111 112 private boolean analyseDirectoryWithDtd(String pathToDtd, File dir) 113 { 114 System.out.println("Analysing directory:" + dir.getAbsolutePath() + ":"); 115 String [] toAnalyse = dir.list(new FilenameFilter () 116 { 117 120 public boolean accept(File dir, String name) 121 { 122 if (name.endsWith(".xml")) 123 return true; 124 else 125 return false; 126 } 127 }); 128 129 boolean allResultsOk = true; 130 boolean test = true; 131 132 for (int i = 0; i < toAnalyse.length; i++) 133 { 134 try 135 { 136 test = analyseXmlAndDtd(pathToDtd, dir.getAbsolutePath() 137 + File.separator + toAnalyse[i]); 138 } 139 catch (Exception e) 140 { 141 test = false; 142 } 143 allResultsOk = allResultsOk && test; 144 145 } 146 147 return allResultsOk; 148 } 149 150 private boolean analyseXmlAndDtd(String pathToDtd, String pathToXml) 151 throws IOException 152 { 153 154 File dtd = new File (pathToDtd); 155 File file = new File (pathToXml); 156 System.out.print("Analysing file[" + (count++) + "]\t:"); 157 FileReader reader = new FileReader (file); 158 159 XmlValidator validator = new XmlValidator(pathToDtd, reader); 160 if (!validator.isDtdValid()) 161 System.out.print("[FAILED: dtd is not valid]"); 162 else if (!validator.isXmlValid()) 163 System.out.print("[FAILED: xml is not valid]"); 164 else if (validator.isXmlValid()) 165 System.out.print("[OK]"); 166 167 System.out.println("\t:" + file.getName() + " with dtd:" + dtd.getName()); 168 169 if (validator.getLastException() != null) 170 { 171 ArrayList errors = validator.getExceptions(); 172 for (int i = 0; i < errors.size(); i++) 173 System.out.println("\t(parsing error):" 174 + ((Exception ) errors.get(i)).getMessage()); 175 } 176 177 if (!validator.isValid()) 178 failed++; 179 180 return validator.isValid(); 181 } 182 } 183 | Popular Tags |