You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ro...@apache.org on 2007/08/21 12:53:05 UTC
svn commit: r568056 - in
/jakarta/httpcomponents/httpcore/trunk/module-main/src:
main/java/org/apache/http/message/ test/java/org/apache/http/message/
Author: rolandw
Date: Tue Aug 21 03:53:00 2007
New Revision: 568056
URL: http://svn.apache.org/viewvc?rev=568056&view=rev
Log:
removed static parser code from BasicHttpVersionFormat, adjusted test cases
Modified:
jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHttpVersionFormat.java
jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicLineParser.java
jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHttpVersion.java
Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHttpVersionFormat.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHttpVersionFormat.java?rev=568056&r1=568055&r2=568056&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHttpVersionFormat.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHttpVersionFormat.java Tue Aug 21 03:53:00 2007
@@ -50,77 +50,9 @@
*/
public class BasicHttpVersionFormat {
- /**
- * Parses the textual representation of the given HTTP protocol version.
- *
- * @return HTTP protocol version.
- *
- * @throws ProtocolException if the string is not a valid HTTP protocol version.
- */
- public static HttpVersion parse(
- final CharArrayBuffer buffer, final int indexFrom, final int indexTo)
- throws ProtocolException {
- if (buffer == null) {
- throw new IllegalArgumentException("Char array buffer may not be null");
- }
- if (indexFrom < 0) {
- throw new IndexOutOfBoundsException();
- }
- if (indexTo > buffer.length()) {
- throw new IndexOutOfBoundsException();
- }
- if (indexFrom > indexTo) {
- throw new IndexOutOfBoundsException();
- }
- try {
- int major, minor;
-
- int i = indexFrom;
- while (HTTP.isWhitespace(buffer.charAt(i))) {
- i++;
- }
- if (buffer.charAt(i ) != 'H'
- || buffer.charAt(i + 1) != 'T'
- || buffer.charAt(i + 2) != 'T'
- || buffer.charAt(i + 3) != 'P'
- || buffer.charAt(i + 4) != '/') {
- throw new ProtocolException("Not a valid HTTP version string: " +
- buffer.substring(indexFrom, indexTo));
- }
- i += 5;
- int period = buffer.indexOf('.', i, indexTo);
- if (period == -1) {
- throw new ProtocolException("Invalid HTTP version number: " +
- buffer.substring(indexFrom, indexTo));
- }
- try {
- major = Integer.parseInt(buffer.substringTrimmed(i, period));
- } catch (NumberFormatException e) {
- throw new ProtocolException("Invalid HTTP major version number: " +
- buffer.substring(indexFrom, indexTo));
- }
- try {
- minor = Integer.parseInt(buffer.substringTrimmed(period + 1, indexTo));
- } catch (NumberFormatException e) {
- throw new ProtocolException("Invalid HTTP minor version number: " +
- buffer.substring(indexFrom, indexTo));
- }
- return new HttpVersion(major, minor);
-
- } catch (IndexOutOfBoundsException e) {
- throw new ProtocolException("Invalid HTTP version string: " +
- buffer.substring(indexFrom, indexTo));
- }
- }
-
- public static final HttpVersion parse(final String s)
- throws ProtocolException {
- if (s == null) {
- throw new IllegalArgumentException("String may not be null");
- }
- CharArrayBuffer buffer = new CharArrayBuffer(s.length());
- buffer.append(s);
- return parse(buffer, 0, buffer.length());
+ /** Disabled default constructor. */
+ private BasicHttpVersionFormat() {
+ // no body
}
public static void format(final CharArrayBuffer buffer, final HttpVersion ver) {
Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicLineParser.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicLineParser.java?rev=568056&r1=568055&r2=568056&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicLineParser.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicLineParser.java Tue Aug 21 03:53:00 2007
@@ -32,6 +32,7 @@
import org.apache.http.HttpException;
import org.apache.http.HttpVersion;
+import org.apache.http.ProtocolException;
import org.apache.http.RequestLine;
import org.apache.http.StatusLine;
import org.apache.http.message.BasicRequestLine;
@@ -274,6 +275,137 @@
}
try {
BasicLineParser.DEFAULT.parseStatusLine(buffer, 2, 1);
+ fail("IllegalArgumentException should have been thrown");
+ } catch (IndexOutOfBoundsException ex) {
+ // expected
+ }
+ }
+
+
+
+ public void testHttpVersionParsing() throws Exception {
+ new HttpVersion(1, 1);
+ String s = "HTTP/1.1";
+ HttpVersion version = BasicLineParser.parseProtocolVersion(s, null);
+ assertEquals("HTTP major version number", 1, version.getMajor());
+ assertEquals("HTTP minor version number", 1, version.getMinor());
+ assertEquals("HTTP version number", s, version.toString());
+
+ s = "HTTP/123.4567";
+ version = BasicLineParser.parseProtocolVersion(s, null);
+ assertEquals("HTTP major version number", 123, version.getMajor());
+ assertEquals("HTTP minor version number", 4567, version.getMinor());
+ assertEquals("HTTP version number", s, version.toString());
+ }
+
+ public void testInvalidHttpVersionParsing() throws Exception {
+ try {
+ BasicLineParser.parseProtocolVersion(null, null);
+ fail("IllegalArgumentException should have been thrown");
+ } catch (IllegalArgumentException e) {
+ //expected
+ }
+ try {
+ BasicLineParser.parseProtocolVersion
+ (" ", null);
+ fail("ProtocolException should have been thrown");
+ } catch (ProtocolException e) {
+ //expected
+ }
+ try {
+ BasicLineParser.parseProtocolVersion
+ ("HTT", null);
+ fail("ProtocolException should have been thrown");
+ } catch (ProtocolException e) {
+ //expected
+ }
+ try {
+ BasicLineParser.parseProtocolVersion
+ ("crap", null);
+ fail("ProtocolException should have been thrown");
+ } catch (ProtocolException e) {
+ //expected
+ }
+ try {
+ BasicLineParser.parseProtocolVersion
+ ("HTTP/crap", null);
+ fail("ProtocolException should have been thrown");
+ } catch (ProtocolException e) {
+ //expected
+ }
+ try {
+ BasicLineParser.parseProtocolVersion
+ ("HTTP/1", null);
+ fail("ProtocolException should have been thrown");
+ } catch (ProtocolException e) {
+ //expected
+ }
+ try {
+ BasicLineParser.parseProtocolVersion
+ ("HTTP/1234 ", null);
+ fail("ProtocolException should have been thrown");
+ } catch (ProtocolException e) {
+ //expected
+ }
+ try {
+ BasicLineParser.parseProtocolVersion
+ ("HTTP/1.", null);
+ fail("ProtocolException should have been thrown");
+ } catch (ProtocolException e) {
+ //expected
+ }
+ try {
+ BasicLineParser.parseProtocolVersion
+ ("HTTP/1.1 crap", null);
+ fail("ProtocolException should have been thrown");
+ } catch (ProtocolException e) {
+ //expected
+ }
+ try {
+ BasicLineParser.parseProtocolVersion
+ ("HTTP/whatever.whatever whatever", null);
+ fail("ProtocolException should have been thrown");
+ } catch (ProtocolException e) {
+ //expected
+ }
+ try {
+ BasicLineParser.parseProtocolVersion
+ ("HTTP/1.whatever whatever", null);
+ fail("ProtocolException should have been thrown");
+ } catch (ProtocolException e) {
+ //expected
+ }
+ }
+
+ public void testHttpVersionParsingInvalidInput() throws Exception {
+ CharArrayBuffer buffer = new CharArrayBuffer(32);
+ buffer.append("HTTP/1.1");
+ try {
+ BasicLineParser.parseProtocolVersion(null, null);
+ fail("IllegalArgumentException should have been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ try {
+ BasicLineParser.DEFAULT.parseProtocolVersion(null, 0, 0);
+ fail("IllegalArgumentException should have been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ try {
+ BasicLineParser.DEFAULT.parseProtocolVersion(buffer, -1, 0);
+ fail("IllegalArgumentException should have been thrown");
+ } catch (IndexOutOfBoundsException ex) {
+ // expected
+ }
+ try {
+ BasicLineParser.DEFAULT.parseProtocolVersion(buffer, 0, 1000);
+ fail("IllegalArgumentException should have been thrown");
+ } catch (IndexOutOfBoundsException ex) {
+ // expected
+ }
+ try {
+ BasicLineParser.DEFAULT.parseProtocolVersion(buffer, 2, 1);
fail("IllegalArgumentException should have been thrown");
} catch (IndexOutOfBoundsException ex) {
// expected
Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHttpVersion.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHttpVersion.java?rev=568056&r1=568055&r2=568056&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHttpVersion.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHttpVersion.java Tue Aug 21 03:53:00 2007
@@ -59,125 +59,6 @@
}
// ------------------------------------------------------------------ Tests
-
- public void testHttpVersionParsing() throws Exception {
- new HttpVersion(1, 1);
- String s = "HTTP/1.1";
- HttpVersion version = BasicHttpVersionFormat.parse(s);
- assertEquals("HTTP major version number", 1, version.getMajor());
- assertEquals("HTTP minor version number", 1, version.getMinor());
- assertEquals("HTTP version number", s, version.toString());
-
- s = "HTTP/123.4567";
- version = BasicHttpVersionFormat.parse(s);
- assertEquals("HTTP major version number", 123, version.getMajor());
- assertEquals("HTTP minor version number", 4567, version.getMinor());
- assertEquals("HTTP version number", s, version.toString());
- }
-
- public void testInvalidHttpVersionParsing() throws Exception {
- try {
- BasicHttpVersionFormat.parse(null);
- fail("IllegalArgumentException should have been thrown");
- } catch (IllegalArgumentException e) {
- //expected
- }
- try {
- BasicHttpVersionFormat.parse(" ");
- fail("ProtocolException should have been thrown");
- } catch (ProtocolException e) {
- //expected
- }
- try {
- BasicHttpVersionFormat.parse("HTT");
- fail("ProtocolException should have been thrown");
- } catch (ProtocolException e) {
- //expected
- }
- try {
- BasicHttpVersionFormat.parse("crap");
- fail("ProtocolException should have been thrown");
- } catch (ProtocolException e) {
- //expected
- }
- try {
- BasicHttpVersionFormat.parse("HTTP/crap");
- fail("ProtocolException should have been thrown");
- } catch (ProtocolException e) {
- //expected
- }
- try {
- BasicHttpVersionFormat.parse("HTTP/1");
- fail("ProtocolException should have been thrown");
- } catch (ProtocolException e) {
- //expected
- }
- try {
- BasicHttpVersionFormat.parse("HTTP/1234 ");
- fail("ProtocolException should have been thrown");
- } catch (ProtocolException e) {
- //expected
- }
- try {
- BasicHttpVersionFormat.parse("HTTP/1.");
- fail("ProtocolException should have been thrown");
- } catch (ProtocolException e) {
- //expected
- }
- try {
- BasicHttpVersionFormat.parse("HTTP/1.1 crap");
- fail("ProtocolException should have been thrown");
- } catch (ProtocolException e) {
- //expected
- }
- try {
- BasicHttpVersionFormat.parse("HTTP/whatever.whatever whatever");
- fail("ProtocolException should have been thrown");
- } catch (ProtocolException e) {
- //expected
- }
- try {
- BasicHttpVersionFormat.parse("HTTP/1.whatever whatever");
- fail("ProtocolException should have been thrown");
- } catch (ProtocolException e) {
- //expected
- }
- }
-
- public void testHttpVersionParsingInvalidInput() throws Exception {
- CharArrayBuffer buffer = new CharArrayBuffer(32);
- buffer.append("HTTP/1.1");
- try {
- BasicHttpVersionFormat.parse(null, 0, 0);
- fail("IllegalArgumentException should have been thrown");
- } catch (IllegalArgumentException ex) {
- // expected
- }
- try {
- BasicHttpVersionFormat.parse(null);
- fail("IllegalArgumentException should have been thrown");
- } catch (IllegalArgumentException ex) {
- // expected
- }
- try {
- BasicHttpVersionFormat.parse(buffer, -1, 0);
- fail("IllegalArgumentException should have been thrown");
- } catch (IndexOutOfBoundsException ex) {
- // expected
- }
- try {
- BasicHttpVersionFormat.parse(buffer, 0, 1000);
- fail("IllegalArgumentException should have been thrown");
- } catch (IndexOutOfBoundsException ex) {
- // expected
- }
- try {
- BasicHttpVersionFormat.parse(buffer, 2, 1);
- fail("IllegalArgumentException should have been thrown");
- } catch (IndexOutOfBoundsException ex) {
- // expected
- }
- }
public void testHttpVersionFormatting() throws Exception {
String s = BasicHttpVersionFormat.format(HttpVersion.HTTP_1_1);