1 2 24 25 package com.sun.enterprise.cli.framework; 26 27 import java.io.BufferedReader ; 28 import java.io.ByteArrayInputStream ; 29 import java.io.ByteArrayOutputStream ; 30 import java.io.File ; 31 import java.io.FileOutputStream ; 32 import java.io.FileInputStream ; 33 import java.io.InputStreamReader ; 34 import java.io.FileReader ; 35 import java.io.FileWriter ; 36 import java.io.IOException ; 37 import java.io.OutputStreamWriter ; 38 import java.io.PrintWriter ; 39 import junit.framework.TestCase; 40 import junit.framework.TestSuite; 41 42 47 48 public class InputsAndOutputsTest extends TestCase { 49 public void testSetGetUserErrorStreamToFile() throws Exception { 50 final File f = File.createTempFile("InputsAndOutputsTest_testSetGetUserOutputStreamToFile", "tmp"); 51 f.deleteOnExit(); 52 io.setErrorOutputFile(f.toString()); 53 final IErrorOutput uo = io.getErrorOutput(); 54 uo.println("foo"); 55 uo.close(); 56 final BufferedReader fr= new BufferedReader (new FileReader (f)); 57 assertEquals("foo", fr.readLine()); 58 } 59 60 public void testSetGetErrorOutputStream(){ 61 final ByteArrayOutputStream baos = new ByteArrayOutputStream (); 62 io.setErrorOutput(baos); 63 final IErrorOutput uo = io.getErrorOutput(); 64 uo.print("foo"); 65 uo.close(); 66 assertEquals("foo", baos.toString()); 67 } 68 69 public void testSettingUserInputFileWithEncoding() throws Exception { 70 final String enc = "ISO-8859-1"; 71 final File f = File.createTempFile("InputsAndOutputsTest_testSettingUserInputFileWithEncoding", "tmp"); 72 f.deleteOnExit(); 73 final PrintWriter pw = new PrintWriter (new OutputStreamWriter (new FileOutputStream (f), enc)); 74 pw.println("line one"); 75 pw.println("line two"); 76 pw.close(); 77 io.setUserInputFile(f.toString(), enc); 78 final IUserInput ui = io.getUserInput(); 79 assertEquals("line one", ui.getLine()); 80 assertEquals("line two", ui.getLine()); 81 assertNull(ui.getLine()); 82 } 83 84 public void testSettingUserInputFile() throws Exception { 85 final File f = File.createTempFile("InputsAndOutputsTest_testSettingUserInputFile", "tmp"); 86 f.deleteOnExit(); 87 final PrintWriter pw = new PrintWriter (new FileWriter (f)); 88 pw.println("line one"); 89 pw.println("line two"); 90 pw.close(); 91 io.setUserInputFile(f.toString()); 92 final IUserInput ui = io.getUserInput(); 93 assertEquals("line one", ui.getLine()); 94 assertEquals("line two", ui.getLine()); 95 assertNull(ui.getLine()); 96 } 97 98 public void testSetGetUserInput() throws IOException { 99 final ByteArrayInputStream in = new ByteArrayInputStream ("one\ntwo".getBytes()); 100 final UserInput ui = new UserInput(in); 101 io.setUserInput(ui); 102 assertEquals(ui, io.getUserInput()); 103 assertEquals("one", io.getUserInput().getLine()); 104 } 105 106 public void testSetGetUserInputEncoding() throws Exception { 107 final ByteArrayInputStream in = new ByteArrayInputStream ("one\ntwo".getBytes()); 108 io.setUserInput(in, "ISO-8859-1"); 109 final IUserInput ui = io.getUserInput(); 110 assertEquals("one", ui.getLine()); 111 } 112 113 public void testSetGetUserInputStream() throws Exception { 114 final ByteArrayInputStream in = new ByteArrayInputStream ("one\ntwo".getBytes()); 115 io.setUserInput(in); 116 final IUserInput ui = io.getUserInput(); 117 assertEquals("one", ui.getLine()); 118 } 119 120 121 public void testSetGetUserOutputStreamToFile() throws Exception { 122 final File f = File.createTempFile("InputsAndOutputsTest_testSettingOutputStreamToFile", "tmp"); 123 f.deleteOnExit(); 124 io.setUserOutputFile(f.toString()); 125 final IUserOutput uo = io.getUserOutput(); 126 uo.println("foo"); 127 final BufferedReader fr= new BufferedReader (new FileReader (f)); 129 assertEquals("foo", fr.readLine()); 130 } 131 132 133 134 public void testSetGetUserOutputStream(){ 135 final ByteArrayOutputStream baos = new ByteArrayOutputStream (); 136 io.setUserOutput(baos); 137 final IUserOutput uo = io.getUserOutput(); 138 uo.print("foo"); 139 assertEquals("foo", baos.toString()); 141 } 142 143 public void testAssignment(){ 144 io.setInstance(InputsAndOutputs.getInstance()); 145 assertEquals(io, InputsAndOutputs.getInstance()); 146 } 147 148 149 public void testConstruction() { 150 assertNotNull(io.getUserOutput()); 151 } 152 153 public InputsAndOutputsTest(String name){ 154 super(name); 155 } 156 InputsAndOutputs io; 157 158 159 protected void setUp() { 160 io = InputsAndOutputs.getInstance(); 161 } 162 163 protected void tearDown() { 164 } 165 166 private void nyi(){ 167 fail("Not Yet Implemented"); 168 } 169 170 public static void main(String args[]){ 171 if (args.length == 0){ 172 junit.textui.TestRunner.run(InputsAndOutputsTest.class); 173 } else { 174 junit.textui.TestRunner.run(makeSuite(args)); 175 } 176 } 177 private static TestSuite makeSuite(String args[]){ 178 final TestSuite ts = new TestSuite(); 179 for (int i = 0; i < args.length; i++){ 180 ts.addTest(new InputsAndOutputsTest(args[i])); 181 } 182 return ts; 183 } 184 } 185 | Popular Tags |