1 /*** 2 * Redistribution and use in source and binary forms, with or without 3 * modification, are permitted provided that the following conditions are 4 * met : 5 * 6 * . Redistributions of source code must retain the above copyright 7 * notice, this list of conditions and the following disclaimer. 8 * 9 * . Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * . The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $Id$ 29 */ 30 31 package palmed.edit.text; 32 33 import java.io.ByteArrayInputStream; 34 import java.io.IOException; 35 import palmed.util.EasyMockTestCase; 36 37 /*** 38 * TextReader test case. 39 * 40 * @author Mathieu Champlon 41 * @version $Revision$ $Date$ 42 */ 43 public class TextReaderTest extends EasyMockTestCase 44 { 45 /*** 46 * Tested object. 47 */ 48 private TextReader reader; 49 /*** 50 * Mock objects. 51 */ 52 private ITextBuilder mockBuilder; 53 54 protected void setUp() 55 { 56 mockBuilder = (ITextBuilder)createMock( ITextBuilder.class ); 57 } 58 59 private ByteArrayInputStream createInputStreamStub( final String string ) 60 { 61 return new ByteArrayInputStream( string.getBytes() ); 62 } 63 64 public void testReadWholeStringWithoutDelimiter() throws IOException 65 { 66 final String string = "this is a test"; 67 reader = new TextReader( createInputStreamStub( string ), mockBuilder ); 68 byte[] buffer = new byte[100]; 69 mockBuilder.append( buffer, 0, string.length() ); 70 replay(); 71 reader.read( buffer ); 72 verify(); 73 } 74 75 public void testReadPartOfStringWithoutDelimiter() throws IOException 76 { 77 final String string = "this is a test"; 78 reader = new TextReader( createInputStreamStub( string ), mockBuilder ); 79 byte[] buffer = new byte[5]; 80 mockBuilder.append( buffer, 0, buffer.length ); 81 replay(); 82 reader.read( buffer ); 83 verify(); 84 } 85 86 public void testReadWholeStringWithCR() throws IOException 87 { 88 final String string1 = "this is"; 89 final String string2 = " a test"; 90 reader = new TextReader( createInputStreamStub( string1 + '\r' + string2 ), mockBuilder ); 91 byte[] buffer = new byte[100]; 92 mockBuilder.append( buffer, 0, string1.length() ); 93 mockBuilder.appendNewLine(); 94 mockBuilder.append( buffer, string1.length() + 1, string2.length() ); 95 replay(); 96 reader.read( buffer ); 97 verify(); 98 } 99 100 public void testReadWholeStringWithLF() throws IOException 101 { 102 final String string1 = "this is"; 103 final String string2 = " a test"; 104 reader = new TextReader( createInputStreamStub( string1 + '\n' + string2 ), mockBuilder ); 105 byte[] buffer = new byte[100]; 106 mockBuilder.append( buffer, 0, string1.length() ); 107 mockBuilder.appendNewLine(); 108 mockBuilder.append( buffer, string1.length() + 1, string2.length() ); 109 replay(); 110 reader.read( buffer ); 111 verify(); 112 } 113 114 public void testReadWholeStringWithCRLF() throws IOException 115 { 116 final String string1 = "this is"; 117 final String string2 = " a test"; 118 reader = new TextReader( createInputStreamStub( string1 + "\r\n" + string2 ), mockBuilder ); 119 byte[] buffer = new byte[100]; 120 mockBuilder.append( buffer, 0, string1.length() ); 121 mockBuilder.appendNewLine(); 122 mockBuilder.append( buffer, string1.length() + 2, string2.length() ); 123 replay(); 124 reader.read( buffer ); 125 verify(); 126 } 127 128 public void testReadPartStringWithCRLF() throws IOException 129 { 130 final String string = "\r\n\r\nthis \r\n\r\nis a test\r\n\r\n"; 131 reader = new TextReader( createInputStreamStub( string ), mockBuilder ); 132 byte[] buffer = new byte[3]; 133 mockBuilder.appendNewLine(); 134 mockBuilder.appendNewLine(); 135 mockBuilder.append( buffer, 1, 2 ); 136 mockBuilder.append( buffer, 0, 3 ); 137 mockBuilder.appendNewLine(); 138 mockBuilder.appendNewLine(); 139 mockBuilder.append( buffer, 1, 2 ); 140 mockBuilder.append( buffer, 0, 3 ); 141 mockBuilder.append( buffer, 0, 3 ); 142 mockBuilder.append( buffer, 0, 1 ); 143 mockBuilder.appendNewLine(); 144 mockBuilder.appendNewLine(); 145 replay(); 146 for( int i = 0; i < string.length() / buffer.length + 1; ++i ) 147 reader.read( buffer ); 148 verify(); 149 } 150 }