01: /*
02:
03: Copyright 2004, Martian Software, Inc.
04:
05: Licensed under the Apache License, Version 2.0 (the "License");
06: you may not use this file except in compliance with the License.
07: You may obtain a copy of the License at
08:
09: http://www.apache.org/licenses/LICENSE-2.0
10:
11: Unless required by applicable law or agreed to in writing, software
12: distributed under the License is distributed on an "AS IS" BASIS,
13: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: See the License for the specific language governing permissions and
15: limitations under the License.
16:
17: */
18:
19: package com.martiansoftware.nailgun;
20:
21: import java.io.ByteArrayInputStream;
22:
23: import junit.framework.TestCase;
24:
25: /**
26: *
27: * @author <a href="http://www.martiansoftware.com/contact.html">Marty Lamb</a>
28: */
29: public class TestNGInputStream extends TestCase {
30:
31: private static final byte[] TESTDATA = { 0x00, 0x00, 0x00, 0x0e,
32: '0', 'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 't',
33: 'e', 's', 't', 0x00, 0x00, 0x00, 0x01, '0', '!', 0x00,
34: 0x00, 0x00, 0x00, '.' };
35:
36: private static final String TESTSTRING = "This is a test!";
37:
38: public void testNGInputStreamIntoArray() throws Exception {
39: NGInputStream in = new NGInputStream(new ByteArrayInputStream(
40: 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(
63: TESTDATA));
64: int c = in.read();
65: while (c != -1) {
66: buf.append((char) c);
67: c = in.read();
68: }
69: assertEquals(TESTSTRING, buf.toString());
70: }
71: }
|