Index: TestHarness4LAMS2/src/org/lamsfoundation/testharness/Call.java
===================================================================
RCS file: /usr/local/cvsroot/TestHarness4LAMS2/src/org/lamsfoundation/testharness/Call.java,v
diff -u -r1.6 -r1.7
--- TestHarness4LAMS2/src/org/lamsfoundation/testharness/Call.java 10 Dec 2013 17:07:11 -0000 1.6
+++ TestHarness4LAMS2/src/org/lamsfoundation/testharness/Call.java 13 Jan 2014 08:47:10 -0000 1.7
@@ -22,14 +22,11 @@
*/
package org.lamsfoundation.testharness;
-import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
-import java.util.Map;
import org.apache.log4j.Logger;
-import org.xml.sax.SAXException;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.PostMethodWebRequest;
@@ -40,64 +37,85 @@
import com.meterware.httpunit.WebResponse;
/**
- * @version
- *
- *
- * View Source
- *
- *
- * @author Fei Yang
+ * @author Fei Yang, Marcin Cieslak
*/
public class Call {
- private static final Logger log = Logger.getLogger(Call.class);
+ static class CallRecord {
+ private int suiteIndex;
+ private String testName;
+ private String callee;
+ private String description;
+ private String snapShotTime;
+ private long timeInMillis;
+ private Integer httpStatusCode;
+ private String message;
- private static final String HTTP = "http://";
+ public CallRecord() {
+ }
- private static final String GET = "GET ";
+ public CallRecord(int suiteIndex, String testName, String callee, String description, String snapShotTime,
+ long timeInMillis, Integer httpStatusCode, String message) {
+ this.suiteIndex = suiteIndex;
+ this.testName = testName;
+ this.callee = callee;
+ this.description = description;
+ this.snapShotTime = snapShotTime;
+ this.timeInMillis = timeInMillis;
+ this.httpStatusCode = httpStatusCode;
+ this.message = message;
+ }
- private static final String POST = "POST ";
+ public String getCallee() {
+ return callee;
+ }
- private static final char COLON = ':';
+ public String getDescription() {
+ return description;
+ }
- private static final char SPACE = ' ';
+ public final Integer getHttpStatusCode() {
+ return httpStatusCode;
+ }
- private static final String NON_HTTP_ERROR = "Check the log for the error message";
+ public String getMessage() {
+ return message;
+ }
- private WebConversation wc; // for WEB
+ public String getSnapShotTime() {
+ return snapShotTime;
+ }
- private AbstractTest test;
+ public int getSuiteIndex() {
+ return suiteIndex;
+ }
- private String description;
+ public String getTestName() {
+ return testName;
+ }
- @SuppressWarnings("unused")
- private String method; // for RMI and WS
+ public long getTimeInMillis() {
+ return timeInMillis;
+ }
+ }
- @SuppressWarnings("unused")
- private Map parameters; // for RMI or WS
+ private static final Logger log = Logger.getLogger(Call.class);
+ private WebConversation wc;
+ private AbstractTest test;
+ private String description;
+ private String url;
+ private WebForm form;
+ private InputStream is;
- private String url; // for WEB
-
- private WebForm form; // for WEB
-
- private InputStream is; // for WEB POST method
-
private String contentType; // for WEB POST method
- public Call(WebConversation wc, AbstractTest test, String description, WebForm form) {
+ public Call(WebConversation wc, AbstractTest test, String description, String url) {
this.wc = wc;
this.test = test;
this.description = description;
- this.form = form;
+ this.url = url;
}
- public Call(AbstractTest test, String description, String method, Map parameters) {
- this.test = test;
- this.description = description;
- this.method = method;
- this.parameters = parameters;
- }
-
public Call(WebConversation wc, AbstractTest test, String description, String url, InputStream is,
String contentType) {
this.wc = wc;
@@ -108,34 +126,20 @@
this.contentType = contentType;
}
- public Call(WebConversation wc, AbstractTest test, String description, String url) {
+ public Call(WebConversation wc, AbstractTest test, String description, WebForm form) {
this.wc = wc;
this.test = test;
this.description = description;
- this.url = url;
+ this.form = form;
}
- public AbstractTest getTest() {
- return test;
+ private static boolean isCancelButton(SubmitButton button) {
+ return button.getName().contains("CANCEL") || button.getName().contains("Cancel")
+ || button.getName().contains("cancel") || button.getValue().contains("cancel")
+ || button.getValue().contains("Cancel") || button.getValue().contains("CANCEL");
}
- /**
- * need add CallRecord to TestReporter
- */
public Object execute() {
- switch (test.callType) {
- case RMI:
- return executeRMI();
- case WS:
- return executeWS();
- case WEB:
- return executeHttpRequest();
- default:
- return null;
- }
- }
-
- private Object executeHttpRequest() {
String message = null;
String callee = null;
Integer httpStatusCode = null;
@@ -145,14 +149,14 @@
WebResponse resp = null;
if (form != null) {
SubmitButton[] submitButtons = filterCancelButton(form.getSubmitButtons());
- Call.log.debug(submitButtons.length + " non-cancel submit button(s) in the form");
+ Call.log.debug(submitButtons.length + " non-cancel submit buttons in the form");
WebRequest req = null;
if (submitButtons.length <= 1) {
req = form.getRequest();
} else {
- req = form.getRequest(submitButtons[TestUtil.generateRandomIndex(submitButtons.length)]);
+ req = form.getRequest(submitButtons[TestUtil.generateRandomNumber(submitButtons.length)]);
}
- callee = form.getMethod().toUpperCase() + Call.SPACE + form.getAction();
+ callee = form.getMethod().toUpperCase() + " " + form.getAction();
Call.log.debug(callee);
start = System.currentTimeMillis();
resp = wc.getResponse(req);
@@ -161,10 +165,10 @@
String absoluteURL = getAbsoluteURL(url);
WebRequest req;
if (is == null) {
- callee = Call.GET + url;
+ callee = "GET " + url;
req = new GetMethodWebRequest(absoluteURL);
} else {
- callee = Call.POST + url;
+ callee = "POST " + url;
req = new PostMethodWebRequest(absoluteURL, is, contentType);
}
Call.log.debug(callee);
@@ -180,15 +184,15 @@
if (resp.getResponseCode() >= 400) {
Call.log.debug(resp.getText());
- throw new TestHarnessException(test.testName + " Got http error code " + httpStatusCode);
+ throw new TestHarnessException(test.testName + " got http error code " + httpStatusCode);
}
for (String headerFieldName : resp.getHeaderFieldNames()) {
if (headerFieldName.equalsIgnoreCase("SET-COOKIE")) {
for (String headerFieldValue : resp.getHeaderFields(headerFieldName)) {
String[] headerFieldSplit = headerFieldValue.split("=|;");
String cookieName = headerFieldSplit[0];
- if ("JSESSIONID".equalsIgnoreCase(cookieName) && wc.getCookieValue(cookieName) == null) {
+ if ("JSESSIONID".equalsIgnoreCase(cookieName) && (wc.getCookieValue(cookieName) == null)) {
String cookieValue = headerFieldSplit[1];
Call.log.debug("Manually setting cookie: " + cookieName + "=" + cookieValue);
wc.putCookie(cookieName, cookieValue);
@@ -198,26 +202,20 @@
}
return resp;
- } catch (IOException e) {
- message = Call.NON_HTTP_ERROR;
- Call.log.debug(e.getMessage(), e);
+ } catch (Exception e) {
throw new RuntimeException(e);
- } catch (SAXException e) {
- message = Call.NON_HTTP_ERROR;
- Call.log.debug(e.getMessage(), e);
- throw new RuntimeException(e);
} finally {
- TestReporter.addCallRecord(new CallRecord(test.getTestSuite().getSuiteIndex(), test.testName, test.callType
- .getName(), callee, description, new SimpleDateFormat("HH:mm:ss SSS").format(new Date(end)), end
- - start, httpStatusCode, message));
+ TestReporter.addCallRecord(new CallRecord(test.getTestSuite().getSuiteIndex(), test.testName, callee,
+ description, new SimpleDateFormat("HH:mm:ss SSS").format(new Date(end)), end - start,
+ httpStatusCode, message));
}
}
private SubmitButton[] filterCancelButton(SubmitButton[] sbmtBtns) {
boolean found = false;
int i = 0;
for (; i < sbmtBtns.length; i++) {
- if (isCancelButton(sbmtBtns[i])) {
+ if (Call.isCancelButton(sbmtBtns[i])) {
found = true;
break;
}
@@ -237,142 +235,19 @@
}
}
- private boolean isCancelButton(SubmitButton button) {
- return button.getName().contains("CANCEL") || button.getName().contains("Cancel")
- || button.getName().contains("cancel") || button.getValue().contains("cancel")
- || button.getValue().contains("Cancel") || button.getValue().contains("CANCEL");
- }
-
- /**
- * TODO implement me
- *
- * @param
- * @return
- */
- private Object executeWS() {
- return null;
- }
-
- /**
- * TODO implement me
- *
- * @param
- * @return
- */
- private Object executeRMI() {
- return null;
- }
-
private String getAbsoluteURL(String url) {
- if (url.startsWith(Call.HTTP)) {
+ if (url.startsWith("http://")) {
return url;
}
String withSlash = url.startsWith("/") ? url : "/" + url;
String context = url.startsWith(test.getTestSuite().getContextRoot()) ? "" : test.getTestSuite()
.getContextRoot();
if (test.getTestSuite().getHttpPort() != 80) {
- return Call.HTTP + test.getTestSuite().getTargetServer() + Call.COLON + test.getTestSuite().getHttpPort()
+ return "http://" + test.getTestSuite().getTargetServer() + ":" + test.getTestSuite().getHttpPort()
+ context + withSlash;
} else {
- return Call.HTTP + test.getTestSuite().getTargetServer() + context + withSlash;
+ return "http://" + test.getTestSuite().getTargetServer() + context + withSlash;
}
}
-
- protected static class CallRecord {
-
- private int suiteIndex;
- private String testName;
- private String type;
- private String callee;
- private String description;
- private String snapShotTime;
- private long timeInMillis;
- private Integer httpStatusCode;// for Web Call only
- private String message;
-
- public CallRecord() {
- // empty constructor
- }
-
- public CallRecord(int suiteIndex, String testName, String type, String callee, String description,
- String snapShotTime, long timeInMillis, Integer httpStatusCode, String message) {
- this.suiteIndex = suiteIndex;
- this.testName = testName;
- this.type = type;
- this.callee = callee;
- this.description = description;
- this.snapShotTime = snapShotTime;
- this.timeInMillis = timeInMillis;
- this.httpStatusCode = httpStatusCode;
- this.message = message;
- }
-
- public String getMessage() {
- return message;
- }
-
- public String getSnapShotTime() {
- return snapShotTime;
- }
-
- public long getTimeInMillis() {
- return timeInMillis;
- }
-
- public String getCallee() {
- return callee;
- }
-
- public String getType() {
- return type;
- }
-
- public String getTestName() {
- return testName;
- }
-
- public int getSuiteIndex() {
- return suiteIndex;
- }
-
- public String getDescription() {
- return description;
- }
-
- public final Integer getHttpStatusCode() {
- return httpStatusCode;
- }
- }
-
- public enum CallType {
-
- RMI, WS, WEB, UNKNOWN;
-
- public static CallType get(String value) {
- if (value.equals("RMI")) {
- return CallType.RMI;
- } else if (value.equals("WS")) {
- return CallType.WS;
- } else if (value.equals("WEB")) {
- return CallType.WEB;
- } else {
- return CallType.UNKNOWN;
- }
- }
-
- public String getName() {
- switch (this) {
- case RMI:
- return "RMI";
- case WS:
- return "WS";
- case WEB:
- return "WEB";
- default:
- return "Unknown";
- }
- }
- }
-
-}
+}
\ No newline at end of file