View Javadoc

1   /*
2    * #%L
3    * JavaHg
4    * %%
5    * Copyright (C) 2011 aragost Trifork ag
6    * %%
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   * 
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   * 
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
24   * #L%
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 }