source: contrib/MailArchiver/sources/vendor/mime4j/custom/core/src/test/java/org/apache/james/mime4j/io/BufferedLineReaderInputStreamBufferTest.java @ 6785

Revision 6785, 8.5 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado codigo do MailArchiver?. Documentação na subpasta DOCS.

Line 
1/****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one   *
3 * or more contributor license agreements.  See the NOTICE file *
4 * distributed with this work for additional information        *
5 * regarding copyright ownership.  The ASF licenses this file   *
6 * to you under the Apache License, Version 2.0 (the            *
7 * "License"); you may not use this file except in compliance   *
8 * with the License.  You may obtain a copy of the License at   *
9 *                                                              *
10 *   http://www.apache.org/licenses/LICENSE-2.0                 *
11 *                                                              *
12 * Unless required by applicable law or agreed to in writing,   *
13 * software distributed under the License is distributed on an  *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15 * KIND, either express or implied.  See the License for the    *
16 * specific language governing permissions and limitations      *
17 * under the License.                                           *
18 ****************************************************************/
19
20package org.apache.james.mime4j.io;
21
22import org.apache.james.mime4j.io.BufferedLineReaderInputStream;
23
24import java.io.ByteArrayInputStream;
25
26import junit.framework.TestCase;
27
28public class BufferedLineReaderInputStreamBufferTest extends TestCase {
29
30    public void testInvalidInput() throws Exception {
31        String text = "blah blah yada yada";
32        byte[] b1 = text.getBytes("US-ASCII");
33        String pattern = "blah";
34        byte[] b2 = pattern.getBytes("US-ASCII");
35        BufferedLineReaderInputStream inbuffer = new BufferedLineReaderInputStream(new ByteArrayInputStream(b1), 4096);
36        inbuffer.fillBuffer();
37       
38        assertEquals('b', inbuffer.read());
39        assertEquals('l', inbuffer.read());
40       
41        try {
42            inbuffer.charAt(1);
43            fail("IndexOutOfBoundsException should have been thrown");
44        } catch (IndexOutOfBoundsException expected) {
45        }
46        try {
47            inbuffer.charAt(20);
48            fail("IndexOutOfBoundsException should have been thrown");
49        } catch (IndexOutOfBoundsException expected) {
50        }
51        try {
52            inbuffer.indexOf(b2, -1, 3);
53            fail("IndexOutOfBoundsException should have been thrown");
54        } catch (IndexOutOfBoundsException expected) {
55        }
56        try {
57            inbuffer.indexOf(b2, 1, 3);
58            fail("IndexOutOfBoundsException should have been thrown");
59        } catch (IndexOutOfBoundsException expected) {
60        }
61        try {
62            inbuffer.indexOf(b2, 2, -1);
63            fail("IndexOutOfBoundsException should have been thrown");
64        } catch (IndexOutOfBoundsException expected) {
65        }
66        try {
67            inbuffer.indexOf(b2, 2, 18);
68            fail("IndexOutOfBoundsException should have been thrown");
69        } catch (IndexOutOfBoundsException expected) {
70        }
71        assertEquals(5, inbuffer.indexOf(b2, 2, 17));
72        try {
73            inbuffer.indexOf((byte)' ', -1, 3);
74            fail("IndexOutOfBoundsException should have been thrown");
75        } catch (IndexOutOfBoundsException expected) {
76        }
77        try {
78            inbuffer.indexOf((byte)' ', 1, 3);
79            fail("IndexOutOfBoundsException should have been thrown");
80        } catch (IndexOutOfBoundsException expected) {
81        }
82        try {
83            inbuffer.indexOf((byte)' ', 2, -1);
84            fail("IndexOutOfBoundsException should have been thrown");
85        } catch (IndexOutOfBoundsException expected) {
86        }
87        try {
88            inbuffer.indexOf((byte)' ', 2, 18);
89            fail("IndexOutOfBoundsException should have been thrown");
90        } catch (IndexOutOfBoundsException expected) {
91        }
92        assertEquals(10, inbuffer.indexOf((byte)'y', 2, 17));
93    }
94     
95    public void testBasicOperations() throws Exception {
96        String text = "bla bla yada yada haha haha";
97        byte[] b1 = text.getBytes("US-ASCII");
98        BufferedLineReaderInputStream inbuffer = new BufferedLineReaderInputStream(new ByteArrayInputStream(b1), 4096);
99        inbuffer.fillBuffer();
100        assertEquals(0, inbuffer.pos());
101        assertEquals(27, inbuffer.limit());
102        assertEquals(27, inbuffer.length());
103
104        inbuffer.read();
105        inbuffer.read();
106
107        assertEquals(2, inbuffer.pos());
108        assertEquals(27, inbuffer.limit());
109        assertEquals(25, inbuffer.length());
110       
111        byte[] tmp1 = new byte[3];
112        assertEquals(3, inbuffer.read(tmp1));
113
114        assertEquals(5, inbuffer.pos());
115        assertEquals(27, inbuffer.limit());
116        assertEquals(22, inbuffer.length());
117       
118        byte[] tmp2 = new byte[22];
119        assertEquals(22, inbuffer.read(tmp2));
120
121        assertEquals(27, inbuffer.pos());
122        assertEquals(27, inbuffer.limit());
123        assertEquals(0, inbuffer.length());
124
125        assertEquals(-1, inbuffer.read(tmp1));
126        assertEquals(-1, inbuffer.read(tmp1));
127        assertEquals(-1, inbuffer.read());
128        assertEquals(-1, inbuffer.read());
129    }
130
131    public void testPatternMatching1() throws Exception {
132        String text = "blabla d is the word";
133        String pattern = "d";
134        byte[] b1 = text.getBytes("US-ASCII");
135        byte[] b2 = pattern.getBytes("US-ASCII");
136        BufferedLineReaderInputStream inbuffer = new BufferedLineReaderInputStream(new ByteArrayInputStream(b1), 4096);
137        inbuffer.fillBuffer();
138        int i = inbuffer.indexOf(b2);
139        assertEquals(7, i);
140    }
141   
142    public void testPatternMatching2() throws Exception {
143        String text = "disddisdissdsidsidsiid";
144        String pattern = "siid";
145        byte[] b1 = text.getBytes("US-ASCII");
146        byte[] b2 = pattern.getBytes("US-ASCII");
147        BufferedLineReaderInputStream inbuffer = new BufferedLineReaderInputStream(new ByteArrayInputStream(b1), 4096);
148        inbuffer.fillBuffer();
149        int i = inbuffer.indexOf(b2);
150        assertEquals(18, i);
151    }
152   
153    public void testPatternMatching3() throws Exception {
154        String text = "bla bla yada yada haha haha";
155        String pattern = "blah";
156        byte[] b1 = text.getBytes("US-ASCII");
157        byte[] b2 = pattern.getBytes("US-ASCII");
158        BufferedLineReaderInputStream inbuffer = new BufferedLineReaderInputStream(new ByteArrayInputStream(b1), 4096);
159        inbuffer.fillBuffer();
160        int i = inbuffer.indexOf(b2);
161        assertEquals(-1, i);
162    }
163   
164    public void testPatternMatching4() throws Exception {
165        String text = "bla bla yada yada haha haha";
166        String pattern = "bla";
167        byte[] b1 = text.getBytes("US-ASCII");
168        byte[] b2 = pattern.getBytes("US-ASCII");
169        BufferedLineReaderInputStream inbuffer = new BufferedLineReaderInputStream(new ByteArrayInputStream(b1), 4096);
170        inbuffer.fillBuffer();
171        int i = inbuffer.indexOf(b2);
172        assertEquals(0, i);
173    }
174
175    public void testPatternOutOfBound() throws Exception {
176        String text = "bla bla yada yada haha haha";
177        String pattern1 = "bla bla";
178        byte[] b1 = text.getBytes("US-ASCII");
179        byte[] b2 = pattern1.getBytes("US-ASCII");
180        BufferedLineReaderInputStream inbuffer = new BufferedLineReaderInputStream(new ByteArrayInputStream(b1), 4096);
181        inbuffer.fillBuffer();
182        byte[] tmp = new byte[3];
183        inbuffer.read(tmp);
184        int i = inbuffer.indexOf(b2, inbuffer.pos(), inbuffer.length());
185        assertEquals(-1, i);
186        i = inbuffer.indexOf(b2, inbuffer.pos(), inbuffer.length() - 1);
187        assertEquals(-1, i);
188    }
189
190    public void testCharOutOfBound() throws Exception {
191        String text = "zzz blah blah blah ggg";
192        byte[] b1 = text.getBytes("US-ASCII");
193        BufferedLineReaderInputStream inbuffer = new BufferedLineReaderInputStream(new ByteArrayInputStream(b1), 4096);
194        inbuffer.fillBuffer();
195        byte[] tmp = new byte[3];
196        inbuffer.read(tmp);
197        int i = inbuffer.indexOf((byte)'z', inbuffer.pos(), inbuffer.length());
198        assertEquals(-1, i);
199        i = inbuffer.indexOf((byte)'g', inbuffer.pos(), inbuffer.length() - 3);
200        assertEquals(-1, i);
201    }
202   
203    public void test0xFFInBinaryStream() throws Exception {
204        byte[] b1 = new byte[] {1, 2, 3, (byte) 0xff, 10, 1, 2, 3};
205        byte[] b2 = new byte[] {10};
206        BufferedLineReaderInputStream inbuffer = new BufferedLineReaderInputStream(new ByteArrayInputStream(b1), 4096);
207        inbuffer.fillBuffer();
208        int i = inbuffer.indexOf(b2);
209        assertEquals(4, i);
210    }
211}
Note: See TracBrowser for help on using the repository browser.