1 18 package org.apache.batik.util; 19 20 import org.apache.batik.test.AbstractTest; 21 import org.apache.batik.test.DefaultTestReport; 22 import org.apache.batik.test.TestReport; 23 24 import java.io.StringWriter ; 25 import java.io.PrintWriter ; 26 import java.io.InputStream ; 27 import java.io.IOException ; 28 35 public class ParsedURLDataTest extends AbstractTest { 36 42 public static final String ERROR_CANNOT_PARSE_URL 43 = "ParsedURLTest.error.cannot.parse.url"; 44 45 50 public static final String ERROR_WRONG_RESULT 51 = "ParsedURLTest.error.wrong.result"; 52 53 public static final String ENTRY_KEY_ERROR_DESCRIPTION 54 = "ParsedURLTest.entry.key.error.description"; 55 56 protected String base = null; 57 protected String ref = null; 58 63 public ParsedURLDataTest(String url, String ref ){ 64 this.base = url; 65 this.ref = ref; 66 } 67 68 71 public String getName() { 72 return ref + " -- " + super.getName(); 73 } 74 75 79 public TestReport runImpl() throws Exception { 80 DefaultTestReport report 81 = new DefaultTestReport(this); 82 83 ParsedURL purl; 84 try { 85 purl = new ParsedURL(base); 86 } catch(Exception e) { 87 StringWriter trace = new StringWriter (); 88 e.printStackTrace(new PrintWriter (trace)); 89 report.setErrorCode(ERROR_CANNOT_PARSE_URL); 90 report.setDescription(new TestReport.Entry[] { 91 new TestReport.Entry 92 (TestMessages.formatMessage 93 (ENTRY_KEY_ERROR_DESCRIPTION, null), 94 TestMessages.formatMessage 95 (ERROR_CANNOT_PARSE_URL, 96 new String []{"null", 97 base, 98 trace.toString()})) 99 }); 100 report.setPassed(false); 101 return report; 102 } 103 104 byte[] data = new byte[5]; 105 int num = 0; 106 try { 107 InputStream is = purl.openStream(); 108 num = is.read(data); 109 } catch (IOException ioe) { 110 ioe.printStackTrace(); 111 } 112 StringBuffer sb = new StringBuffer (); 113 for (int i=0; i<num; i++) { 114 int val = ((int)data[i])&0xFF; 115 if (val < 16) { 116 sb.append("0"); 117 } 118 sb.append(Integer.toHexString(val) + " "); 119 } 120 121 String info = ( "CT: " + purl.getContentType() + 122 " CE: " + purl.getContentEncoding() + 123 " DATA: " + sb + 124 "URL: " + purl); 125 126 if (ref.equals(info)) { 127 report.setPassed(true); 128 return report; 129 } 130 131 report.setErrorCode(ERROR_WRONG_RESULT); 132 report.setDescription(new TestReport.Entry[] { 133 new TestReport.Entry 134 (TestMessages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null), 135 TestMessages.formatMessage 136 (ERROR_WRONG_RESULT, new String []{info, ref })) 137 }); 138 report.setPassed(false); 139 return report; 140 } 141 } 142 | Popular Tags |