1 23 24 package com.sun.enterprise.cli.framework; 25 26 import java.io.BufferedReader ; 27 import java.io.IOException ; 28 import java.io.PipedReader ; 29 import java.io.PipedWriter ; 30 import java.io.StringReader ; 31 import java.io.Writer ; 32 import junit.framework.*; 33 38 39 public class PagerTest extends TestCase { 40 public void testNegativeLinesPerPage() throws IOException { 41 final Pager m = new Pager(-1, new StringReader ("1\n2\n3"), 42 stdout.getWriter()); 43 m.nextPage(); 44 assertEquals("1", stdout.readLine()); 45 assertEquals("2", stdout.readLine()); 46 assertEquals("3", stdout.readLine()); 47 assertTrue("Output should no longer be ready", !stdout.ready()); 48 } 49 50 public void testHasNext() throws IOException { 51 final Pager m = new Pager(2, new StringReader ("1\n2\n3"), 52 stdout.getWriter()); 53 assertTrue("Didn't expect anything in the output!", 54 !stdout.ready()); 55 m.nextPage(); 56 assertTrue(m.hasNext()); 57 m.nextPage(); 58 assertTrue(!m.hasNext()); 59 } 60 61 62 public void testZeroLengthPage() throws IOException { 63 final Pager m = new Pager(0, new StringReader ("1\n2\n3\n4\n5\n6\n7"), 64 stdout.getWriter()); 65 assertTrue("Didn't expect anything in the output!", 66 !stdout.ready()); 67 m.nextPage(); 68 assertTrue("Didn't expect anything in the output!", 69 !stdout.ready()); 70 } 71 72 73 74 75 public void testMultiplePages() throws IOException { 76 final Pager m = new Pager(2, new StringReader ("1\n2\n3\n4\n5\n6\n7"), stdout.getWriter()); 77 assertTrue("Didn't expect anything in the output!", !stdout.ready()); 78 m.nextPage(); 79 assertTrue("Expected something on the output!", stdout.ready()); 80 assertEquals("1", stdout.readLine()); 81 assertEquals("2", stdout.readLine()); 82 assertTrue("Expected end of page 1", !stdout.ready()); 83 m.nextPage(); 84 assertTrue("Expected page 2 to be ready", stdout.ready()); 85 assertEquals("3", stdout.readLine()); 86 assertEquals("4", stdout.readLine()); 87 assertTrue("Expected to be waiting for page 3", !stdout.ready()); 88 m.nextPage(); 89 assertEquals("5", stdout.readLine()); 90 assertEquals("6", stdout.readLine()); 91 assertTrue("Expected to be waiting for page 4", !stdout.ready()); 92 m.nextPage(); 93 assertEquals("7", stdout.readLine()); 94 assertTrue("Expected end of page 4", !stdout.ready()); 95 } 96 97 public void testSinglePage() throws IOException { 98 final Pager m = new Pager(2, new StringReader ("1\n2\n"), stdout.getWriter()); 99 assertTrue("Didn't expect anything in the output!", !stdout.ready()); 100 m.nextPage(); 101 assertTrue("Expected something on the output!", stdout.ready()); 102 assertEquals("1", stdout.readLine()); 103 assertEquals("2", stdout.readLine()); 104 assertTrue("Expected no more output", !stdout.ready()); 105 } 106 107 108 public PagerTest(String name){ 109 super(name); 110 } 111 Pipe stdout; 112 protected void setUp() throws IOException { 113 stdout = new Pipe(); 114 } 115 116 protected void tearDown() { 117 } 118 119 private void nyi(){ 120 fail("Not Yet Implemented"); 121 } 122 123 public static void main(String args[]){ 124 if (args.length == 0){ 125 junit.textui.TestRunner.run(PagerTest.class); 126 } else { 127 junit.textui.TestRunner.run(makeSuite(args)); 128 } 129 } 130 private static TestSuite makeSuite(String args[]){ 131 final TestSuite ts = new TestSuite(); 132 for (int i = 0; i < args.length; i++){ 133 ts.addTest(new PagerTest(args[i])); 134 } 135 return ts; 136 } 137 138 class Pipe 139 { 140 PipedWriter pw = new PipedWriter (); 141 PipedReader pr = new PipedReader (); 142 BufferedReader br = new BufferedReader (pr); 143 Writer getWriter(){ return pw; } 144 BufferedReader getReader() { return br; } 145 146 Pipe() throws IOException { pr.connect(pw); } 147 String readLine() throws IOException { 148 return br.readLine(); 149 } 150 boolean ready() throws IOException { 151 return br.ready(); 152 } 153 154 155 } 156 157 158 159 } 160 | Popular Tags |