1 33 34 72 73 package edu.rice.cs.util; 74 75 import edu.rice.cs.drjava.DrJavaTestCase; 76 77 import java.io.BufferedReader ; 78 import java.io.IOException ; 79 import java.io.InputStreamReader ; 80 81 84 public class StreamRedirectorTest extends DrJavaTestCase { 85 88 public void testEmptyInput() throws IOException { 89 InputStreamRedirector isr = new InputStreamRedirector() { 90 protected String _getInput() { 91 return ""; 92 } 93 }; 94 try { 95 isr.read(); 96 fail("Should have thrown IOException on empty input!"); 97 } 98 catch (IOException ioe) { 99 } 101 } 102 103 106 public void testStaticInput() throws IOException { 107 InputStreamRedirector isr = new InputStreamRedirector() { 108 protected String _getInput() { 109 return "Hello World!\n"; 110 } 111 }; 112 BufferedReader br = new BufferedReader (new InputStreamReader (isr)); 113 assertEquals("First read", "Hello World!", br.readLine()); 114 assertEquals("Second read", "Hello World!", br.readLine()); } 116 117 120 public void testDynamicInput() throws IOException { 121 InputStreamRedirector isr = new InputStreamRedirector() { 122 int x = -1; 123 protected String _getInput() { 124 x++; 125 return x + "\n"; 126 } 127 }; 128 BufferedReader br = new BufferedReader (new InputStreamReader (isr)); 129 assertEquals("First read", "0", br.readLine()); 130 assertEquals("Second read", "1", br.readLine()); 132 assertEquals("Third read", "2", br.readLine()); 133 } 134 135 139 public void testMultiLineInput() throws IOException { 140 InputStreamRedirector isr = new InputStreamRedirector() { 141 private boolean alreadyCalled = false; 142 143 protected String _getInput() { 144 if (alreadyCalled) { 145 throw new RuntimeException ("_getInput() has already been called!"); 146 } 147 alreadyCalled = true; 148 return "Line 1\nLine 2\n"; 149 } 150 }; 151 BufferedReader br = new BufferedReader (new InputStreamReader (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 re) { 159 assertEquals("Should have thrown correct exception.", 160 "_getInput() has already been called!", re.getMessage()); 161 } 162 } 163 } | Popular Tags |