001: /*
002: * @(#)TestResultDescription.java 1.5 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package gunit.framework;
028:
029: import java.io.*;
030: import java.util.*;
031: import java.awt.*;
032: import java.net.*;
033:
034: /**
035: * <code>TestResultDescription</code> describes the test result in text
036: * and/or image formats. The testcase developer needs to provide the
037: * following for testcases that needs user verification.
038: * <ul>
039: * <li>create a file with the same name as the TestCase java file with
040: * the <b>.java</b> extension replaced with <b>.xml</b> in the same
041: * same directory as the java source file</li>
042: * <li>Inside the xml file, specify the text and image description for
043: * each of the testcase method that needs user verification as
044: * mentioned in the section <b>Format</b> below</li>
045: * </ul>
046: * <h4>Format</h4>
047: * <pre>
048: * <methodName>
049: * <test>
050: * <b>test description line 1</b>
051: * <b>test description line 2</b>
052: * ...
053: * <b>test description line n</b>
054: * </test>
055: * <image>
056: * <b>URL of the imagefile</b>
057: * </image>
058: * </methodName>
059: * </pre>
060: */
061: public class TestResultDescription {
062: // Map<Class,Map<String,TestResultDescription>>
063: static Map descriptionMap = new HashMap();
064: static final TestResultDescription defaultDescription = new TestResultDescription();
065:
066: static TestResultDescription getInstance(Class c, String methodName) {
067: Map class_desc_map = (Map) descriptionMap.get(c);
068: if (class_desc_map == null) {
069: String file = c.getName();
070: int index = file.lastIndexOf('.');
071: if (index > 0) {
072: // there is a package name, extract just the class name
073: file = file.substring(index + 1);
074: }
075: file = file + ".xml";
076: InputStream stream = c.getResourceAsStream(file);
077: class_desc_map = new HashMap(); // create empty one
078: if (stream != null)
079: XMLParser.parse(stream, class_desc_map);
080: descriptionMap.put(c, class_desc_map);
081: }
082:
083: TestResultDescription desc = (TestResultDescription) class_desc_map
084: .get(methodName);
085: if (desc != null)
086: return desc;
087: return defaultDescription;
088: }
089:
090: private String text;
091: private String imageURL;
092:
093: private TestResultDescription() {
094: this (null, null);
095: }
096:
097: private TestResultDescription(String text, String imageURL) {
098: this .text = text;
099: this .imageURL = imageURL;
100: }
101:
102: public String getText() {
103: return this .text;
104: }
105:
106: public String getImageURL() {
107: return this .imageURL;
108: }
109:
110: public Image getImage() {
111: Image ret = null;
112: if (this .imageURL == null)
113: return null;
114: try {
115: URL url = new URL(this .imageURL);
116: ret = Toolkit.getDefaultToolkit().createImage(url);
117: BaseTestCase.trackImage(ret);
118: } catch (Exception ex) {
119: }
120: return ret;
121: }
122:
123: static class XMLParser {
124: private static final String TEXT_START_TAG = "<text>";
125: private static final String IMAGE_START_TAG = "<image>";
126: private static final String TEXT_END_TAG = "</text>";
127: private static final String IMAGE_END_TAG = "</image>";
128:
129: static void parse(InputStream stream, Map map) {
130: BufferedReader reader = null;
131: String line = null;
132: String method_start_tag = null;
133: boolean in_text_tag = false;
134: boolean in_image_tag = false;
135: String image_content = null;
136: StringBuffer text_content = new StringBuffer("");
137: try {
138: reader = new BufferedReader(new InputStreamReader(
139: stream));
140: while ((line = reader.readLine()) != null) {
141: line = line.trim(); // trim white spaces
142: if (line.startsWith("</")) { // check if end tag
143: if (TEXT_END_TAG.equals(line) && in_text_tag) {
144: in_text_tag = false;
145: } else if (IMAGE_END_TAG.equals(line)
146: && in_image_tag) {
147: in_image_tag = false;
148: } else if (method_start_tag != null
149: && line.substring(2).startsWith(
150: method_start_tag)) {
151: // process an end tag only if
152: // we had processed a start tag AND
153: // the end tag markup is the same as the start tag
154:
155: TestResultDescription desc = new TestResultDescription(
156: text_content.toString(),
157: image_content);
158: map.put(method_start_tag, desc);
159:
160: text_content.delete(0, text_content
161: .length());
162: image_content = null;
163: method_start_tag = null;
164: in_text_tag = false; // force it
165: in_image_tag = false; // force it
166: }
167: } else if (line.startsWith(TEXT_START_TAG)) {
168: in_text_tag = true;
169: } else if (line.startsWith(IMAGE_START_TAG)) {
170: in_image_tag = true;
171: } else if (in_text_tag) {
172: text_content.append(line + " ");
173: } else if (in_image_tag) {
174: image_content = line;
175: } else if (line.startsWith("<")) { // check for start tag
176: int end_index = line.indexOf(">");
177: if (end_index < 0)
178: continue; // no markup termination
179: line = line.substring(1, end_index).trim();
180: if (line.length() < 0)
181: continue; // no content inside markup
182: method_start_tag = line;
183: }
184: }
185: } catch (Exception ex) {
186: } finally {
187: try {
188: reader.close();
189: } catch (Exception ex) {
190: }
191: }
192: }
193: }
194: }
|