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/TestIdentityEncoder.java $
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
14
* http://www.apache.org/licenses/LICENSE-2.0
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
22
* ====================================================================
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/>.
31
package org.apache.http.impl.nio.codecs;
33
import java.io.ByteArrayOutputStream;
34
import java.nio.ByteBuffer;
35
import java.nio.channels.Channels;
36
import java.nio.channels.WritableByteChannel;
38
import junit.framework.Test;
39
import junit.framework.TestCase;
40
import junit.framework.TestSuite;
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;
50
* Simple tests for {@link IdentityEncoder}.
53
* @version $Id: TestIdentityEncoder.java 744515 2009-02-14 16:36:56Z sebb $
55
public class TestIdentityEncoder extends TestCase {
57
// ------------------------------------------------------------ Constructor
58
public TestIdentityEncoder(String testName) {
62
// ------------------------------------------------------------------- Main
63
public static void main(String args[]) {
64
String[] testCaseName = { TestIdentityEncoder.class.getName() };
65
junit.textui.TestRunner.main(testCaseName);
68
// ------------------------------------------------------- TestCase Methods
70
public static Test suite() {
71
return new TestSuite(TestIdentityEncoder.class);
74
private static ByteBuffer wrap(final String s) {
75
return ByteBuffer.wrap(EncodingUtils.getAsciiBytes(s));
78
private static WritableByteChannel newChannel(final ByteArrayOutputStream baos) {
79
return Channels.newChannel(baos);
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();
89
IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics);
90
encoder.write(wrap("stuff"));
93
String s = baos.toString("US-ASCII");
95
assertTrue(encoder.isCompleted());
96
assertEquals("stuff", s);
99
public void testCodingEmptyBuffer() throws Exception {
100
ByteArrayOutputStream baos = new ByteArrayOutputStream();
101
WritableByteChannel channel = newChannel(baos);
102
HttpParams params = new BasicHttpParams();
103
SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128, params);
104
HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
106
IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics);
107
encoder.write(wrap("stuff"));
109
ByteBuffer empty = ByteBuffer.allocate(100);
111
encoder.write(empty);
116
String s = baos.toString("US-ASCII");
118
assertTrue(encoder.isCompleted());
119
assertEquals("stuff", s);
122
public void testCodingCompleted() throws Exception {
123
ByteArrayOutputStream baos = new ByteArrayOutputStream();
124
WritableByteChannel channel = newChannel(baos);
125
HttpParams params = new BasicHttpParams();
126
SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128, params);
127
HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
129
IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics);
130
encoder.write(wrap("stuff"));
134
encoder.write(wrap("more stuff"));
135
fail("IllegalStateException should have been thrown");
136
} catch (IllegalStateException ex) {
141
public void testInvalidConstructor() {
142
ByteArrayOutputStream baos = new ByteArrayOutputStream();
143
WritableByteChannel channel = newChannel(baos);
144
HttpParams params = new BasicHttpParams();
145
SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128, params);
148
new IdentityEncoder(null, null, null);
149
fail("IllegalArgumentException should have been thrown");
150
} catch (IllegalArgumentException ex) {
154
new IdentityEncoder(channel, null, null);
155
fail("IllegalArgumentException should have been thrown");
156
} catch (IllegalArgumentException ex) {
160
new IdentityEncoder(channel, outbuf, null);
161
fail("IllegalArgumentException should have been thrown");
162
} catch (IllegalArgumentException ex) {