KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > test > web > unit > WebUnit


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.test.web.unit;
6
7 import java.util.* ;
8 import org.exoplatform.test.web.*;
9 import org.exoplatform.test.web.condition.Condition;
10 import org.exoplatform.test.web.validator.Validator;
11 import com.meterware.httpunit.*;
12
13 /**
14  * May 21, 2004
15  * @author: Tuan Nguyen
16  * @email: tuan08@users.sourceforge.net
17  * @version: $Id: WebUnit.java,v 1.1 2004/10/11 23:36:06 tuan08 Exp $
18  **/

19 abstract public class WebUnit {
20     final static public String JavaDoc ACTION = "op" ;
21
22   private String JavaDoc name_ ;
23   private String JavaDoc description_ ;
24   protected List validators_ ;
25   protected List conditions_ ;
26   protected WebUnitMonitor monitor_ ;
27   protected String JavaDoc blockId_;
28
29   public WebUnit() {
30     name_ = getClass().getName() ;
31     description_ = name_ ;
32     monitor_ = new WebUnitMonitor() ;
33   }
34
35   
36
37   public WebUnit(String JavaDoc name , String JavaDoc desc) {
38     name_ = name ;
39     description_ = desc ;
40     monitor_ = new WebUnitMonitor() ;
41   }
42  
43   public void reset() { monitor_.reset() ; }
44   
45   public String JavaDoc getBlockId() { return blockId_ ; }
46
47   public WebUnit setBlockId(String JavaDoc id) {
48     blockId_ = id ;
49     return this ;
50   }
51
52   public String JavaDoc getName() { return name_ ; }
53
54   public String JavaDoc getActionDescription() { return "No Description" ; }
55   
56   public String JavaDoc getDescription() {
57     if(description_ == null) return "No Description" ;
58     return description_ ;
59   }
60   
61   public String JavaDoc getExtraInfo() { return "N/A" ; }
62   
63   abstract public WebResponse execute(WebResponse previousResponse, WebTable block,
64                                         ExoWebClient client) throws Exception JavaDoc ;
65
66   public List getValidators() { return validators_ ; }
67
68   public WebUnit addValidator(Validator validator) {
69     if(validators_ == null) validators_ = new ArrayList() ;
70     validators_.add(validator) ;
71     return this ;
72   }
73
74
75   public List getConditions() { return conditions_ ; }
76
77   public WebUnit addCondition(Condition condition) {
78     if(conditions_ == null) conditions_ = new ArrayList() ;
79     conditions_.add(condition) ;
80     return this ;
81   }
82
83   public WebUnitMonitor getMonitor() { return monitor_ ; }
84
85   
86
87   static public String JavaDoc getRealValue(ExoWebClient client, String JavaDoc value) {
88     if(value.length() < 3 ) return value ;
89     String JavaDoc oldValue = value ;
90     if(value.charAt(0) == '#' && value.charAt(1) == '{' &&
91        value.charAt(value.length() - 1) == '}') {
92       value = value.substring(2, value.length() - 1) ;
93       value = (String JavaDoc) client.getAttribute(value) ;
94       if(value == null) return oldValue ;
95     }
96     return value ;
97   }
98
99   public boolean checkConditions(WebResponse response ,WebTable block, ExoWebClient client) throws Exception JavaDoc {
100     if(conditions_ != null) {
101         for(int k = 0 ; k < conditions_.size(); k++) {
102             Condition condition = (Condition) conditions_.get(k) ;
103             if (!condition.checkCondition(response, block, client)) {
104                 return false ;
105             }
106         }
107     }
108     return true ;
109   }
110
111   public boolean validate(WebResponse response , ExoWebClient client) throws Exception JavaDoc {
112     if(validators_ != null) {
113       for(int k = 0 ; k < validators_.size(); k++) {
114         Validator validator = (Validator) validators_.get(k) ;
115         if (!validator.validate(response, client)) {
116           return false ;
117         }
118       }
119     }
120     return true ;
121   }
122
123   public void log(long executionTime , int contentLength, boolean error, boolean malformed) {
124     monitor_.log(executionTime, contentLength, error, malformed) ;
125   }
126   
127   static public String JavaDoc STYLES =
128     "<style type='text/css'> \n" +
129     " .WebUnit { \n" +
130     " } \n" +
131     " .WebUnit th { \n" +
132     " border: 1px solid #CCCCCC ; \n" +
133     " padding: 2px ; \n" +
134     " background: #bbb ; \n" +
135     " color: #074B88; \n" +
136     " } \n" +
137     " .WebUnit td { \n" +
138     " border: 1px solid #CCCCCC ; \n" +
139     " padding: 2px ; \n" +
140     " } \n" +
141     "</style>\n" ;
142   
143   public String JavaDoc getUnitSummaryInXHTML() {
144     StringBuffer JavaDoc b = new StringBuffer JavaDoc() ;
145     b.append("<html>\n") ;
146     b. append("<head>\n") ;
147     b.append(STYLES) ;
148     b. append("</head>\n") ;
149     b. append("<body>\n") ;
150     b. append("<table class='WebUnit'>\n") ;
151     b. append("<tr>\n") ;
152     b. append("<td>Web Unit Name</td>\n") ;
153     b. append("<td>").append(name_).append("</td>\n") ;
154     b. append("</tr>\n") ;
155     
156     b. append("<tr>\n") ;
157     b. append("<td>Description</td>\n") ;
158     b. append("<td>").append(getDescription()).append("</td>\n") ;
159     b. append("</tr>\n") ;
160     
161     b. append("<tr>\n") ;
162     b. append("<td>Action Description</td>\n") ;
163     b. append("<td>").append(getActionDescription()).append("</td>\n") ;
164     b. append("</tr>\n") ;
165     
166     if(conditions_ != null) {
167       b. append("<tr>\n") ;
168       b. append("<td>Conditions</td>\n") ;
169       b. append("<td>");
170       for(int i = 0 ; i < conditions_.size(); i++) {
171         Condition condition = (Condition) conditions_.get(i) ;
172         b.append(condition.getName()).append(": ").
173           append(condition.getDescription()).append("<br/>") ;
174       }
175       b. append("</td>\n") ;
176       b. append("</tr>\n") ;
177     }
178     
179     if(validators_ != null) {
180       b. append("<tr>\n") ;
181       b. append("<td>Validators</td>\n") ;
182       b. append("<td>");
183       for(int i = 0 ; i < validators_.size(); i++) {
184         Validator validator = (Validator) validators_.get(i) ;
185         b.append(validator.getName()).append(": ").
186           append(validator.getDescription()).append("<br/>") ;
187       }
188       b. append("</td>\n") ;
189       b. append("</tr>\n") ;
190     }
191     
192     b. append("<tr>\n") ;
193     b. append("<td>Extra Information</td>\n") ;
194     b. append("<td>").append(getExtraInfo()).append("</td>\n") ;
195     b. append("</tr>\n") ;
196     
197     b. append("<table>\n") ;
198     b. append("</body>\n") ;
199     b.append("</html>\n") ;
200     return b.toString() ;
201   }
202 }
203
Popular Tags