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