01: /*
02: * Copyright (C) 2004-2007 Stephen Ostermiller
03: * http://ostermiller.org/contact.pl?regarding=Java+Utilities
04: *
05: * This program is free software; you can redistribute it and/or modify
06: * it under the terms of the GNU General Public License as published by
07: * the Free Software Foundation; either version 2 of the License, or
08: * (at your option) any later version.
09: *
10: * This program is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: * GNU General Public License for more details.
14: *
15: * See COPYING.TXT for details.
16: */
17: package com.Ostermiller.util;
18:
19: import java.io.*;
20:
21: /**
22: * Regression test for SizeLimitInputStreams.
23: * More information about this class is available from <a target="_top" href=
24: * "http://ostermiller.org/utils/SizeLimitInputStream.html">ostermiller.org</a>.
25: *
26: * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
27: * @since ostermillerutils 1.04.00
28: */
29: class SizeLimitInputStreamTests {
30: /**
31: * Main method for regression test
32: * @param args command line arguments (ignored)
33: */
34: public static void main(String[] args) {
35: try {
36: SizeLimitInputStream slis;
37:
38: slis = new SizeLimitInputStream(new ByteArrayInputStream(
39: new byte[] { 1, 2, 3, 4 }), 3);
40: if (slis.read() != 1)
41: throw new Exception("Expected 1");
42: if (slis.read() != 2)
43: throw new Exception("Expected 2");
44: if (slis.read() != 3)
45: throw new Exception("Expected 3");
46: if (slis.read() != -1)
47: throw new Exception("Expected -1");
48:
49: slis = new SizeLimitInputStream(new ByteArrayInputStream(
50: new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }), 6);
51: if (slis.read() != 1)
52: throw new Exception("Expected 1");
53: if (slis.read(new byte[4]) != 4)
54: throw new Exception("Expected 4 read");
55: if (slis.read(new byte[4]) != 1)
56: throw new Exception("Expected 2 read");
57: if (slis.read() != -1)
58: throw new Exception("Expected -1");
59:
60: InputStream in = new ByteArrayInputStream(("one" + "two"
61: + "three" + "four" + "five" + "six" + "seven")
62: .getBytes("ASCII"));
63: compare("one", readFully(new SizeLimitInputStream(in, 3)));
64: compare("", readFully(new SizeLimitInputStream(in, 0)));
65: compare("two", readFully(new SizeLimitInputStream(in, 3)));
66: compare("three", readFully(new SizeLimitInputStream(in, 5)));
67: compare("four", readFully(new SizeLimitInputStream(in, 4)));
68: compare("five", readFully(new SizeLimitInputStream(in, 4)));
69: compare("six", readFully(new SizeLimitInputStream(in, 3)));
70: compare("s", readFully(new SizeLimitInputStream(in, 1)));
71: compare("even", readFully(new SizeLimitInputStream(in, 4)));
72:
73: } catch (Exception x) {
74: System.err.println(x.getMessage());
75: System.exit(1);
76: }
77:
78: }
79:
80: private static String readFully(InputStream in) throws IOException {
81: StringBuffer sb = new StringBuffer();
82: int read;
83: while ((read = in.read()) != -1) {
84: sb.append((char) read);
85: }
86: return sb.toString();
87: }
88:
89: private static void compare(String s1, String s2) throws Exception {
90: if (!s1.equals(s2))
91: throw new Exception("Expected " + s1 + " but got " + s2);
92: }
93:
94: }
|