KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Ostermiller > util > SizeLimitInputStreamTests


1 /*
2  * SizeLimitInputStream tests
3  * Copyright (C) 2004 Stephen Ostermiller
4  * http://ostermiller.org/contact.pl?regarding=Java+Utilities
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * See COPYING.TXT for details.
17  */

18 package com.Ostermiller.util;
19
20 import java.io.*;
21
22 /**
23  * Regression test for SizeLimitInputStreams.
24  * More information about this class is available from <a target="_top" HREF=
25  * "http://ostermiller.org/utils/SizeLimitInputStream.html">ostermiller.org</a>.
26  *
27  * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
28  * @since ostermillerutils 1.04.00
29  */

30 class SizeLimitInputStreamTests {
31     public static void main (String JavaDoc[] args){
32         try {
33             SizeLimitInputStream slis;
34
35             slis = new SizeLimitInputStream(
36                 new ByteArrayInputStream(
37                     new byte[] {
38                         1,2,3,4
39                     }
40                 ),
41                 3
42             );
43             if (slis.read() != 1) throw new Exception JavaDoc ("Expected 1");
44             if (slis.read() != 2) throw new Exception JavaDoc ("Expected 2");
45             if (slis.read() != 3) throw new Exception JavaDoc ("Expected 3");
46             if (slis.read() != -1) throw new Exception JavaDoc ("Expected -1");
47
48             slis = new SizeLimitInputStream(
49                 new ByteArrayInputStream(
50                     new byte[] {
51                         1,2,3,4,5,6,7,8,9
52                     }
53                 ),
54                 6
55             );
56             if (slis.read() != 1) throw new Exception JavaDoc ("Expected 1");
57             if (slis.read(new byte[4]) != 4) throw new Exception JavaDoc("Expected 4 read");
58             if (slis.read(new byte[4]) != 1) throw new Exception JavaDoc("Expected 2 read");
59             if (slis.read() != -1) throw new Exception JavaDoc ("Expected -1");
60
61             InputStream in = new ByteArrayInputStream(
62                 "onetwothreefourfivesixseven".getBytes("ASCII")
63             );
64             compare("one", readFully(new SizeLimitInputStream(in,3)));
65             compare("", readFully(new SizeLimitInputStream(in,0)));
66             compare("two", readFully(new SizeLimitInputStream(in,3)));
67             compare("three", readFully(new SizeLimitInputStream(in,5)));
68             compare("four", readFully(new SizeLimitInputStream(in,4)));
69             compare("five", readFully(new SizeLimitInputStream(in,4)));
70             compare("six", readFully(new SizeLimitInputStream(in,3)));
71             compare("s", readFully(new SizeLimitInputStream(in,1)));
72             compare("even", readFully(new SizeLimitInputStream(in,4)));
73
74         } catch (Exception JavaDoc x){
75             System.err.println(x.getMessage());
76             System.exit(1);
77         }
78
79     }
80
81     private static String JavaDoc readFully(InputStream in) throws IOException {
82         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
83         int read;
84         while ((read = in.read()) != -1){
85             sb.append((char)read);
86         }
87         return sb.toString();
88     }
89
90     private static void compare(String JavaDoc s1, String JavaDoc s2) throws Exception JavaDoc {
91         if (!s1.equals(s2)) throw new Exception JavaDoc ("Expected " + s1 + " but got " + s2);
92     }
93
94 }
95
Popular Tags