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 */ 16package okio; 17 18import java.io.ByteArrayInputStream; 19import java.io.ByteArrayOutputStream; 20import java.io.File; 21import java.io.InputStream; 22import org.junit.Rule; 23import org.junit.Test; 24import org.junit.rules.TemporaryFolder; 25 26import static okio.TestUtil.repeat; 27import static okio.Util.UTF_8; 28import static org.junit.Assert.assertEquals; 29import static org.junit.Assert.assertTrue; 30import static org.junit.Assert.fail; 31 32public final class OkioTest { 33 @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); 34 35 @Test public void readWriteFile() throws Exception { 36 File file = temporaryFolder.newFile(); 37 38 BufferedSink sink = Okio.buffer(Okio.sink(file)); 39 sink.writeUtf8("Hello, java.io file!"); 40 sink.close(); 41 assertTrue(file.exists()); 42 assertEquals(20, file.length()); 43 44 BufferedSource source = Okio.buffer(Okio.source(file)); 45 assertEquals("Hello, java.io file!", source.readUtf8()); 46 source.close(); 47 } 48 49 @Test public void appendFile() throws Exception { 50 File file = temporaryFolder.newFile(); 51 52 BufferedSink sink = Okio.buffer(Okio.appendingSink(file)); 53 sink.writeUtf8("Hello, "); 54 sink.close(); 55 assertTrue(file.exists()); 56 assertEquals(7, file.length()); 57 58 sink = Okio.buffer(Okio.appendingSink(file)); 59 sink.writeUtf8("java.io file!"); 60 sink.close(); 61 assertEquals(20, file.length()); 62 63 BufferedSource source = Okio.buffer(Okio.source(file)); 64 assertEquals("Hello, java.io file!", source.readUtf8()); 65 source.close(); 66 } 67 68 // ANDROID-BEGIN 69 // @Test public void readWritePath() throws Exception { 70 // Path path = temporaryFolder.newFile().toPath(); 71 // 72 // BufferedSink sink = Okio.buffer(Okio.sink(path)); 73 // sink.writeUtf8("Hello, java.nio file!"); 74 // sink.close(); 75 // assertTrue(Files.exists(path)); 76 // assertEquals(21, Files.size(path)); 77 // 78 // BufferedSource source = Okio.buffer(Okio.source(path)); 79 // assertEquals("Hello, java.nio file!", source.readUtf8()); 80 // source.close(); 81 // } 82 // ANDROID-END 83 84 @Test public void sinkFromOutputStream() throws Exception { 85 Buffer data = new Buffer(); 86 data.writeUtf8("a"); 87 data.writeUtf8(repeat('b', 9998)); 88 data.writeUtf8("c"); 89 90 ByteArrayOutputStream out = new ByteArrayOutputStream(); 91 Sink sink = Okio.sink(out); 92 sink.write(data, 3); 93 assertEquals("abb", out.toString("UTF-8")); 94 sink.write(data, data.size()); 95 assertEquals("a" + repeat('b', 9998) + "c", out.toString("UTF-8")); 96 } 97 98 @Test public void sourceFromInputStream() throws Exception { 99 InputStream in = new ByteArrayInputStream( 100 ("a" + repeat('b', Segment.SIZE * 2) + "c").getBytes(UTF_8)); 101 102 // Source: ab...bc 103 Source source = Okio.source(in); 104 Buffer sink = new Buffer(); 105 106 // Source: b...bc. Sink: abb. 107 assertEquals(3, source.read(sink, 3)); 108 assertEquals("abb", sink.readUtf8(3)); 109 110 // Source: b...bc. Sink: b...b. 111 assertEquals(Segment.SIZE, source.read(sink, 20000)); 112 assertEquals(repeat('b', Segment.SIZE), sink.readUtf8()); 113 114 // Source: b...bc. Sink: b...bc. 115 assertEquals(Segment.SIZE - 1, source.read(sink, 20000)); 116 assertEquals(repeat('b', Segment.SIZE - 2) + "c", sink.readUtf8()); 117 118 // Source and sink are empty. 119 assertEquals(-1, source.read(sink, 1)); 120 } 121 122 @Test public void sourceFromInputStreamBounds() throws Exception { 123 Source source = Okio.source(new ByteArrayInputStream(new byte[100])); 124 try { 125 source.read(new Buffer(), -1); 126 fail(); 127 } catch (IllegalArgumentException expected) { 128 } 129 } 130} 131