1 18 package org.apache.batik.svggen; 19 20 import org.apache.batik.svggen.SVGGeneratorContext.GraphicContextDefaults; 21 22 import java.awt.Dimension ; 23 import java.awt.Font ; 24 25 import java.net.URL ; 26 27 import java.io.BufferedInputStream ; 28 import java.io.BufferedReader ; 29 import java.io.InputStreamReader ; 30 import java.io.File ; 31 import java.io.FileOutputStream ; 32 import java.io.InputStream ; 33 import java.io.IOException ; 34 import java.io.ByteArrayInputStream ; 35 import java.io.ByteArrayOutputStream ; 36 import java.io.OutputStreamWriter ; 37 import java.io.StringWriter ; 38 import java.io.PrintWriter ; 39 40 import org.apache.batik.util.SVGConstants; 41 import org.apache.batik.test.AbstractTest; 42 import org.apache.batik.test.DefaultTestReport; 43 import org.apache.batik.test.TestReport; 44 45 import org.apache.batik.svggen.SVGGraphics2D; 46 import org.apache.batik.dom.GenericDOMImplementation; 47 import org.w3c.dom.Document ; 48 import org.w3c.dom.DOMImplementation ; 49 50 59 public class SVGAccuracyTest extends AbstractTest 60 implements SVGConstants{ 61 69 public static final String ERROR_CANNOT_GENERATE_SVG 70 = "SVGAccuracyTest.error.cannot.generate.svg"; 71 72 77 public static final String ERROR_CANNOT_OPEN_REFERENCE_SVG_FILE 78 = "SVGAccuracyTest.error.cannot.open.reference.svg.file"; 79 80 86 public static final String ERROR_ERROR_WHILE_COMPARING_FILES 87 = "SVGAccuracyTest.error.while.comparing.files"; 88 89 92 public static final String ERROR_GENERATED_SVG_INACCURATE 93 = "SVGAccuracyTest.error.generated.svg.inaccurate"; 94 95 public static final String ENTRY_KEY_ERROR_DESCRIPTION 96 = "SVGAccuracyTest.entry.key.error.description"; 97 98 public static final String ENTRY_KEY_LINE_NUMBER 99 = "SVGAccuracyTest.entry.key.line.number"; 100 101 public static final String ENTRY_KEY_COLUMN_NUMBER 102 = "SVGAccuracyTest.entry.key.column.number"; 103 104 public static final String ENTRY_KEY_COLUMN_EXPECTED_VALUE 105 = "SVGAccuracyTest.entry.key.column.expected.value"; 106 107 public static final String ENTRY_KEY_COLUMN_FOUND_VALUE 108 = "SVGAccuracyTest.entry.key.column.found.value"; 109 110 public static final String ENTRY_KEY_REFERENCE_LINE 111 = "SVGAccuracyTest.entry.key.reference.line"; 112 113 public static final String ENTRY_KEY_NEW_LINE 114 = "SVGAccuracyTest.entry.key.new.line"; 115 116 119 public static final Dimension CANVAS_SIZE 120 = new Dimension (300, 400); 121 122 126 private Painter painter; 127 128 131 private URL refURL; 132 133 136 private File saveSVG; 137 138 146 public SVGAccuracyTest(Painter painter, 147 URL refURL){ 148 this.painter = painter; 149 this.refURL = refURL; 150 } 151 152 public File getSaveSVG(){ 153 return saveSVG; 154 } 155 156 public void setSaveSVG(File saveSVG){ 157 this.saveSVG = saveSVG; 158 } 159 160 164 public TestReport runImpl() throws Exception { 165 DefaultTestReport report 166 = new DefaultTestReport(this); 167 168 SVGGraphics2D g2d = buildSVGGraphics2D(); 169 g2d.setSVGCanvasSize(CANVAS_SIZE); 170 171 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 175 OutputStreamWriter osw = new OutputStreamWriter (bos, "UTF-8"); 176 try{ 177 painter.paint(g2d); 178 configureSVGGraphics2D(g2d); 179 g2d.stream(osw); 180 osw.flush(); 181 bos.flush(); 182 bos.close(); 183 }catch(Exception e){ 184 StringWriter trace = new StringWriter (); 185 e.printStackTrace(new PrintWriter (trace)); 186 report.setErrorCode(ERROR_CANNOT_GENERATE_SVG); 187 report.setDescription(new TestReport.Entry[]{ 188 new TestReport.Entry(Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null), 189 Messages.formatMessage(ERROR_CANNOT_GENERATE_SVG, 190 new String []{painter == null? "null" : painter.getClass().getName(), 191 e.getClass().getName(), 192 e.getMessage(), 193 trace.toString() })) }); 194 report.setPassed(false); 195 return report; 196 } 197 198 InputStream refStream = null; 202 try { 203 refStream = 204 new BufferedInputStream (refURL.openStream()); 205 }catch(Exception e){ 206 report.setErrorCode(ERROR_CANNOT_OPEN_REFERENCE_SVG_FILE); 207 report.setDescription( new TestReport.Entry[]{ 208 new TestReport.Entry(Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null), 209 Messages.formatMessage(ERROR_CANNOT_OPEN_REFERENCE_SVG_FILE, 210 new Object []{refURL != null? refURL.toExternalForm() : "null", 211 e.getMessage()})) }); 212 report.setPassed(false); 213 save(bos.toByteArray()); 214 return report; 215 } 216 217 InputStream newStream = new ByteArrayInputStream (bos.toByteArray()); 218 219 boolean accurate = true; 220 String refLine = null; 221 String newLine = null; 222 int ln = 1; 223 224 try{ 225 BufferedReader refReader = new BufferedReader (new InputStreamReader (refStream)); 227 BufferedReader newReader = new BufferedReader (new InputStreamReader (newStream)); 228 while((refLine = refReader.readLine()) != null){ 229 newLine = newReader.readLine(); 230 if(newLine == null || !refLine.equals(newLine)){ 231 accurate = false; 232 break; 233 } 234 ln++; 235 } 236 237 if(accurate){ 238 newLine = newReader.readLine(); 240 if(newLine != null){ 241 accurate = false; 242 } 243 } 244 245 } catch(IOException e) { 246 report.setErrorCode(ERROR_ERROR_WHILE_COMPARING_FILES); 247 report.setDescription(new TestReport.Entry[]{ 248 new TestReport.Entry(Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null), 249 Messages.formatMessage(ERROR_ERROR_WHILE_COMPARING_FILES, 250 new Object []{refURL.toExternalForm(), 251 e.getMessage()}))}); 252 report.setPassed(false); 253 save(bos.toByteArray()); 254 return report; 255 } 256 257 if(!accurate){ 258 save(bos.toByteArray()); 259 int cn = computeColumnNumber(refLine, newLine); 260 String expectedChar = "eol"; 261 if(cn >= 0 && refLine != null && refLine.length() > cn){ 262 expectedChar = (new Character (refLine.charAt(cn))).toString(); 263 } 264 String foundChar = "null"; 265 if(cn >=0 && newLine != null && newLine.length() > cn){ 266 foundChar = (new Character (newLine.charAt(cn))).toString(); 267 } 268 269 if(expectedChar.equals(" ")){ 270 expectedChar = "' '"; 271 } 272 if(foundChar.equals(" ")){ 273 foundChar = "' '"; 274 } 275 276 report.setErrorCode(ERROR_GENERATED_SVG_INACCURATE); 277 report.addDescriptionEntry(Messages.formatMessage(ENTRY_KEY_LINE_NUMBER,null), new Integer (ln)); 278 report.addDescriptionEntry(Messages.formatMessage(ENTRY_KEY_COLUMN_NUMBER,null), new Integer (cn)); 279 report.addDescriptionEntry(Messages.formatMessage(ENTRY_KEY_COLUMN_EXPECTED_VALUE,null), expectedChar); 280 report.addDescriptionEntry(Messages.formatMessage(ENTRY_KEY_COLUMN_FOUND_VALUE,null), foundChar); 281 report.addDescriptionEntry(Messages.formatMessage(ENTRY_KEY_REFERENCE_LINE,null), refLine); 282 report.addDescriptionEntry(Messages.formatMessage(ENTRY_KEY_NEW_LINE,null), newLine); 283 report.setPassed(false); 284 } 285 else{ 286 report.setPassed(true); 287 } 288 289 return report; 290 } 291 292 public int computeColumnNumber(String aStr, String bStr){ 293 if(aStr == null || bStr == null){ 294 return -1; 295 } 296 297 int n = aStr.length(); 298 int i = -1; 299 for(i=0; i<n; i++){ 300 char a = aStr.charAt(i); 301 if(i < bStr.length()){ 302 char b = bStr.charAt(i); 303 if(a != b){ 304 break; 305 } 306 } 307 else { 308 break; 309 } 310 } 311 312 return i; 313 } 314 315 319 protected void save(byte[] data) throws IOException { 320 if(saveSVG == null){ 321 return; 322 } 323 324 FileOutputStream os = new FileOutputStream (saveSVG); 325 os.write(data); 326 os.close(); 327 } 328 329 332 protected boolean byteCompare(InputStream refStream, 333 InputStream newStream) 334 throws IOException { 335 int b = 0; 336 int nb = 0; 337 do { 338 if (b == nb || nb != 13) 339 b = refStream.read(); 340 nb = newStream.read(); 341 } while (b != -1 && nb != -1 && (b == nb || nb == 13)); 342 refStream.close(); 343 newStream.close(); 344 return (b == nb || nb == 13); 345 } 346 347 351 protected SVGGraphics2D buildSVGGraphics2D() { 352 DOMImplementation impl = GenericDOMImplementation.getDOMImplementation(); 354 String namespaceURI = SVGConstants.SVG_NAMESPACE_URI; 355 Document domFactory = impl.createDocument(namespaceURI, SVG_SVG_TAG, null); 356 SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(domFactory); 357 GraphicContextDefaults defaults 358 = new GraphicContextDefaults(); 359 defaults.font = new Font ("Arial", Font.PLAIN, 12); 360 ctx.setGraphicContextDefaults(defaults); 361 ctx.setPrecision(12); 362 return new SVGGraphics2D(ctx, false); 363 } 364 365 366 370 protected void configureSVGGraphics2D(SVGGraphics2D g2d) {} 371 } 372 | Popular Tags |