1 36 package org.columba.ristretto.message.io; 37 38 import java.io.IOException ; 39 import java.util.regex.Matcher ; 40 import java.util.regex.Pattern ; 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 54 public ConcatenatedSourceTest(String 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 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 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 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 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 pattern = Pattern.compile("\\r\\n--boundary\\r\\n"); 126 Matcher 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 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 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 |