1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package com.aragost.javahg.internals;
27
28 import java.io.ByteArrayInputStream;
29 import java.io.IOException;
30 import java.nio.charset.CharsetDecoder;
31
32 import junit.framework.Assert;
33
34 import org.junit.Test;
35
36 import com.aragost.javahg.test.AbstractTestCase;
37
38 public class HgInputStreamTest extends AbstractTestCase {
39
40 @Test
41 public void testUpToNoMatch() throws IOException {
42 HgInputStream stream = createStream("abcc");
43 byte[] bytes = stream.upTo("ccc".getBytes());
44 Assert.assertNull(bytes);
45 }
46
47 @Test
48 public void testUpToMatch() throws IOException {
49 HgInputStream stream = createStream("aaaaab");
50 String s = new String(stream.upTo("aaab".getBytes()));
51 Assert.assertEquals("aa", s);
52 }
53
54 @Test
55 public void testMatch() throws IOException {
56 HgInputStream stream = createStream("aaa");
57 boolean match = stream.match("aa".getBytes());
58 Assert.assertTrue(match);
59 }
60
61 @Test
62 public void testPeek() throws IOException {
63 HgInputStream stream = createStream("a");
64 Assert.assertEquals('a', stream.peek());
65 Assert.assertEquals('a', stream.read());
66 Assert.assertEquals(-1, stream.peek());
67 Assert.assertEquals(-1, stream.peek());
68 }
69
70 @Test
71 public void testReadDecimal() throws IOException {
72 HgInputStream stream = createStream("");
73 Assert.assertNull(stream.readDecimal());
74 Assert.assertEquals(-1, stream.read());
75
76 stream = createStream("0");
77 Assert.assertEquals(Integer.valueOf(0), stream.readDecimal());
78 Assert.assertEquals(-1, stream.read());
79
80 stream = createStream("1234321.");
81 Assert.assertEquals(Integer.valueOf(1234321), stream.readDecimal());
82 Assert.assertEquals('.', stream.read());
83
84 }
85
86 @Test(expected = IllegalArgumentException.class)
87 public void testFindEmptyInEmptyStream() throws IOException {
88 HgInputStream stream = createStream("");
89 stream.find(new byte[0]);
90 }
91
92 @Test(expected = IllegalArgumentException.class)
93 public void testFindEmptyInNonEmptyStream() throws IOException {
94 HgInputStream stream = createStream("aaa");
95 stream.find(new byte[0]);
96 }
97
98 @Test
99 public void testFindInEmptyStream() throws IOException {
100 HgInputStream stream = createStream("");
101 boolean found = stream.find("a".getBytes());
102 Assert.assertFalse(found);
103 Assert.assertTrue(stream.isEof());
104 }
105
106 @Test
107 public void testFindNoMatch() throws IOException {
108 HgInputStream stream = createStream("abccde");
109 boolean found = stream.find("ccc".getBytes());
110 Assert.assertFalse(found);
111 Assert.assertTrue(stream.isEof());
112 }
113
114 @Test
115 public void testFindMatchAtEof() throws IOException {
116 HgInputStream stream = createStream("aaaaab");
117 boolean found = stream.find("aab".getBytes());
118 Assert.assertTrue(found);
119 Assert.assertTrue(stream.isEof());
120 }
121
122 @Test
123 public void testFindMatch() throws IOException {
124 HgInputStream stream = createStream("aaaaabc");
125 boolean found = stream.find("aab".getBytes());
126 Assert.assertTrue(found);
127 Assert.assertFalse(stream.isEof());
128 Assert.assertEquals("c", Utils.readStream(stream, utf8().newDecoder()));
129 }
130
131 private HgInputStream createStream(String s) {
132 CharsetDecoder decoder = utf8().newDecoder();
133 return new HgInputStream(new ByteArrayInputStream(s.getBytes()), decoder);
134 }
135 }