View Javadoc

1   package jline;
2   
3   import java.io.ByteArrayInputStream;
4   import java.io.InputStream;
5   import java.io.StringWriter;
6   import java.io.Writer;
7   
8   import junit.framework.TestCase;
9   
10  public class ConsoleReaderTest extends TestCase {
11  
12      public ConsoleReaderTest(String name) {
13          super(name);
14      }
15  
16      protected void setUp() throws Exception {
17          System.setProperty("jline.WindowsTerminal.directConsole", "false");
18      }
19  
20      public void testDeleteAndBackspaceKeymappings() throws Exception {
21          // test only works on Windows
22          if (!(Terminal.getTerminal() instanceof WindowsTerminal))
23              return;
24  
25          ConsoleReader consoleReader = new ConsoleReader();
26          assertNotNull(consoleReader);
27          assertEquals(127, consoleReader
28                  .getKeyForAction(ConsoleReader.DELETE_NEXT_CHAR));
29          assertEquals(8, consoleReader
30                  .getKeyForAction(ConsoleReader.DELETE_PREV_CHAR));
31      }
32  
33      public void testReadline() throws Exception {
34          ConsoleReader consoleReader = createConsole("Sample String\r\n"
35                  .getBytes());
36          assertNotNull(consoleReader);
37          String line = consoleReader.readLine();
38          assertEquals("Sample String", line);
39  
40      }
41  
42      public void testDeleteOnWindowsTerminal() throws Exception {
43          // test only works on Windows
44          if (!(Terminal.getTerminal() instanceof WindowsTerminal))
45              return;
46  
47          char[] characters = new char[] { 'S', 's',
48                  WindowsTerminal.SPECIAL_KEY_INDICATOR,
49                  WindowsTerminal.LEFT_ARROW_KEY,
50                  WindowsTerminal.SPECIAL_KEY_INDICATOR,
51                  WindowsTerminal.DELETE_KEY, '\r', 'n' };
52          assertWindowsKeyBehavior("S", characters);
53      }
54  
55      public void testNumpadDeleteOnWindowsTerminal() throws Exception {
56          // test only works on Windows
57          if (!(Terminal.getTerminal() instanceof WindowsTerminal))
58              return;
59  
60          char[] characters = new char[] { 'S', 's',
61                  WindowsTerminal.NUMPAD_KEY_INDICATOR,
62                  WindowsTerminal.LEFT_ARROW_KEY,
63                  WindowsTerminal.NUMPAD_KEY_INDICATOR,
64                  WindowsTerminal.DELETE_KEY, '\r', 'n' };
65          assertWindowsKeyBehavior("S", characters);
66      }
67  
68      public void testHomeKeyOnWindowsTerminal() throws Exception {
69          // test only works on Windows
70          if (!(Terminal.getTerminal() instanceof WindowsTerminal))
71              return;
72  
73          char[] characters = new char[] { 'S', 's',
74                  WindowsTerminal.SPECIAL_KEY_INDICATOR,
75                  WindowsTerminal.HOME_KEY, 'x', '\r', '\n' };
76          assertWindowsKeyBehavior("xSs", characters);
77  
78      }
79  
80      public void testEndKeyOnWindowsTerminal() throws Exception {
81          // test only works on Windows
82          if (!(Terminal.getTerminal() instanceof WindowsTerminal))
83              return;
84  
85          char[] characters = new char[] { 'S', 's',
86                  WindowsTerminal.SPECIAL_KEY_INDICATOR,
87                  WindowsTerminal.HOME_KEY, 'x',
88                  WindowsTerminal.SPECIAL_KEY_INDICATOR, WindowsTerminal.END_KEY,
89                  'j', '\r', '\n' };
90          assertWindowsKeyBehavior("xSsj", characters);
91      }
92  
93      public void testPageUpOnWindowsTerminal() throws Exception {
94          // test only works on Windows
95          if (!(Terminal.getTerminal() instanceof WindowsTerminal))
96              return;
97  
98          char[] characters = new char[] { WindowsTerminal.SPECIAL_KEY_INDICATOR,
99                  WindowsTerminal.PAGE_UP_KEY, '\r', '\n' };
100         assertWindowsKeyBehavior("dir", characters);
101     }
102 
103     public void testPageDownOnWindowsTerminal() throws Exception {
104         // test only works on Windows
105         if (!(Terminal.getTerminal() instanceof WindowsTerminal))
106             return;
107 
108         char[] characters = new char[] { WindowsTerminal.SPECIAL_KEY_INDICATOR,
109                 WindowsTerminal.PAGE_DOWN_KEY, '\r', '\n' };
110         assertWindowsKeyBehavior("mkdir monkey", characters);
111     }
112 
113     public void testEscapeOnWindowsTerminal() throws Exception {
114         // test only works on Windows
115         if (!(Terminal.getTerminal() instanceof WindowsTerminal))
116             return;
117 
118         char[] characters = new char[] { 's', 's', 's',
119                 WindowsTerminal.SPECIAL_KEY_INDICATOR,
120                 WindowsTerminal.ESCAPE_KEY, '\r', '\n' };
121         assertWindowsKeyBehavior("", characters);
122     }
123 
124     public void testInsertOnWindowsTerminal() throws Exception {
125         // test only works on Windows
126         if (!(Terminal.getTerminal() instanceof WindowsTerminal))
127             return;
128 
129         char[] characters = new char[] { 'o', 'p', 's',
130                 WindowsTerminal.SPECIAL_KEY_INDICATOR,
131                 WindowsTerminal.HOME_KEY,
132                 WindowsTerminal.SPECIAL_KEY_INDICATOR,
133                 WindowsTerminal.INSERT_KEY, 'o', 'o', 'p', 's', '\r', '\n' };
134         assertWindowsKeyBehavior("oops", characters);
135     }
136 
137     private void assertWindowsKeyBehavior(String expected, char[] input)
138             throws Exception {
139         StringBuffer buffer = new StringBuffer();
140         buffer.append(input);
141         ConsoleReader reader = createConsole(buffer.toString().getBytes());
142         assertNotNull(reader);
143         String line = reader.readLine();
144         assertEquals(expected, line);
145     }
146 
147     private ConsoleReader createConsole(byte[] bytes) throws Exception {
148         InputStream in = new ByteArrayInputStream(bytes);
149         Writer writer = new StringWriter();
150         ConsoleReader reader = new ConsoleReader(in, writer);
151         reader.setHistory(createSeededHistory());
152         return reader;
153     }
154 
155     private History createSeededHistory() {
156         History history = new History();
157         history.addToHistory("dir");
158         history.addToHistory("cd c://");
159         history.addToHistory("mkdir monkey");
160         return history;
161     }
162 }