1 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 17 public class ExoWebClient implements Runnable { 18 private static WellFormedXhtmlValidator 19 wellFormedXhtmlValidator_ = new WellFormedXhtmlValidator() ; 20 private String name_ ; 21 private String 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 name, String 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 getName() { return name_ ; } 59 public void setName(String name) { name_ = name ; } 60 61 public String getHomePageURL() { return url_ ; } 62 public void setHomePageURL(String s) { url_ = s ; } 63 64 public long getInterval() { return interval_ ; } 65 public void setInterval(long interval) { interval_ = interval ; } 66 67 public Object getAttribute(String key) { return attributes_.get(key) ; } 68 public void setAttribute(String key , Object 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 getResponseText() throws Exception { return response_.getText() ; } 92 93 public WebResponse beforeRunSuite(WebResponse previousResponse) throws Exception { 94 return previousResponse ; 95 } 96 97 public WebResponse afterRunSuite(WebResponse previousResponse) throws Exception { 98 return previousResponse ; 99 } 100 101 public void executeUnit(WebUnit webUnit) throws Exception { 102 WebTable table = null ; 103 if(webUnit.getBlockId() != null) { 104 table = response_.getFirstMatchingTable(WebTable.MATCH_ID, webUnit.getBlockId()) ; 105 } 106 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 ex) { 136 Thread.currentThread().interrupt() ; 137 } catch (Exception ex) { 138 if(webUnit != null) { 139 System.err.println(webUnit.getUnitSummaryInXHTML()) ; 140 } 141 ex.printStackTrace() ; 142 } 143 } 144 145 public String getTextSummary() { 146 if(suites_ == null) { return "N/A" ; } 147 return suites_.getTextSummary() ; 148 } 149 150 public String getHtmlSummary() throws Exception { 151 if(suites_ == null) { return "N/A" ; } 152 return suites_.getHtmlSummary() ; 153 } 154 155 public ExoWebClient clone(String 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 |