hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1172021 - in /httpcomponents/httpcore/trunk/httpcore-nio/src: main/java/org/apache/http/nio/protocol/ test/java/org/apache/http/nio/protocol/
Date Sat, 17 Sep 2011 17:08:37 GMT
Author: olegk
Date: Sat Sep 17 17:08:37 2011
New Revision: 1172021

URL: http://svn.apache.org/viewvc?rev=1172021&view=rev
Log:
Added unit test for BasicAsyncResponseConsumer and BasicAsyncRequestConsumer

Added:
    httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncRequestConsumer.java
  (with props)
    httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncResponseConsumer.java
  (with props)
    httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncResponseProducer.java
  (with props)
Modified:
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestConsumer.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncResponseConsumer.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncRequestProducer.java

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestConsumer.java?rev=1172021&r1=1172020&r2=1172021&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestConsumer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncRequestConsumer.java
Sat Sep 17 17:08:37 2011
@@ -52,6 +52,9 @@ public class BasicAsyncRequestConsumer e
 
     public BasicAsyncRequestConsumer(final ByteBufferAllocator allocator) {
         super();
+        if (allocator == null) {
+            throw new IllegalArgumentException("Byte buffer allocator is null");
+        }
         this.allocator = allocator;
     }
 

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncResponseConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncResponseConsumer.java?rev=1172021&r1=1172020&r2=1172021&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncResponseConsumer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncResponseConsumer.java
Sat Sep 17 17:08:37 2011
@@ -51,6 +51,9 @@ public class BasicAsyncResponseConsumer 
 
     public BasicAsyncResponseConsumer(final ByteBufferAllocator allocator) {
         super();
+        if (allocator == null) {
+            throw new IllegalArgumentException("Byte buffer allocator is null");
+        }
         this.allocator = allocator;
     }
 

Added: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncRequestConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncRequestConsumer.java?rev=1172021&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncRequestConsumer.java
(added)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncRequestConsumer.java
Sat Sep 17 17:08:37 2011
@@ -0,0 +1,89 @@
+package org.apache.http.nio.protocol;
+
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import junit.framework.Assert;
+
+import org.apache.http.HttpEntityEnclosingRequest;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.nio.ContentDecoder;
+import org.apache.http.nio.IOControl;
+import org.apache.http.protocol.HttpContext;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+
+public class TestBasicAsyncRequestConsumer {
+
+    private BasicAsyncRequestConsumer consumer;
+    @Mock private HttpEntityEnclosingRequest request;
+    @Mock private HttpContext context;
+    @Mock private ContentDecoder decoder;
+    @Mock private IOControl ioctrl;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+
+        consumer = Mockito.spy(new BasicAsyncRequestConsumer());
+    }
+
+    @After
+    public void tearDown() throws Exception {
+    }
+
+    @Test(expected=IllegalArgumentException.class)
+    public void testNullTargetArgConstructor() throws Exception {
+        new BasicAsyncRequestConsumer(null);
+    }
+
+    @Test
+    public void testRequestProcessing() throws Exception {
+        when(request.getEntity()).thenReturn(new StringEntity("stuff"));
+
+        consumer.requestReceived(request);
+        consumer.consumeContent(decoder, ioctrl);
+        consumer.requestCompleted(context);
+
+        verify(consumer).releaseResources();
+        verify(consumer).buildResult(context);
+        Assert.assertTrue(consumer.isDone());
+        Assert.assertSame(request, consumer.getResult());
+
+        consumer.requestCompleted(context);
+        verify(consumer, times(1)).releaseResources();
+        verify(consumer, times(1)).buildResult(context);
+    }
+
+    @Test
+    public void testResponseProcessingWithException() throws Exception {
+        when(request.getEntity()).thenReturn(new StringEntity("stuff"));
+        RuntimeException ooopsie = new RuntimeException();
+        when(consumer.buildResult(context)).thenThrow(ooopsie);
+
+        consumer.requestReceived(request);
+        consumer.consumeContent(decoder, ioctrl);
+        consumer.requestCompleted(context);
+
+        verify(consumer).releaseResources();
+        Assert.assertTrue(consumer.isDone());
+        Assert.assertSame(ooopsie, consumer.getException());
+    }
+
+    @Test
+    public void testClose() throws Exception {
+        consumer.close();
+
+        verify(consumer).releaseResources();
+        Assert.assertTrue(consumer.isDone());
+
+        consumer.close();
+
+        verify(consumer, times(1)).releaseResources();
+    }
+
+}

Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncRequestConsumer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncRequestConsumer.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncRequestConsumer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncRequestProducer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncRequestProducer.java?rev=1172021&r1=1172020&r2=1172021&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncRequestProducer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncRequestProducer.java
Sat Sep 17 17:08:37 2011
@@ -63,14 +63,14 @@ public class TestBasicAsyncRequestProduc
     public void testGenerateRequest() {
         HttpRequest res = producer.generateRequest();
 
-        Assert.assertEquals(request, res);
+        Assert.assertSame(request, res);
     }
 
     @Test
     public void testGetTarget() {
         HttpHost res = producer.getTarget();
 
-        Assert.assertEquals(target, res);
+        Assert.assertSame(target, res);
     }
 
     @Test

Added: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncResponseConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncResponseConsumer.java?rev=1172021&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncResponseConsumer.java
(added)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncResponseConsumer.java
Sat Sep 17 17:08:37 2011
@@ -0,0 +1,123 @@
+package org.apache.http.nio.protocol;
+
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import junit.framework.Assert;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.nio.ContentDecoder;
+import org.apache.http.nio.IOControl;
+import org.apache.http.protocol.HttpContext;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+
+public class TestBasicAsyncResponseConsumer {
+
+    private BasicAsyncResponseConsumer consumer;
+    @Mock private HttpResponse response;
+    @Mock private HttpContext context;
+    @Mock private ContentDecoder decoder;
+    @Mock private IOControl ioctrl;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+
+        consumer = Mockito.spy(new BasicAsyncResponseConsumer());
+    }
+
+    @After
+    public void tearDown() throws Exception {
+    }
+
+    @Test(expected=IllegalArgumentException.class)
+    public void testNullTargetArgConstructor() throws Exception {
+        new BasicAsyncResponseConsumer(null);
+    }
+
+    @Test
+    public void testResponseProcessing() throws Exception {
+        when(response.getEntity()).thenReturn(new StringEntity("stuff"));
+
+        consumer.responseReceived(response);
+        consumer.consumeContent(decoder, ioctrl);
+        consumer.responseCompleted(context);
+
+        verify(consumer).releaseResources();
+        verify(consumer).buildResult(context);
+        Assert.assertTrue(consumer.isDone());
+        Assert.assertSame(response, consumer.getResult());
+
+        consumer.responseCompleted(context);
+        verify(consumer, times(1)).releaseResources();
+        verify(consumer, times(1)).buildResult(context);
+    }
+
+    @Test
+    public void testResponseProcessingWithException() throws Exception {
+        when(response.getEntity()).thenReturn(new StringEntity("stuff"));
+        RuntimeException ooopsie = new RuntimeException();
+        when(consumer.buildResult(context)).thenThrow(ooopsie);
+
+        consumer.responseReceived(response);
+        consumer.consumeContent(decoder, ioctrl);
+        consumer.responseCompleted(context);
+
+        verify(consumer).releaseResources();
+        Assert.assertTrue(consumer.isDone());
+        Assert.assertSame(ooopsie, consumer.getException());
+    }
+
+    @Test
+    public void testCancel() throws Exception {
+        Assert.assertTrue(consumer.cancel());
+
+        verify(consumer).releaseResources();
+        Assert.assertTrue(consumer.isDone());
+
+        Assert.assertFalse(consumer.cancel());
+        verify(consumer, times(1)).releaseResources();
+    }
+
+    @Test
+    public void testFailed() throws Exception {
+        RuntimeException ooopsie = new RuntimeException();
+
+        consumer.failed(ooopsie);
+
+        verify(consumer).releaseResources();
+        Assert.assertTrue(consumer.isDone());
+        Assert.assertSame(ooopsie, consumer.getException());
+    }
+
+    @Test
+    public void testFailedAfterDone() throws Exception {
+        RuntimeException ooopsie = new RuntimeException();
+
+        consumer.cancel();
+        consumer.failed(ooopsie);
+
+        verify(consumer, times(1)).releaseResources();
+        Assert.assertTrue(consumer.isDone());
+        Assert.assertNull(consumer.getException());
+    }
+
+    @Test
+    public void testClose() throws Exception {
+        consumer.close();
+
+        verify(consumer).releaseResources();
+        Assert.assertTrue(consumer.isDone());
+
+        consumer.close();
+
+        verify(consumer, times(1)).releaseResources();
+    }
+
+}

Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncResponseConsumer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncResponseConsumer.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncResponseConsumer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncResponseProducer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncResponseProducer.java?rev=1172021&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncResponseProducer.java
(added)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncResponseProducer.java
Sat Sep 17 17:08:37 2011
@@ -0,0 +1,74 @@
+package org.apache.http.nio.protocol;
+
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.never;
+import junit.framework.Assert;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.nio.ContentEncoder;
+import org.apache.http.nio.entity.ProducingNHttpEntity;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+public class TestBasicAsyncResponseProducer {
+
+    private BasicAsyncResponseProducer producer;
+    @Mock private ProducingNHttpEntity entity;
+    @Mock private HttpResponse response;
+    @Mock private ContentEncoder encoder;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+
+        when(response.getEntity()).thenReturn(entity);
+
+        producer = new BasicAsyncResponseProducer(response);
+    }
+
+    @After
+    public void tearDown() throws Exception {
+    }
+
+    @Test(expected=IllegalArgumentException.class)
+    public void testNullTargetArgConstructor() throws Exception {
+        producer = new BasicAsyncResponseProducer(null);
+    }
+
+    @Test
+    public void testGenerateRequest() {
+        HttpResponse res = producer.generateResponse();
+
+        Assert.assertSame(response, res);
+    }
+
+    @Test
+    public void testProduceContentEncoderCompleted() throws Exception {
+        when(encoder.isCompleted()).thenReturn(true);
+
+        producer.produceContent(encoder,  null);
+
+        verify(entity, times(1)).finish();
+    }
+
+    @Test
+    public void testProduceContentEncoderNotCompleted() throws Exception {
+        when(encoder.isCompleted()).thenReturn(false);
+
+        producer.produceContent(encoder,  null);
+
+        verify(entity, never()).finish();
+    }
+
+    @Test
+    public void testClose() throws Exception {
+        producer.close();
+        verify(entity, times(1)).finish();
+    }
+
+}

Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncResponseProducer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncResponseProducer.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncResponseProducer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message