001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.util;
020:
021: import org.apache.batik.test.AbstractTest;
022: import org.apache.batik.test.DefaultTestReport;
023: import org.apache.batik.test.TestReport;
024:
025: import java.io.StringWriter;
026: import java.io.PrintWriter;
027: import java.io.InputStream;
028: import java.io.IOException;
029:
030: /**
031: * This test validates that the ParsedURL class properly parses and
032: * cascades URLs.
033: *
034: * @author <a href="mailto:deweese@apache.org">Thomas DeWeese</a>
035: * @version $Id: ParsedURLDataTest.java 475477 2006-11-15 22:44:28Z cam $
036: */
037: public class ParsedURLDataTest extends AbstractTest {
038: /**
039: * Error when unable to parse URL
040: * {0} = 'Base URL' or NULL
041: * {1} = Sub URL
042: * {2} = exception stack trace.
043: */
044: public static final String ERROR_CANNOT_PARSE_URL = "ParsedURLTest.error.cannot.parse.url";
045:
046: /**
047: * Result didn't match expected result.
048: * {0} = result
049: * {1} = expected result
050: */
051: public static final String ERROR_WRONG_RESULT = "ParsedURLTest.error.wrong.result";
052:
053: public static final String ENTRY_KEY_ERROR_DESCRIPTION = "ParsedURLTest.entry.key.error.description";
054:
055: protected String base = null;
056: protected String ref = null;
057:
058: /**
059: * Constructor
060: * @param url The url to parse
061: * @param ref The expected result.
062: */
063: public ParsedURLDataTest(String url, String ref) {
064: this .base = url;
065: this .ref = ref;
066: }
067:
068: /**
069: * Returns this Test's name
070: */
071: public String getName() {
072: return ref + " -- " + super .getName();
073: }
074:
075: /**
076: * This method will only throw exceptions if some aspect
077: * of the test's internal operation fails.
078: */
079: public TestReport runImpl() throws Exception {
080: DefaultTestReport report = new DefaultTestReport(this );
081:
082: ParsedURL purl;
083: try {
084: purl = new ParsedURL(base);
085: } catch (Exception e) {
086: StringWriter trace = new StringWriter();
087: e.printStackTrace(new PrintWriter(trace));
088: report.setErrorCode(ERROR_CANNOT_PARSE_URL);
089: report
090: .setDescription(new TestReport.Entry[] { new TestReport.Entry(
091: TestMessages.formatMessage(
092: ENTRY_KEY_ERROR_DESCRIPTION, null),
093: TestMessages.formatMessage(
094: ERROR_CANNOT_PARSE_URL,
095: new String[] { "null", base,
096: trace.toString() })) });
097: report.setPassed(false);
098: return report;
099: }
100:
101: byte[] data = new byte[5];
102: int num = 0;
103: try {
104: InputStream is = purl.openStream();
105: num = is.read(data);
106: } catch (IOException ioe) {
107: ioe.printStackTrace();
108: }
109: StringBuffer sb = new StringBuffer();
110: for (int i = 0; i < num; i++) {
111: int val = ((int) data[i]) & 0xFF;
112: if (val < 16) {
113: sb.append("0");
114: }
115: sb.append(Integer.toHexString(val) + " ");
116: }
117:
118: String info = ("CT: " + purl.getContentType() + " CE: "
119: + purl.getContentEncoding() + " DATA: " + sb + "URL: " + purl);
120:
121: if (ref.equals(info)) {
122: report.setPassed(true);
123: return report;
124: }
125:
126: report.setErrorCode(ERROR_WRONG_RESULT);
127: report
128: .setDescription(new TestReport.Entry[] { new TestReport.Entry(
129: TestMessages.formatMessage(
130: ENTRY_KEY_ERROR_DESCRIPTION, null),
131: TestMessages.formatMessage(ERROR_WRONG_RESULT,
132: new String[] { info, ref })) });
133: report.setPassed(false);
134: return report;
135: }
136: }
|