Debian Codegraph @

src/test/java/ezvcard/util/GobbleTest.java

raw symbols SCIP
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 Copyright (c) 2012-2023, Michael Angstadt
21 All rights reserved.
22
23 Redistribution and use in source and binary forms, with or without
24 modification, are permitted provided that the following conditions are met:
25
26 1. Redistributions of source code must retain the above copyright notice, this
27 list of conditions and the following disclaimer.
28 2. Redistributions in binary form must reproduce the above copyright notice,
29 this list of conditions and the following disclaimer in the documentation
30 and/or other materials provided with the distribution.
31
32 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
33 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
36 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
39 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42
43 The views and conclusions contained in the software and documentation are those
44 of the authors and should not be interpreted as representing official policies,
45 either expressed or implied, of the FreeBSD Project.
46 */
47
48 /**
49 * @author Michael Angstadt
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()); //input stream was consumed
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 //expected
95 }
96 }
97 }