From 5f3869c451e46aadc943d00087d6847877dd1c50 Mon Sep 17 00:00:00 2001 From: lresende Date: Wed, 19 Nov 2008 05:27:58 +0000 Subject: Merging the 1.x delta on top of the equinox based modules git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@718858 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/binding/http/HTTPBindingCacheTestCase.java | 1647 ++++++++++---------- .../sca/binding/http/TestBindingCacheImpl.java | 10 +- 2 files changed, 821 insertions(+), 836 deletions(-) (limited to 'java/sca/modules/binding-http-runtime/src/test') diff --git a/java/sca/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HTTPBindingCacheTestCase.java b/java/sca/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HTTPBindingCacheTestCase.java index 50022e9663..bb5f6d5a8b 100644 --- a/java/sca/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HTTPBindingCacheTestCase.java +++ b/java/sca/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HTTPBindingCacheTestCase.java @@ -29,10 +29,7 @@ import java.util.Date; import junit.framework.TestCase; -import org.apache.tuscany.sca.node.Contribution; -import org.apache.tuscany.sca.node.ContributionLocationHelper; -import org.apache.tuscany.sca.node.Node; -import org.apache.tuscany.sca.node.NodeFactory; +import org.apache.tuscany.sca.host.embedded.SCADomain; /** * HTTP binding unit tests. @@ -40,831 +37,819 @@ import org.apache.tuscany.sca.node.NodeFactory; * @version $Rev$ $Date$ */ public class HTTPBindingCacheTestCase extends TestCase { - // RFC 822 date time - protected static final SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z"); - - // Request with no predicates in header. - private static final String REQUEST1 = "{0} /httpbinding/{1} HTTP/1.0\n" + "Host: localhost\n" + "Content-Type: text/xml\n" + "Connection: close\n" + "Content-Length: {2}" + "\n\n{3}"; - - // Request with predicates in header - private static final String REQUEST2 = "{0} /httpbinding/{1} HTTP/1.0\n" + "Host: localhost\n" + "Content-Type: text/xml\n" + "{2}: {3}\n" // predicate - // ( - // If - // - - // Match - // , - // If - // - - // None - // - - // Match - // , - // If - // - - // Modified - // - - // Since - // , - // If - // - - // NotModified - // - - // Since - // ) - // : - // value - // ( - // date - // or - // ETag - // ) - + "Connection: close\n" - + "Content-Length: {4}" - + "\n\n{5}"; - - private static final int HTTP_PORT = 8085; - - private Node node; - - @Override - protected void setUp() throws Exception { - String contribution = ContributionLocationHelper.getContributionLocation(HTTPBindingCacheTestCase.class); - node = NodeFactory.newInstance().createNode("testCache.composite", new Contribution("test", contribution)); - node.start(); - } - - @Override - protected void tearDown() throws Exception { - node.stop(); - node.destroy(); - } - - /** - * Test invoking a POJO get method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testGet() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST1, "GET", index, content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - assertTrue(document.indexOf("

item=" + index) != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalGetIfModifiedNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "GET", index, "If-Modified-Since", dateFormat.format(new Date(0)), content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - assertTrue(document.indexOf("

item=" + index) != -1); - // Should return code 304 Not Modified. - // assertTrue(document.indexOf("HTTP/1.1 304") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalGetIfModifiedPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "GET", index, "If-Modified-Since", dateFormat.format(new Date(0)), content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("

item=" + index) != -1); - // Should return code 304 Not Modified. - assertTrue(document.indexOf("HTTP/1.1 304") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalGetIfUnmodifiedNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "GET", index, "If-Unmodified-Since", dateFormat.format(new Date()), content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - assertTrue(document.indexOf("

item=" + index) != -1); - // Should return code 304 Not Modified. - // assertTrue(document.indexOf("HTTP/1.1 304") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalGetIfUnmodifiedPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "GET", index, "If-Unmodified-Since", dateFormat.format(new Date(0)), content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("

item=" + index) != -1); - // Should return code 412 PreconditionFailed. - assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalGetIfMatchNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "GET", index, "If-Match", "eTagXXX", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("

item=" + index) != -1); - // Should return code 412 precondition failed. - assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalGetIfMatchPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "GET", index, "If-Match", "eTagXXX", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - assertTrue(document.indexOf("

item=" + index) != -1); - // Should return code 412 PreconditionFailed. - // assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalGetIfNoneMatchNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "GET", index, "If-None-Match", "eTagXXX", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - assertTrue(document.indexOf("

item=" + index) != -1); - // Should return code 412 precondition failed. - // assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalGetIfNoneMatchPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "GET", index, "If-None-Match", "eTagXXX", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("

item=" + index) != -1); - // Should return code 412 PreconditionFailed. - assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a POJO get method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testDelete() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST1, "DELETE", index, content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - assertTrue(document.indexOf("deleted item=" + index) != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalDeleteIfModifiedNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "DELETE", index, "If-Modified-Since", dateFormat.format(new Date(0)), content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - assertTrue(document.indexOf("deleted item=" + index) != -1); - // Should return code 304 Not Modified. - // assertTrue(document.indexOf("HTTP/1.1 304") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalDeleteIfModifiedPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "DELETE", index, "If-Modified-Since", dateFormat.format(new Date(0)), content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("deleted item=" + index) != -1); - // Should return code 304 Not Modified. - assertTrue(document.indexOf("HTTP/1.1 304") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalDeleteIfUnmodifiedNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "DELETE", index, "If-Unmodified-Since", dateFormat.format(new Date()), content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - assertTrue(document.indexOf("deleted item=" + index) != -1); - // Should return code 304 Not Modified. - // assertTrue(document.indexOf("HTTP/1.1 304") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalDeleteIfUnmodifiedPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "DELETE", index, "If-Unmodified-Since", dateFormat.format(new Date(0)), content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("deleted item=" + index) != -1); - // Should return code 412 PreconditionFailed. - assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalDeleteIfMatchNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "DELETE", index, "If-Match", "eTagXXX", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("deleted item=" + index) != -1); - // Should return code 412 precondition failed. - assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalDeleteIfMatchPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "DELETE", index, "If-Match", "eTagXXX", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - assertTrue(document.indexOf("deleted item=" + index) != -1); - // Should return code 412 PreconditionFailed. - // assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalDeleteIfNoneMatchNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "DELETE", index, "If-None-Match", "eTagXXX", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - assertTrue(document.indexOf("deleted item=" + index) != -1); - // Should return code 412 precondition failed. - // assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalDeleteIfNoneMatchPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "DELETE", index, "If-None-Match", "eTagXXX", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("deleted item=" + index) != -1); - // Should return code 412 PreconditionFailed. - assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a POJO get method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testPost() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST1, "POST", index, content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - assertTrue(document.indexOf("HTTP/1.1 200 OK") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalPostIfModifiedNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "POST", index, "If-Modified-Since", dateFormat.format(new Date()), content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return code 200 OK - // assertTrue(document.indexOf("posted item=" + index) != -1); - assertTrue(document.indexOf("HTTP/1.1 200 OK") != -1); - // Should return code 304 Not Modified. - // assertTrue(document.indexOf("HTTP/1.1 304") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalPostIfModifiedPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "POST", index, "If-Modified-Since", dateFormat.format(new Date(0)), content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("posted item=" + index) != -1); - // Should return code 304 Not Modified. - assertTrue(document.indexOf("HTTP/1.1 304") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalPostIfUnmodifiedNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "POST", index, "If-Unmodified-Since", dateFormat.format(new Date()), content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return code 200 OK - assertTrue(document.indexOf("HTTP/1.1 200 OK") != -1); - // Should return code 304 Not Modified. - // assertTrue(document.indexOf("HTTP/1.1 304") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalPostIfUnmodifiedPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "POST", index, "If-Unmodified-Since", dateFormat.format(new Date(0)), content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("posted item=" + index) != -1); - // Should return code 412 PreconditionFailed. - assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalPostIfMatchNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "POST", index, "If-Match", "eTagMatch", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return code 200 OK. - assertTrue(document.indexOf("HTTP/1.1 200 OK") != -1); - // Should return code 412 precondition failed. - // assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalPostIfMatchPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "POST", index, "If-Match", "eTagNoneMatch", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("posted item=" + index) != -1); - // Should return code 412 PreconditionFailed. - assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalPostIfNoneMatchNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "POST", index, "If-None-Match", "eTagNoneMatch", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return code 200 OK - assertTrue(document.indexOf("HTTP/1.1 200 OK") != -1); - // Should return code 412 precondition failed. - // assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalPostIfNoneMatchPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "POST", index, "If-None-Match", "eTagMatch", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("posted item=" + index) != -1); - // Should return code 412 PreconditionFailed. - assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a POJO get method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testPut() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST1, "PUT", index, content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - assertTrue(document.indexOf("updated item=" + index) != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalPutIfModifiedNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "PUT", index, "If-Modified-Since", dateFormat.format(new Date(0)), content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - assertTrue(document.indexOf("updated item=" + index) != -1); - // Should return code 304 Not Modified. - // assertTrue(document.indexOf("HTTP/1.1 304") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalPutIfModifiedPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "PUT", index, "If-Modified-Since", dateFormat.format(new Date(0)), content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("updated item=" + index) != -1); - // Should return code 304 Not Modified. - assertTrue(document.indexOf("HTTP/1.1 304") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalPutIfUnmodifiedNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "PUT", index, "If-Unmodified-Since", dateFormat.format(new Date()), content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - assertTrue(document.indexOf("updated item=" + index) != -1); - // Should return code 304 Not Modified. - // assertTrue(document.indexOf("HTTP/1.1 304") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalPutIfUnmodifiedPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "PUT", index, "If-Unmodified-Since", dateFormat.format(new Date(0)), content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("updated item=" + index) != -1); - // Should return code 412 PreconditionFailed. - assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalPutIfMatchNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "PUT", index, "If-Match", "eTagXXX", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("updated item=" + index) != -1); - // Should return code 412 precondition failed. - assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalPutIfMatchPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "PUT", index, "If-Match", "eTagXXX", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - assertTrue(document.indexOf("updated item=" + index) != -1); - // Should return code 412 PreconditionFailed. - // assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalPutIfNoneMatchNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "PUT", index, "If-None-Match", "eTagXXX", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - assertTrue(document.indexOf("updated item=" + index) != -1); - // Should return code 412 precondition failed. - // assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * - * @throws Exception - */ - public void testConditionalPutIfNoneMatchPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "PUT", index, "If-None-Match", "eTagXXX", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("updated item=" + index) != -1); - // Should return code 412 PreconditionFailed. - assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Read response stream from the given socket. - * - * @param socket - * @return - * @throws IOException - */ - private static String read(Socket socket) throws IOException { - BufferedReader reader = null; - try { - reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); - StringBuffer sb = new StringBuffer(); - String str; - while ((str = reader.readLine()) != null) { - sb.append(str); - } - return sb.toString(); - } finally { - if (reader != null) { - reader.close(); - } - } - } + // RFC 822 date time + protected static final SimpleDateFormat dateFormat = new SimpleDateFormat( + "EEE, dd MMM yyyy HH:mm:ss Z"); + + // Request with no predicates in header. + private static final String REQUEST1 = "{0} /httpbinding/{1} HTTP/1.0\n" + + "Host: localhost\n" + "Content-Type: text/xml\n" + + "Connection: close\n" + "Content-Length: {2}" + "\n\n{3}"; + + // Request with predicates in header + private static final String REQUEST2 = "{0} /httpbinding/{1} HTTP/1.0\n" + + "Host: localhost\n" + "Content-Type: text/xml\n" + "{2}: {3}\n" // predicate (If-Match, If-None-Match, If-Modified-Since, If-NotModified-Since): value (date or ETag) + + "Connection: close\n" + "Content-Length: {4}" + "\n\n{5}"; + + private static final int HTTP_PORT = 8085; + + private SCADomain scaDomain; + + @Override + protected void setUp() throws Exception { + scaDomain = SCADomain.newInstance("testCache.composite"); + } + + @Override + protected void tearDown() throws Exception { + scaDomain.close(); + } + + /** + * Test invoking a POJO get method implementation using the HTTP binding. + * @throws Exception + */ + public void testGet() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST1, "GET", index, content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + assertTrue(document.indexOf("

item=" + index) != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalGetIfModifiedNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "GET", index, + "If-Modified-Since", dateFormat.format(new Date(0)), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + assertTrue(document.indexOf("

item=" + index) != -1); + // Should return code 304 Not Modified. + // assertTrue(document.indexOf("HTTP/1.1 304") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalGetIfModifiedPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "GET", index, + "If-Modified-Since", dateFormat.format(new Date(0)), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("

item=" + index) != -1); + // Should return code 304 Not Modified. + assertTrue(document.indexOf("HTTP/1.1 304") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalGetIfUnmodifiedNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "GET", index, + "If-Unmodified-Since", dateFormat.format(new Date()), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + assertTrue(document.indexOf("

item=" + index) != -1); + // Should return code 304 Not Modified. + // assertTrue(document.indexOf("HTTP/1.1 304") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalGetIfUnmodifiedPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "GET", index, + "If-Unmodified-Since", dateFormat.format(new Date(0)), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("

item=" + index) != -1); + // Should return code 412 PreconditionFailed. + assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalGetIfMatchNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "GET", index, + "If-Match", "eTagXXX", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("

item=" + index) != -1); + // Should return code 412 precondition failed. + assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalGetIfMatchPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "GET", index, + "If-Match", "eTagXXX", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + assertTrue(document.indexOf("

item=" + index) != -1); + // Should return code 412 PreconditionFailed. + // assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalGetIfNoneMatchNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "GET", index, + "If-None-Match", "eTagXXX", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + assertTrue(document.indexOf("

item=" + index) != -1); + // Should return code 412 precondition failed. + // assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalGetIfNoneMatchPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "GET", index, + "If-None-Match", "eTagXXX", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("

item=" + index) != -1); + // Should return code 412 PreconditionFailed. + assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a POJO get method implementation using the HTTP binding. + * @throws Exception + */ + public void testDelete() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST1, "DELETE", index, + content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + assertTrue(document.indexOf("deleted item=" + index) != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalDeleteIfModifiedNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "DELETE", index, + "If-Modified-Since", dateFormat.format(new Date(0)), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + assertTrue(document.indexOf("deleted item=" + index) != -1); + // Should return code 304 Not Modified. + // assertTrue(document.indexOf("HTTP/1.1 304") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalDeleteIfModifiedPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "DELETE", index, + "If-Modified-Since", dateFormat.format(new Date(0)), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("deleted item=" + index) != -1); + // Should return code 304 Not Modified. + assertTrue(document.indexOf("HTTP/1.1 304") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalDeleteIfUnmodifiedNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "DELETE", index, + "If-Unmodified-Since", dateFormat.format(new Date()), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + assertTrue(document.indexOf("deleted item=" + index) != -1); + // Should return code 304 Not Modified. + // assertTrue(document.indexOf("HTTP/1.1 304") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalDeleteIfUnmodifiedPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "DELETE", index, + "If-Unmodified-Since", dateFormat.format(new Date(0)), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("deleted item=" + index) != -1); + // Should return code 412 PreconditionFailed. + assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalDeleteIfMatchNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "DELETE", index, + "If-Match", "eTagXXX", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("deleted item=" + index) != -1); + // Should return code 412 precondition failed. + assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalDeleteIfMatchPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "DELETE", index, + "If-Match", "eTagXXX", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + assertTrue(document.indexOf("deleted item=" + index) != -1); + // Should return code 412 PreconditionFailed. + // assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalDeleteIfNoneMatchNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "DELETE", index, + "If-None-Match", "eTagXXX", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + assertTrue(document.indexOf("deleted item=" + index) != -1); + // Should return code 412 precondition failed. + // assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalDeleteIfNoneMatchPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "DELETE", index, + "If-None-Match", "eTagXXX", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("deleted item=" + index) != -1); + // Should return code 412 PreconditionFailed. + assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a POJO get method implementation using the HTTP binding. + * @throws Exception + */ + public void testPost() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST1, "POST", index, content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + assertTrue(document.indexOf("HTTP/1.1 200 OK") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalPostIfModifiedNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "POST", index, + "If-Modified-Since", dateFormat.format(new Date()), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return code 200 OK + // assertTrue(document.indexOf("posted item=" + index) != -1); + assertTrue(document.indexOf("HTTP/1.1 200 OK") != -1); + // Should return code 304 Not Modified. + // assertTrue(document.indexOf("HTTP/1.1 304") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalPostIfModifiedPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "POST", index, + "If-Modified-Since", dateFormat.format(new Date(0)), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("posted item=" + index) != -1); + // Should return code 304 Not Modified. + assertTrue(document.indexOf("HTTP/1.1 304") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalPostIfUnmodifiedNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "POST", index, + "If-Unmodified-Since", dateFormat.format(new Date()), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return code 200 OK + assertTrue(document.indexOf("HTTP/1.1 200 OK") != -1); + // Should return code 304 Not Modified. + // assertTrue(document.indexOf("HTTP/1.1 304") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalPostIfUnmodifiedPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "POST", index, + "If-Unmodified-Since", dateFormat.format(new Date(0)), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("posted item=" + index) != -1); + // Should return code 412 PreconditionFailed. + assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalPostIfMatchNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "POST", index, + "If-Match", "eTagMatch", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return code 200 OK. + assertTrue(document.indexOf("HTTP/1.1 200 OK") != -1); + // Should return code 412 precondition failed. + // assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalPostIfMatchPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat + .format(REQUEST2, "POST", index, "If-Match", "eTagNoneMatch", + content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("posted item=" + index) != -1); + // Should return code 412 PreconditionFailed. + assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalPostIfNoneMatchNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "POST", index, + "If-None-Match", "eTagNoneMatch", content.getBytes().length, + content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return code 200 OK + assertTrue(document.indexOf("HTTP/1.1 200 OK") != -1); + // Should return code 412 precondition failed. + // assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalPostIfNoneMatchPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "POST", index, + "If-None-Match", "eTagMatch", content.getBytes().length, + content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("posted item=" + index) != -1); + // Should return code 412 PreconditionFailed. + assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a POJO get method implementation using the HTTP binding. + * @throws Exception + */ + public void testPut() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST1, "PUT", index, content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + assertTrue(document.indexOf("updated item=" + index) != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalPutIfModifiedNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "PUT", index, + "If-Modified-Since", dateFormat.format(new Date(0)), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + assertTrue(document.indexOf("updated item=" + index) != -1); + // Should return code 304 Not Modified. + // assertTrue(document.indexOf("HTTP/1.1 304") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalPutIfModifiedPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "PUT", index, + "If-Modified-Since", dateFormat.format(new Date(0)), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("updated item=" + index) != -1); + // Should return code 304 Not Modified. + assertTrue(document.indexOf("HTTP/1.1 304") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalPutIfUnmodifiedNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "PUT", index, + "If-Unmodified-Since", dateFormat.format(new Date()), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + assertTrue(document.indexOf("updated item=" + index) != -1); + // Should return code 304 Not Modified. + // assertTrue(document.indexOf("HTTP/1.1 304") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalPutIfUnmodifiedPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "PUT", index, + "If-Unmodified-Since", dateFormat.format(new Date(0)), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("updated item=" + index) != -1); + // Should return code 412 PreconditionFailed. + assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalPutIfMatchNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "PUT", index, + "If-Match", "eTagXXX", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("updated item=" + index) != -1); + // Should return code 412 precondition failed. + assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalPutIfMatchPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "PUT", index, + "If-Match", "eTagXXX", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + assertTrue(document.indexOf("updated item=" + index) != -1); + // Should return code 412 PreconditionFailed. + // assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalPutIfNoneMatchNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "PUT", index, + "If-None-Match", "eTagXXX", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + assertTrue(document.indexOf("updated item=" + index) != -1); + // Should return code 412 precondition failed. + // assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + public void testConditionalPutIfNoneMatchPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "PUT", index, + "If-None-Match", "eTagXXX", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("updated item=" + index) != -1); + // Should return code 412 PreconditionFailed. + assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Read response stream from the given socket. + * @param socket + * @return + * @throws IOException + */ + private static String read(Socket socket) throws IOException { + BufferedReader reader = null; + try { + reader = new BufferedReader(new InputStreamReader(socket + .getInputStream())); + StringBuffer sb = new StringBuffer(); + String str; + while ((str = reader.readLine()) != null) { + sb.append(str); + } + return sb.toString(); + } finally { + if (reader != null) { + reader.close(); + } + } + } } diff --git a/java/sca/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestBindingCacheImpl.java b/java/sca/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestBindingCacheImpl.java index a3fce376f3..f2456f3175 100644 --- a/java/sca/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestBindingCacheImpl.java +++ b/java/sca/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestBindingCacheImpl.java @@ -49,7 +49,7 @@ public class TestBindingCacheImpl { * @param id * @return */ - public InputStream conditionalGet(String id, CacheContext cacheContext) + public InputStream conditionalGet(String id, HTTPCacheContext cacheContext) throws NotModifiedException, PreconditionFailedException { if (cacheContext != null) { @@ -99,7 +99,7 @@ public class TestBindingCacheImpl { * @param id * @return */ - public InputStream conditionalDelete(String id, CacheContext cacheContext) + public InputStream conditionalDelete(String id, HTTPCacheContext cacheContext) throws NotModifiedException, PreconditionFailedException { if (cacheContext != null) { @@ -151,7 +151,7 @@ public class TestBindingCacheImpl { * @param id * @return */ - public CacheContext conditionalPost(CacheContext cacheContext) + public HTTPCacheContext conditionalPost(HTTPCacheContext cacheContext) throws NotModifiedException, PreconditionFailedException { String id = "" + (new java.util.Random()).nextInt(Integer.MAX_VALUE); @@ -180,7 +180,7 @@ public class TestBindingCacheImpl { } // Return the ETag and LastModfied fields by serialize to a byte array - CacheContext returnContext = new CacheContext(); + HTTPCacheContext returnContext = new HTTPCacheContext(); returnContext.setETag( "ETag" + (new java.util.Random()).nextInt(Integer.MAX_VALUE) ); returnContext.setLastModified( new Date() ); return returnContext; @@ -202,7 +202,7 @@ public class TestBindingCacheImpl { * @param id * @return */ - public InputStream conditionalPut(String id, CacheContext cacheContext) + public InputStream conditionalPut(String id, HTTPCacheContext cacheContext) throws NotModifiedException, PreconditionFailedException { if (cacheContext != null) { -- cgit v1.2.3