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.PipedOutputStream ; 25 import java.io.PipedInputStream ; 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 import java.io.IOException ; 29 import java.io.StringWriter ; 30 import java.io.PrintWriter ; 31 32 import java.net.URL ; 33 34 40 public class Base64Test extends AbstractTest { 41 45 public static final String ERROR_BAD_ACTION_STRING 46 = "Base64Test.error.bad.action.string"; 47 48 53 public static final String ERROR_CANNOT_READ_IN_URL 54 = "Base64Test.error.cannot.read.in.url"; 55 56 61 public static final String ERROR_CANNOT_READ_REF_URL 62 = "Base64Test.error.cannot.read.ref.url"; 63 64 68 public static final String ERROR_WRONG_RESULT 69 = "Base64Test.error.wrong.result"; 70 71 public static final String ENTRY_KEY_ERROR_DESCRIPTION 72 = "Base64Test.entry.key.error.description"; 73 74 protected String action = null; 75 protected URL in = null; 76 protected URL ref = null; 77 78 87 public Base64Test(String action, URL in, URL ref) { 88 this.action = action; 89 this.in = in; 90 this.ref = ref; 91 } 92 93 97 public Base64Test(URL in) { 98 this.action = "ROUND"; 99 this.in = in; 100 } 101 102 105 public String getName() { 106 return action + " -- " + in + " -- " + super.getName(); 107 } 108 109 113 public TestReport runImpl() throws Exception { 114 DefaultTestReport report 115 = new DefaultTestReport(this); 116 117 InputStream inIS; 118 119 try { 120 inIS = in.openStream(); 121 } catch(Exception e) { 122 StringWriter trace = new StringWriter (); 123 e.printStackTrace(new PrintWriter (trace)); 124 report.setErrorCode(ERROR_CANNOT_READ_IN_URL); 125 report.setDescription(new TestReport.Entry[] { 126 new TestReport.Entry 127 (TestMessages.formatMessage 128 (ENTRY_KEY_ERROR_DESCRIPTION, null), 129 TestMessages.formatMessage 130 (ERROR_CANNOT_READ_IN_URL, 131 new String []{in.toString(), trace.toString()})) 132 }); 133 report.setPassed(false); 134 return report; 135 } 136 137 if (action.equals("ROUND")) 138 this.ref = in; 139 else if (!action.equals("ENCODE") && 140 !action.equals("DECODE")) { 141 report.setErrorCode(ERROR_BAD_ACTION_STRING); 142 report.setDescription(new TestReport.Entry[] { 143 new TestReport.Entry 144 (TestMessages.formatMessage 145 (ENTRY_KEY_ERROR_DESCRIPTION, null), 146 TestMessages.formatMessage(ERROR_BAD_ACTION_STRING, 147 new String []{action})) 148 }); 149 report.setPassed(false); 150 return report; 151 } 152 153 InputStream refIS; 154 try { 155 refIS = ref.openStream(); 156 } catch(Exception e) { 157 StringWriter trace = new StringWriter (); 158 e.printStackTrace(new PrintWriter (trace)); 159 report.setErrorCode(ERROR_CANNOT_READ_REF_URL); 160 report.setDescription(new TestReport.Entry[] { 161 new TestReport.Entry 162 (TestMessages.formatMessage 163 (ENTRY_KEY_ERROR_DESCRIPTION, null), 164 TestMessages.formatMessage 165 (ERROR_CANNOT_READ_REF_URL, 166 new String []{ref.toString(), trace.toString()})) 167 }); 168 report.setPassed(false); 169 return report; 170 } 171 172 if (action.equals("ENCODE") || 173 action.equals("ROUND")) { 174 PipedOutputStream pos = new PipedOutputStream (); 176 OutputStream os = new Base64EncoderStream(pos); 177 178 Thread t = new StreamCopier(inIS, os); 180 181 inIS = new PipedInputStream (pos); 183 t.start(); 184 } 185 186 if (action.equals("DECODE")|| 187 action.equals("ROUND")) { 188 inIS = new Base64DecodeStream(inIS); 189 } 190 191 192 int mismatch = compareStreams(inIS, refIS, action.equals("ENCODE")); 193 194 if (mismatch == -1) { 195 report.setPassed(true); 196 return report; 197 } 198 199 report.setErrorCode(ERROR_WRONG_RESULT); 200 report.setDescription(new TestReport.Entry[] { 201 new TestReport.Entry 202 (TestMessages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null), 203 TestMessages.formatMessage(ERROR_WRONG_RESULT, 204 new String []{""+mismatch})) 205 }); 206 report.setPassed(false); 207 return report; 208 } 209 210 214 public static int compareStreams(InputStream is1, InputStream is2, 215 boolean skipws) { 216 byte [] data1 = new byte[100]; 217 byte [] data2 = new byte[100]; 218 int off1=0; 219 int off2=0; 220 int idx=0; 221 222 try { 223 while(true) { 224 int len1 = is1.read(data1, off1, data1.length-off1); 225 int len2 = is2.read(data2, off2, data2.length-off2); 226 227 if (off1 != 0) { 228 if (len1 == -1) 229 len1 = off1; 230 else 231 len1 += off1; 232 } 233 234 if (off2 != 0) { 235 if (len2 == -1) 236 len2 = off2; 237 else 238 len2 += off2; 239 } 240 241 if (len1 == -1) { 242 if (len2 == -1) 243 break; 245 if (!skipws) 247 return idx; 248 249 for (int i2=0; i2<len2; i2++) 251 if ((data2[i2] != '\n') && 252 (data2[i2] != '\r') && 253 (data2[i2] != ' ')) 254 return idx+i2; 255 off1 = off2 = 0; 256 continue; 257 } 258 259 if (len2 == -1) { 260 if (!skipws) 262 return idx; 263 264 for (int i1=0; i1<len1; i1++) 266 if ((data1[i1] != '\n') && 267 (data1[i1] != '\r') && 268 (data1[i1] != ' ')) 269 return idx+i1; 270 off1 = off2 = 0; 271 continue; 272 } 273 274 int i1=0; 275 int i2=0; 276 while((i1<len1) && (i2<len2)) { 277 if (skipws) { 278 if ((data1[i1] == '\n') || 279 (data1[i1] == '\r') || 280 (data1[i1] == ' ')) { 281 i1++; 282 continue; 283 } 284 if ((data2[i2] == '\n') || 285 (data2[i2] == '\r') || 286 (data2[i2] == ' ')) { 287 i2++; 288 continue; 289 } 290 } 291 if (data1[i1] != data2[i2]) 292 return idx+i2; 293 294 i1++; 295 i2++; 296 } 297 298 if (i1 != len1) 299 System.arraycopy(data1, i1, data1, 0, len1-i1); 300 if (i2 != len2) 301 System.arraycopy(data2, i2, data2, 0, len2-i2); 302 off1 = len1-i1; 303 off2 = len2-i2; 304 idx+=i2; 305 } 306 } catch(IOException ioe) { 307 ioe.printStackTrace(); 308 return idx; 309 } 310 311 return -1; 312 } 313 314 315 static class StreamCopier extends Thread { 316 InputStream src; 317 OutputStream dst; 318 319 public StreamCopier(InputStream src, 320 OutputStream dst) { 321 this.src = src; 322 this.dst = dst; 323 } 324 325 public void run() { 326 try { 327 byte [] data = new byte[1000]; 328 while(true) { 329 int len = src.read(data, 0, data.length); 330 if (len == -1) break; 331 332 dst.write(data, 0, len); 333 } 334 } catch (IOException ioe) { 335 } 337 try { 338 dst.close(); 339 } catch (IOException ioe) { 340 } 342 } 343 } 344 } 345 | Popular Tags |