• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 Square, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package okio;
17 
18 import java.io.IOException;
19 import org.junit.Test;
20 
21 import static okio.TestUtil.repeat;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.fail;
24 
25 public class GzipSinkTest {
gzipGunzip()26   @Test public void gzipGunzip() throws Exception {
27     Buffer data = new Buffer();
28     String original = "It's a UNIX system! I know this!";
29     data.writeUtf8(original);
30     Buffer sink = new Buffer();
31     GzipSink gzipSink = new GzipSink(sink);
32     gzipSink.write(data, data.size());
33     gzipSink.close();
34     Buffer inflated = gunzip(sink);
35     assertEquals(original, inflated.readUtf8());
36   }
37 
closeWithExceptionWhenWritingAndClosing()38   @Test public void closeWithExceptionWhenWritingAndClosing() throws IOException {
39     MockSink mockSink = new MockSink();
40     mockSink.scheduleThrow(0, new IOException("first"));
41     mockSink.scheduleThrow(1, new IOException("second"));
42     GzipSink gzipSink = new GzipSink(mockSink);
43     gzipSink.write(new Buffer().writeUtf8(repeat('a', Segment.SIZE)), Segment.SIZE);
44     try {
45       gzipSink.close();
46       fail();
47     } catch (IOException expected) {
48       assertEquals("first", expected.getMessage());
49     }
50     mockSink.assertLogContains("close()");
51   }
52 
gunzip(Buffer gzipped)53   private Buffer gunzip(Buffer gzipped) throws IOException {
54     Buffer result = new Buffer();
55     GzipSource source = new GzipSource(gzipped);
56     while (source.read(result, Integer.MAX_VALUE) != -1) {
57     }
58     return result;
59   }
60 }
61