]> git.proxmox.com Git - ceph.git/blame - ceph/src/jaegertracing/thrift/lib/java/test/org/apache/thrift/transport/TestTZlibTransport.java
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / jaegertracing / thrift / lib / java / test / org / apache / thrift / transport / TestTZlibTransport.java
CommitLineData
f67539c2
TL
1/*
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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
17 * under the License.
18 */
19package org.apache.thrift.transport;
20
21import java.io.BufferedOutputStream;
22import java.io.ByteArrayInputStream;
23import java.io.ByteArrayOutputStream;
24import java.io.DataInputStream;
25import java.io.DataOutputStream;
26import java.io.IOException;
27import java.util.Arrays;
28import java.util.zip.DataFormatException;
29import java.util.zip.DeflaterOutputStream;
30import java.util.zip.InflaterInputStream;
31
32import junit.framework.TestCase;
33
34public class TestTZlibTransport extends TestCase {
35
36 protected TTransport getTransport(TTransport underlying) {
37 return new TZlibTransport(underlying);
38 }
39
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);
44 }
45 return result;
46 }
47
48 public void testClose() throws TTransportException {
49 ByteArrayOutputStream baos = new ByteArrayOutputStream();
50 WriteCountingTransport countingTrans = new WriteCountingTransport(new TIOStreamTransport(new BufferedOutputStream
51 (baos)));
52 TTransport trans = getTransport(countingTrans);
53 trans.write(byteSequence(0, 245));
54 countingTrans.close();
55 trans.close();
56 }
57
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);
63 trans.close();
64 final byte[] compressed = baos.toByteArray();
65
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);
70 transRead.close();
71 }
72
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));
79
80 deflaterOutputStream.finish();
81
82 TMemoryBuffer membuf = new TMemoryBuffer(0);
83 membuf.write(baos.toByteArray());
84
85 ReadCountingTransport countTrans = new ReadCountingTransport(membuf);
86 TTransport trans = getTransport(countTrans);
87
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);
92
93 trans.read(readBuf, 0, 10);
94 assertTrue(Arrays.equals(readBuf, byteSequence(10,19)));
95 assertEquals(1, countTrans.readCount);
96
97 assertEquals(30, trans.read(new byte[30], 0, 30));
98 assertEquals(1, countTrans.readCount);
99
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);
104 }
105
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);
110
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);
116
117 trans.flush();
118 assertEquals(2, countingTrans.writeCount);
119
120 trans.write(byteSequence(0, 245));
121 trans.flush();
122 assertEquals(3, countingTrans.writeCount);
123
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));
129
130 buf = new byte[246];
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]);
135 }
136
137 assertTrue(Arrays.equals(byteSequence(0,245), buf));
138 }
139
140}