KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.util.regex.Matcher JavaDoc;
41 import java.util.regex.Pattern JavaDoc;
42
43 import org.columba.ristretto.io.FileSource;
44 import org.columba.ristretto.io.Source;
45
46 import junit.framework.TestCase;
47
48 public class FileSourceTest extends TestCase {
49
50     private static final String JavaDoc TEST_FILENAME_STR = "src/test/org/columba/ristretto/message/io/FileSourceTest.eml";
51
52     /**
53      * Constructor for FileSourceTest.
54      *
55      * @param arg0 name of test
56      */

57     public FileSourceTest(String JavaDoc arg0) {
58         super(arg0);
59     }
60
61     public void testLength() throws IOException JavaDoc {
62         File JavaDoc file = new File JavaDoc(TEST_FILENAME_STR);
63         FileSource source = new FileSource(file);
64         assertEquals( file.length(), source.length() );
65     }
66     
67     public void testFromActualPosition1() throws IOException JavaDoc {
68         FileSource source = new FileSource(new File JavaDoc(TEST_FILENAME_STR));
69         source.seek(10);
70         Source subsource = source.fromActualPosition();
71         assertTrue(subsource.next() == 't');
72         assertTrue(subsource.next() == 'e');
73         assertTrue(subsource.next() == 's');
74         assertTrue(subsource.next() == 't');
75     }
76
77     public void testFromActualPosition2() throws IOException JavaDoc {
78         FileSource source = new FileSource(new File JavaDoc(TEST_FILENAME_STR));
79         source.seek(50);
80         Source subsource = source.fromActualPosition();
81         assertEquals("is a test", subsource.toString());
82     }
83
84     public void testFromActualPosition3() throws IOException JavaDoc {
85         FileSource source = new FileSource(new File JavaDoc(TEST_FILENAME_STR));
86         source.seek(50);
87         Source subsource = source.fromActualPosition();
88         subsource.seek(5);
89         Source subsubsource = subsource.fromActualPosition();
90         assertTrue(subsubsource.toString().equals("test"));
91     }
92
93     public void testNext() throws IOException JavaDoc {
94         FileSource source = new FileSource(new File JavaDoc(TEST_FILENAME_STR));
95         assertTrue(source.next() == 'T');
96         assertTrue(source.next() == 'h');
97         assertTrue(source.next() == 'i');
98         assertTrue(source.next() == 's');
99     }
100
101     public void testRegexp() throws IOException JavaDoc {
102         FileSource source = new FileSource(new File JavaDoc(TEST_FILENAME_STR));
103         Pattern JavaDoc pattern = Pattern.compile("test");
104         Matcher JavaDoc matcher = pattern.matcher(source);
105         assertTrue(matcher.find());
106     }
107
108     public void testSeek() throws IOException JavaDoc {
109         FileSource source = new FileSource(new File JavaDoc(TEST_FILENAME_STR));
110         source.seek(10);
111         assertTrue(source.next() == 't');
112         assertTrue(source.next() == 'e');
113         assertTrue(source.next() == 's');
114         assertTrue(source.next() == 't');
115     }
116
117     public void testSubSequence1() throws IOException JavaDoc {
118         FileSource source = new FileSource(new File JavaDoc(TEST_FILENAME_STR));
119         Source subsource = source.subSource(10, 13);
120         assertTrue(subsource.next() == 't');
121         assertTrue(subsource.next() == 'e');
122         assertTrue(subsource.next() == 's');
123         assertTrue(subsource.next() == 't');
124     }
125
126     public void testSubSequence2() throws IOException JavaDoc {
127         FileSource source = new FileSource(new File JavaDoc(TEST_FILENAME_STR));
128         Source subsource = source.subSource(5, 9);
129         assertTrue(subsource.toString().equals("is a"));
130     }
131
132     public void testSubSequence3() throws IOException JavaDoc {
133         FileSource source = new FileSource(new File JavaDoc(TEST_FILENAME_STR));
134         Source subsource = source.subSource(5, 9);
135         Source subsubsource = subsource.subSource(0, 2);
136         assertTrue(subsubsource.toString().equals("is"));
137     }
138
139 }
140
Popular Tags