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 java.io.StringReader ; 32 import java.io.Writer ; 33 import junit.framework.*; 34 39 40 43 public class MoreTest extends TestCase { 44 public void testShortInput() throws IOException { 46 final More m = new More(3, new StringReader ("1\n2\n"), destination.getWriter(), new StringReader ("\n123\nx\n"), toUser.getWriter(), quitChar, prompt); 47 assertEquals("1", destination.readLine()); 48 assertTrue(!toUser.getReader().ready()); 49 } 50 51 52 53 public void testEmptyInput() throws IOException { 55 final More m = new More(1, new StringReader (""), destination.getWriter(), new StringReader ("\n123\nx\n"), toUser.getWriter(), quitChar, prompt); 56 assertTrue(!destination.getReader().ready()); 57 } 58 59 public void testSimpleUsage() throws IOException { 61 final More m = new More(1, new StringReader ("1\n2\n3\n4"), destination.getWriter(), new StringReader ("\n123\nx\n"), toUser.getWriter(), quitChar, prompt); 62 63 assertEquals("1", destination.readLine()); 64 assertEquals(prompt, toUser.readLine()); 65 assertEquals("2", destination.readLine()); 66 assertEquals(prompt, toUser.readLine()); 67 assertEquals("3", destination.readLine()); 68 assertEquals(prompt, toUser.readLine()); 69 assertTrue(!destination.getReader().ready()); 70 71 } 72 73 74 public MoreTest(String name){ 75 super(name); 76 } 77 78 final String quitChar = "x"; 79 final String prompt = "enter to continue, x to quit: "; 80 81 Pipe toUser; 82 Pipe destination; 83 84 protected void setUp() throws IOException { 85 toUser = new Pipe(); 86 destination = new Pipe(); 87 } 88 89 protected void tearDown() { 90 } 91 92 private void nyi(){ 93 fail("Not Yet Implemented"); 94 } 95 96 public static void main(String args[]){ 97 if (args.length == 0){ 98 junit.textui.TestRunner.run(MoreTest.class); 99 } else { 100 junit.textui.TestRunner.run(makeSuite(args)); 101 } 102 } 103 private static TestSuite makeSuite(String args[]){ 104 final TestSuite ts = new TestSuite(); 105 for (int i = 0; i < args.length; i++){ 106 ts.addTest(new MoreTest(args[i])); 107 } 108 return ts; 109 } 110 111 class Pipe 112 { 113 PipedWriter pw = new PipedWriter (); 114 PipedReader pr = new PipedReader (); 115 BufferedReader br = new BufferedReader (pr); 116 BufferedWriter bw = new BufferedWriter (pw); 117 118 BufferedWriter getWriter(){ return bw; } 119 BufferedReader getReader() { return br; } 120 121 Pipe() throws IOException { pr.connect(pw); } 122 void writeLine(String s) throws IOException { 123 bw.write(s); 124 bw.newLine(); 125 bw.flush(); 126 } 127 128 String readLine() throws IOException { 129 return br.readLine(); 130 } 131 boolean ready() throws IOException { 132 return br.ready(); 133 } 134 135 } 136 137 } 138 | Popular Tags |