| 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.runtime; |
| 20 |
|
| 21 |
import junit.framework.TestCase; |
| 22 |
|
| 23 |
import org.apache.felix.gogo.runtime.Parser; |
| 24 |
import org.apache.felix.gogo.runtime.Token; |
| 25 |
import org.apache.felix.service.command.CommandSession; |
| 26 |
import org.apache.felix.service.command.Function; |
| 27 |
|
| 28 |
import java.io.BufferedReader; |
| 29 |
import java.io.IOException; |
| 30 |
import java.io.InputStreamReader; |
| 31 |
import java.io.StringWriter; |
| 32 |
import java.util.ArrayList; |
| 33 |
import java.util.Collection; |
| 34 |
import java.util.List; |
| 35 |
import java.util.regex.Pattern; |
| 36 |
|
| 37 |
public class TestParser extends TestCase |
| 38 |
{ |
| 39 |
int beentheredonethat = 0; |
| 40 |
|
| 41 |
public void testEvaluatation() throws Exception |
| 42 |
{ |
| 43 |
Context c = new Context(); |
| 44 |
c.addCommand("echo", this); |
| 45 |
c.addCommand("capture", this); |
| 46 |
|
| 47 |
assertEquals("a", c.execute("echo a | capture")); |
| 48 |
assertEquals("a", c.execute("(echo a) | capture")); |
| 49 |
assertEquals("a", c.execute("((echo a)) | capture")); |
| 50 |
} |
| 51 |
|
| 52 |
public void testUnknownCommand() throws Exception |
| 53 |
{ |
| 54 |
Context c = new Context(); |
| 55 |
try |
| 56 |
{ |
| 57 |
c.execute("echo"); |
| 58 |
fail("Execution should have failed due to missing command"); |
| 59 |
} |
| 60 |
catch (IllegalArgumentException e) |
| 61 |
{ |
| 62 |
// expected |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
public void testSpecialValues() throws Exception |
| 67 |
{ |
| 68 |
Context c = new Context(); |
| 69 |
assertEquals(false, c.execute("false")); |
| 70 |
assertEquals(true, c.execute("true")); |
| 71 |
assertEquals(null, c.execute("null")); |
| 72 |
} |
| 73 |
|
| 74 |
public void testQuotes() throws Exception |
| 75 |
{ |
| 76 |
Context c = new Context(); |
| 77 |
c.addCommand("echo", this); |
| 78 |
c.set("c", "a"); |
| 79 |
|
| 80 |
assertEquals("a b", c.execute("echo a b")); |
| 81 |
assertEquals("a b", c.execute("echo 'a b'")); |
| 82 |
assertEquals("a b", c.execute("echo \"a b\"")); |
| 83 |
assertEquals("a b", c.execute("echo a b")); |
| 84 |
assertEquals("a b", c.execute("echo 'a b'")); |
| 85 |
assertEquals("a b", c.execute("echo \"a b\"")); |
| 86 |
assertEquals("a b", c.execute("echo $c b")); |
| 87 |
assertEquals("$c b", c.execute("echo '$c b'")); |
| 88 |
assertEquals("a b", c.execute("echo \"$c b\"")); |
| 89 |
assertEquals("a b", c.execute("echo ${c} b")); |
| 90 |
assertEquals("${c} b", c.execute("echo '${c} b'")); |
| 91 |
assertEquals("a b", c.execute("echo \"${c} b\"")); |
| 92 |
assertEquals("aa", c.execute("echo $c$c")); |
| 93 |
assertEquals("a ;a", c.execute("echo a\\ \\;a")); |
| 94 |
assertEquals("baabab", c.execute("echo b${c}${c}b${c}b")); |
| 95 |
|
| 96 |
c.set("d", "a b "); |
| 97 |
assertEquals("a b ", c.execute("echo \"$d\"")); |
| 98 |
} |
| 99 |
|
| 100 |
public void testScope() throws Exception |
| 101 |
{ |
| 102 |
Context c = new Context(); |
| 103 |
c.addCommand("echo", this); |
| 104 |
assertEquals("$a", c.execute("test:echo \\$a")); |
| 105 |
assertEquals("file://poo", c.execute("test:echo file://poo")); |
| 106 |
} |
| 107 |
|
| 108 |
public void testPipe() throws Exception |
| 109 |
{ |
| 110 |
Context c = new Context(); |
| 111 |
c.addCommand("echo", this); |
| 112 |
c.addCommand("capture", this); |
| 113 |
c.addCommand("grep", this); |
| 114 |
c.addCommand("echoout", this); |
| 115 |
c.execute("myecho = { echoout $args }"); |
| 116 |
assertEquals("def", c.execute("echo def|grep d.*|capture")); |
| 117 |
assertEquals("def", c.execute("echoout def|grep d.*|capture")); |
| 118 |
assertEquals("def", c.execute("myecho def|grep d.*|capture")); |
| 119 |
assertEquals("def", |
| 120 |
c.execute("(echoout abc; echoout def; echoout ghi)|grep d.*|capture")); |
| 121 |
assertEquals("", c.execute("echoout def; echoout ghi | grep d.* | capture")); |
| 122 |
assertEquals("hello world", c.execute("echo hello world|capture")); |
| 123 |
assertEquals("defghi", |
| 124 |
c.execute("(echoout abc; echoout def; echoout ghi)|grep 'def|ghi'|capture")); |
| 125 |
} |
| 126 |
|
| 127 |
public void testAssignment() throws Exception |
| 128 |
{ |
| 129 |
Context c = new Context(); |
| 130 |
c.addCommand("echo", this); |
| 131 |
c.addCommand("grep", this); |
| 132 |
assertEquals("a", c.execute("a = a; echo ${$a}")); |
| 133 |
|
| 134 |
assertEquals("hello", c.execute("echo hello")); |
| 135 |
assertEquals("hello", c.execute("a = (echo hello)")); |
| 136 |
//assertEquals("a", c.execute("a = a; echo $(echo a)")); // #p2 - no eval in var expansion |
| 137 |
assertEquals("3", c.execute("a=3; echo $a")); |
| 138 |
assertEquals("3", c.execute("a = 3; echo $a")); |
| 139 |
assertEquals("a", c.execute("a = a; echo ${$a}")); |
| 140 |
} |
| 141 |
|
| 142 |
public void testComment() throws Exception |
| 143 |
{ |
| 144 |
Context c = new Context(); |
| 145 |
c.addCommand("echo", this); |
| 146 |
assertEquals("1", c.execute("echo 1 // hello")); |
| 147 |
} |
| 148 |
|
| 149 |
public void testClosure() throws Exception |
| 150 |
{ |
| 151 |
Context c = new Context(); |
| 152 |
c.addCommand("echo", this); |
| 153 |
c.addCommand("capture", this); |
| 154 |
|
| 155 |
assertEquals("a", c.execute("e = { echo $1 } ; e a b")); |
| 156 |
assertEquals("b", c.execute("e = { echo $2 } ; e a b")); |
| 157 |
assertEquals("b", c.execute("e = { eval $args } ; e echo b")); |
| 158 |
assertEquals("ca b", c.execute("e = { echo c$args } ; e a b")); |
| 159 |
assertEquals("c a b", c.execute("e = { echo c $args } ; e a b")); |
| 160 |
assertEquals("ca b", c.execute("e = { echo c$args } ; e 'a b'")); |
| 161 |
} |
| 162 |
|
| 163 |
public void testArray() throws Exception |
| 164 |
{ |
| 165 |
Context c = new Context(); |
| 166 |
c.set("echo", true); |
| 167 |
assertEquals("http://www.aqute.biz?com=2&biz=1", |
| 168 |
c.execute("['http://www.aqute.biz?com=2&biz=1'] get 0")); |
| 169 |
assertEquals("{a=2, b=3}", c.execute("[a=2 b=3]").toString()); |
| 170 |
assertEquals(3L, c.execute("[a=2 b=3] get b")); |
| 171 |
assertEquals("[3, 4]", c.execute("[1 2 [3 4] 5 6] get 2").toString()); |
| 172 |
assertEquals(5, c.execute("[1 2 [3 4] 5 6] size")); |
| 173 |
} |
| 174 |
|
| 175 |
public void testParentheses() |
| 176 |
{ |
| 177 |
Parser parser = new Parser("(a|b)|(d|f)"); |
| 178 |
List<List<List<Token>>> p = parser.program(); |
| 179 |
assertEquals("a|b", p.get(0).get(0).get(0).toString()); |
| 180 |
|
| 181 |
parser = new Parser("grep (d.*)|grep (d|f)"); |
| 182 |
p = parser.program(); |
| 183 |
assertEquals("d.*", p.get(0).get(0).get(1).toString()); |
| 184 |
} |
| 185 |
|
| 186 |
public void testEcho() throws Exception |
| 187 |
{ |
| 188 |
Context c = new Context(); |
| 189 |
c.addCommand("echo", this); |
| 190 |
c.execute("echo peter"); |
| 191 |
} |
| 192 |
|
| 193 |
public void grep(String match) throws IOException |
| 194 |
{ |
| 195 |
Pattern p = Pattern.compile(match); |
| 196 |
BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in)); |
| 197 |
String s = rdr.readLine(); |
| 198 |
while (s != null) |
| 199 |
{ |
| 200 |
if (p.matcher(s).find()) |
| 201 |
{ |
| 202 |
System.out.println(s); |
| 203 |
} |
| 204 |
s = rdr.readLine(); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
public String capture() throws IOException |
| 209 |
{ |
| 210 |
StringWriter sw = new StringWriter(); |
| 211 |
BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in)); |
| 212 |
String s = rdr.readLine(); |
| 213 |
while (s != null) |
| 214 |
{ |
| 215 |
sw.write(s); |
| 216 |
s = rdr.readLine(); |
| 217 |
} |
| 218 |
return sw.toString(); |
| 219 |
} |
| 220 |
|
| 221 |
public void testVars() throws Exception |
| 222 |
{ |
| 223 |
Context c = new Context(); |
| 224 |
c.addCommand("echo", this); |
| 225 |
|
| 226 |
assertEquals("", c.execute("echo ${very.likely.that.this.does.not.exist}")); |
| 227 |
assertNotNull(c.execute("echo ${java.shell.name}")); |
| 228 |
assertEquals("a", c.execute("a = a; echo ${a}")); |
| 229 |
} |
| 230 |
|
| 231 |
public void testFunny() throws Exception |
| 232 |
{ |
| 233 |
Context c = new Context(); |
| 234 |
c.addCommand("echo", this); |
| 235 |
assertEquals("a", c.execute("echo a") + ""); |
| 236 |
assertEquals("a", c.execute("eval (echo echo) a") + ""); |
| 237 |
//assertEquals("a", c.execute("((echo echo) echo) (echo a)") + ""); |
| 238 |
assertEquals("3", c.execute("[a=2 (echo b)=(echo 3)] get b").toString()); |
| 239 |
} |
| 240 |
|
| 241 |
public CharSequence echo(Object args[]) |
| 242 |
{ |
| 243 |
if (args == null) |
| 244 |
{ |
| 245 |
return ""; |
| 246 |
} |
| 247 |
|
| 248 |
StringBuilder sb = new StringBuilder(); |
| 249 |
for (Object arg : args) |
| 250 |
{ |
| 251 |
if (arg != null) |
| 252 |
{ |
| 253 |
if (sb.length() > 0) |
| 254 |
sb.append(' '); |
| 255 |
sb.append(arg); |
| 256 |
} |
| 257 |
} |
| 258 |
return sb.toString(); |
| 259 |
} |
| 260 |
|
| 261 |
public void echoout(Object args[]) |
| 262 |
{ |
| 263 |
System.out.println(echo(args)); |
| 264 |
} |
| 265 |
|
| 266 |
public void testContext() throws Exception |
| 267 |
{ |
| 268 |
Context c = new Context(); |
| 269 |
c.addCommand("ls", this); |
| 270 |
beentheredonethat = 0; |
| 271 |
c.execute("ls"); |
| 272 |
assertEquals(1, beentheredonethat); |
| 273 |
|
| 274 |
beentheredonethat = 0; |
| 275 |
c.execute("ls 10"); |
| 276 |
assertEquals(10, beentheredonethat); |
| 277 |
|
| 278 |
beentheredonethat = 0; |
| 279 |
c.execute("ls a b c d e f g h i j"); |
| 280 |
assertEquals(10, beentheredonethat); |
| 281 |
|
| 282 |
beentheredonethat = 0; |
| 283 |
Integer result = (Integer) c.execute("ls (ls 5)"); |
| 284 |
assertEquals(10, beentheredonethat); |
| 285 |
assertEquals((Integer) 5, result); |
| 286 |
} |
| 287 |
|
| 288 |
public void ls() |
| 289 |
{ |
| 290 |
beentheredonethat++; |
| 291 |
System.out.println("ls(): Yes!"); |
| 292 |
} |
| 293 |
|
| 294 |
public int ls(int onoff) |
| 295 |
{ |
| 296 |
beentheredonethat += onoff; |
| 297 |
System.out.println("ls(int) " + onoff); |
| 298 |
return onoff; |
| 299 |
} |
| 300 |
|
| 301 |
public void ls(Object args[]) |
| 302 |
{ |
| 303 |
beentheredonethat = args.length; |
| 304 |
System.out.print("ls(Object[]) ["); |
| 305 |
for (Object i : args) |
| 306 |
{ |
| 307 |
System.out.print(i + " "); |
| 308 |
} |
| 309 |
System.out.println("]"); |
| 310 |
} |
| 311 |
|
| 312 |
public void testProgram() |
| 313 |
{ |
| 314 |
List<List<List<Token>>> x = new Parser("abc def|ghi jkl;mno pqr|stu vwx").program(); |
| 315 |
assertEquals("abc", x.get(0).get(0).get(0).toString()); |
| 316 |
assertEquals("def", x.get(0).get(0).get(1).toString()); |
| 317 |
assertEquals("ghi", x.get(0).get(1).get(0).toString()); |
| 318 |
assertEquals("jkl", x.get(0).get(1).get(1).toString()); |
| 319 |
assertEquals("mno", x.get(1).get(0).get(0).toString()); |
| 320 |
assertEquals("pqr", x.get(1).get(0).get(1).toString()); |
| 321 |
assertEquals("stu", x.get(1).get(1).get(0).toString()); |
| 322 |
assertEquals("vwx", x.get(1).get(1).get(1).toString()); |
| 323 |
} |
| 324 |
|
| 325 |
public void testStatements() |
| 326 |
{ |
| 327 |
List<List<Token>> x = new Parser("abc def|ghi jkl|mno pqr").program().get(0); |
| 328 |
assertEquals("abc", x.get(0).get(0).toString()); |
| 329 |
assertEquals("def", x.get(0).get(1).toString()); |
| 330 |
assertEquals("ghi", x.get(1).get(0).toString()); |
| 331 |
assertEquals("jkl", x.get(1).get(1).toString()); |
| 332 |
assertEquals("mno", x.get(2).get(0).toString()); |
| 333 |
assertEquals("pqr", x.get(2).get(1).toString()); |
| 334 |
} |
| 335 |
|
| 336 |
public void testSimpleValue() |
| 337 |
{ |
| 338 |
List<Token> x = new Parser( |
| 339 |
"abc def.ghi http://www.osgi.org?abc=&x=1 [1,2,3] {{{{{{{xyz}}}}}}} (immediate) {'{{{{{'} {\\{} 'abc{}'") |
| 340 |
.program().get(0).get(0); |
| 341 |
assertEquals("abc", x.get(0).toString()); |
| 342 |
assertEquals("def.ghi", x.get(1).toString()); |
| 343 |
assertEquals("http://www.osgi.org?abc=&x=1", x.get(2).toString()); |
| 344 |
assertEquals("1,2,3", x.get(3).toString()); |
| 345 |
assertEquals("{{{{{{xyz}}}}}}", x.get(4).toString()); |
| 346 |
assertEquals("immediate", x.get(5).toString()); |
| 347 |
assertEquals("'{{{{{'", x.get(6).toString()); |
| 348 |
assertEquals("\\{", x.get(7).toString()); |
| 349 |
assertEquals("'abc{}'", x.get(8).toString()); |
| 350 |
} |
| 351 |
|
| 352 |
void each(CommandSession session, Collection<Object> list, Function closure) |
| 353 |
throws Exception |
| 354 |
{ |
| 355 |
List<Object> args = new ArrayList<Object>(); |
| 356 |
args.add(null); |
| 357 |
for (Object x : list) |
| 358 |
{ |
| 359 |
args.set(0, x); |
| 360 |
closure.execute(session, args); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
} |