1 23 24 package com.sun.enterprise.cli.framework; 25 26 import java.io.BufferedReader ; 27 import java.io.BufferedWriter ; 28 import java.io.IOException ; 29 import java.io.PipedReader ; 30 import java.io.PipedWriter ; 31 import junit.framework.*; 32 37 38 public class UserTest extends TestCase { 39 public void testUser() throws Exception { 40 final Pipe toUser = new Pipe(); 41 final Pipe fromUser = new Pipe(); 42 final String quit = "q"; 43 final String prompt = "Press return to continue, q then return to quit"; 44 45 final User user = new User( toUser.getReader(), fromUser.getWriter(), quit, prompt); 46 toUser.writeLine(""); 47 assertTrue(user.wantsToContinue()); 48 assertEquals(prompt, fromUser.readLine()); 49 toUser.writeLine("123"); 50 assertTrue(user.wantsToContinue()); 51 assertEquals(prompt, fromUser.readLine()); 52 toUser.writeLine("123" + quit); 53 assertTrue(user.wantsToContinue()); 54 assertEquals(prompt, fromUser.readLine()); 55 toUser.writeLine(quit + "1234"); 56 assertTrue(!user.wantsToContinue()); 57 assertEquals(prompt, fromUser.readLine()); 58 59 } 60 61 public UserTest(String name){ 62 super(name); 63 } 64 65 protected void setUp() { 66 } 67 68 protected void tearDown() { 69 } 70 71 private void nyi(){ 72 fail("Not Yet Implemented"); 73 } 74 75 public static void main(String args[]){ 76 if (args.length == 0){ 77 junit.textui.TestRunner.run(UserTest.class); 78 } else { 79 junit.textui.TestRunner.run(makeSuite(args)); 80 } 81 } 82 private static TestSuite makeSuite(String args[]){ 83 final TestSuite ts = new TestSuite(); 84 for (int i = 0; i < args.length; i++){ 85 ts.addTest(new UserTest(args[i])); 86 } 87 return ts; 88 } 89 90 class Pipe 91 { 92 PipedWriter pw = new PipedWriter (); 93 PipedReader pr = new PipedReader (); 94 BufferedReader br = new BufferedReader (pr); 95 BufferedWriter bw = new BufferedWriter (pw); 96 97 BufferedWriter getWriter(){ return bw; } 98 BufferedReader getReader() { return br; } 99 100 Pipe() throws IOException { pr.connect(pw); } 101 void writeLine(String s) throws IOException { 102 bw.write(s); 103 bw.newLine(); 104 bw.flush(); 105 } 106 107 String readLine() throws IOException { 108 return br.readLine(); 109 } 110 boolean ready() throws IOException { 111 return br.ready(); 112 } 113 114 115 } 116 117 } 118 | Popular Tags |