001    package jline;
002    
003    import java.io.ByteArrayInputStream;
004    import java.io.InputStream;
005    import java.io.StringWriter;
006    import java.io.Writer;
007    
008    import junit.framework.TestCase;
009    
010    public class ConsoleReaderTest extends TestCase {
011    
012        public ConsoleReaderTest(String name) {
013            super(name);
014        }
015    
016        protected void setUp() throws Exception {
017            System.setProperty("jline.WindowsTerminal.directConsole", "false");
018        }
019    
020        public void testDeleteAndBackspaceKeymappings() throws Exception {
021            // test only works on Windows
022            if (!(Terminal.getTerminal() instanceof WindowsTerminal))
023                return;
024    
025            ConsoleReader consoleReader = new ConsoleReader();
026            assertNotNull(consoleReader);
027            assertEquals(127, consoleReader
028                    .getKeyForAction(ConsoleReader.DELETE_NEXT_CHAR));
029            assertEquals(8, consoleReader
030                    .getKeyForAction(ConsoleReader.DELETE_PREV_CHAR));
031        }
032    
033        public void testReadline() throws Exception {
034            ConsoleReader consoleReader = createConsole("Sample String\r\n"
035                    .getBytes());
036            assertNotNull(consoleReader);
037            String line = consoleReader.readLine();
038            assertEquals("Sample String", line);
039    
040        }
041    
042        public void testDeleteOnWindowsTerminal() throws Exception {
043            // test only works on Windows
044            if (!(Terminal.getTerminal() instanceof WindowsTerminal))
045                return;
046    
047            char[] characters = new char[] { 'S', 's',
048                    WindowsTerminal.SPECIAL_KEY_INDICATOR,
049                    WindowsTerminal.LEFT_ARROW_KEY,
050                    WindowsTerminal.SPECIAL_KEY_INDICATOR,
051                    WindowsTerminal.DELETE_KEY, '\r', 'n' };
052            assertWindowsKeyBehavior("S", characters);
053        }
054    
055        public void testNumpadDeleteOnWindowsTerminal() throws Exception {
056            // test only works on Windows
057            if (!(Terminal.getTerminal() instanceof WindowsTerminal))
058                return;
059    
060            char[] characters = new char[] { 'S', 's',
061                    WindowsTerminal.NUMPAD_KEY_INDICATOR,
062                    WindowsTerminal.LEFT_ARROW_KEY,
063                    WindowsTerminal.NUMPAD_KEY_INDICATOR,
064                    WindowsTerminal.DELETE_KEY, '\r', 'n' };
065            assertWindowsKeyBehavior("S", characters);
066        }
067    
068        public void testHomeKeyOnWindowsTerminal() throws Exception {
069            // test only works on Windows
070            if (!(Terminal.getTerminal() instanceof WindowsTerminal))
071                return;
072    
073            char[] characters = new char[] { 'S', 's',
074                    WindowsTerminal.SPECIAL_KEY_INDICATOR,
075                    WindowsTerminal.HOME_KEY, 'x', '\r', '\n' };
076            assertWindowsKeyBehavior("xSs", characters);
077    
078        }
079    
080        public void testEndKeyOnWindowsTerminal() throws Exception {
081            // test only works on Windows
082            if (!(Terminal.getTerminal() instanceof WindowsTerminal))
083                return;
084    
085            char[] characters = new char[] { 'S', 's',
086                    WindowsTerminal.SPECIAL_KEY_INDICATOR,
087                    WindowsTerminal.HOME_KEY, 'x',
088                    WindowsTerminal.SPECIAL_KEY_INDICATOR, WindowsTerminal.END_KEY,
089                    'j', '\r', '\n' };
090            assertWindowsKeyBehavior("xSsj", characters);
091        }
092    
093        public void testPageUpOnWindowsTerminal() throws Exception {
094            // test only works on Windows
095            if (!(Terminal.getTerminal() instanceof WindowsTerminal))
096                return;
097    
098            char[] characters = new char[] { WindowsTerminal.SPECIAL_KEY_INDICATOR,
099                    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    }