1 18 19 package com.martiansoftware.nailgun; 20 21 import java.io.ByteArrayInputStream ; 22 23 import junit.framework.TestCase; 24 25 29 public class TestNGInputStream extends TestCase { 30 31 private static final byte[] TESTDATA = {0x00, 0x00, 0x00, 0x0e, '0', 32 'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 't', 'e', 's', 't', 33 0x00, 0x00, 0x00, 0x01, '0', '!', 34 0x00, 0x00, 0x00, 0x00, '.' 35 }; 36 37 private static final String TESTSTRING = "This is a test!"; 38 39 public void testNGInputStreamIntoArray() throws Exception { 40 NGInputStream in = new NGInputStream(new ByteArrayInputStream (TESTDATA)); 41 42 assertTrue(in.available() > 0); 43 assertFalse(in.markSupported()); 44 45 byte[] buf = new byte[1024]; 46 int bytesRead = 0; 47 int totalBytes = 0; 48 StringBuffer sbuf = new StringBuffer (); 49 do { 50 bytesRead = in.read(buf); 51 if (bytesRead > 0) { 52 totalBytes += bytesRead; 53 sbuf.append(new String (buf, 0, bytesRead, "US-ASCII")); 54 } 55 } while (bytesRead > 0); 56 assertEquals(15, totalBytes); 57 assertEquals(TESTSTRING, sbuf.toString()); 58 } 59 60 public void testNGInputStreamCharByChar() throws Exception { 61 StringBuffer buf = new StringBuffer (); 62 NGInputStream in = new NGInputStream(new ByteArrayInputStream (TESTDATA)); 63 int c = in.read(); 64 while (c != -1) { 65 buf.append((char) c); 66 c = in.read(); 67 } 68 assertEquals(TESTSTRING, buf.toString()); 69 } 70 } 71 | Popular Tags |