KickJava   Java API By Example, From Geeks To Geeks.

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


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.Source;
44
45 import junit.framework.TestCase;
46
47 public class StringSourceTest extends TestCase {
48
49     /**
50      * Constructor for StringSourceTest.
51      *
52      * @param arg0
53      */

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