1 18 package org.apache.batik.svggen; 19 20 import java.io.File ; 21 import java.net.URL ; 22 23 import org.apache.batik.test.Test; 24 import org.apache.batik.test.DefaultTestSuite; 25 import org.apache.batik.test.svg.SVGRenderingAccuracyTest; 26 import org.apache.batik.test.util.ImageCompareTest; 27 28 40 public class SVGGeneratorTests extends DefaultTestSuite { 41 public static final String GENERATOR_REFERENCE_BASE 42 = "test-references/org/apache/batik/svggen/"; 43 44 public static final String RENDERING_DIR 45 = "rendering"; 46 47 public static final String ACCEPTED_VARIATION_DIR 48 = "accepted-variation"; 49 50 public static final String CANDIDATE_VARIATION_DIR 51 = "candidate-variation"; 52 53 public static final String CANDIDATE_REF_DIR 54 = "candidate-ref"; 55 56 public static final String RENDERING_CANDIDATE_REF_DIR 57 = "candidate-reference"; 58 59 public static final String PNG_EXTENSION 60 = ".png"; 61 62 public static final String SVG_EXTENSION 63 = ".svg"; 64 65 public static final String PLAIN_GENERATION_PREFIX = ""; 66 67 public static final String CUSTOM_CONTEXT_GENERATION_PREFIX = "Context"; 68 69 72 public SVGGeneratorTests(){ 73 } 74 75 80 public void setId(String id){ 81 super.setId(id); 82 String clName = getPackageName() + "." + id; 83 Class cl = null; 84 85 try{ 86 cl = Class.forName(clName); 87 }catch(ClassNotFoundException e){ 88 throw new IllegalArgumentException (clName); 89 } 90 91 Object o = null; 92 93 try { 94 o = cl.newInstance(); 95 }catch(Exception e){ 96 throw new IllegalArgumentException (clName); 97 } 98 99 if(!(o instanceof Painter)){ 100 throw new IllegalArgumentException (clName); 101 } 102 103 Painter painter = (Painter)o; 104 105 addTest(makeSVGAccuracyTest(painter, id)); 106 addTest(makeGeneratorContext(painter, id)); 107 addTest(makeSVGRenderingAccuracyTest(painter, id, PLAIN_GENERATION_PREFIX)); 108 addTest(makeSVGRenderingAccuracyTest(painter, id, CUSTOM_CONTEXT_GENERATION_PREFIX)); 109 addTest(makeImageCompareTest(painter, id, PLAIN_GENERATION_PREFIX, 110 CUSTOM_CONTEXT_GENERATION_PREFIX)); 111 } 112 113 116 public String getName(){ 117 return "SVGGeneratorTest - " + getId(); 118 } 119 120 protected String getPackageName(){ 121 return "org.apache.batik.svggen"; 122 } 123 124 private Test makeImageCompareTest(Painter painter, 125 String id, 126 String prefixA, 127 String prefixB){ 128 String cl = getNonQualifiedClassName(painter); 129 String clA = prefixA + cl; 130 String clB = prefixB + cl; 131 String testReferenceA = GENERATOR_REFERENCE_BASE + RENDERING_DIR + "/" + clA + PNG_EXTENSION; 132 String testReferenceB = GENERATOR_REFERENCE_BASE + RENDERING_DIR + "/" + clB + PNG_EXTENSION; 133 ImageCompareTest t = new ImageCompareTest(testReferenceA, testReferenceB); 134 t.setName(id + "-RenderingComparison"); 135 t.setId(id + ".renderingComparison"); 136 return t; 137 } 138 139 private Test makeSVGRenderingAccuracyTest(Painter painter, String id, String prefix){ 140 String cl = prefix + getNonQualifiedClassName(painter); 141 String testSource = GENERATOR_REFERENCE_BASE + cl + SVG_EXTENSION; 142 String testReference = GENERATOR_REFERENCE_BASE + RENDERING_DIR + "/" + cl + PNG_EXTENSION; 143 String variationURL = GENERATOR_REFERENCE_BASE + RENDERING_DIR + "/" + ACCEPTED_VARIATION_DIR + "/" + cl + PNG_EXTENSION; 144 String saveVariation = GENERATOR_REFERENCE_BASE + RENDERING_DIR + "/" + CANDIDATE_VARIATION_DIR + "/" + cl + PNG_EXTENSION; 145 String candidateReference = GENERATOR_REFERENCE_BASE + RENDERING_DIR + "/" + RENDERING_CANDIDATE_REF_DIR + "/" + cl + PNG_EXTENSION; 146 147 SVGRenderingAccuracyTest test = new SVGRenderingAccuracyTest(testSource, testReference); 148 test.setVariationURL(variationURL); 149 test.setSaveVariation(new File (saveVariation)); 150 test.setCandidateReference(new File (candidateReference)); 151 152 test.setName(id + "-" + prefix + "RenderingCheck"); 153 test.setId(id + "." + prefix + "renderingCheck"); 154 return test; 155 } 156 157 private Test makeGeneratorContext(Painter painter, String id){ 158 String cl = CUSTOM_CONTEXT_GENERATION_PREFIX + getNonQualifiedClassName(painter); 159 160 GeneratorContext test 161 = new GeneratorContext(painter, makeURL(painter, CUSTOM_CONTEXT_GENERATION_PREFIX)); 162 163 test.setSaveSVG(new File (GENERATOR_REFERENCE_BASE + CANDIDATE_REF_DIR + "/" + cl + SVG_EXTENSION)); 164 test.setName(id + "-ConfiguredContextGeneration"); 165 test.setId(id + ".configuredContextGeneration"); 166 return test; 167 } 168 169 private Test makeSVGAccuracyTest(Painter painter, String id){ 170 String cl = PLAIN_GENERATION_PREFIX + getNonQualifiedClassName(painter); 171 172 SVGAccuracyTest test 173 = new SVGAccuracyTest(painter, makeURL(painter, PLAIN_GENERATION_PREFIX)); 174 175 test.setSaveSVG(new File (GENERATOR_REFERENCE_BASE + CANDIDATE_REF_DIR + "/" + cl + SVG_EXTENSION)); 176 test.setName(id + "-DefaultContextGeneration"); 177 test.setId(id + ".defaultContextGeneration"); 178 return test; 179 } 180 181 private String getNonQualifiedClassName(Painter painter){ 182 String cl = painter.getClass().getName(); 183 int n = cl.lastIndexOf("."); 184 return cl.substring(n+1); 185 } 186 187 private URL makeURL(Painter painter, String prefix){ 188 String urlString = "file:" + GENERATOR_REFERENCE_BASE 189 + prefix + getNonQualifiedClassName(painter) + SVG_EXTENSION; 190 URL url = null; 191 try{ 192 url = new URL (urlString); 193 }catch(Exception e){ 194 throw new Error (); } 196 197 return url; 198 } 199 } 200 201 | Popular Tags |