KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > test > web > ExoWebClient


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;
6
7 import java.util.*;
8 import org.exoplatform.test.web.unit.WebUnit;
9 import org.exoplatform.test.web.validator.WellFormedXhtmlValidator;
10 import com.meterware.httpunit.*;
11 /**
12  * May 21, 2004
13  * @author: Tuan Nguyen
14  * @email: tuan08@users.sourceforge.net
15  * @version: $Id: ExoWebClient.java,v 1.1 2004/10/11 23:36:03 tuan08 Exp $
16  **/

17 public class ExoWebClient implements Runnable JavaDoc {
18   private static WellFormedXhtmlValidator
19     wellFormedXhtmlValidator_ = new WellFormedXhtmlValidator() ;
20   private String JavaDoc name_ ;
21   private String JavaDoc url_ ;
22   private TestSuites suites_ ;
23   private long interval_ = 0 ;
24   private Map attributes_;
25   private Map roles_ ;
26   
27   private boolean validateWellFormedXhtml_ ;
28   private boolean validateWebUnit_ ;
29   private boolean printSummary_ ;
30   private WebClient webClient_ ;
31   private TimeListener timeListener_ ;
32   private boolean error_ ;
33   
34   private WebResponse response_ ;
35   
36   public ExoWebClient(String JavaDoc name, String JavaDoc url) {
37     name_ = name ;
38     url_ = url ;
39     validateWebUnit_ = true ;
40     validateWellFormedXhtml_ = false ;
41     printSummary_ = true ;
42     webClient_ = new WebConversation();
43     timeListener_ = new TimeListener() ;
44     webClient_.addClientListener(timeListener_) ;
45     
46     attributes_ = new HashMap() ;
47     roles_ = new HashMap() ;
48     roles_.put("guest", "guest") ;
49     attributes_.put("client.name", name_ );
50     reset() ;
51   }
52  
53   public void reset() {
54     response_ = null ;
55     error_ = false ;
56   }
57   
58   public String JavaDoc getName() { return name_ ; }
59   public void setName(String JavaDoc name) { name_ = name ; }
60   
61   public String JavaDoc getHomePageURL() { return url_ ; }
62   public void setHomePageURL(String JavaDoc s) { url_ = s ; }
63  
64   public long getInterval() { return interval_ ; }
65   public void setInterval(long interval) { interval_ = interval ; }
66   
67   public Object JavaDoc getAttribute(String JavaDoc key) { return attributes_.get(key) ; }
68   public void setAttribute(String JavaDoc key , Object JavaDoc obj) { attributes_.put(key, obj) ;}
69   
70   public Map getRoles() { return roles_ ; }
71
72   public void setSuites(TestSuites suites) { suites_ = suites ; }
73   
74   public WebClient getWebClient() { return webClient_ ; }
75   public void setWebClient(WebClient wc) {
76     webClient_ = wc ;
77     webClient_.addClientListener(timeListener_) ;
78   }
79
80   public void setValidateWellFormedXhtml(boolean b) { validateWellFormedXhtml_ = b ; }
81   
82   public void setValidateWebUnit(boolean b) { validateWebUnit_ = b ; }
83   
84   public boolean getPrintSummary() { return printSummary_ ; }
85   public void setPrintSummary(boolean b) { printSummary_ = b ; }
86   
87   public boolean hasError() { return error_ ; }
88   
89   public WebResponse getResponse() { return response_ ; }
90   
91   public String JavaDoc getResponseText() throws Exception JavaDoc { return response_.getText() ; }
92   
93   public WebResponse beforeRunSuite(WebResponse previousResponse) throws Exception JavaDoc {
94     return previousResponse ;
95   }
96   
97   public WebResponse afterRunSuite(WebResponse previousResponse) throws Exception JavaDoc {
98     return previousResponse ;
99   }
100   
101   public void executeUnit(WebUnit webUnit) throws Exception JavaDoc {
102     WebTable table = null ;
103     if(webUnit.getBlockId() != null) {
104         table = response_.getFirstMatchingTable(WebTable.MATCH_ID, webUnit.getBlockId()) ;
105     }
106     //check conidtions
107
if(!webUnit.checkConditions(response_, table, this)) return ;
108     response_ = webUnit.execute(response_, table,this) ;
109     int contentLength = response_.getText().length() ;
110     boolean malformed = false ;
111     if(validateWellFormedXhtml_) {
112       malformed = !wellFormedXhtmlValidator_.validate(response_, this) ;
113     }
114     boolean hasError = !webUnit.validate(response_, this) ;
115     webUnit.log(timeListener_.getExecutionTime(), contentLength, hasError, malformed) ;
116     if(hasError) {
117       error_ = true ;
118       suites_.getCurrentWebUnitSuite().setStatus(WebUnitSuite.ERROR_STATUS) ;
119     }
120     return ;
121   }
122   
123   public void run() { run(suites_) ; }
124   
125   public void run(TestSuites suites) {
126     WebUnit webUnit = null ;
127     try {
128       suites_ = suites ;
129       while(suites.nextUnit()) {
130         webUnit = suites.getCurrentWebUnit() ;
131         executeUnit(webUnit) ;
132         if(interval_ > 0 ) Thread.sleep(interval_) ;
133         if(Thread.interrupted()) return ;
134       }
135     } catch (InterruptedException JavaDoc ex) {
136       Thread.currentThread().interrupt() ;
137     } catch (Exception JavaDoc ex) {
138       if(webUnit != null) {
139         System.err.println(webUnit.getUnitSummaryInXHTML()) ;
140       }
141       ex.printStackTrace() ;
142     }
143   }
144   
145   public String JavaDoc getTextSummary() {
146     if(suites_ == null) { return "N/A" ; }
147     return suites_.getTextSummary() ;
148   }
149   
150   public String JavaDoc getHtmlSummary() throws Exception JavaDoc {
151     if(suites_ == null) { return "N/A" ; }
152     return suites_.getHtmlSummary() ;
153   }
154   
155   public ExoWebClient clone(String JavaDoc name) {
156     ExoWebClient newClient = new ExoWebClient(name, url_) ;
157     if(suites_ != null) newClient.setSuites( suites_.softClone()) ;
158     newClient.setInterval(interval_) ;
159     newClient.setValidateWebUnit(validateWebUnit_) ;
160     newClient.setValidateWellFormedXhtml(validateWellFormedXhtml_) ;
161     return newClient ;
162   }
163
164   class TimeListener implements WebClientListener {
165     private long endTime_ ;
166     private long startTime_ ;
167     
168     public long getExecutionTime() { return endTime_ - startTime_ ;}
169     
170     public void requestSent(com.meterware.httpunit.WebClient src, WebRequest req) {
171       startTime_ = System.currentTimeMillis() ;
172     }
173
174     public void responseReceived(com.meterware.httpunit.WebClient src, WebResponse resp) {
175       endTime_ = System.currentTimeMillis() ;
176     }
177   }
178 }
179
Popular Tags