KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > geom > RectListManagerTest


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.batik.ext.awt.geom;
18
19 import java.awt.Rectangle JavaDoc;
20 import java.io.BufferedReader JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.io.PrintStream JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.io.StringWriter JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.StringTokenizer JavaDoc;
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 /**
44  * This test validates that the text selection API's work properly.
45  *
46  * @author <a HREF="mailto:deweese@apache.org">Thomas DeWeese</a>
47  * @version $Id: RectListManagerTest.java,v 1.5 2005/04/01 02:28:16 deweese Exp $
48  */

49 public class RectListManagerTest extends AbstractTest {
50
51     /**
52      * Directory for reference files
53      */

54     public static final String JavaDoc REFERENCE_DIR
55         = "test-references/org/apache/batik/ext/awt/geom/";
56
57     public static final String JavaDoc VARIATION_DIR
58         = "variation/";
59
60     public static final String JavaDoc CANDIDATE_DIR
61         = "candidate/";
62
63
64     /**
65      * Error when unable to load requested rects file
66      * {0} = file
67      * {1} = exception
68      */

69     public static final String JavaDoc ERROR_READING_RECTS
70         = "RectListManagerTest.error.reading.rects";
71
72     /**
73      * Error when unable to read/open ref URL
74      * {0} = URL
75      * {1} = exception stack trace.
76      */

77     public static final String JavaDoc ERROR_CANNOT_READ_REF_URL
78         = "RectListManagerTest.error.cannot.read.ref.url";
79
80     /**
81      * Result didn't match reference result.
82      * {0} = first byte of mismatch
83      */

84     public static final String JavaDoc ERROR_WRONG_RESULT
85         = "RectListManagerTest.error.wrong.result";
86
87     /**
88      * No Reference or Variation file to compaire with.
89      * {0} = reference url
90      */

91     public static final String JavaDoc ERROR_NO_REFERENCE
92         = "RectListManagerTest.error.no.reference";
93
94
95     public static final String JavaDoc ENTRY_KEY_ERROR_DESCRIPTION
96         = "RectListManagerTest.entry.key.error.description";
97
98     protected URL JavaDoc rects = null;
99     protected URL JavaDoc ref = null;
100     protected URL JavaDoc var = null;
101     protected File JavaDoc can = null;
102
103     /**
104      * Constructor. ref is ignored if action == ROUND.
105      * @param rects The rects file to load
106      * @param ref The reference file.
107      */

108     public RectListManagerTest(String JavaDoc rects, String JavaDoc 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 JavaDoc(REFERENCE_DIR+CANDIDATE_DIR+ref);
113     }
114
115     /**
116      * Resolves the input string as follows.
117      * + First, the string is interpreted as a file description.
118      * If the file exists, then the file name is turned into
119      * a URL.
120      * + Otherwise, the string is supposed to be a URL. If it
121      * is an invalid URL, an IllegalArgumentException is thrown.
122      */

123     protected URL JavaDoc resolveURL(String JavaDoc url){
124         // Is url a file?
125
File JavaDoc f = (new File JavaDoc(url)).getAbsoluteFile();
126         if(f.getParentFile().exists()){
127             try{
128                 return f.toURL();
129             }catch(MalformedURLException JavaDoc e){
130                 throw new IllegalArgumentException JavaDoc();
131             }
132         }
133         
134         // url is not a file. It must be a regular URL...
135
try{
136             return new URL JavaDoc(url);
137         }catch(MalformedURLException JavaDoc e){
138             throw new IllegalArgumentException JavaDoc(url);
139         }
140     }
141
142     /**
143      * Returns this Test's name
144      */

145     public String JavaDoc getName() {
146         return rects.toString();
147     }
148
149
150     static final String JavaDoc RECT_PREF ="rect";
151     static final String JavaDoc RLM_PREF ="rectlistmanger";
152     static final String JavaDoc MERGE_PREF ="merge";
153     static final String JavaDoc ADD_PREF ="add";
154     static final String JavaDoc SUBTRACT_PREF ="subtract";
155     static final String JavaDoc CONTAINS_ALL_PREF ="containsall";
156     static final String JavaDoc REMOVE_ALL_PREF ="removeall";
157     static final String JavaDoc RETAIN_ALL_PREF ="retainall";
158     static final String JavaDoc PRINT_PREF ="print";
159
160     /**
161      * This method will only throw exceptions if some aspect
162      * of the test's internal operation fails.
163      */

164     public TestReport runImpl() throws Exception JavaDoc {
165         DefaultTestReport report = new DefaultTestReport(this);
166
167         int lineNo=0;
168         try {
169             BufferedReader JavaDoc reader;
170             reader = new BufferedReader JavaDoc
171                 (new InputStreamReader JavaDoc(rects.openStream()));
172
173             // Now write a canidate reference/variation file...
174
if (can.exists())
175                 can.delete();
176
177             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(can);
178             PrintStream JavaDoc ps = new PrintStream JavaDoc(fos);
179
180             Map JavaDoc rlms = new HashMap JavaDoc();
181             RectListManager currRLM = null;
182             String JavaDoc currID = null;
183             String JavaDoc line;
184             while ((line = reader.readLine()) != null) {
185                 lineNo++;
186                 line = line.toLowerCase();
187                 StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(line);
188
189                 // Check blank line...
190
if (!st.hasMoreTokens()) continue;
191
192                 // Get first token
193
String JavaDoc pref = st.nextToken();
194
195                 // Check for comment.
196
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 JavaDoc(x, y, w, h));
207                 }
208                 else if (RLM_PREF.equals(pref)) {
209                     String JavaDoc id = st.nextToken();
210                     Object JavaDoc 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 JavaDoc id = st.nextToken();
227                     Object JavaDoc 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 JavaDoc id = st.nextToken();
234                     Object JavaDoc 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 JavaDoc id = st.nextToken();
244                     Object JavaDoc 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 JavaDoc id = st.nextToken();
261                     Object JavaDoc 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 JavaDoc id = st.nextToken();
268                     Object JavaDoc 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 JavaDoc 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 JavaDoc e) {
287             StringWriter JavaDoc trace = new StringWriter JavaDoc();
288             e.printStackTrace(new PrintWriter JavaDoc(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 JavaDoc[]{""+lineNo, rects.toString(),
296                                    trace.toString()}))
297                     });
298             report.setPassed(false);
299             return report;
300         }
301
302         InputStream JavaDoc refIS = null;
303         try {
304             refIS = var.openStream();
305         } catch(Exception JavaDoc e) {
306             try {
307                 refIS = ref.openStream();
308             } catch(Exception JavaDoc ex) {
309                 StringWriter JavaDoc trace = new StringWriter JavaDoc();
310                 e.printStackTrace(new PrintWriter JavaDoc(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 JavaDoc[]{ref.toString(), trace.toString()}))
320                             });
321                 report.setPassed(false);
322             }
323         }
324
325         int mismatch = -2;
326         if (refIS != null) {
327             InputStream JavaDoc canIS = new FileInputStream JavaDoc(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 JavaDoc[]{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 JavaDoc[]{""+mismatch}))
355                     });
356         }
357         report.setPassed(false);
358
359         return report;
360     }
361
362     public static class Checker extends Thread JavaDoc {
363         int mismatch = -2;
364         InputStream JavaDoc is1, is2;
365         public Checker(InputStream JavaDoc is1, InputStream JavaDoc 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 JavaDoc 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