|
| 1 | +package org.baeldung.guava; |
| 2 | + |
| 3 | +import static org.hamcrest.Matchers.contains; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | +import static org.junit.Assert.assertThat; |
| 6 | + |
| 7 | +import java.io.DataInputStream; |
| 8 | +import java.io.File; |
| 9 | +import java.io.FileInputStream; |
| 10 | +import java.io.FileOutputStream; |
| 11 | +import java.io.FileReader; |
| 12 | +import java.io.IOException; |
| 13 | +import java.net.URL; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +import com.google.common.base.Charsets; |
| 19 | +import com.google.common.base.Joiner; |
| 20 | +import com.google.common.collect.Lists; |
| 21 | +import com.google.common.io.ByteSink; |
| 22 | +import com.google.common.io.ByteSource; |
| 23 | +import com.google.common.io.ByteStreams; |
| 24 | +import com.google.common.io.CharSink; |
| 25 | +import com.google.common.io.CharSource; |
| 26 | +import com.google.common.io.CharStreams; |
| 27 | +import com.google.common.io.Files; |
| 28 | +import com.google.common.io.LittleEndianDataInputStream; |
| 29 | +import com.google.common.io.LittleEndianDataOutputStream; |
| 30 | +import com.google.common.io.Resources; |
| 31 | + |
| 32 | +public class GuavaIOTest { |
| 33 | + |
| 34 | + @Test |
| 35 | + public void whenWriteUsingFiles_thenWritten() throws IOException { |
| 36 | + final String expected_value = "Hello world"; |
| 37 | + final File file = new File("src/test/resources/test.out"); |
| 38 | + |
| 39 | + Files.write(expected_value, file, Charsets.UTF_8); |
| 40 | + |
| 41 | + final String result = Files.toString(file, Charsets.UTF_8); |
| 42 | + assertEquals(expected_value, result); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void whenWriteStringBuilderUsingFiles_thenWritten() throws IOException { |
| 47 | + final String expected_value = "Hello world"; |
| 48 | + final File file = new File("src/test/resources/test.out"); |
| 49 | + final StringBuilder builder = new StringBuilder(); |
| 50 | + builder.append(expected_value); |
| 51 | + |
| 52 | + Files.write(builder, file, Charsets.UTF_8); |
| 53 | + |
| 54 | + final String result = Files.toString(file, Charsets.UTF_8); |
| 55 | + assertEquals(expected_value, result); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void whenWriteUsingCharSink_thenWritten() throws IOException { |
| 60 | + final String expected_value = "Hello world"; |
| 61 | + final File file = new File("src/test/resources/test.out"); |
| 62 | + final CharSink sink = Files.asCharSink(file, Charsets.UTF_8); |
| 63 | + |
| 64 | + sink.write(expected_value); |
| 65 | + |
| 66 | + final String result = Files.toString(file, Charsets.UTF_8); |
| 67 | + assertEquals(expected_value, result); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void whenWriteMultipleLinesUsingCharSink_thenWritten() throws IOException { |
| 72 | + final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom"); |
| 73 | + final File file = new File("src/test/resources/test.out"); |
| 74 | + final CharSink sink = Files.asCharSink(file, Charsets.UTF_8); |
| 75 | + |
| 76 | + sink.writeLines(names, " "); |
| 77 | + |
| 78 | + final String result = Files.toString(file, Charsets.UTF_8); |
| 79 | + final String expected_value = Joiner.on(" ").join(names); |
| 80 | + assertEquals(expected_value, result.trim()); |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void whenWriteUsingByteSink_thenWritten() throws IOException { |
| 86 | + final String expected_value = "Hello world"; |
| 87 | + final File file = new File("src/test/resources/test.out"); |
| 88 | + final ByteSink sink = Files.asByteSink(file); |
| 89 | + |
| 90 | + sink.write(expected_value.getBytes()); |
| 91 | + |
| 92 | + final String result = Files.toString(file, Charsets.UTF_8); |
| 93 | + assertEquals(expected_value, result); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void whenReadUsingFiles_thenRead() throws IOException { |
| 98 | + final String expected_value = "Hello world"; |
| 99 | + final File file = new File("src/test/resources/test1.in"); |
| 100 | + |
| 101 | + final String result = Files.toString(file, Charsets.UTF_8); |
| 102 | + assertEquals(expected_value, result); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void whenReadMultipleLinesUsingFiles_thenRead() throws IOException { |
| 107 | + final File file = new File("src/test/resources/test2.in"); |
| 108 | + |
| 109 | + final List<String> result = Files.readLines(file, Charsets.UTF_8); |
| 110 | + assertThat(result, contains("John", "Jane", "Adam", "Tom")); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void whenReadUsingCharSource_thenRead() throws IOException { |
| 115 | + final String expected_value = "Hello world"; |
| 116 | + final File file = new File("src/test/resources/test1.in"); |
| 117 | + |
| 118 | + final CharSource source = Files.asCharSource(file, Charsets.UTF_8); |
| 119 | + |
| 120 | + final String result = source.read(); |
| 121 | + assertEquals(expected_value, result); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void whenReadMultipleCharSources_thenRead() throws IOException { |
| 126 | + final String expected_value = "Hello worldTest"; |
| 127 | + final File file1 = new File("src/test/resources/test1.in"); |
| 128 | + final File file2 = new File("src/test/resources/test1_1.in"); |
| 129 | + |
| 130 | + final CharSource source1 = Files.asCharSource(file1, Charsets.UTF_8); |
| 131 | + final CharSource source2 = Files.asCharSource(file2, Charsets.UTF_8); |
| 132 | + final CharSource source = CharSource.concat(source1, source2); |
| 133 | + |
| 134 | + final String result = source.read(); |
| 135 | + |
| 136 | + assertEquals(expected_value, result); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void whenReadUsingCharStream_thenRead() throws IOException { |
| 141 | + final String expected_value = "Hello world"; |
| 142 | + |
| 143 | + final FileReader reader = new FileReader("src/test/resources/test1.in"); |
| 144 | + final String result = CharStreams.toString(reader); |
| 145 | + reader.close(); |
| 146 | + |
| 147 | + assertEquals(expected_value, result); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void whenReadUsingByteSource_thenRead() throws IOException { |
| 152 | + final String expected_value = "Hello world"; |
| 153 | + final File file = new File("src/test/resources/test1.in"); |
| 154 | + |
| 155 | + final ByteSource source = Files.asByteSource(file); |
| 156 | + |
| 157 | + final byte[] result = source.read(); |
| 158 | + assertEquals(expected_value, new String(result)); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void whenReadAfterOffsetUsingByteSource_thenRead() throws IOException { |
| 163 | + final String expected_value = "lo world"; |
| 164 | + final File file = new File("src/test/resources/test1.in"); |
| 165 | + |
| 166 | + final long offset = 3; |
| 167 | + final long length = 1000; |
| 168 | + final ByteSource source = Files.asByteSource(file).slice(offset, length); |
| 169 | + |
| 170 | + final byte[] result = source.read(); |
| 171 | + assertEquals(expected_value, new String(result)); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + public void whenReadUsingByteStream_thenRead() throws IOException { |
| 176 | + final String expected_value = "Hello world"; |
| 177 | + |
| 178 | + final FileInputStream reader = new FileInputStream("src/test/resources/test1.in"); |
| 179 | + final byte[] result = ByteStreams.toByteArray(reader); |
| 180 | + reader.close(); |
| 181 | + |
| 182 | + assertEquals(expected_value, new String(result)); |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + public void whenReadUsingResources_thenRead() throws IOException { |
| 187 | + final String expected_value = "Hello world"; |
| 188 | + |
| 189 | + final URL url = Resources.getResource("test1.in"); |
| 190 | + final String result = Resources.toString(url, Charsets.UTF_8); |
| 191 | + |
| 192 | + assertEquals(expected_value, result); |
| 193 | + } |
| 194 | + |
| 195 | + @Test |
| 196 | + public void whenCopyFileUsingFiles_thenCopied() throws IOException { |
| 197 | + final String expected_value = "Hello world"; |
| 198 | + |
| 199 | + final File file1 = new File("src/test/resources/test1.in"); |
| 200 | + final File file2 = new File("src/test/resources/test_copy.in"); |
| 201 | + |
| 202 | + Files.copy(file1, file2); |
| 203 | + final String result = Files.toString(file2, Charsets.UTF_8); |
| 204 | + |
| 205 | + assertEquals(expected_value, result); |
| 206 | + } |
| 207 | + |
| 208 | + @Test |
| 209 | + public void whenWriteReadLittleEndian_thenCorrect() throws IOException { |
| 210 | + final int value = 100; |
| 211 | + |
| 212 | + final LittleEndianDataOutputStream writer = new LittleEndianDataOutputStream(new FileOutputStream("src/test/resources/test_le.txt")); |
| 213 | + writer.writeInt(value); |
| 214 | + writer.close(); |
| 215 | + |
| 216 | + final LittleEndianDataInputStream reader = new LittleEndianDataInputStream(new DataInputStream(new FileInputStream("src/test/resources/test_le.txt"))); |
| 217 | + final int result = reader.readInt(); |
| 218 | + reader.close(); |
| 219 | + |
| 220 | + assertEquals(value, result); |
| 221 | + } |
| 222 | + |
| 223 | + |
| 224 | +} |
| 225 | + |
| 226 | +// |
| 227 | + |
| 228 | +// |
| 229 | +// |
| 230 | +// |
| 231 | +// |
| 232 | +// |
| 233 | + |
0 commit comments