KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > message > io > ByteArraySourceTest


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package org.columba.ristretto.message.io;
37
38 import java.io.IOException JavaDoc;
39 import java.util.regex.Matcher JavaDoc;
40 import java.util.regex.Pattern JavaDoc;
41
42 import org.columba.ristretto.io.ByteBufferSource;
43 import org.columba.ristretto.io.Source;
44
45 import junit.framework.TestCase;
46
47
48 public class ByteArraySourceTest extends TestCase {
49
50     /**
51      * Constructor for StringSourceTest.
52      * @param arg0
53      */

54     public ByteArraySourceTest(String JavaDoc arg0) {
55         super(arg0);
56     }
57     
58     public void testFromActualPosition1() {
59         try {
60             Source source = new ByteBufferSource("This is a test".getBytes("ISO-8859-1"));
61             source.seek(10);
62             Source subsource = source.fromActualPosition();
63             assertTrue(subsource.next() == 't');
64             assertTrue(subsource.next() == 'e');
65             assertTrue(subsource.next() == 's');
66             assertTrue(subsource.next() == 't');
67         } catch (IOException JavaDoc e) {
68             e.printStackTrace();
69         }
70     }
71
72
73     public void testFromActualPosition2() {
74         try {
75             Source source = new ByteBufferSource("This is a test".getBytes("ISO-8859-1"));
76             source.seek(5);
77             Source subsource = source.fromActualPosition();
78             assertTrue(subsource.toString().equals("is a test"));
79         } catch (IOException JavaDoc e) {
80             e.printStackTrace();
81         }
82     }
83
84
85     public void testFromActualPosition3() {
86         try {
87             Source source = new ByteBufferSource("This is a test".getBytes("ISO-8859-1"));
88             source.seek(5);
89             Source subsource = source.fromActualPosition();
90             subsource.seek(5);
91             Source subsubsource = subsource.fromActualPosition();
92             assertTrue(subsubsource.toString().equals("test"));
93         } catch (IOException JavaDoc e) {
94             e.printStackTrace();
95         }
96     }
97
98
99     public void testNext() {
100         try {
101             Source source = new ByteBufferSource("test".getBytes("ISO-8859-1"));
102             assertTrue(source.next() == 't');
103             assertTrue(source.next() == 'e');
104             assertTrue(source.next() == 's');
105             assertTrue(source.next() == 't');
106         } catch (IOException JavaDoc e) {
107             e.printStackTrace();
108         }
109     }
110
111
112     public void testRegexp() throws Exception JavaDoc {
113         Source test = new ByteBufferSource("This is a test\r\n--boundary\r\n".getBytes("ISO-8859-1"));
114
115         Pattern JavaDoc pattern = Pattern.compile("\\r\\n--boundary\\r\\n");
116         Matcher JavaDoc matcher = pattern.matcher(test);
117         assertTrue(matcher.find());
118     }
119
120
121     public void testSeek() {
122         try {
123             Source source = new ByteBufferSource("This is a test".getBytes("ISO-8859-1"));
124             source.seek(10);
125             assertTrue(source.next() == 't');
126             assertTrue(source.next() == 'e');
127             assertTrue(source.next() == 's');
128             assertTrue(source.next() == 't');
129         } catch (IOException JavaDoc e) {
130             e.printStackTrace();
131         }
132     }
133
134
135     public void testSubSequence1() {
136         try {
137             Source source = new ByteBufferSource("This is a test".getBytes("ISO-8859-1"));
138             Source subsource = (Source) source.subSequence(10, 13);
139             assertTrue(subsource.next() == 't');
140             assertTrue(subsource.next() == 'e');
141             assertTrue(subsource.next() == 's');
142             assertTrue(subsource.next() == 't');
143         } catch (IOException JavaDoc e) {
144             e.printStackTrace();
145         }
146     }
147
148
149     public void testSubSequence2() throws Exception JavaDoc {
150         Source source = new ByteBufferSource("This is a test".getBytes("ISO-8859-1"));
151         Source subsource = (Source) source.subSequence(5, 9);
152         assertTrue(subsource.toString().equals("is a"));
153     }
154
155
156     public void testSubSequence3() throws Exception JavaDoc {
157         Source source = new ByteBufferSource("This is a test".getBytes("ISO-8859-1"));
158         Source subsource = (Source) source.subSequence(5, 9);
159         Source subsubsource = (Source) subsource.subSequence(0, 2);
160         assertTrue(subsubsource.toString().equals("is"));
161     }
162
163
164 }
165
Popular Tags