KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Copyright 2001-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
27 /**
28  * This test validates that the ParsedURL class properly parses and
29  * cascades URLs.
30  *
31  * @author <a HREF="mailto:deweese@apache.org">Thomas DeWeese</a>
32  * @version $Id: ParsedURLTest.java,v 1.5 2004/08/18 07:17:15 vhardy Exp $
33  */

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

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

49     public static final String JavaDoc ERROR_WRONG_RESULT
50         = "ParsedURLTest.error.wrong.result";
51
52     public static final String JavaDoc ENTRY_KEY_ERROR_DESCRIPTION
53         = "ParsedURLTest.entry.key.error.description";
54
55     protected String JavaDoc base = null;
56     protected String JavaDoc sub = 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 ParsedURLTest(String JavaDoc url, String JavaDoc ref ){
64         this.base = url;
65         this.ref = ref;
66     }
67
68     /**
69      * Constructor
70      * @param base The base url to parse
71      * @param sub The sub url (relative to base).
72      * @param ref The expected result.
73      */

74     public ParsedURLTest(String JavaDoc base, String JavaDoc sub, String JavaDoc ref){
75         this.base = base;
76         this.sub = sub;
77         this.ref = ref;
78     }
79
80     /**
81      * Returns this Test's name
82      */

83     public String JavaDoc getName() {
84         return ref + " -- " + super.getName();
85     }
86
87     /**
88      * This method will only throw exceptions if some aspect
89      * of the test's internal operation fails.
90      */

91     public TestReport runImpl() throws Exception JavaDoc {
92         DefaultTestReport report
93             = new DefaultTestReport(this);
94
95         ParsedURL url;
96         try {
97             url = new ParsedURL(base);
98             if (sub != null) {
99                 url = new ParsedURL(url, sub);
100             }
101         } catch(Exception JavaDoc e) {
102             StringWriter JavaDoc trace = new StringWriter JavaDoc();
103             e.printStackTrace(new PrintWriter JavaDoc(trace));
104             report.setErrorCode(ERROR_CANNOT_PARSE_URL);
105             report.setDescription(new TestReport.Entry[] {
106                 new TestReport.Entry
107                     (TestMessages.formatMessage
108                      (ENTRY_KEY_ERROR_DESCRIPTION, null),
109                      TestMessages.formatMessage
110                      (ERROR_CANNOT_PARSE_URL,
111                       new String JavaDoc[]{base,
112                                    (sub == null) ? "null" : sub,
113                                    trace.toString()}))
114                     });
115             report.setPassed(false);
116             return report;
117         }
118
119         if (ref.equals(url.toString())) {
120             report.setPassed(true);
121             return report;
122         }
123
124         report.setErrorCode(ERROR_WRONG_RESULT);
125         report.setDescription(new TestReport.Entry[] {
126           new TestReport.Entry
127             (TestMessages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null),
128              TestMessages.formatMessage
129              (ERROR_WRONG_RESULT, new String JavaDoc[]{url.toString(), ref }))
130             });
131         report.setPassed(false);
132         return report;
133     }
134 }
135
Popular Tags