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.Source; 44 45 import junit.framework.TestCase; 46 47 public class StringSourceTest extends TestCase { 48 49 54 public StringSourceTest(String 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 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 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 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 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 pattern = Pattern.compile("\\r\\n--boundary\\r\\n"); 115 Matcher 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 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 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 |