From 310120d9a57a3ea5e2262e91f4d808833e965164 Mon Sep 17 00:00:00 2001 From: costin Date: Thu, 25 Jun 2009 15:16:44 +0000 Subject: [PATCH] Adjusting the tests git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@788385 13f79535-47bb-0310-9956-ffa450edef68 --- .../test/org/apache/tomcat/lite/HexDump.java | 252 +++++++++++++++++++++ .../org/apache/tomcat/lite/LiteTestHelper.java | 59 ++--- .../tomcat/lite/LiteWatchdogServletTests.java | 6 +- .../org/apache/tomcat/lite/PropertiesSpiTest.java | 6 +- .../tomcat/lite/TomcatLiteNoConnectorTest.java | 82 ------- .../apache/tomcat/lite/TomcatLiteSimpleTest.java | 8 +- .../org/apache/tomcat/test/watchdog/GTest.java | 11 +- .../tomcat/test/watchdog/WatchdogHttpClient.java | 4 +- 8 files changed, 292 insertions(+), 136 deletions(-) create mode 100644 modules/tomcat-lite/test/org/apache/tomcat/lite/HexDump.java delete mode 100644 modules/tomcat-lite/test/org/apache/tomcat/lite/TomcatLiteNoConnectorTest.java diff --git a/modules/tomcat-lite/test/org/apache/tomcat/lite/HexDump.java b/modules/tomcat-lite/test/org/apache/tomcat/lite/HexDump.java new file mode 100644 index 000000000..ab9ece33c --- /dev/null +++ b/modules/tomcat-lite/test/org/apache/tomcat/lite/HexDump.java @@ -0,0 +1,252 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.tomcat.lite; + +import java.io.ByteArrayOutputStream; + +import org.apache.tomcat.util.res.StringManager; + +/** + * Tables useful when converting byte arrays to and from strings of hexadecimal + * digits. + * Code from Ajp11, from Apache's JServ. + * + * @author Craig R. McClanahan + */ + +public final class HexDump { + + + // -------------------------------------------------------------- Constants + static StringManager sm; + + /** + * Table for HEX to DEC byte translation. + */ + public static final int[] DEC = { + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 00, 01, 02, 03, 04, 05, 06, 07, 8, 9, -1, -1, -1, -1, -1, -1, + -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + }; + + + /** + * Table for DEC to HEX byte translation. + */ + public static final byte[] HEX = + { (byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4', (byte) '5', + (byte) '6', (byte) '7', (byte) '8', (byte) '9', (byte) 'a', (byte) 'b', + (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f' }; + + + // --------------------------------------------------------- Static Methods + + + /** + * Convert a String of hexadecimal digits into the corresponding + * byte array by encoding each two hexadecimal digits as a byte. + * + * @param digits Hexadecimal digits representation + * + * @exception IllegalArgumentException if an invalid hexadecimal digit + * is found, or the input string contains an odd number of hexadecimal + * digits + */ + public static byte[] convert(String digits) { + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + for (int i = 0; i < digits.length(); i += 2) { + char c1 = digits.charAt(i); + if ((i+1) >= digits.length()) + throw new IllegalArgumentException + (sm.getString("hexUtil.odd")); + char c2 = digits.charAt(i + 1); + byte b = 0; + if ((c1 >= '0') && (c1 <= '9')) + b += ((c1 - '0') * 16); + else if ((c1 >= 'a') && (c1 <= 'f')) + b += ((c1 - 'a' + 10) * 16); + else if ((c1 >= 'A') && (c1 <= 'F')) + b += ((c1 - 'A' + 10) * 16); + else + throw new IllegalArgumentException + (sm.getString("hexUtil.bad")); + if ((c2 >= '0') && (c2 <= '9')) + b += (c2 - '0'); + else if ((c2 >= 'a') && (c2 <= 'f')) + b += (c2 - 'a' + 10); + else if ((c2 >= 'A') && (c2 <= 'F')) + b += (c2 - 'A' + 10); + else + throw new IllegalArgumentException + (sm.getString("hexUtil.bad")); + baos.write(b); + } + return (baos.toByteArray()); + + } + + + /** + * Convert a byte array into a printable format containing a + * String of hexadecimal digit characters (two per byte). + * + * @param bytes Byte array representation + */ + public static String convert(byte bytes[]) { + + StringBuffer sb = new StringBuffer(bytes.length * 2); + for (int i = 0; i < bytes.length; i++) { + sb.append(convertDigit((bytes[i] >> 4))); + sb.append(convertDigit((bytes[i] & 0x0f))); + } + return (sb.toString()); + + } + + + /** + * Convert 4 hex digits to an int, and return the number of converted + * bytes. + * + * @param hex Byte array containing exactly four hexadecimal digits + * + * @exception IllegalArgumentException if an invalid hexadecimal digit + * is included + */ + public static int convert2Int( byte[] hex ) { + // Code from Ajp11, from Apache's JServ + + // assert b.length==4 + // assert valid data + int len; + if(hex.length < 4 ) return 0; + if( DEC[hex[0]]<0 ) + throw new IllegalArgumentException(sm.getString("hexUtil.bad")); + len = DEC[hex[0]]; + len = len << 4; + if( DEC[hex[1]]<0 ) + throw new IllegalArgumentException(sm.getString("hexUtil.bad")); + len += DEC[hex[1]]; + len = len << 4; + if( DEC[hex[2]]<0 ) + throw new IllegalArgumentException(sm.getString("hexUtil.bad")); + len += DEC[hex[2]]; + len = len << 4; + if( DEC[hex[3]]<0 ) + throw new IllegalArgumentException(sm.getString("hexUtil.bad")); + len += DEC[hex[3]]; + return len; + } + + + + /** + * Provide a mechanism for ensuring this class is loaded. + */ + public static void load() { + // Nothing to do + } + + /** + * [Private] Convert the specified value (0 .. 15) to the corresponding + * hexadecimal digit. + * + * @param value Value to be converted + */ + private static char convertDigit(int value) { + + value &= 0x0f; + if (value >= 10) + return ((char) (value - 10 + 'a')); + else + return ((char) (value + '0')); + + } + + /** + * getHexValue displays a formatted hex + * representation of the passed byte array. It also + * allows for only a specified offset and length of + * a particular array to be returned. + * + * @param bytes byte[] array to process. + * @param pos offset to begin processing. + * @param len number of bytes to process. + * @return String formatted hex representation of processed + * array. + */ + public static String getHexDump(byte[] bytes, int pos, int len, + boolean displayOffset) { + StringBuffer out = new StringBuffer( len * 2 ); + + for (int j = 0; j < len; j += 16) { + hexLine(out, bytes, pos + j, pos + len, displayOffset); + } + + return out.toString(); + } + + private static void hexLine(StringBuffer out, + byte[] bytes, int start, int end, + boolean displayOffset) { + + if ( displayOffset ) { + out.append(convertDigit((int) (start >> 12))); + out.append(convertDigit((int) (start >> 8))); + out.append(convertDigit((int) (start >> 4))); + out.append(convertDigit(start & 0x0F)); + out.append(": "); + } + for (int i = start; i < start + 16; i++) { + + if (i < end) { + out.append(convertDigit((int) (bytes[i] >> 4))); + out.append(convertDigit(bytes[i] & 0x0F)); + out.append(" "); + } else { + out.append(" "); + } + } + + out.append(" | "); + + for (int i = start; i < start + 16 && i < end; i++) { + if( ! Character.isISOControl( (char)bytes[i] )) { + out.append( new Character((char)bytes[i]) ); + } else { + out.append( "." ); + } + } + + out.append("\n"); + } +} diff --git a/modules/tomcat-lite/test/org/apache/tomcat/lite/LiteTestHelper.java b/modules/tomcat-lite/test/org/apache/tomcat/lite/LiteTestHelper.java index 66477dfcb..ddb9b30a6 100644 --- a/modules/tomcat-lite/test/org/apache/tomcat/lite/LiteTestHelper.java +++ b/modules/tomcat-lite/test/org/apache/tomcat/lite/LiteTestHelper.java @@ -15,26 +15,29 @@ import javax.servlet.http.HttpServletResponse; import org.apache.coyote.OutputBuffer; import org.apache.coyote.Response; -import org.apache.tomcat.lite.coyote.CoyoteHttp; +import org.apache.tomcat.lite.coyote.CoyoteConnector; import org.apache.tomcat.util.buf.ByteChunk; public class LiteTestHelper { - public static void initServletsAndRun(TomcatLite lite, int port) throws ServletException, IOException { + public static void addContext(TomcatLite lite) throws ServletException { ServletContextImpl ctx = - (ServletContextImpl) lite.addServletContext(null, null, "/test1"); - - ctx.addServlet("test", new SimpleServlet()); - ctx.addMapping("/1stTest", "test"); + (ServletContextImpl) lite.addServletContext(null, null, "/test1"); + + ctx.addServlet("test", new SimpleServlet()); + ctx.addMapping("/1stTest", "test"); - ctx.addServlet("testException", new HttpServlet() { - public void doGet(HttpServletRequest req, HttpServletResponse res) - throws IOException { - throw new NullPointerException(); - } - }); - ctx.addMapping("/testException", "testException"); - + ctx.addServlet("testException", new HttpServlet() { + public void doGet(HttpServletRequest req, HttpServletResponse res) + throws IOException { + throw new NullPointerException(); + } + }); + ctx.addMapping("/testException", "testException"); + } + + public static void initServletsAndRun(TomcatLite lite, int port) throws ServletException, IOException { + addContext(lite); lite.init(); lite.start(); @@ -54,9 +57,9 @@ public class LiteTestHelper { public static void addConnector(TomcatLite lite, int port, boolean daemon) { - CoyoteHttp coyoteAdapter = (CoyoteHttp) lite.getConnector(); - coyoteAdapter.getConnectors().setPort(port); - coyoteAdapter.getConnectors().setDaemon(daemon); + CoyoteConnector coyoteAdapter = (CoyoteConnector) lite.getConnector(); + coyoteAdapter.setPort(port); + coyoteAdapter.setDaemon(daemon); } /** @@ -79,29 +82,7 @@ public class LiteTestHelper { } return out; } - - /** - * Create a ServletRequestImpl object that can be used with - * TomcatLite.service(request). - * - * All output will be added to the ByteChunk out. - * - * This requires no HTTP connector. - * - * @see TomcatLiteNoConnector - */ - public static ServletRequestImpl createMessage(TomcatLite lite, - String uri, - final ByteChunk out) { - ServletRequestImpl req = lite.createMessage(); - req.setRequestURI(uri); - ServletResponseImpl res = req.getResponse(); - res.getCoyoteResponse().setOutputBuffer( - new ByteChunkOutputBuffer(out)); - return req; - } - static class ByteChunkOutputBuffer implements OutputBuffer { protected ByteChunk output = null; diff --git a/modules/tomcat-lite/test/org/apache/tomcat/lite/LiteWatchdogServletTests.java b/modules/tomcat-lite/test/org/apache/tomcat/lite/LiteWatchdogServletTests.java index a7b7f6468..ebfd7e533 100644 --- a/modules/tomcat-lite/test/org/apache/tomcat/lite/LiteWatchdogServletTests.java +++ b/modules/tomcat-lite/test/org/apache/tomcat/lite/LiteWatchdogServletTests.java @@ -61,6 +61,10 @@ public class LiteWatchdogServletTests extends WatchdogClient { } } + protected void addConnector(TomcatLite liteServer) { + LiteTestHelper.addConnector(liteServer, 8080, true); + } + public void initServerWithWatchdog(String wdDir) throws ServletException, IOException { @@ -70,7 +74,7 @@ public class LiteWatchdogServletTests extends WatchdogClient { //connector.addAdapter("/", new MapperAdapter()); TomcatLite liteServer = new TomcatLite(); - LiteTestHelper.addConnector(liteServer, 8080, true); + addConnector(liteServer); liteServer.init("webapps/ROOT", "/"); for (String s : new String[] { diff --git a/modules/tomcat-lite/test/org/apache/tomcat/lite/PropertiesSpiTest.java b/modules/tomcat-lite/test/org/apache/tomcat/lite/PropertiesSpiTest.java index 53a8ad5b3..f9e9297f2 100644 --- a/modules/tomcat-lite/test/org/apache/tomcat/lite/PropertiesSpiTest.java +++ b/modules/tomcat-lite/test/org/apache/tomcat/lite/PropertiesSpiTest.java @@ -23,9 +23,9 @@ public class PropertiesSpiTest extends TestCase { } public void testArgs() throws IOException { - Properties res = new Properties(); - SimpleObjectManager.processArgs(new String[] { - "-a=1", "-b", "2"}, res); + spi = new SimpleObjectManager(new String[] { + "-a=1", "-b", "2"}); + Properties res = spi.getProperties(); assertEquals("1", res.get("a")); assertEquals("2", res.get("b")); diff --git a/modules/tomcat-lite/test/org/apache/tomcat/lite/TomcatLiteNoConnectorTest.java b/modules/tomcat-lite/test/org/apache/tomcat/lite/TomcatLiteNoConnectorTest.java deleted file mode 100644 index 30d944fb0..000000000 --- a/modules/tomcat-lite/test/org/apache/tomcat/lite/TomcatLiteNoConnectorTest.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.tomcat.lite; - - - -import java.io.IOException; - -import junit.framework.TestCase; - -import org.apache.coyote.OutputBuffer; -import org.apache.coyote.Response; -import org.apache.tomcat.util.buf.ByteChunk; - -public class TomcatLiteNoConnectorTest extends TestCase { - - TomcatLite lite = new TomcatLite(); - - public void setUp() throws Exception { - LiteTestHelper.initServletsAndRun(lite, 0); - } - - public void tearDown() throws Exception { - lite.stop(); - } - - - - public void testSimpleRequest() throws Exception { - ByteChunk out = new ByteChunk(); - - ServletRequestImpl req = - LiteTestHelper.createMessage(lite, "/test1/1stTest", out); - - // more changes can be made to the req, populate fields that a - // connector would - - ServletResponseImpl res = lite.service(req); - - assertEquals("Hello world", out.toString()); - // Headers are still in the response - assertEquals(res.getHeader("Foo"), "Bar"); - assertEquals(res.getStatus(), 200); - } - - public void testPostRequest() throws Exception { - ByteChunk out = new ByteChunk(); - ServletRequestImpl req = - LiteTestHelper.createMessage(lite, "/test1/1stTest", out); - req.setMethod("POST"); - - ServletResponseImpl res = lite.service(req); - - assertEquals("Hello post world", out.toString()); - // Headers are still in the response - assertEquals(res.getHeader("Foo"), "Post"); - assertEquals(res.getStatus(), 200); - } - - public void testException() throws IOException, Exception { - ByteChunk out = new ByteChunk(); - ServletRequestImpl req = - LiteTestHelper.createMessage(lite, "/test1/testException", out); - ServletResponseImpl res = lite.service(req); - assertEquals(res.getStatus(), 500); - } - -} diff --git a/modules/tomcat-lite/test/org/apache/tomcat/lite/TomcatLiteSimpleTest.java b/modules/tomcat-lite/test/org/apache/tomcat/lite/TomcatLiteSimpleTest.java index 3b0b53446..4953eaf16 100644 --- a/modules/tomcat-lite/test/org/apache/tomcat/lite/TomcatLiteSimpleTest.java +++ b/modules/tomcat-lite/test/org/apache/tomcat/lite/TomcatLiteSimpleTest.java @@ -22,14 +22,10 @@ import org.apache.tomcat.util.buf.ByteChunk; public class TomcatLiteSimpleTest extends TestCase { - TomcatLite lite = new TomcatLite(); - - void initServer() throws Exception { - LiteTestHelper.initServletsAndRun(lite, 8804); - } + protected TomcatLite lite = new TomcatLite(); public void setUp() throws Exception { - initServer(); + LiteTestHelper.initServletsAndRun(lite, 8804); } public void tearDown() throws Exception { diff --git a/modules/tomcat-lite/test/org/apache/tomcat/test/watchdog/GTest.java b/modules/tomcat-lite/test/org/apache/tomcat/test/watchdog/GTest.java index b809d7c3b..d799d019b 100644 --- a/modules/tomcat-lite/test/org/apache/tomcat/test/watchdog/GTest.java +++ b/modules/tomcat-lite/test/org/apache/tomcat/test/watchdog/GTest.java @@ -35,7 +35,8 @@ import java.util.Iterator; import java.util.StringTokenizer; import java.util.Vector; -import org.apache.tomcat.util.buf.HexUtils; +import org.apache.tomcat.lite.HexDump; +import org.apache.tools.ant.BuildException; // derived from Jsp @@ -679,7 +680,9 @@ public class GTest { for ( int i = 0; i < eSize; i++ ) { if ( headerValues.contains( unexpectedValues.get( i ) ) ) { numberFound++; - headerValues.remove( headerValues.indexOf( headerFieldName ) ); + if (headerValues.indexOf(headerFieldName) >= 0) { + headerValues.remove( headerValues.indexOf( headerFieldName ) ); + } } } if ( numberFound == eSize ) { @@ -1044,8 +1047,8 @@ public class GTest { private void dumpHex( byte[] serverResponse, byte[] goldenFile ) { StringBuffer outBuf = new StringBuffer( ( serverResponse.length + goldenFile.length ) * 2 ); - String fromServerString = HexUtils.getHexDump( serverResponse, 0, serverResponse.length, true ); - String fromGoldenFileString = HexUtils.getHexDump( goldenFile, 0, goldenFile.length, true ); + String fromServerString = HexDump.getHexDump( serverResponse, 0, serverResponse.length, true ); + String fromGoldenFileString = HexDump.getHexDump( goldenFile, 0, goldenFile.length, true ); outBuf.append( " Hex dump of server response and goldenfile below.\n\n### RESPONSE FROM SERVER ###\n" ); outBuf.append( "----------------------------\n" ); diff --git a/modules/tomcat-lite/test/org/apache/tomcat/test/watchdog/WatchdogHttpClient.java b/modules/tomcat-lite/test/org/apache/tomcat/test/watchdog/WatchdogHttpClient.java index 0f52a6c37..30cf4ae05 100644 --- a/modules/tomcat-lite/test/org/apache/tomcat/test/watchdog/WatchdogHttpClient.java +++ b/modules/tomcat-lite/test/org/apache/tomcat/test/watchdog/WatchdogHttpClient.java @@ -46,7 +46,7 @@ public class WatchdogHttpClient { // XXX headers are ignored Socket socket = new Socket( host, port ); - + //socket obtained, rebuild the request. rebuildRequest(client, client.request, socket); @@ -249,6 +249,8 @@ public class WatchdogHttpClient { System.out.println( "Partial read: " + sb.toString() ); ex.printStackTrace(); } + input.close(); + break; } } return sb.toString(); -- 2.11.0