KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > util > ParsedURLDataTest


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

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 JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 /**
29  * This test validates that the ParsedURL class properly parses and
30  * cascades URLs.
31  *
32  * @author <a HREF="mailto:deweese@apache.org">Thomas DeWeese</a>
33  * @version $Id: ParsedURLDataTest.java,v 1.4 2004/08/18 07:17:15 vhardy Exp $
34  */

35 public class ParsedURLDataTest extends AbstractTest {
36     /**
37      * Error when unable to parse URL
38      * {0} = 'Base URL' or NULL
39      * {1} = Sub URL
40      * {2} = exception stack trace.
41      */

42     public static final String JavaDoc ERROR_CANNOT_PARSE_URL
43         = "ParsedURLTest.error.cannot.parse.url";
44
45     /**
46      * Result didn't match expected result.
47      * {0} = result
48      * {1} = expected result
49      */

50     public static final String JavaDoc ERROR_WRONG_RESULT
51         = "ParsedURLTest.error.wrong.result";
52
53     public static final String JavaDoc ENTRY_KEY_ERROR_DESCRIPTION
54         = "ParsedURLTest.entry.key.error.description";
55
56     protected String JavaDoc base = null;
57     protected String JavaDoc ref = null;
58     /**
59      * Constructor
60      * @param url The url to parse
61      * @param ref The expected result.
62      */

63     public ParsedURLDataTest(String JavaDoc url, String JavaDoc ref ){
64         this.base = url;
65         this.ref = ref;
66     }
67
68     /**
69      * Returns this Test's name
70      */

71     public String JavaDoc getName() {
72         return ref + " -- " + super.getName();
73     }
74
75     /**
76      * This method will only throw exceptions if some aspect
77      * of the test's internal operation fails.
78      */

79     public TestReport runImpl() throws Exception JavaDoc {
80         DefaultTestReport report
81             = new DefaultTestReport(this);
82
83         ParsedURL purl;
84         try {
85             purl = new ParsedURL(base);
86         } catch(Exception JavaDoc e) {
87             StringWriter JavaDoc trace = new StringWriter JavaDoc();
88             e.printStackTrace(new PrintWriter JavaDoc(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 JavaDoc[]{"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 JavaDoc is = purl.openStream();
108             num = is.read(data);
109         } catch (IOException JavaDoc ioe) {
110             ioe.printStackTrace();
111         }
112         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc 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 JavaDoc[]{info, ref }))
137             });
138         report.setPassed(false);
139         return report;
140     }
141 }
142
Popular Tags