~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/TestIdentityEncoder.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/TestIdentityEncoder.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 IdentityEncoder}.
 
51
 *
 
52
 * 
 
53
 * @version $Id: TestIdentityEncoder.java 744515 2009-02-14 16:36:56Z sebb $
 
54
 */
 
55
public class TestIdentityEncoder extends TestCase {
 
56
 
 
57
    // ------------------------------------------------------------ Constructor
 
58
    public TestIdentityEncoder(String testName) {
 
59
        super(testName);
 
60
    }
 
61
 
 
62
    // ------------------------------------------------------------------- Main
 
63
    public static void main(String args[]) {
 
64
        String[] testCaseName = { TestIdentityEncoder.class.getName() };
 
65
        junit.textui.TestRunner.main(testCaseName);
 
66
    }
 
67
 
 
68
    // ------------------------------------------------------- TestCase Methods
 
69
 
 
70
    public static Test suite() {
 
71
        return new TestSuite(TestIdentityEncoder.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
 
 
89
        IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics);
 
90
        encoder.write(wrap("stuff"));
 
91
        encoder.complete();
 
92
        
 
93
        String s = baos.toString("US-ASCII");
 
94
        
 
95
        assertTrue(encoder.isCompleted());
 
96
        assertEquals("stuff", s);
 
97
    }
 
98
    
 
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();
 
105
 
 
106
        IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics);
 
107
        encoder.write(wrap("stuff"));
 
108
        
 
109
        ByteBuffer empty = ByteBuffer.allocate(100);
 
110
        empty.flip();
 
111
        encoder.write(empty);
 
112
        encoder.write(null);
 
113
        
 
114
        encoder.complete();
 
115
        
 
116
        String s = baos.toString("US-ASCII");
 
117
        
 
118
        assertTrue(encoder.isCompleted());
 
119
        assertEquals("stuff", s);
 
120
    }
 
121
 
 
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();
 
128
 
 
129
        IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics);
 
130
        encoder.write(wrap("stuff"));
 
131
        encoder.complete();
 
132
 
 
133
        try {
 
134
            encoder.write(wrap("more stuff"));
 
135
            fail("IllegalStateException should have been thrown");
 
136
        } catch (IllegalStateException ex) {
 
137
            // ignore
 
138
        }
 
139
    }
 
140
 
 
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);
 
146
 
 
147
        try {
 
148
            new IdentityEncoder(null, null, null);
 
149
            fail("IllegalArgumentException should have been thrown");
 
150
        } catch (IllegalArgumentException ex) {
 
151
            // ignore
 
152
        }
 
153
        try {
 
154
            new IdentityEncoder(channel, null, null);
 
155
            fail("IllegalArgumentException should have been thrown");
 
156
        } catch (IllegalArgumentException ex) {
 
157
            // ignore
 
158
        }
 
159
        try {
 
160
            new IdentityEncoder(channel, outbuf, null);
 
161
            fail("IllegalArgumentException should have been thrown");
 
162
        } catch (IllegalArgumentException ex) {
 
163
            // ignore
 
164
        }
 
165
    }
 
166
    
 
167
}