KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > StreamRedirectorTest


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2006 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 /*BEGIN_COPYRIGHT_BLOCK
35  *
36  * This file is a part of DrJava. Current versions of this project are available
37  * at http://sourceforge.net/projects/drjava
38  *
39  * Copyright (C) 2001-2002 JavaPLT group at Rice University (javaplt@rice.edu)
40  *
41  * DrJava is free software; you can redistribute it and/or modify
42  * it under the terms of the GNU General Public License as published by
43  * the Free Software Foundation; either version 2 of the License, or
44  * (at your option) any later version.
45  *
46  * DrJava is distributed in the hope that it will be useful,
47  * but WITHOUT ANY WARRANTY; without even the implied warranty of
48  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
49  * GNU General Public License for more details.
50  *
51  * You should have received a copy of the GNU General Public License
52  * along with this program; if not, write to the Free Software
53  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
54  * or see http://www.gnu.org/licenses/gpl.html
55  *
56  * In addition, as a special exception, the JavaPLT group at Rice University
57  * (javaplt@rice.edu) gives permission to link the code of DrJava with
58  * the classes in the gj.util package, even if they are provided in binary-only
59  * form, and distribute linked combinations including the DrJava and the
60  * gj.util package. You must obey the GNU General Public License in all
61  * respects for all of the code used other than these classes in the gj.util
62  * package: Dictionary, HashtableEntry, ValueEnumerator, Enumeration,
63  * KeyEnumerator, Vector, Hashtable, Stack, VectorEnumerator.
64  *
65  * If you modify this file, you may extend this exception to your version of the
66  * file, but you are not obligated to do so. If you do not wish to
67  * do so, delete this exception statement from your version. (However, the
68  * present version of DrJava depends on these classes, so you'd want to
69  * remove the dependency first!)
70  *
71 END_COPYRIGHT_BLOCK*/

72
73 package edu.rice.cs.util;
74
75 import edu.rice.cs.drjava.DrJavaTestCase;
76
77 import java.io.BufferedReader JavaDoc;
78 import java.io.IOException JavaDoc;
79 import java.io.InputStreamReader JavaDoc;
80
81 /**
82  * Test suite over InputStreamRedirector.
83  */

84 public class StreamRedirectorTest extends DrJavaTestCase {
85   /**
86    * Tests that an InputStreamRedirector correctly rejects empty input.
87    */

88   public void testEmptyInput() throws IOException JavaDoc {
89     InputStreamRedirector isr = new InputStreamRedirector() {
90       protected String JavaDoc _getInput() {
91         return "";
92       }
93     };
94     try {
95       isr.read();
96       fail("Should have thrown IOException on empty input!");
97     }
98     catch (IOException JavaDoc ioe) {
99       // correct behavior
100
}
101   }
102
103   /**
104    * Tests that an InputStreamRedirector correctly redirects input that is static.
105    */

106   public void testStaticInput() throws IOException JavaDoc {
107     InputStreamRedirector isr = new InputStreamRedirector() {
108       protected String JavaDoc _getInput() {
109         return "Hello World!\n";
110       }
111     };
112     BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(isr));
113     assertEquals("First read", "Hello World!", br.readLine());
114     assertEquals("Second read", "Hello World!", br.readLine()); //behavior should be consistent
115
}
116
117   /**
118    * Tests that an InputStreamRedirector correctly redirects input that changes.
119    */

120   public void testDynamicInput() throws IOException JavaDoc {
121     InputStreamRedirector isr = new InputStreamRedirector() {
122       int x = -1;
123       protected String JavaDoc _getInput() {
124         x++;
125         return x + "\n";
126       }
127     };
128     BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(isr));
129     assertEquals("First read", "0", br.readLine());
130     // x should get incremented on each call
131
assertEquals("Second read", "1", br.readLine());
132     assertEquals("Third read", "2", br.readLine());
133   }
134
135   /**
136    * Tests that an InputStreamRedirector correctly calls _getInput() only
137    * when it is needed.
138    */

139   public void testMultiLineInput() throws IOException JavaDoc {
140     InputStreamRedirector isr = new InputStreamRedirector() {
141       private boolean alreadyCalled = false;
142
143       protected String JavaDoc _getInput() {
144         if (alreadyCalled) {
145           throw new RuntimeException JavaDoc("_getInput() has already been called!");
146         }
147         alreadyCalled = true;
148         return "Line 1\nLine 2\n";
149       }
150     };
151     BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(isr));
152     assertEquals("First read calls _getInput()", "Line 1", br.readLine());
153     assertEquals("First read does not call _getInput()", "Line 2", br.readLine());
154     try {
155       br.readLine();
156       fail("_getInput() should be called again!");
157     }
158     catch(RuntimeException JavaDoc re) {
159       assertEquals("Should have thrown correct exception.",
160                    "_getInput() has already been called!", re.getMessage());
161     }
162   }
163 }
Popular Tags