1 18 package org.apache.batik.test.svg; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 25 import org.w3c.dom.Document ; 26 import org.w3c.dom.Element ; 27 import org.w3c.dom.NodeList ; 28 29 import org.apache.batik.bridge.BaseScriptingEnvironment; 30 import org.apache.batik.bridge.BridgeContext; 31 import org.apache.batik.bridge.BridgeException; 32 import org.apache.batik.bridge.GVTBuilder; 33 import org.apache.batik.bridge.UserAgent; 34 import org.apache.batik.bridge.UserAgentAdapter; 35 import org.apache.batik.dom.svg.SAXSVGDocumentFactory; 36 import org.apache.batik.test.AbstractTest; 37 import org.apache.batik.test.DefaultTestReport; 38 import org.apache.batik.test.TestReport; 39 import org.apache.batik.util.XMLResourceDescriptor; 40 41 72 public class SelfContainedSVGOnLoadTest extends AbstractTest { 73 78 public static final String ERROR_CANNOT_LOAD_SVG_DOCUMENT 79 = "SelfContainedSVGOnLoadTest.error.cannot.load.svg.document"; 80 81 85 public static final String ERROR_WHILE_PROCESSING_SVG_DOCUMENT 86 = "SelfContainedSVGOnLoadTest.error.while.processing.svg.document"; 87 88 92 public static final String ERROR_UNEXPECTED_NUMBER_OF_TEST_RESULT_ELEMENTS 93 = "SelfContainedSVGOnLoadTest.error.unexpected.number.of.test.result.elements"; 94 95 98 public static final String ERROR_UNEXPECTED_RESULT_VALUE 99 = "SelfContainedSVGOnLoadTest.error.unexpected.result.value"; 100 101 105 public static final String ERROR_MISSING_OR_EMPTY_ERROR_CODE_ON_FAILED_TEST 106 = "SelfContainedSVGOnLoadTest.error.missing.or.empty.error.code.on.failed.test"; 107 108 111 public static final String ENTRY_KEY_ERROR_DESCRIPTION 112 = "SelfContainedSVGOnLoadTest.entry.key.error.description"; 113 114 118 public static final String ENTRY_KEY_NUMBER_OF_TEST_RESULT_ELEMENTS 119 = "SelfContainedSVGOnLoadTest.entry.key.number.of.test.result.elements"; 120 121 125 public static final String ENTRY_KEY_RESULT_VALUE 126 = "SelfContainedSVGOnLoadTest.entry.key.result.value"; 127 128 131 public static final String testNS = "http://xml.apache.org/batik/test"; 132 133 136 public static final String TAG_TEST_RESULT = "testResult"; 137 public static final String TAG_ERROR_DESCRIPTION_ENTRY = "errorDescriptionEntry"; 138 public static final String ATTRIBUTE_RESULT = "result"; 139 public static final String ATTRIBUTE_KEY = "id"; 140 public static final String ATTRIBUTE_VALUE = "value"; 141 public static final String TEST_RESULT_PASSED = "passed"; 142 public static final String TEST_RESULT_FAILED = "failed"; 143 144 147 protected String svgURL; 148 149 152 public SelfContainedSVGOnLoadTest(String svgURL){ 153 this.svgURL = resolveURL(svgURL); 154 } 155 156 159 protected SelfContainedSVGOnLoadTest(){ 160 } 161 162 170 protected String resolveURL(String url){ 171 File f = (new File (url)).getAbsoluteFile(); 173 if(f.getParentFile().exists()){ 174 try{ 175 return f.toURL().toString(); 176 }catch(MalformedURLException e){ 177 throw new IllegalArgumentException (); 178 } 179 } 180 181 try{ 183 return (new URL (url)).toString(); 184 }catch(MalformedURLException e){ 185 throw new IllegalArgumentException (url); 186 } 187 } 188 189 190 202 public TestReport runImpl() throws Exception { 203 DefaultTestReport report 204 = new DefaultTestReport(this); 205 206 String parserClassName = XMLResourceDescriptor.getXMLParserClassName(); 212 SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parserClassName); 213 Document doc = null; 214 215 try { 216 doc = f.createDocument(svgURL); 217 } catch(IOException e){ 218 report.setErrorCode(ERROR_CANNOT_LOAD_SVG_DOCUMENT); 219 report.addDescriptionEntry(ENTRY_KEY_ERROR_DESCRIPTION, 220 e.getMessage()); 221 report.setPassed(false); 222 return report; 223 } catch(Exception e){ 224 report.setErrorCode(ERROR_CANNOT_LOAD_SVG_DOCUMENT); 225 report.addDescriptionEntry(ENTRY_KEY_ERROR_DESCRIPTION, 226 e.getMessage()); 227 report.setPassed(false); 228 return report; 229 } 230 231 UserAgent userAgent = buildUserAgent(); 238 GVTBuilder builder = new GVTBuilder(); 239 BridgeContext ctx = new BridgeContext(userAgent); 240 ctx.setDynamic(true); 241 242 try { 243 builder.build(ctx, doc); 244 BaseScriptingEnvironment scriptEnvironment 245 = new BaseScriptingEnvironment(ctx); 246 scriptEnvironment.loadScripts(); 247 scriptEnvironment.dispatchSVGLoadEvent(); 248 } catch (BridgeException e){ 249 e.printStackTrace(); 250 report.setErrorCode(ERROR_WHILE_PROCESSING_SVG_DOCUMENT); 251 report.addDescriptionEntry(ENTRY_KEY_ERROR_DESCRIPTION, 252 e.getMessage()); 253 report.setPassed(false); 254 return report; 255 } catch(Exception e){ 256 e.printStackTrace(); 257 report.setErrorCode(ERROR_WHILE_PROCESSING_SVG_DOCUMENT); 258 report.addDescriptionEntry(ENTRY_KEY_ERROR_DESCRIPTION, 259 e.getMessage()); 260 report.setPassed(false); 261 return report; 262 } 263 264 NodeList testResultList = doc.getElementsByTagNameNS(testNS, 270 TAG_TEST_RESULT); 271 272 if(testResultList.getLength() != 1){ 274 report.setErrorCode(ERROR_UNEXPECTED_NUMBER_OF_TEST_RESULT_ELEMENTS); 275 report.addDescriptionEntry(ENTRY_KEY_NUMBER_OF_TEST_RESULT_ELEMENTS, 276 "" + testResultList.getLength()); 277 report.setPassed(false); 278 return report; 279 } 280 281 Element testResult = (Element )testResultList.item(0); 282 283 String result = testResult.getAttributeNS(null, ATTRIBUTE_RESULT); 285 boolean passed = true; 286 if(TEST_RESULT_PASSED.equals(result)){ 287 } else if (TEST_RESULT_FAILED.equals(result)){ 289 passed = false; 290 } else { 291 report.setErrorCode(ERROR_UNEXPECTED_RESULT_VALUE); 292 report.addDescriptionEntry(ENTRY_KEY_RESULT_VALUE, result); 293 report.setPassed(false); 294 return report; 295 } 296 297 if( !passed ){ 299 String errorCode = testResult.getAttributeNS(null, "errorCode"); 300 if("".equals(errorCode)){ 301 report.setErrorCode(ERROR_MISSING_OR_EMPTY_ERROR_CODE_ON_FAILED_TEST); 302 report.setPassed(false); 303 return report; 304 } 305 306 report.setErrorCode(errorCode); 308 309 NodeList desc = testResult.getElementsByTagNameNS(testNS, 311 TAG_ERROR_DESCRIPTION_ENTRY); 312 int nDesc = desc.getLength(); 313 for (int i=0; i<nDesc; i++){ 314 Element entry = (Element )desc.item(i); 315 String key = entry.getAttributeNS(null, ATTRIBUTE_KEY); 316 String value = entry.getAttributeNS(null, ATTRIBUTE_VALUE); 317 report.addDescriptionEntry(key, value); 318 } 319 report.setPassed(false); 320 return report; 321 } 322 323 return report; 324 } 325 326 329 protected UserAgent buildUserAgent(){ 330 return new UserAgentAdapter(); 331 } 332 333 } 334 | Popular Tags |