KickJava   Java API By Example, From Geeks To Geeks.

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


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.CharSequenceSource;
43 import org.columba.ristretto.io.ConcatenatedSource;
44 import org.columba.ristretto.io.Source;
45
46 import junit.framework.TestCase;
47
48 public class ConcatenatedSourceTest extends TestCase {
49
50     /**
51      * Constructor for ConcatedatedSourceTest.
52      * @param arg0
53      */

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