1 16 17 package org.apache.batik.ext.awt.geom; 18 19 import java.awt.Rectangle ; 20 import java.io.BufferedReader ; 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.FileOutputStream ; 24 import java.io.InputStream ; 25 import java.io.InputStreamReader ; 26 import java.io.PrintStream ; 27 import java.io.PrintWriter ; 28 import java.io.StringWriter ; 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 import java.util.StringTokenizer ; 35 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.Base64Test; 40 41 42 43 49 public class RectListManagerTest extends AbstractTest { 50 51 54 public static final String REFERENCE_DIR 55 = "test-references/org/apache/batik/ext/awt/geom/"; 56 57 public static final String VARIATION_DIR 58 = "variation/"; 59 60 public static final String CANDIDATE_DIR 61 = "candidate/"; 62 63 64 69 public static final String ERROR_READING_RECTS 70 = "RectListManagerTest.error.reading.rects"; 71 72 77 public static final String ERROR_CANNOT_READ_REF_URL 78 = "RectListManagerTest.error.cannot.read.ref.url"; 79 80 84 public static final String ERROR_WRONG_RESULT 85 = "RectListManagerTest.error.wrong.result"; 86 87 91 public static final String ERROR_NO_REFERENCE 92 = "RectListManagerTest.error.no.reference"; 93 94 95 public static final String ENTRY_KEY_ERROR_DESCRIPTION 96 = "RectListManagerTest.entry.key.error.description"; 97 98 protected URL rects = null; 99 protected URL ref = null; 100 protected URL var = null; 101 protected File can = null; 102 103 108 public RectListManagerTest(String rects, String ref) { 109 this.rects = resolveURL(REFERENCE_DIR+rects); 110 this.ref = resolveURL(REFERENCE_DIR+ref); 111 this.var = resolveURL(REFERENCE_DIR+VARIATION_DIR+ref); 112 this.can = new File (REFERENCE_DIR+CANDIDATE_DIR+ref); 113 } 114 115 123 protected URL resolveURL(String url){ 124 File f = (new File (url)).getAbsoluteFile(); 126 if(f.getParentFile().exists()){ 127 try{ 128 return f.toURL(); 129 }catch(MalformedURLException e){ 130 throw new IllegalArgumentException (); 131 } 132 } 133 134 try{ 136 return new URL (url); 137 }catch(MalformedURLException e){ 138 throw new IllegalArgumentException (url); 139 } 140 } 141 142 145 public String getName() { 146 return rects.toString(); 147 } 148 149 150 static final String RECT_PREF ="rect"; 151 static final String RLM_PREF ="rectlistmanger"; 152 static final String MERGE_PREF ="merge"; 153 static final String ADD_PREF ="add"; 154 static final String SUBTRACT_PREF ="subtract"; 155 static final String CONTAINS_ALL_PREF ="containsall"; 156 static final String REMOVE_ALL_PREF ="removeall"; 157 static final String RETAIN_ALL_PREF ="retainall"; 158 static final String PRINT_PREF ="print"; 159 160 164 public TestReport runImpl() throws Exception { 165 DefaultTestReport report = new DefaultTestReport(this); 166 167 int lineNo=0; 168 try { 169 BufferedReader reader; 170 reader = new BufferedReader 171 (new InputStreamReader (rects.openStream())); 172 173 if (can.exists()) 175 can.delete(); 176 177 FileOutputStream fos = new FileOutputStream (can); 178 PrintStream ps = new PrintStream (fos); 179 180 Map rlms = new HashMap (); 181 RectListManager currRLM = null; 182 String currID = null; 183 String line; 184 while ((line = reader.readLine()) != null) { 185 lineNo++; 186 line = line.toLowerCase(); 187 StringTokenizer st = new StringTokenizer (line); 188 189 if (!st.hasMoreTokens()) continue; 191 192 String pref = st.nextToken(); 194 195 if (pref.startsWith("#")) continue; 197 198 if (RECT_PREF.equals(pref)) { 199 if (st.countTokens() != 4) continue; 200 if (currRLM == null) continue; 201 202 int x = Integer.parseInt(st.nextToken()); 203 int y = Integer.parseInt(st.nextToken()); 204 int w = Integer.parseInt(st.nextToken()); 205 int h = Integer.parseInt(st.nextToken()); 206 currRLM.add(new Rectangle (x, y, w, h)); 207 } 208 else if (RLM_PREF.equals(pref)) { 209 String id = st.nextToken(); 210 Object o = rlms.get(id); 211 if (o == null) { 212 o = new RectListManager(); 213 rlms.put(id, o); 214 } 215 currRLM = (RectListManager)o; 216 currID = id; 217 } 218 else if (MERGE_PREF.equals(pref)) { 219 if (currRLM == null) continue; 220 int overhead = Integer.parseInt(st.nextToken()); 221 int lineOverhead = Integer.parseInt(st.nextToken()); 222 currRLM.mergeRects(overhead, lineOverhead); 223 } 224 else if (ADD_PREF.equals(pref)) { 225 if (currRLM == null) continue; 226 String id = st.nextToken(); 227 Object o = rlms.get(id); 228 if (o == null) continue; 229 currRLM.add((RectListManager)o); 230 } 231 else if (SUBTRACT_PREF.equals(pref)) { 232 if (currRLM == null) continue; 233 String id = st.nextToken(); 234 Object o = rlms.get(id); 235 if (o == null) continue; 236 int overhead = Integer.parseInt(st.nextToken()); 237 int lineOverhead = Integer.parseInt(st.nextToken()); 238 currRLM.subtract((RectListManager)o, 239 overhead, lineOverhead); 240 } 241 else if (CONTAINS_ALL_PREF.equals(pref)) { 242 if (currRLM == null) continue; 243 String id = st.nextToken(); 244 Object o = rlms.get(id); 245 if (o == null) continue; 246 RectListManager rlm = (RectListManager)o; 247 ps.println("ID: " + currID + " Sz: " + currRLM.size()); 248 249 if (currRLM.containsAll(rlm)) { 250 ps.println(" Contains all: " + id + 251 " Sz: " + rlm.size()); 252 } else { 253 ps.println(" Does not contain all: " + id + 254 " Sz: " + rlm.size()); 255 } 256 ps.println(); 257 } 258 else if (REMOVE_ALL_PREF.equals(pref)) { 259 if (currRLM == null) continue; 260 String id = st.nextToken(); 261 Object o = rlms.get(id); 262 if (o == null) continue; 263 currRLM.removeAll((RectListManager)o); 264 } 265 else if (RETAIN_ALL_PREF.equals(pref)) { 266 if (currRLM == null) continue; 267 String id = st.nextToken(); 268 Object o = rlms.get(id); 269 if (o == null) continue; 270 currRLM.retainAll((RectListManager)o); 271 } 272 else if (PRINT_PREF.equals(pref)) { 273 if (currRLM == null) continue; 274 275 Iterator i = currRLM.iterator(); 276 ps.println("ID: " + currID + " Sz: " + currRLM.size()); 277 while (i.hasNext()) { 278 ps.println(" " + i.next()); 279 } 280 ps.println(); 281 } 282 } 283 284 ps.close(); 285 fos.close(); 286 } catch(Exception e) { 287 StringWriter trace = new StringWriter (); 288 e.printStackTrace(new PrintWriter (trace)); 289 report.setErrorCode(ERROR_READING_RECTS); 290 report.setDescription(new TestReport.Entry[] { 291 new TestReport.Entry 292 (Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null), 293 Messages.formatMessage 294 (ERROR_READING_RECTS, 295 new String []{""+lineNo, rects.toString(), 296 trace.toString()})) 297 }); 298 report.setPassed(false); 299 return report; 300 } 301 302 InputStream refIS = null; 303 try { 304 refIS = var.openStream(); 305 } catch(Exception e) { 306 try { 307 refIS = ref.openStream(); 308 } catch(Exception ex) { 309 StringWriter trace = new StringWriter (); 310 e.printStackTrace(new PrintWriter (trace)); 311 report.setErrorCode(ERROR_CANNOT_READ_REF_URL); 312 report.setDescription 313 (new TestReport.Entry[] { 314 new TestReport.Entry 315 (Messages.formatMessage 316 (ENTRY_KEY_ERROR_DESCRIPTION, null), 317 Messages.formatMessage 318 (ERROR_CANNOT_READ_REF_URL, 319 new String []{ref.toString(), trace.toString()})) 320 }); 321 report.setPassed(false); 322 } 323 } 324 325 int mismatch = -2; 326 if (refIS != null) { 327 InputStream canIS = new FileInputStream (can); 328 Checker check = new Checker(canIS, refIS); 329 check.start(); 330 mismatch = check.getMismatch(); 331 332 } 333 334 if (mismatch == -1) { 335 report.setPassed(true); 336 can.delete(); 337 return report; 338 } 339 340 if (mismatch == -2) { 341 report.setErrorCode(ERROR_NO_REFERENCE); 342 report.setDescription(new TestReport.Entry[] { 343 new TestReport.Entry 344 (Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null), 345 Messages.formatMessage(ERROR_NO_REFERENCE, 346 new String []{ref.toString()})) 347 }); 348 } else { 349 report.setErrorCode(ERROR_WRONG_RESULT); 350 report.setDescription(new TestReport.Entry[] { 351 new TestReport.Entry 352 (Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null), 353 Messages.formatMessage(ERROR_WRONG_RESULT, 354 new String []{""+mismatch})) 355 }); 356 } 357 report.setPassed(false); 358 359 return report; 360 } 361 362 public static class Checker extends Thread { 363 int mismatch = -2; 364 InputStream is1, is2; 365 public Checker(InputStream is1, InputStream is2) { 366 this.is1 = is1; 367 this.is2 = is2; 368 } 369 public int getMismatch() { 370 while (true) { 371 try { 372 this.join(); 373 break; 374 } catch (InterruptedException ie) { } 375 } 376 377 return mismatch; 378 } 379 public void run() { 380 mismatch = Base64Test.compareStreams (is1, is2, true); 381 } 382 } 383 } 384 385 | Popular Tags |