| 1 |
package ezvcard.util; |
| 2 |
|
| 3 |
import static org.junit.Assert.assertArrayEquals; |
| 4 |
import static org.junit.Assert.assertEquals; |
| 5 |
import static org.junit.Assert.fail; |
| 6 |
|
| 7 |
import java.io.ByteArrayInputStream; |
| 8 |
import java.io.InputStream; |
| 9 |
import java.io.Reader; |
| 10 |
import java.io.StringReader; |
| 11 |
import java.io.Writer; |
| 12 |
import java.nio.file.Files; |
| 13 |
import java.nio.file.Path; |
| 14 |
|
| 15 |
import org.junit.Rule; |
| 16 |
import org.junit.Test; |
| 17 |
import org.junit.rules.TemporaryFolder; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class GobbleTest { |
| 52 |
@Rule |
| 53 |
public TemporaryFolder folder = new TemporaryFolder(); |
| 54 |
|
| 55 |
@Test |
| 56 |
public void file() throws Exception { |
| 57 |
String data = "one two three"; |
| 58 |
|
| 59 |
Path file = folder.newFile().toPath(); |
| 60 |
try (Writer writer = Files.newBufferedWriter(file)) { |
| 61 |
writer.write(data); |
| 62 |
} |
| 63 |
|
| 64 |
Gobble stream = new Gobble(file); |
| 65 |
assertEquals(data, stream.asString()); |
| 66 |
assertArrayEquals(data.getBytes(), stream.asByteArray()); |
| 67 |
} |
| 68 |
|
| 69 |
@Test |
| 70 |
public void inputStream() throws Exception { |
| 71 |
String data = "one two three"; |
| 72 |
|
| 73 |
InputStream in = new ByteArrayInputStream(data.getBytes()); |
| 74 |
Gobble stream = new Gobble(in); |
| 75 |
assertEquals(data, stream.asString()); |
| 76 |
assertArrayEquals(new byte[0], stream.asByteArray()); |
| 77 |
|
| 78 |
in = new ByteArrayInputStream(data.getBytes()); |
| 79 |
stream = new Gobble(in); |
| 80 |
assertArrayEquals(data.getBytes(), stream.asByteArray()); |
| 81 |
} |
| 82 |
|
| 83 |
@Test |
| 84 |
public void reader() throws Exception { |
| 85 |
String data = "one two three"; |
| 86 |
|
| 87 |
Reader reader = new StringReader(data); |
| 88 |
Gobble stream = new Gobble(reader); |
| 89 |
assertEquals(data, stream.asString()); |
| 90 |
try { |
| 91 |
stream.asByteArray(); |
| 92 |
fail(); |
| 93 |
} catch (IllegalStateException e) { |
| 94 |
|
| 95 |
} |
| 96 |
} |
| 97 |
} |