KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > martiansoftware > nailgun > TestNGInputStream


1 /*
2
3   Copyright 2004, Martian Software, Inc.
4
5   Licensed under the Apache License, Version 2.0 (the "License");
6   you may not use this file except in compliance with the License.
7   You may obtain a copy of the License at
8
9   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 JavaDoc;
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, '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 JavaDoc TESTSTRING = "This is a test!";
38     
39     public void testNGInputStreamIntoArray() throws Exception JavaDoc {
40         NGInputStream in = new NGInputStream(new ByteArrayInputStream JavaDoc(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 JavaDoc sbuf = new StringBuffer JavaDoc();
49         do {
50             bytesRead = in.read(buf);
51             if (bytesRead > 0) {
52                 totalBytes += bytesRead;
53                 sbuf.append(new String JavaDoc(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 JavaDoc {
61         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
62         NGInputStream in = new NGInputStream(new ByteArrayInputStream JavaDoc(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