1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.jameleon.bean;
18
19 import java.io.BufferedReader;
20 import java.io.File;
21 import java.io.FileReader;
22 import java.io.IOException;
23 import java.net.URL;
24 import java.util.*;
25
26 import net.sf.jameleon.XMLable;
27 import net.sf.jameleon.exception.JameleonScriptException;
28 import net.sf.jameleon.util.Configurator;
29 import net.sf.jameleon.util.JameleonUtility;
30 import net.sf.jameleon.util.XMLHelper;
31
32 /***
33 * This class represents a test case. This is currently used only for test case
34 * documentation generation
35 * A TestCase consists of:
36 * <ul>
37 * <li>name - The name of the test case</li>
38 * <li>summary - A brief description of the test case</li>
39 * <li>author - The person that wrote the test case</li>
40 * <li>funcationPointTested - The functional point being tested</li>
41 * <li>bug - The bug this test case was written for.</li>
42 * <li>sessions - List of Sessions that then contain a list of FunctionalPoints</li>
43 * <li>testLevels - List of test levels that caterogize the test case</li>
44 * </ul>
45 */
46 public class TestCase implements XMLable {
47 private static final long serialVersionUID = 1L;
48 /***
49 * The name of the test case
50 */
51 protected String name;
52 /***
53 * A brief description of the test case
54 */
55 protected String summary;
56 /***
57 * The person that wrote the test case
58 */
59 protected String author;
60 /***
61 * The application being tested
62 */
63 protected String application;
64 /***
65 * The bug related to this test case
66 */
67 protected Set bugs;
68 /***
69 * The functional point tested by the test case
70 */
71 protected String functionalPointTested;
72 /***
73 * The level(s) of the test case - SMOKE, FUNCTIONAL, REGRESSION, ACCEPTANCE, to list a few
74 */
75 protected List testLevels;
76 /***
77 * The test case file
78 */
79 protected String file;
80 /***
81 * The encoding to use in the toXML() method
82 */
83 protected String encoding;
84 /***
85 * The unique id of the test case.
86 */
87 protected String testCaseId;
88 /***
89 * The organization the test case is set to execute against.
90 */
91 protected String organization;
92 /***
93 * The link to the requirement being tested
94 */
95 protected String testCaseRequirement;
96 /***
97 * The test evironment this test case is being test against
98 */
99 protected String testEnvironment;
100 /***
101 * A list of sessions in this test case
102 */
103 protected List sessions;
104
105 /***
106 * Default constructor only used to initialize variables
107 */
108 public TestCase() {
109 sessions = new LinkedList();
110 testLevels = new LinkedList();
111 bugs = new HashSet();
112 }
113
114 public void addBug(String bug){
115 bugs.add(bug);
116 }
117
118 public void addSession(Session s){
119 sessions.add(s);
120 }
121
122 public void addTestLevel(String testLevel){
123 if (testLevels != null && !testLevels.contains(testLevel)){
124 testLevels.add(testLevel);
125 }
126 }
127
128 public String getApplication(){
129 return application;
130 }
131
132 public String getAuthor(){
133 return author;
134 }
135
136 public Set getBugs(){
137 return bugs;
138 }
139
140 /***
141 * @return the encoding style to use in the toXML() method
142 */
143 public String getEncoding(){
144 return encoding;
145 }
146
147 public String getFile(){
148 return file;
149 }
150
151 public String getScriptContents(){
152 StringBuffer contents = new StringBuffer();
153 if (file != null){
154 File f = new File(file);
155 if (f.exists() && f.isFile()) {
156 try{
157 BufferedReader reader = new BufferedReader(new FileReader(f));
158 String line = null;
159 while((line = reader.readLine()) != null){
160 contents.append(line+"\n");
161 }
162 }catch(Exception e){
163 e.printStackTrace();
164 }
165 }
166 }
167 return JameleonUtility.decodeTextToXML(contents.toString());
168 }
169
170 public String getFunctionalPointTested(){
171 return functionalPointTested;
172 }
173
174 public String getName(){
175 return name;
176 }
177
178 public String getOrganization(){
179 return organization;
180 }
181
182 public List getSessions(){
183 return sessions;
184 }
185
186 public String getSummary(){
187 return summary;
188 }
189
190 public String getTestCaseId(){
191 return testCaseId;
192 }
193
194 public String getTestCaseRequirement(){
195 return testCaseRequirement;
196 }
197
198 public String getTestEnvironment(){
199 return testEnvironment;
200 }
201
202 public List getTestLevels(){
203 return testLevels;
204 }
205
206 /***
207 * Populates this test case object from a test script script
208 */
209 public void readFromScript(String script){
210 Configurator config = Configurator.getInstance();
211 organization = config.getValue("organization");
212 testEnvironment = config.getValue("testEnvironment");
213 try{
214 URL scriptUrl = new URL(script);
215 XMLHelper xmlHelper = new XMLHelper(scriptUrl);
216 author = xmlHelper.getValueFromXPath("//jm:test-case-author");
217 application = xmlHelper.getValueFromXPath("//jm:application-tested");
218 functionalPointTested = xmlHelper.getValueFromXPath("//jm:functional-point-tested");
219 String tmpName = xmlHelper.getValueFromXPath("/jm:testcase/@name");
220 if (tmpName != null) {
221 name = tmpName;
222 }else{
223 name = JameleonUtility.getFileNameFromPath(scriptUrl.getFile());
224 }
225 summary = xmlHelper.getValueFromXPath("//jm:test-case-summary");
226 testCaseId = xmlHelper.getValueFromXPath("//jm:test-case-id");
227 testCaseRequirement = xmlHelper.getValueFromXPath("//jm:test-case-requirement");
228 file = scriptUrl.getFile();
229 bugs = new HashSet(xmlHelper.getValuesFromXPath("//jm:test-case-bug"));
230 testLevels = xmlHelper.getValuesFromXPath("//jm:test-case-level");
231 List fps = xmlHelper.getValuesFromXPath("//@functionId");
232 Session s = new Session();
233 for (Iterator it = fps.iterator(); it.hasNext(); ) {
234 FunctionalPoint fp = new FunctionalPoint();
235 fp.setFunctionId((String)it.next());
236 s.addFunctionalPoint(fp);
237 }
238 addSession(s);
239 }catch(IOException ioe){
240 throw new JameleonScriptException(ioe.getMessage(), ioe);
241 }
242 }
243
244 public void setApplication(String application){
245 this.application = application;
246 }
247
248 public void setAuthor(String author){
249 this.author = author;
250 }
251
252 /***
253 * Sets the encoding to use in the toXML() results
254 * @param encoding - the encoding to use in the toXML() results
255 */
256 public void setEncoding(String encoding){
257 this.encoding = encoding;
258 }
259
260 public void setFile(String file){
261 this.file = file;
262 }
263
264 public void setFunctionalPointTested(String functionalPointTested){
265 this.functionalPointTested = functionalPointTested;
266 }
267
268 public void setName(String name){
269 this.name = name;
270 }
271
272 public void setOrganization(String organization){
273 this.organization = organization;
274 }
275 public void setSummary(String summary){
276 this.summary = summary;
277 }
278
279 public void setTestCaseId(String testCaseId){
280 this.testCaseId = testCaseId;
281 }
282
283 public void setTestCaseRequirement(String testCaseRequirement){
284 this.testCaseRequirement = testCaseRequirement;
285 }
286
287 public void setTestEnvironment(String testEnvironment){
288 this.testEnvironment = testEnvironment;
289 }
290
291 public void setTestLevels(LinkedList testLevels){
292 this.testLevels = testLevels;
293 }
294
295 public String toXML(){
296 StringBuffer sb = new StringBuffer("<?xml version=\"1.0\" encoding=\"");
297 sb.append(encoding).append("\"?>\n");
298 sb.append("<test-case>\n");
299 sb.append("\t<test-case-name>").append(name).append("</test-case-name>\n");
300 if (summary != null && summary.length() > 0) {
301 sb.append("\t<test-case-summary>").append(summary).append("</test-case-summary>\n");
302 }
303 if (application != null && application.length() > 0) {
304 sb.append("\t<application-tested>").append(application).append("</application-tested>\n");
305 }
306 if (testEnvironment != null && testEnvironment.length() > 0) {
307 sb.append("\t<test-environment>").append(testEnvironment).append("</test-environment>\n");
308 }
309 if (author != null && author.length() > 0) {
310 sb.append("\t<test-case-author>").append(author).append("</test-case-author>\n");
311 }
312 if (functionalPointTested != null && functionalPointTested.length() > 0) {
313 sb.append("\t<functional-point-tested>").append(functionalPointTested).append("</functional-point-tested>\n");
314 }
315 if (testCaseId != null && testCaseId.length() > 0) {
316 sb.append("\t<test-case-id>").append(testCaseId).append("</test-case-id>\n");
317 }
318 if (testCaseRequirement != null && testCaseRequirement.length() > 0) {
319 sb.append("\t<test-case-requirement>").append(testCaseRequirement).append("</test-case-requirement>\n");
320 }
321 if (testLevels != null && testLevels.size() > 0) {
322 Iterator it = testLevels.iterator();
323 while (it.hasNext()) {
324 sb.append("\t<test-case-level>").append((String)it.next()).append("</test-case-level>\n");
325 }
326 }
327 if (bugs != null && bugs.size() > 0) {
328 Iterator it = bugs.iterator();
329 while (it.hasNext()) {
330 sb.append("\t<test-case-bug>").append((String)it.next()).append("</test-case-bug>\n");
331 }
332 }
333 if (file != null && file.length() > 0) {
334 sb.append("\t<test-case-file>").append(file).append("</test-case-file>\n");
335 }
336 Iterator it = sessions.iterator();
337 while (it.hasNext()) {
338 sb.append(((XMLable)it.next()).toXML());
339 }
340 sb.append("</test-case>\n");
341 return sb.toString();
342 }
343
344 }