2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
19 package org
.apache
.thrift
.transport
;
21 import java
.io
.BufferedOutputStream
;
22 import java
.io
.ByteArrayInputStream
;
23 import java
.io
.ByteArrayOutputStream
;
24 import java
.io
.DataInputStream
;
25 import java
.io
.DataOutputStream
;
26 import java
.io
.IOException
;
27 import java
.util
.Arrays
;
28 import java
.util
.zip
.DataFormatException
;
29 import java
.util
.zip
.DeflaterOutputStream
;
30 import java
.util
.zip
.InflaterInputStream
;
32 import junit
.framework
.TestCase
;
34 public class TestTZlibTransport
extends TestCase
{
36 protected TTransport
getTransport(TTransport underlying
) {
37 return new TZlibTransport(underlying
);
40 public static byte[] byteSequence(int start
, int end
) {
41 byte[] result
= new byte[end
-start
+1];
42 for (int i
= 0; i
<= (end
-start
); i
++) {
43 result
[i
] = (byte)(start
+i
);
48 public void testClose() throws TTransportException
{
49 ByteArrayOutputStream baos
= new ByteArrayOutputStream();
50 WriteCountingTransport countingTrans
= new WriteCountingTransport(new TIOStreamTransport(new BufferedOutputStream
52 TTransport trans
= getTransport(countingTrans
);
53 trans
.write(byteSequence(0, 245));
54 countingTrans
.close();
58 public void testCloseOpen() throws TTransportException
{
59 ByteArrayOutputStream baos
= new ByteArrayOutputStream();
60 TTransport trans
= getTransport(new TIOStreamTransport(baos
));
61 byte[] uncompressed
= byteSequence(0, 245);
62 trans
.write(uncompressed
);
64 final byte[] compressed
= baos
.toByteArray();
66 final byte[] buf
= new byte[255];
67 TTransport transRead
= getTransport(new TIOStreamTransport(new ByteArrayInputStream(compressed
)));
68 int readBytes
= transRead
.read(buf
, 0, buf
.length
);
69 assertEquals(uncompressed
.length
, readBytes
);
73 public void testRead() throws IOException
, TTransportException
{
74 ByteArrayOutputStream baos
= new ByteArrayOutputStream();
75 DeflaterOutputStream deflaterOutputStream
= new DeflaterOutputStream(baos
);
76 DataOutputStream dos
= new DataOutputStream(deflaterOutputStream
);
77 dos
.write(byteSequence(0, 49));
78 dos
.write(byteSequence(0, 219));
80 deflaterOutputStream
.finish();
82 TMemoryBuffer membuf
= new TMemoryBuffer(0);
83 membuf
.write(baos
.toByteArray());
85 ReadCountingTransport countTrans
= new ReadCountingTransport(membuf
);
86 TTransport trans
= getTransport(countTrans
);
88 byte[] readBuf
= new byte[10];
89 trans
.read(readBuf
, 0, 10);
90 assertTrue(Arrays
.equals(readBuf
, byteSequence(0,9)));
91 assertEquals(1, countTrans
.readCount
);
93 trans
.read(readBuf
, 0, 10);
94 assertTrue(Arrays
.equals(readBuf
, byteSequence(10,19)));
95 assertEquals(1, countTrans
.readCount
);
97 assertEquals(30, trans
.read(new byte[30], 0, 30));
98 assertEquals(1, countTrans
.readCount
);
100 readBuf
= new byte[220];
101 assertEquals(220, trans
.read(readBuf
, 0, 220));
102 assertTrue(Arrays
.equals(readBuf
, byteSequence(0, 219)));
103 assertEquals(1, countTrans
.readCount
);
106 public void testWrite() throws TTransportException
, IOException
, DataFormatException
{
107 ByteArrayOutputStream baos
= new ByteArrayOutputStream();
108 WriteCountingTransport countingTrans
= new WriteCountingTransport(new TIOStreamTransport(new BufferedOutputStream(baos
)));
109 TTransport trans
= getTransport(countingTrans
);
111 trans
.write(byteSequence(0, 100));
112 assertEquals(1, countingTrans
.writeCount
);
113 trans
.write(byteSequence(101, 200));
114 trans
.write(byteSequence(201, 255));
115 assertEquals(1, countingTrans
.writeCount
);
118 assertEquals(2, countingTrans
.writeCount
);
120 trans
.write(byteSequence(0, 245));
122 assertEquals(3, countingTrans
.writeCount
);
124 DataInputStream din
= new DataInputStream(new InflaterInputStream(new ByteArrayInputStream(baos
.toByteArray())));
125 byte[] buf
= new byte[256];
126 int n
= din
.read(buf
, 0, 256);
127 assertEquals(n
, 256);
128 assertTrue(Arrays
.equals(byteSequence(0, 255), buf
));
131 n
= din
.read(buf
, 0, 246);
132 assertEquals(n
, 246);
133 for (int i
= 0; i
<buf
.length
; i
++) {
134 assertEquals("for "+i
, byteSequence(0,245)[i
], buf
[i
]);
137 assertTrue(Arrays
.equals(byteSequence(0,245), buf
));