juergen 02/02/25 01:54:58
Modified:
testsuite/testsuite/junit/src/org/apache/slide/testsuite/testtools/tprocessor
TProcessors.java StreamResponseBodyAssert.java
GetAssert.java
Log:
Allow an encoding specification at the file reference attribute to prepare more
advanced I18N test cases.
Revision Changes Path
1.10 +129 -60
jakarta-slide/testsuite/testsuite/junit/src/org/apache/slide/testsuite/testtools/tprocessor/TProcessors.java
Index: TProcessors.java
===================================================================
RCS file:
/home/cvs/jakarta-slide/testsuite/testsuite/junit/src/org/apache/slide/testsuite/testtools/tprocessor/TProcessors.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- TProcessors.java 18 Feb 2002 09:35:05 -0000 1.9
+++ TProcessors.java 25 Feb 2002 09:54:57 -0000 1.10
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-slide/testsuite/testsuite/junit/src/org/apache/slide/testsuite/testtools/tprocessor/TProcessors.java,v
1.9 2002/02/18 09:35:05 juergen Exp $
- * $Revision: 1.9 $
- * $Date: 2002/02/18 09:35:05 $
+ * $Header:
/home/cvs/jakarta-slide/testsuite/testsuite/junit/src/org/apache/slide/testsuite/testtools/tprocessor/TProcessors.java,v
1.10 2002/02/25 09:54:57 juergen Exp $
+ * $Revision: 1.10 $
+ * $Date: 2002/02/25 09:54:57 $
*
* ====================================================================
*
@@ -84,7 +84,7 @@
* Main class to performe a test case and check and report the results
*
* @author Software AG
- * @version $Revision: 1.9 $
+ * @version $Revision: 1.10 $
*/
public class TProcessors {
@@ -490,13 +490,13 @@
WebdavState state = new WebdavState();
state.setAuthenticateToken("Basic realm=xxx"); // currently only basic is
supported
client.startSession((String)startUp.get("host"),
- ((Integer)startUp.get("port") ).intValue(),
+ ((Integer)startUp.get("port") ).intValue(),
new Credentials(user, password));
client.setState(state);
client.setDebug(0);
-// System.out.println("############### user " + user);
-// System.out.println("############### pwd " + password);
+ // System.out.println("############### user " + user);
+ // System.out.println("############### pwd " + password);
xmlresult.writeElementStart("exceuteStep");
@@ -516,19 +516,19 @@
client.executeMethod(method);
client.endSession();
- // System.out.println("Status Code "+ m.getStatusCode());
- // System.out.println("Status Text "+ m.getStatusText());
+// System.out.println("Status Code "+ m.getStatusCode());
+// System.out.println("Status Text "+ m.getStatusText());
- // if (method instanceof PropFindMethod) {
- // System.out.println("###################");
- // System.out.println("###################");
- // System.out.println("###################");
- // if (((PropFindMethod)method).getResponseDocument() !=
null) System.out.println("body " +
getElementString(((PropFindMethod)method).getResponseDocument().getDocumentElement()));
- // System.out.println("###################");
- // System.out.println("###################");
- // System.out.println("###################");
- // }
+// if (method instanceof PropFindMethod) {
+// System.out.println("###################");
+// System.out.println("###################");
+// System.out.println("###################");
+// if (((PropFindMethod)method).getResponseDocument() != null)
System.out.println("body " +
getElementString(((PropFindMethod)method).getResponseDocument().getDocumentElement()));
+// System.out.println("###################");
+// System.out.println("###################");
+// System.out.println("###################");
+// }
fillAutomatedVariables(method);
@@ -558,7 +558,7 @@
* @param Element
*/
- private HttpMethod executeRequest(Element request){
+ private HttpMethod executeRequest(Element request) throws Exception {
HttpMethod method = null;
List list = request.getChildren();
Iterator items = list.iterator();
@@ -597,12 +597,59 @@
* @return true if the file name is valid
*/
private boolean checkFileName(String name){
+// System.out.println("Checking " + name + " = " + new File(name).isFile());
return new File(name).isFile();
}
/**
+ * This method returns the name of a valid file
+ * @param name is the possibly unqualified file name
+ * @return return name, if the unqualified File exists, else qualify the name
+ */
+ private String getFileReferenceName(Element name){
+ String result = null;
+ String fileName = name.getAttributeValue("fileReference");
+ if (fileName != null && fileName.length() > 1) {
+ result = getFileReferenceName(fileName);
+ }
+ return result;
+ }
+
+
+ /**
+ * This method returns the name of a valid file
+ * @param name is the possibly unqualified file name
+ * @return return name, if the unqualified File exists, else qualify the name
+ */
+ private String getFileReferenceName(String name){
+ String result = name;
+ if (globalAbsolutePath != null) {
+ if (checkFileName(name)) {
+ result = name;
+ } else if (checkFileName(globalAbsolutePath+File.separatorChar+name)) {
+ result = globalAbsolutePath+File.separatorChar+name;
+ }
+ }
+ return result;
+ }
+
+
+
+ /**
+ * This method returns the name of a valid file
+ * @param name is the possibly unqualified file name
+ * @return return name, if the unqualified File exists, else qualify the name
+ */
+ private String getFileEncoding(Element name){
+ String result = name.getAttributeValue("fileEncoding");
+ if (result == null || result.equals("")) result = "UTF-8";
+ return result;
+ }
+
+
+ /**
* This method is used to replace variables in a string
* @param element the element to replacement should be applied
* @param defaultValue if element is null, return this default
@@ -610,36 +657,31 @@
*/
private String getBodyValue(Element body){
String result = body.getText();
- String fileName = body.getAttributeValue("fileReference");
- if (fileName != null && fileName.length() > 1) {
+ String fileName = getFileReferenceName(body);
+ if (fileName != null) {
- try {
- if (globalAbsolutePath != null) {
- if (checkFileName(fileName)) {
- fileName = fileName;
- } else if
(checkFileName(globalAbsolutePath+File.separatorChar+fileName)) {
- fileName = globalAbsolutePath+File.separatorChar+fileName;
- }
-
- }
- Reader input = new BufferedReader(new FileReader(fileName));
- StringWriter output = new StringWriter();
- char[] buffer = new char[1024];
- int numberOfBytesRead;
-
- while ((numberOfBytesRead=input.read(buffer)) > 0) {
- output.write (buffer, 0, numberOfBytesRead);
- }
-
- result = output.toString();
- input.close();
- output.close();
-
- }
- catch (Exception e) {
- e.printStackTrace();
+ try {
+
+ Reader input = new BufferedReader(
+ new InputStreamReader(
+ new FileInputStream(fileName), getFileEncoding(body)));
+ StringWriter output = new StringWriter();
+ char[] buffer = new char[1024];
+ int numberOfBytesRead;
+
+ while ((numberOfBytesRead=input.read(buffer)) > 0) {
+ output.write (buffer, 0, numberOfBytesRead);
}
+ result = output.toString();
+ input.close();
+ output.close();
+
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ }
+
}
return result;
@@ -708,7 +750,8 @@
* This method is used to find out the name of the Resquest Method
* @param List
*/
- private HttpMethod fillMethodName(Element methodElement, HttpMethod method){
+ private HttpMethod fillMethodName(Element methodElement, HttpMethod method)
+ throws Exception {
StringTokenizer st = new
StringTokenizer(replaceKnownVariable(methodElement));
method = HttpMethodFactory(st.nextToken(), st.nextToken());
return method;
@@ -727,6 +770,13 @@
return method;
}
+ private boolean binaryReadRequest(Element element) {
+ String fileReference = getFileReferenceName(element);
+ String fileEncoding = element.getAttributeValue("fileEncoding");
+ boolean result = (fileReference != null) &&
+ (fileEncoding == null || fileEncoding.equals("binary"));
+ return result;
+ }
@@ -734,12 +784,13 @@
* This method is used to set the Header in request Method
* @param List
*/
- private HttpMethod fillBody (Element element, HttpMethod method){
- // if (method instanceof PutMethod) {
- // ((PutMethod)method).sendData(replaceKnownVariable(element));
- // } else {
- method.setQuery(replaceKnownVariable(element));
- // }
+ private HttpMethod fillBody (Element element, HttpMethod method) throws
Exception {
+ if (method instanceof PutMethod && binaryReadRequest(element)) {
+ ((PutMethod)method).sendData(new File(getFileReferenceName(element)));
+ }
+ else {
+ method.setQuery(replaceKnownVariable(element));
+ }
return method;
}
@@ -755,7 +806,7 @@
* This method is used to return body as String
* @param String
*/
- public boolean responseAssert (HttpMethod m, Element elt){
+ public boolean responseAssert (HttpMethod m, Element elt) throws Exception {
boolean firstPart = false;
boolean secondPart = false;
Iterator header = responseHeader(elt.getChild("response"));
@@ -1115,8 +1166,22 @@
}
- private Reader expectedResponseAsStream(Element expectedResponse) {
- return new StringReader(expectedResponseAsString(expectedResponse));
+ private InputStream expectedResponseAsStream(Element element)
+ throws Exception {
+
+ Element expectedResponse = element.getChild("response").getChild("body");
+ InputStream result = null;
+ if (binaryReadRequest(expectedResponse)) {
+
+ result = new FileInputStream(getFileReferenceName(expectedResponse));
+ }
+ else {
+ result = new ByteArrayInputStream(
+ replaceKnownVariable(expectedResponse).getBytes(
+ getFileEncoding(element)));
+ }
+
+ return result;
}
private String expectedResponseAsString(Element expectedResponse) {
@@ -1128,7 +1193,7 @@
- private ResponseBodyAssert assertFactory(HttpMethod m, Element
expectedResponse) {
+ private ResponseBodyAssert assertFactory(HttpMethod m, Element
expectedResponse) throws Exception {
ResponseBodyAssert result = null;
if(m instanceof PropFindMethod){
@@ -1171,7 +1236,7 @@
* Factory method to return a method object based on the methodName.
* If the method name is not known, null is returned
*/
- public static HttpMethod HttpMethodFactory(String methodName) {
+ public static HttpMethod HttpMethodFactory(String methodName) throws Exception {
HttpMethod result = null;
if (methodName.equalsIgnoreCase("COPY")){
result = new CopyMethod();
@@ -1237,9 +1302,11 @@
else if(methodName.equalsIgnoreCase("UNCHECKOUT")){
result = new UncheckoutMethod();
}
- else if(methodName.equalsIgnoreCase("VERSIONCONTROL")){
+ else if(methodName.equalsIgnoreCase("VERSION-CONTROL")){
result = new VersionControlMethod();
}
+ if (result == null)
+ throw new Exception("Method " + methodName + " does not exist");
return result;
}
@@ -1250,7 +1317,8 @@
* Factory method to return a method object based on the methodName.
* If the method name is not known, null is returned
*/
- public static HttpMethod HttpMethodFactory(String methodName, String path) {
+ public static HttpMethod HttpMethodFactory(String methodName, String path)
+ throws Exception {
HttpMethod result = HttpMethodFactory(methodName);
if (result != null){
result.setPath(path);
@@ -1383,6 +1451,7 @@
}
+
1.2 +116 -61
jakarta-slide/testsuite/testsuite/junit/src/org/apache/slide/testsuite/testtools/tprocessor/StreamResponseBodyAssert.java
Index: StreamResponseBodyAssert.java
===================================================================
RCS file:
/home/cvs/jakarta-slide/testsuite/testsuite/junit/src/org/apache/slide/testsuite/testtools/tprocessor/StreamResponseBodyAssert.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- StreamResponseBodyAssert.java 20 Nov 2001 14:35:31 -0000 1.1
+++ StreamResponseBodyAssert.java 25 Feb 2002 09:54:58 -0000 1.2
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-slide/testsuite/testsuite/junit/src/org/apache/slide/testsuite/testtools/tprocessor/StreamResponseBodyAssert.java,v
1.1 2001/11/20 14:35:31 juergen Exp $
- * $Revision: 1.1 $
- * $Date: 2001/11/20 14:35:31 $
+ * $Header:
/home/cvs/jakarta-slide/testsuite/testsuite/junit/src/org/apache/slide/testsuite/testtools/tprocessor/StreamResponseBodyAssert.java,v
1.2 2002/02/25 09:54:58 juergen Exp $
+ * $Revision: 1.2 $
+ * $Date: 2002/02/25 09:54:58 $
*
* ====================================================================
*
@@ -78,22 +78,22 @@
* Abstract root class to perform the necessary checks for the received and
expected stream body
*
* @author Software AG
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
*/
public abstract class StreamResponseBodyAssert extends ResponseBodyAssert{
-
+ private final static boolean extendedPrintlnsrequested = false;
protected HttpMethod method;
- protected Reader expectedBody;
- protected Reader receivedBody;
+ protected InputStream expectedBody;
+ protected InputStream receivedBody;
/** constructer
* @param WebdavMethodBase Element
*/
- public StreamResponseBodyAssert(HttpMethod method, Reader expectedBody, XConf
xconf, XMLOutput xmlresult){
+ public StreamResponseBodyAssert(HttpMethod method, InputStream expectedBody,
XConf xconf, XMLOutput xmlresult){
super(xconf, method.getName(), xmlresult);
@@ -103,7 +103,7 @@
// ugly quick hack to read the body
try {
if (method.hasResponseBody() && method instanceof GetMethod) {
- this.receivedBody = new
StringReader(((GetMethod)method).getDataAsString());
+ this.receivedBody = ((GetMethod)method).getData();
}
}
catch (Exception e) {
@@ -133,75 +133,130 @@
/**
- * checks if Expected Response and Response are same
- */
- private boolean compareStreams(Reader receivedBody, Reader expectedBody){
- String receivedString = getStringFromStream(receivedBody);
- String expectedString = getStringFromStream(expectedBody);
-
-//System.out.println("");
-//System.out.println("##########Expected#########");
-//System.out.println(expectedString);
-//System.out.println("##########Expected#########");
-//System.out.println("##########Received#########");
-//System.out.println(receivedString);
-//System.out.println("##########Received#########");
-
- if (expectedString.length() != receivedString.length()) {
- xmlresult.writeElement("ContentLengthError", "Received = " +
receivedString.length() + " Expected = " + expectedString.length() );
- return false;
- }
- if (!expectedString.equals(receivedString)) {
- xmlresult.writeElement("ContentValueError", "Differ Position = " +
getFirstDifferentPosition(expectedString, receivedString));
- return false;
- }
-
- return true; // length and value are identical
- }
-
-
-
- /**
- * checks if Expected Response and Response are same
+ * checks if Expected and the received Response bodies are identical
*/
- private String getStringFromStream(Reader input){
- String result = null;
-
-
+ private boolean compareStreams(InputStream receivedBody, InputStream
expectedBody){
+ String contentValueProblem = null;
+ byte[] receivedBuffer = new byte[1];
+ byte[] expectedBuffer = new byte[1];
+ int receivedCount = 0;
+ int expectedCount = 0;
+ int bytesRead = 0;
try {
- StringWriter output = new StringWriter();
- char[] buffer = new char[1024];
- int numberOfBytesRead;
-
- while ((numberOfBytesRead=input.read(buffer)) > 0) {
- output.write (buffer, 0, numberOfBytesRead);
+ receivedCount=receivedBody.read(receivedBuffer);
+ expectedCount=expectedBody.read(expectedBuffer);
+ while ((receivedCount > 0) && (expectedCount > 0)) {
+ bytesRead ++;
+
+if (extendedPrintlnsrequested) System.out.println(getStreamCharByte(bytesRead,
expectedBuffer, receivedBuffer) );
+
+ if (receivedBuffer[0] != expectedBuffer[0]) {
+ if (contentValueProblem == null) {
+ contentValueProblem = "Position = " + bytesRead;
+ } else {
+ contentValueProblem += " ," + bytesRead;
+ }
+ }
+ receivedCount=receivedBody.read(receivedBuffer);
+ expectedCount=expectedBody.read(expectedBuffer);
}
-
- result = output.toString();
- input.close();
- output.close();
-
}
- catch (Exception e) {
+ catch (IOException e) {
e.printStackTrace();
xmlresult.writeException(e);
}
- return result;
+if (extendedPrintlnsrequested && expectedCount > 0) {
+ System.out.println(" Expected ");
+ printPostfixStreamCharByte(bytesRead, expectedBuffer, expectedBody);
+}
+if (extendedPrintlnsrequested && receivedCount > 0) {
+ System.out.println(" Received ");
+ printPostfixStreamCharByte(bytesRead, receivedBuffer, receivedBody);
+}
+
+ if (receivedCount != expectedCount) {
+ xmlresult.writeElement("ContentLengthError", "Position = " + bytesRead
);
+ }
+ if (contentValueProblem != null) {
+ xmlresult.writeElement("ContentValueError", contentValueProblem);
+ }
+
+ // length and value are identical
+ return (receivedCount == expectedCount) && (contentValueProblem == null);
}
+//////////////////////////////////////////////
+// some debugging printlns and methods //
+//////////////////////////////////////////////
+
+
+ protected static final String[] hexadecimal =
+ {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
+ "A", "B", "C", "D", "E", "F"};
+
+
+ private static String convertHexDigit( byte toEncode ) {
+ String result;
+ int low = (int) (toEncode & 0x0f);
+ int high = (int) ((toEncode & 0xf0) >> 4);
+ result = hexadecimal[high] + hexadecimal[low];
+ return result;
+ }
+
+
/**
- * checks if Expected Response and Response are same
+ * get a byte as decimal and (try) as character
+ */
+ private String getStreamCharByte(byte[] input){
+ return " " + new String(input) + " [" + convertHexDigit(input[0]) + "]";
+ }
+
+
+ /**
+ * get a byte as decimal and (try) as character with prefix
+ */
+ private String getStreamCharByte(int counter, byte[] input){
+ return getPrefixStreamCharByte(counter) + getStreamCharByte(input);
+ }
+
+
+
+ /**
+ * get a byte as decimal and (try) as character with prefix
+ */
+ private String getStreamCharByte(int counter, byte[] exp, byte[] rec){
+ if (exp[0] == rec[0]) return getPrefixStreamCharByte(counter) +
+ getStreamCharByte(exp);
+ else return getPrefixStreamCharByte(counter) +
+ getStreamCharByte(exp) +
+ " " + getStreamCharByte(rec);
+ }
+
+
+ /**
+ * get a prefix string for printing
*/
- private int getFirstDifferentPosition(String receivedBody, String expectedBody){
- int result = 1;
- while (result <= receivedBody.length()) {
- if (receivedBody.charAt(result) != expectedBody.charAt(result)) return
result;
- result++;
+ private String getPrefixStreamCharByte(int position){
+ return " " + position + ": \t" ;
+ }
+
+
+
+ /**
+ * Print the final stream content a prefix string for printing
+ */
+ private void printPostfixStreamCharByte(int counter, byte[] buffer, InputStream
input){
+ try {
+ System.out.println(getStreamCharByte(++counter, buffer));
+ int bytesRead;
+ while (((bytesRead=input.read(buffer)) > 0) ) {
+ System.out.println(getStreamCharByte(++counter, buffer));
+ }
}
- return result;
+ catch (IOException e) { e.printStackTrace();}
}
1.2 +3 -3
jakarta-slide/testsuite/testsuite/junit/src/org/apache/slide/testsuite/testtools/tprocessor/GetAssert.java
Index: GetAssert.java
===================================================================
RCS file:
/home/cvs/jakarta-slide/testsuite/testsuite/junit/src/org/apache/slide/testsuite/testtools/tprocessor/GetAssert.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- GetAssert.java 20 Nov 2001 14:35:31 -0000 1.1
+++ GetAssert.java 25 Feb 2002 09:54:58 -0000 1.2
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-slide/testsuite/testsuite/junit/src/org/apache/slide/testsuite/testtools/tprocessor/GetAssert.java,v
1.1 2001/11/20 14:35:31 juergen Exp $
- * $Revision: 1.1 $
- * $Date: 2001/11/20 14:35:31 $
+ * $Header:
/home/cvs/jakarta-slide/testsuite/testsuite/junit/src/org/apache/slide/testsuite/testtools/tprocessor/GetAssert.java,v
1.2 2002/02/25 09:54:58 juergen Exp $
+ * $Revision: 1.2 $
+ * $Date: 2002/02/25 09:54:58 $
*
* ====================================================================
*
@@ -78,7 +78,7 @@
* Perform the necessary checks for the received and expected Get body
*
* @author Software AG
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
*/
public class GetAssert extends StreamResponseBodyAssert {
@@ -86,7 +86,7 @@
/** constructer
* @param WebdavMethodBase Element
*/
- public GetAssert(HttpMethod method, Reader expectedBody, XConf xconf, XMLOutput
xmlresult){
+ public GetAssert(HttpMethod method, InputStream expectedBody, XConf xconf,
XMLOutput xmlresult){
super(method, expectedBody, xconf, xmlresult);
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>