~ubuntu-branches/ubuntu/trusty/httpcomponents-core/trusty

« back to all changes in this revision

Viewing changes to httpcore-nio/src/test/java/org/apache/http/impl/nio/codecs/TestChunkEncoder.java

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2010-06-12 08:37:34 UTC
  • Revision ID: james.westby@ubuntu.com-20100612083734-1y8kp6qm4sjk60az
Tags: upstream-4.0.1
ImportĀ upstreamĀ versionĀ 4.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0.1/httpcore-nio/src/test/java/org/apache/http/impl/nio/codecs/TestChunkEncoder.java $
 
3
 * $Revision: 744515 $
 
4
 * $Date: 2009-02-14 17:36:56 +0100 (Sat, 14 Feb 2009) $
 
5
 * ====================================================================
 
6
 * Licensed to the Apache Software Foundation (ASF) under one
 
7
 * or more contributor license agreements.  See the NOTICE file
 
8
 * distributed with this work for additional information
 
9
 * regarding copyright ownership.  The ASF licenses this file
 
10
 * to you under the Apache License, Version 2.0 (the
 
11
 * "License"); you may not use this file except in compliance
 
12
 * with the License.  You may obtain a copy of the License at
 
13
 *
 
14
 *   http://www.apache.org/licenses/LICENSE-2.0
 
15
 *
 
16
 * Unless required by applicable law or agreed to in writing,
 
17
 * software distributed under the License is distributed on an
 
18
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 
19
 * KIND, either express or implied.  See the License for the
 
20
 * specific language governing permissions and limitations
 
21
 * under the License.
 
22
 * ====================================================================
 
23
 *
 
24
 * This software consists of voluntary contributions made by many
 
25
 * individuals on behalf of the Apache Software Foundation.  For more
 
26
 * information on the Apache Software Foundation, please see
 
27
 * <http://www.apache.org/>.
 
28
 *
 
29
 */
 
30
 
 
31
package org.apache.http.impl.nio.codecs;
 
32
 
 
33
import java.io.ByteArrayOutputStream;
 
34
import java.nio.ByteBuffer;
 
35
import java.nio.channels.Channels;
 
36
import java.nio.channels.WritableByteChannel;
 
37
 
 
38
import junit.framework.Test;
 
39
import junit.framework.TestCase;
 
40
import junit.framework.TestSuite;
 
41
 
 
42
import org.apache.http.impl.io.HttpTransportMetricsImpl;
 
43
import org.apache.http.impl.nio.reactor.SessionOutputBufferImpl;
 
44
import org.apache.http.nio.reactor.SessionOutputBuffer;
 
45
import org.apache.http.params.BasicHttpParams;
 
46
import org.apache.http.params.HttpParams;
 
47
import org.apache.http.util.EncodingUtils;
 
48
 
 
49
/**
 
50
 * Simple tests for {@link ChunkEncoder}.
 
51
 *
 
52
 * 
 
53
 * @version $Id: TestChunkEncoder.java 744515 2009-02-14 16:36:56Z sebb $
 
54
 */
 
55
public class TestChunkEncoder extends TestCase {
 
56
 
 
57
    // ------------------------------------------------------------ Constructor
 
58
    public TestChunkEncoder(String testName) {
 
59
        super(testName);
 
60
    }
 
61
 
 
62
    // ------------------------------------------------------------------- Main
 
63
    public static void main(String args[]) {
 
64
        String[] testCaseName = { TestChunkEncoder.class.getName() };
 
65
        junit.textui.TestRunner.main(testCaseName);
 
66
    }
 
67
 
 
68
    // ------------------------------------------------------- TestCase Methods
 
69
 
 
70
    public static Test suite() {
 
71
        return new TestSuite(TestChunkEncoder.class);
 
72
    }
 
73
 
 
74
    private static ByteBuffer wrap(final String s) {
 
75
        return ByteBuffer.wrap(EncodingUtils.getAsciiBytes(s));
 
76
    }
 
77
    
 
78
    private static WritableByteChannel newChannel(final ByteArrayOutputStream baos) {
 
79
        return Channels.newChannel(baos);
 
80
    }
 
81
    
 
82
    public void testBasicCoding() throws Exception {
 
83
        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
 
84
        WritableByteChannel channel = newChannel(baos);
 
85
        HttpParams params = new BasicHttpParams();
 
86
        SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128, params);
 
87
        HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
 
88
        ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
 
89
        
 
90
        encoder.write(wrap("12345"));
 
91
        encoder.write(wrap("678"));
 
92
        encoder.write(wrap("90"));
 
93
        encoder.complete();
 
94
        
 
95
        outbuf.flush(channel);
 
96
        
 
97
        String s = baos.toString("US-ASCII");
 
98
        
 
99
        assertTrue(encoder.isCompleted());
 
100
        assertEquals("5\r\n12345\r\n3\r\n678\r\n2\r\n90\r\n0\r\n\r\n", s);
 
101
    }
 
102
    
 
103
    public void testChunkNoExceed() throws Exception {
 
104
        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
 
105
        WritableByteChannel channel = newChannel(baos);
 
106
        HttpParams params = new BasicHttpParams();
 
107
        SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 16, params);
 
108
        HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
 
109
        ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
 
110
        encoder.write(wrap("1234"));
 
111
        encoder.complete();
 
112
        
 
113
        outbuf.flush(channel);
 
114
        
 
115
        String s = baos.toString("US-ASCII");
 
116
        
 
117
        assertTrue(encoder.isCompleted());
 
118
        assertEquals("4\r\n1234\r\n0\r\n\r\n", s);    
 
119
    }
 
120
    
 
121
    
 
122
    public void testChunkExceed() throws Exception {
 
123
        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
 
124
        WritableByteChannel channel = newChannel(baos);
 
125
        HttpParams params = new BasicHttpParams();
 
126
        SessionOutputBuffer outbuf = new SessionOutputBufferImpl(16, 16, params);
 
127
        HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
 
128
        ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
 
129
        
 
130
        ByteBuffer src = wrap("0123456789ABCDEF");
 
131
        
 
132
        encoder.write(src);
 
133
        assertTrue(src.hasRemaining());
 
134
        assertEquals(10, src.remaining());
 
135
 
 
136
        encoder.write(src);
 
137
        assertTrue(src.hasRemaining());
 
138
        assertEquals(4, src.remaining());
 
139
 
 
140
        encoder.write(src);
 
141
        assertFalse(src.hasRemaining());
 
142
        
 
143
        outbuf.flush(channel);
 
144
        String s = baos.toString("US-ASCII");
 
145
        assertEquals("6\r\n012345\r\n6\r\n6789AB\r\n4\r\nCDEF\r\n", s);    
 
146
        
 
147
    }
 
148
 
 
149
    public void testCodingEmptyBuffer() throws Exception {
 
150
        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
 
151
        WritableByteChannel channel = newChannel(baos);
 
152
        HttpParams params = new BasicHttpParams();
 
153
        SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128, params);
 
154
        HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
 
155
        ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
 
156
        
 
157
        encoder.write(wrap("12345"));
 
158
        encoder.write(wrap("678"));
 
159
        encoder.write(wrap("90"));
 
160
        
 
161
        ByteBuffer empty = ByteBuffer.allocate(100);
 
162
        empty.flip();
 
163
        encoder.write(empty);
 
164
        encoder.write(null);
 
165
        
 
166
        encoder.complete();
 
167
        
 
168
        outbuf.flush(channel);
 
169
        
 
170
        String s = baos.toString("US-ASCII");
 
171
        
 
172
        assertTrue(encoder.isCompleted());
 
173
        assertEquals("5\r\n12345\r\n3\r\n678\r\n2\r\n90\r\n0\r\n\r\n", s);
 
174
    }
 
175
 
 
176
    public void testCodingCompleted() throws Exception {
 
177
        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
 
178
        WritableByteChannel channel = newChannel(baos);
 
179
        HttpParams params = new BasicHttpParams();
 
180
        SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128, params);
 
181
        HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
 
182
        ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
 
183
        
 
184
        encoder.write(wrap("12345"));
 
185
        encoder.write(wrap("678"));
 
186
        encoder.write(wrap("90"));
 
187
        encoder.complete();
 
188
 
 
189
        try {
 
190
            encoder.write(wrap("more stuff"));
 
191
            fail("IllegalStateException should have been thrown");
 
192
        } catch (IllegalStateException ex) {
 
193
            // ignore
 
194
        }
 
195
        try {
 
196
            encoder.complete();
 
197
            fail("IllegalStateException should have been thrown");
 
198
        } catch (IllegalStateException ex) {
 
199
            // ignore
 
200
        }
 
201
    }
 
202
 
 
203
    public void testInvalidConstructor() {
 
204
        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
 
205
        WritableByteChannel channel = newChannel(baos);
 
206
        HttpParams params = new BasicHttpParams();
 
207
        SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128, params);
 
208
 
 
209
        try {
 
210
            new ChunkEncoder(null, null, null);
 
211
            fail("IllegalArgumentException should have been thrown");
 
212
        } catch (IllegalArgumentException ex) {
 
213
            // ignore
 
214
        }
 
215
        try {
 
216
            new ChunkEncoder(channel, null, null);
 
217
            fail("IllegalArgumentException should have been thrown");
 
218
        } catch (IllegalArgumentException ex) {
 
219
            // ignore
 
220
        }
 
221
        try {
 
222
            new ChunkEncoder(channel, outbuf, null);
 
223
            fail("IllegalArgumentException should have been thrown");
 
224
        } catch (IllegalArgumentException ex) {
 
225
            // ignore
 
226
        }
 
227
    }
 
228
    
 
229
}