Debian Codegraph @

src/test/java/org/apache/felix/gogo/runtime/TestParser2.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.runtime;
20
21 import java.io.EOFException;
22
23 import junit.framework.TestCase;
24
25 /*
26 * Test features of the new parser/tokenizer, many of which are not supported
27 * by the original parser.
28 */
29 public class TestParser2 extends TestCase
30 {
31 public void testComment() throws Exception
32 {
33 Context c = new Context();
34 c.addCommand("echo", this);
35
36 assertEquals("file://wibble#tag", c.execute("echo file://wibble#tag"));
37 assertEquals("file:", c.execute("echo file: //wibble#tag"));
38
39 assertEquals("PWD/*.java", c.execute("echo PWD/*.java"));
40 try
41 {
42 c.execute("echo PWD /*.java");
43 fail("expected EOFException");
44 }
45 catch (EOFException e)
46 {
47 // expected
48 }
49
50 assertEquals("ok", c.execute("// can't quote\necho ok\n"));
51
52 // quote in comment in closure
53 assertEquals("ok", c.execute("x = { // can't quote\necho ok\n}; x"));
54 assertEquals("ok", c.execute("x = {\n// can't quote\necho ok\n}; x"));
55 assertEquals("ok", c.execute("x = {// can't quote\necho ok\n}; x"));
56 }
57
58 public void testCoercion() throws Exception
59 {
60 Context c = new Context();
61 c.addCommand("echo", this);
62 // FELIX-2432
63 assertEquals("null x", c.execute("echo $expandsToNull x"));
64 }
65
66 public void testStringExecution() throws Exception
67 {
68 Context c = new Context();
69 c.addCommand("echo", this);
70 c.addCommand("new", this);
71
72 // FELIX-2433
73 assertEquals("helloworld", c.execute("echo \"$(echo hello)world\""));
74
75 // FELIX-1473 - allow method calls on String objects
76 assertEquals("hello", c.execute("cmd = echo; eval $cmd hello"));
77 assertEquals(4, c.execute("'four' length"));
78 try {
79 c.execute("four length");
80 fail("expected: command not found: four");
81 } catch (IllegalArgumentException e) {
82 }
83
84 // check CharSequence types are preserved
85 Object b = c.execute("b = new java.lang.StringBuilder");
86 assertTrue(b instanceof StringBuilder);
87 assertEquals(b, c.execute("c = $b"));
88 }
89
90 public CharSequence echo(Object args[])
91 {
92 if (args == null)
93 {
94 return "null args!";
95 }
96
97 StringBuilder sb = new StringBuilder();
98 for (Object arg : args)
99 {
100 if (sb.length() > 0)
101 sb.append(' ');
102 sb.append(String.valueOf(arg));
103 }
104 return sb.toString();
105 }
106
107 public Object _new(String className) throws Exception {
108 return Class.forName(className).newInstance();
109 }
110
111 }