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.ByteArrayOutputStream; 35 import java.io.IOException; 36 import junit.framework.TestCase; 37 import org.easymock.MockControl; 38 39 /*** 40 * LineDelimiterInspector test case. 41 * 42 * @author Mathieu Champlon 43 * @version $Revision$ $Date$ 44 */ 45 public class LineDelimiterInspectorTest extends TestCase 46 { 47 /*** 48 * Tested object. 49 */ 50 private LineDelimiterInspector inspector; 51 /*** 52 * Stub objects. 53 */ 54 private ByteArrayOutputStream outputStreamStub; 55 56 protected void setUp() 57 { 58 outputStreamStub = new ByteArrayOutputStream(); 59 } 60 61 private void assertArrayEquals( byte[] array1, byte[] array2 ) 62 { 63 final MockControl controlArrayComparator = MockControl.createControl( IArrayComparator.class ); 64 final IArrayComparator mockArrayComparator = (IArrayComparator)controlArrayComparator.getMock(); 65 mockArrayComparator.compare( array1 ); 66 controlArrayComparator.setMatcher( MockControl.ARRAY_MATCHER ); 67 controlArrayComparator.replay(); 68 mockArrayComparator.compare( array2 ); 69 controlArrayComparator.verify(); 70 } 71 72 private ByteArrayInputStream createInputStreamStub( final String string ) 73 { 74 return new ByteArrayInputStream( string.getBytes() ); 75 } 76 77 public void testDefaultDelimiterIsCRLF() throws IOException 78 { 79 inspector = new LineDelimiterInspector(); 80 inspector.write( outputStreamStub ); 81 assertArrayEquals( "\r\n".getBytes(), outputStreamStub.toByteArray() ); 82 } 83 84 public void testWrappedStreamContentIsForwarded() throws IOException 85 { 86 final String string = "this is a test"; 87 inspector = new LineDelimiterInspector( createInputStreamStub( string ) ); 88 final byte[] output = new byte[string.length()]; 89 inspector.read( output ); 90 assertArrayEquals( string.getBytes(), output ); 91 } 92 93 public void testWrappedStreamPartialContentIsForwarded() throws IOException 94 { 95 final String string = "this is a test"; 96 inspector = new LineDelimiterInspector( createInputStreamStub( string ) ); 97 byte[] output = new byte[5]; 98 inspector.read( output, 0, 5 ); 99 assertArrayEquals( string.substring( 0, 5 ).getBytes(), output ); 100 } 101 102 public void testDetectsCR() throws IOException 103 { 104 final String string = "this is\r a test"; 105 inspector = new LineDelimiterInspector( createInputStreamStub( string ) ); 106 final byte[] output = new byte[string.length()]; 107 inspector.read( output ); 108 assertArrayEquals( string.getBytes(), output ); 109 inspector.write( outputStreamStub ); 110 assertArrayEquals( "\r".getBytes(), outputStreamStub.toByteArray() ); 111 } 112 113 public void testDetectsLF() throws IOException 114 { 115 final String string = "this is\n a test"; 116 inspector = new LineDelimiterInspector( createInputStreamStub( string ) ); 117 final byte[] output = new byte[string.length()]; 118 inspector.read( output ); 119 assertArrayEquals( string.getBytes(), output ); 120 inspector.write( outputStreamStub ); 121 assertArrayEquals( "\n".getBytes(), outputStreamStub.toByteArray() ); 122 } 123 124 public void testDetectsCRLF() throws IOException 125 { 126 final String string = "this is\r\n a test"; 127 inspector = new LineDelimiterInspector( createInputStreamStub( string ) ); 128 final byte[] output = new byte[string.length()]; 129 inspector.read( output, 0, string.length() ); 130 assertArrayEquals( string.getBytes(), output ); 131 inspector.write( outputStreamStub ); 132 assertArrayEquals( "\r\n".getBytes(), outputStreamStub.toByteArray() ); 133 } 134 135 public void testPartialStreamDetectsCRLF() throws IOException 136 { 137 final String string = "\r\n\r\n"; 138 inspector = new LineDelimiterInspector( createInputStreamStub( string ) ); 139 final byte[] output = new byte[3]; 140 for( int i = 0; i < string.length() / output.length + 1; ++i ) 141 inspector.read( output, 0, output.length ); 142 inspector.write( outputStreamStub ); 143 assertArrayEquals( "\r\n".getBytes(), outputStreamStub.toByteArray() ); 144 } 145 }