View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.client.entity;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.IOException;
33  import java.io.InputStream;
34  import java.util.zip.CRC32;
35  import java.util.zip.CheckedInputStream;
36  import java.util.zip.Checksum;
37  
38  import org.apache.http.Consts;
39  import org.apache.http.HttpEntity;
40  import org.apache.http.entity.InputStreamEntity;
41  import org.apache.http.entity.StringEntity;
42  import org.apache.http.util.EntityUtils;
43  import org.junit.Assert;
44  import org.junit.Test;
45  
46  public class TestDecompressingEntity {
47  
48      @Test
49      public void testNonStreaming() throws Exception {
50          final CRC32 crc32 = new CRC32();
51          final StringEntity wrapped = new StringEntity("1234567890", "ASCII");
52          final ChecksumEntity entity = new ChecksumEntity(wrapped, crc32);
53          Assert.assertFalse(entity.isStreaming());
54          final String s = EntityUtils.toString(entity);
55          Assert.assertEquals("1234567890", s);
56          Assert.assertEquals(639479525L, crc32.getValue());
57          final InputStream in1 = entity.getContent();
58          final InputStream in2 = entity.getContent();
59          Assert.assertTrue(in1 != in2);
60      }
61  
62      @Test
63      public void testStreaming() throws Exception {
64          final CRC32 crc32 = new CRC32();
65          final ByteArrayInputStream in = new ByteArrayInputStream("1234567890".getBytes(Consts.ASCII));
66          final InputStreamEntity wrapped = new InputStreamEntity(in, -1);
67          final ChecksumEntity entity = new ChecksumEntity(wrapped, crc32);
68          Assert.assertTrue(entity.isStreaming());
69          final String s = EntityUtils.toString(entity);
70          Assert.assertEquals("1234567890", s);
71          Assert.assertEquals(639479525L, crc32.getValue());
72          final InputStream in1 = entity.getContent();
73          final InputStream in2 = entity.getContent();
74          Assert.assertTrue(in1 == in2);
75          EntityUtils.consume(entity);
76          EntityUtils.consume(entity);
77      }
78  
79      @Test
80      public void testWriteToStream() throws Exception {
81          final CRC32 crc32 = new CRC32();
82          final StringEntity wrapped = new StringEntity("1234567890", "ASCII");
83          final ChecksumEntity entity = new ChecksumEntity(wrapped, crc32);
84          Assert.assertFalse(entity.isStreaming());
85  
86          final ByteArrayOutputStream out = new ByteArrayOutputStream();
87          entity.writeTo(out);
88  
89          final String s = new String(out.toByteArray(), "ASCII");
90          Assert.assertEquals("1234567890", s);
91          Assert.assertEquals(639479525L, crc32.getValue());
92      }
93  
94      static class ChecksumEntity extends DecompressingEntity {
95  
96          public ChecksumEntity(final HttpEntity wrapped, final Checksum checksum) {
97              super(wrapped, new InputStreamFactory() {
98  
99                  @Override
100                 public InputStream create(final InputStream inStream) throws IOException {
101                     return new CheckedInputStream(inStream, checksum);
102                 }
103 
104             });
105         }
106 
107     }
108 
109 }