Debian Codegraph @

src/test/java/org/apache/felix/gogo/shell/HistoryTest.java

raw
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.felix.gogo.shell;
20
21 import java.util.Iterator;
22
23 import junit.framework.TestCase;
24
25 import org.apache.felix.gogo.shell.History;
26 import org.junit.Before;
27 import org.junit.Test;
28
29 public class HistoryTest {
30
31 private static final String[] COMMANDS = {
32 "cd home", "ls", "more config", "more xconfig"
33 };
34
35 private History history;
36
37 @Before
38 public void setup() {
39 this.history = new History();
40 for (String cmd : COMMANDS) {
41 this.history.append(cmd);
42 }
43 }
44
45 @Test
46 public void test_add_get_history() {
47 final History his = new History();
48
49 his.append("cmd1");
50 Iterator<String> hi = his.getHistory();
51 TestCase.assertEquals("cmd1", hi.next());
52 TestCase.assertFalse(hi.hasNext());
53 }
54
55 @Test
56 public void test_fill_history() {
57 final History his = new History();
58
59 // NOTE: Assumes a fixed history size of 100
60 for (int i = 0; i < 100; i++) {
61 his.append("cmd" + i);
62 }
63
64 Iterator<String> hi = his.getHistory();
65 for (int i = 0; i < 100; i++) {
66 TestCase.assertEquals("cmd" + i, hi.next());
67 }
68 TestCase.assertFalse(hi.hasNext());
69 }
70
71 @Test
72 public void test_overflow_history() {
73 final History his = new History();
74
75 // NOTE: Assumes a fixed history size of 100
76 for (int i = -20; i < 100; i++) {
77 his.append("cmd" + i);
78 }
79
80 Iterator<String> hi = his.getHistory();
81 for (int i = 0; i < 100; i++) {
82 TestCase.assertEquals("cmd" + i, hi.next());
83 }
84 TestCase.assertFalse(hi.hasNext());
85 }
86
87 @Test
88 public void test_get_recent() {
89 TestCase.assertEquals(COMMANDS[3], history.evaluate("!!"));
90
91 TestCase.assertEquals(COMMANDS[3], history.evaluate("!-1"));
92 TestCase.assertEquals(COMMANDS[2], history.evaluate("!-2"));
93 TestCase.assertEquals(COMMANDS[1], history.evaluate("!-3"));
94 TestCase.assertEquals(COMMANDS[0], history.evaluate("!-4"));
95
96 TestCase.assertEquals(COMMANDS[0], history.evaluate("!1"));
97 TestCase.assertEquals(COMMANDS[1], history.evaluate("!2"));
98 TestCase.assertEquals(COMMANDS[2], history.evaluate("!3"));
99 TestCase.assertEquals(COMMANDS[3], history.evaluate("!4"));
100 }
101
102 @Test
103 public void test_get_recent_wrong_index() {
104 try {
105 history.evaluate("!0");
106 TestCase.fail("Expected IllegalArgumentException");
107 } catch (IllegalArgumentException iae) {
108 // expected
109 }
110
111 try {
112 history.evaluate("!5");
113 TestCase.fail("Expected IllegalArgumentException");
114 } catch (IllegalArgumentException iae) {
115 // expected
116 }
117
118 try {
119 history.evaluate("!-5");
120 TestCase.fail("Expected IllegalArgumentException");
121 } catch (IllegalArgumentException iae) {
122 // expected
123 }
124 }
125
126 @Test
127 public void test_caret() {
128 TestCase.assertEquals("ls xconfig", history.evaluate("^more^ls^"));
129 TestCase.assertEquals("ls xconfig", history.evaluate("^more^ls"));
130 TestCase.assertEquals("ls -l xconfig", history.evaluate("^more^ls -l"));
131 }
132
133 @Test
134 public void test_caret_fail() {
135 try {
136 history.evaluate("^ls^cat^");
137 TestCase.fail("Expected failure for impossible replacement");
138 } catch (IllegalArgumentException iae) {
139 // expected
140 }
141 }
142
143 @Test
144 public void test_s_last() {
145 TestCase.assertEquals("ls xconfig", history.evaluate("!!:s?more?ls?"));
146 TestCase.assertEquals("ls xconfig", history.evaluate("!!:s/more/ls"));
147 TestCase.assertEquals("ls -l xconfig", history.evaluate("!!:s^more^ls -l"));
148
149 TestCase.assertEquals("mare xconfig", history.evaluate("!!:s?o?a?"));
150 }
151
152 @Test
153 public void test_s_last_fail() {
154 try {
155 history.evaluate("!!:s/ls/cat/");
156 TestCase.fail("Expected failure for impossible replacement");
157 } catch (IllegalArgumentException iae) {
158 // expected
159 }
160 }
161
162 @Test
163 public void test_s_g_a_last() {
164 TestCase.assertEquals("mire xcinfig", history.evaluate("!!:gs?o?i?"));
165 TestCase.assertEquals("mire xcinfig", history.evaluate("!!:gs/o/i"));
166 TestCase.assertEquals("mi-xre xci-xnfig", history.evaluate("!!:gs^o^i-x"));
167
168 TestCase.assertEquals("mire xcinfig", history.evaluate("!!:as?o?i?"));
169 TestCase.assertEquals("mire xcinfig", history.evaluate("!!:as/o/i"));
170 TestCase.assertEquals("mi-xre xci-xnfig", history.evaluate("!!:as^o^i-x"));
171 }
172
173 @Test
174 public void test_s_neg_2() {
175 TestCase.assertEquals("ls config", history.evaluate("!-2:s?more?ls?"));
176 TestCase.assertEquals("ls config", history.evaluate("!-2:s/more/ls"));
177 TestCase.assertEquals("ls -l config", history.evaluate("!-2:s^more^ls -l"));
178
179 TestCase.assertEquals("mare config", history.evaluate("!-2:s?o?a?"));
180 }
181
182 @Test
183 public void test_s_neg_2_fail() {
184 try {
185 history.evaluate("!-2:s/ls/cat/");
186 TestCase.fail("Expected failure for impossible replacement");
187 } catch (IllegalArgumentException iae) {
188 // expected
189 }
190 }
191
192 @Test
193 public void test_s_3() {
194 TestCase.assertEquals("ls config", history.evaluate("!-2:s?more?ls?"));
195 TestCase.assertEquals("ls config", history.evaluate("!-2:s/more/ls"));
196 TestCase.assertEquals("ls -l config", history.evaluate("!-2:s^more^ls -l"));
197
198 TestCase.assertEquals("mare config", history.evaluate("!-2:s?o?a?"));
199 }
200
201 @Test
202 public void test_s_3_fail() {
203 try {
204 history.evaluate("!-2:s/ls/cat/");
205 TestCase.fail("Expected failure for impossible replacement");
206 } catch (IllegalArgumentException iae) {
207 // expected
208 }
209 }
210
211 @Test
212 public void test_contains() {
213 TestCase.assertEquals("ls", history.evaluate("!?s"));
214 TestCase.assertEquals("ls", history.evaluate("!?s?"));
215
216 try {
217 history.evaluate("!?s:s/l/x/");
218 TestCase.fail("IllegalArgumentException expected");
219 } catch (IllegalArgumentException iae) {
220 // expected
221 }
222
223 TestCase.assertEquals("xs", history.evaluate("!?s?:s/l/x/"));
224 }
225
226 @Test
227 public void test_startsWith() {
228 TestCase.assertEquals("cd home", history.evaluate("!cd"));
229 TestCase.assertEquals("cd home", history.evaluate("!cd "));
230
231 try {
232 history.evaluate("!cd:s/x/y/");
233 TestCase.fail("IllegalArgumentException expected");
234 } catch (IllegalArgumentException iae) {
235 // expected
236 }
237
238 TestCase.assertEquals("ls home", history.evaluate("!cd:s/cd/ls/"));
239 }
240 }