Modified: felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Tokenizer.java URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Tokenizer.java?rev=1735994&r1=1735993&r2=1735994&view=diff ============================================================================== --- felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Tokenizer.java (original) +++ felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Tokenizer.java Mon Mar 21 16:52:52 2016 @@ -18,782 +18,138 @@ */ package org.apache.felix.gogo.runtime; -/** - * Bash-like tokenizer. - * - * Single and double quotes are just like Bash - single quotes escape everything - * (including backslashes), newlines are allowed in quotes. - * backslash-newline indicates a line continuation and is removed. - * - * Variable expansion is just like Bash: $NAME or ${NAME[[:][-+=?WORD]}, - * except it can yield any Object. Variables expanded within double-quotes, - * or adjacent to a String are converted to String. - * - * Unlike bash, indirect variable expansion is supported using ${$NAME}. - * - * Only a single variable assignment is recognized, with '=' being the second token. - * (Bash allows name1=value1 name2=value2 ... command args) - * - * Comments can only start where white space is allowed: - * # or // starts a line comment, /* starts a block comment. - * The following common uses do NOT start comments: - * ls http://example.com#anchor - * ls $dir/*.java - * - * @see http://wiki.bash-hackers.org/syntax/basicgrammar - */ -public class Tokenizer +public class Tokenizer extends BaseTokenizer { - public enum Type - { - ASSIGN('='), PIPE('|'), SEMICOLON(';'), NEWLINE, ARRAY, CLOSURE, EXPR, EXECUTION, WORD, EOT; - - private char c; - Type() - { - } + protected boolean inArray; + protected int word = 0; - Type(char c) - { - this.c = c; - } - - @Override - public String toString() - { - return (c == 0 ? super.toString() : "'" + c + "'"); - } - } - - private static final boolean DEBUG = false; - private static final char EOT = (char) -1; - - private final CharSequence text; - private final Evaluate evaluate; - private final boolean inArray; - private final boolean inQuote; - - private Type type = Type.NEWLINE; - private CharSequence value; - private Token token; - - private short line; - private short column; - private char ch; - private int index; - private boolean firstWord; + protected Token pushed; + protected Token last; public Tokenizer(CharSequence text) { - this(text, null, false); - } - - public Tokenizer(CharSequence text, Evaluate evaluate, boolean inQuote) - { - this.text = text; - this.evaluate = evaluate; - this.inQuote = inQuote; - index = 0; - line = column = 1; - - boolean array = false; - - if (text instanceof Token) - { - Token t = (Token) text; - line = t.line; - column = t.column; - array = (Type.ARRAY == t.type); - } - - inArray = array; - getch(); - - if (DEBUG) - { - if (inArray) - System.err.println("Tokenizer[" + text + "]"); - else - System.err.println("Tokenizer<" + text + ">"); - } - } - - public Type type() - { - return type; - } - - public CharSequence value() - { - return value; + super(text); } - public Token token() + public Token text() { - return token; + return text; } - public Type next() + public Token next() { - final Type prevType = type; - token = null; - value = null; - - short tLine; - short tColumn; - + if (pushed != null) + { + Token t = pushed; + pushed = null; + return t; + } + skipSpace(last == null || Token.eq(last, "\n")); + int start = index - 1; while (true) { - skipSpace(); - tLine = line; - tColumn = column; - switch (ch) { case EOT: - type = Type.EOT; - break; - - case '\n': - getch(); - if (inArray) - continue; - // only return NEWLINE once and not if not preceded by ; or | - switch (prevType) - { - case PIPE: - case SEMICOLON: - case NEWLINE: - continue; - - default: - type = Type.NEWLINE; - break; - } - break; - + return token(start); + case '[': + word = 0; + inArray = true; + return token(start); + case ']': + inArray = false; + word++; + return token(start); case '{': case '(': - case '[': - value = group(); - getch(); - break; - - case ';': - getch(); - type = Type.SEMICOLON; - break; - - case '|': - getch(); - type = Type.PIPE; - break; - - case '=': - if (firstWord || inArray) + if (start == index - 1) + { + word = 0; + return token(start); + } + else { + if (ch == '{') + { + find('}', '{'); + } + else + { + find(')', '('); + } getch(); - type = Type.ASSIGN; break; } - // fall through - default: - value = word(); - type = Type.WORD; - } - - firstWord = (Type.WORD == type && (Type.WORD != prevType && Type.ASSIGN != prevType)); - token = new Token(type, value, tLine, tColumn); - - if (DEBUG) - { - System.err.print("<" + type + ">"); - if (Type.EOT == type) - { - System.err.println(); - } - } - - return type; - } - } - - private CharSequence word() - { - int start = index - 1; - int skipCR = 0; - - do - { - switch (ch) - { - case '\n': - if (index >= 2 && text.charAt(index - 2) == '\r') - skipCR = 1; - // fall through - case '=': - if ((Type.WORD == type || Type.ASSIGN == type) && '=' == ch - && !inArray) - continue; - // fall through + case ';': + case '|': + word = 0; + return token(start); + case '}': + case ')': case ' ': case '\t': - case '|': - case ';': - return text.subSequence(start, index - 1 - skipCR); - - case '(': - case '{': - group(); + case '\n': + case '\r': + word++; + return token(start); + case '=': + if (inArray || word < 1 || index == start + 1) + { + word++; + return token(start); + } + getch(); break; - case '\\': escape(); + getch(); break; - case '\'': case '"': skipQuote(); + getch(); break; - } - } - while (getch() != EOT); - - return text.subSequence(start, index - 1); - } - - private CharSequence group() - { - final char push = ch; - final char pop; - - switch (ch) - { - case '{': - type = Type.CLOSURE; - pop = '}'; - break; - case '(': - type = Type.EXECUTION; - pop = ')'; - break; - case '[': - type = Type.ARRAY; - pop = ']'; - break; - default: - assert false; - pop = 0; - } - - short sLine = line; - short sCol = column; - int start = index; - int depth = 1; - - while (true) - { - boolean comment = false; - - switch (ch) - { - case '{': - case '(': - case '[': - case '\n': - comment = true; - break; - } - - if (getch() == EOT) - { - throw new EOFError(sLine, sCol, "unexpected EOT looking for matching '" - + pop + "'"); - } - - // don't recognize comments that start within a word - if (comment || isBlank(ch)) - skipSpace(); - - switch (ch) - { - case '"': - case '\'': - skipQuote(); - break; - - case '\\': - ch = escape(); - break; - default: - if (push == ch) - depth++; - else if (pop == ch && --depth == 0) - return text.subSequence(start, index - 1); + getch(); + break; } } - } - private char escape() + private Token token(int start) { - assert '\\' == ch; - - switch (getch()) - { - case 'u': - getch(); - getch(); - getch(); - getch(); - - if (EOT == ch) - { - throw new EOFError(line, column, "unexpected EOT in \\u escape"); - } - - String u = text.subSequence(index - 4, index).toString(); - - try - { - return (char) Integer.parseInt(u, 16); - } - catch (NumberFormatException e) - { - throw new SyntaxError(line, column, "bad unicode escape: \\u" + u); - } - - case EOT: - throw new EOFError(line, column, "unexpected EOT in \\ escape"); - - case '\n': - return '\0'; // line continuation - - case '\\': - case '\'': - case '"': - case '$': - return ch; - - default: - return ch; - } - } - - private void skipQuote() - { - assert '\'' == ch || '"' == ch; - final char quote = ch; - final short sLine = line; - final short sCol = column; - - while (getch() != EOT) - { - if (quote == ch) - return; - - if ((quote == '"') && ('\\' == ch)) - escape(); - } - - throw new EOFError(sLine, sCol, "unexpected EOT looking for matching quote: " - + quote); - } - - private void skipSpace() - { - while (true) + if (start == index - 1) { - while (isBlank(ch)) + if (ch == EOT) { - getch(); + return null; } - - // skip continuation lines, but not other escapes - if (('\\' == ch) && (peek() == '\n')) + if (ch == '\r' && peek() == '\n') { getch(); - getch(); - continue; - } - - // skip comments - if (('/' == ch) || ('#' == ch)) - { - if (('#' == ch) || (peek() == '/')) - { - while ((getch() != EOT) && ('\n' != ch)) - { - } - continue; - } - else if ('*' == peek()) - { - short sLine = line; - short sCol = column; - getch(); - - while ((getch() != EOT) && !(('*' == ch) && (peek() == '/'))) - { - } - - if (EOT == ch) - { - throw new EOFError(sLine, sCol, - "unexpected EOT looking for closing comment: */"); - } - - getch(); - getch(); - continue; - } - } - - break; - } - } - - private boolean isBlank(char ch) - { - return ' ' == ch || '\t' == ch; - } - - private boolean isName(char ch) - { - return Character.isJavaIdentifierPart(ch) && (ch != '$') || ('.' == ch); - } - - /** - * expand variables, quotes and escapes in word. - * @param vars - * @return - * @throws Exception - */ - public static Object expand(CharSequence word, Evaluate eval) throws Exception - { - return expand(word, eval, false); - } - - private static Object expand(CharSequence word, Evaluate eval, boolean inQuote) throws Exception - { - final String special = "%$\\\"'"; - int i = word.length(); - - while ((--i >= 0) && (special.indexOf(word.charAt(i)) == -1)) - { - } - - // shortcut if word doesn't contain any special characters - if (i < 0) - return word; - - return new Tokenizer(word, eval, inQuote).expand(); - } - - public Object expand(CharSequence word, short line, short column) throws Exception - { - return expand(new Token(Type.WORD, word, line, column), evaluate, inQuote); - } - - private Token word(CharSequence value) - { - return new Token(Type.WORD, value, line, column); - } - - private Object expand() throws Exception - { - StringBuilder buf = new StringBuilder(); - - while (ch != EOT) - { - int start = index; - - switch (ch) - { - case '%': - Object exp = expandExp(); - - if (EOT == ch && buf.length() == 0) - { - return exp; - } - - if (null != exp) - { - buf.append(exp); - } - - continue; // expandVar() has already read next char - - case '$': - Object val = expandVar(); - - if (EOT == ch && buf.length() == 0) - { - return val; - } - - if (null != val) - { - buf.append(val); - } - - continue; // expandVar() has already read next char - - case '\\': - ch = (inQuote && ("u$\\\n\"".indexOf(peek()) == -1)) ? '\\' - : escape(); - - if (ch != '\0') // ignore line continuation - { - buf.append(ch); - } - - break; - - case '"': - Token ww = word(null); - skipQuote(); - ww.value = text.subSequence(start, index - 1); - value = ww; - Object expand = expand(value, evaluate, true); - - if (eot() && buf.length() == 0 && value == expand) - { - // FELIX-2468 avoid returning CharSequence implementation - return ww.value.toString(); - } - - if (null != expand) - { - buf.append(expand.toString()); - } - break; - - case '\'': - if (!inQuote) - { - skipQuote(); - value = text.subSequence(start, index - 1); - - if (eot() && buf.length() == 0) - { - return value; - } - - buf.append(value); - break; - } - // else fall through - default: - buf.append(ch); } - - getch(); - } - - return buf.toString(); - } - - private Object expandExp() throws Exception - { - assert '%' == ch; - Object val; - - if (getch() == '(') - { - short sLine = line; - short sCol = column; - val = evaluate.eval(new Token(Type.EXPR, group(), sLine, sCol)); getch(); - return val; + last = text.subSequence(index - 2, index - 1); } else { - throw new SyntaxError(line, column, "bad expression: " + text); + last = text.subSequence(start, index - 1); } + return last; } - private Object expandVar() throws Exception + public void push(Token token) { - assert '$' == ch; - Object val; - - if (getch() != '{') - { - if ('(' == ch) - { // support $(...) FELIX-2433 - short sLine = line; - short sCol = column; - val = evaluate.eval(new Token(Type.EXECUTION, group(), sLine, sCol)); - getch(); - } - else - { - int start = index - 1; - while (isName(ch)) - { - getch(); - } - - if (index - 1 == start) - { - val = "$"; - } - else - { - String name = text.subSequence(start, index - 1).toString(); - val = evaluate.get(name); - } - } - } - else - { - // ${NAME[[:]-+=?]WORD} - short sLine = line; - short sCol = column; - CharSequence group = group(); - char c; - int i = 0; - - while (i < group.length()) - { - switch (group.charAt(i)) - { - case ':': - case '-': - case '+': - case '=': - case '?': - break; - - default: - ++i; - continue; - } - break; - } - - sCol += i; - - String name = String.valueOf(expand(group.subSequence(0, i), sLine, sCol)); - - for (int j = 0; j < name.length(); ++j) - { - if (!isName(name.charAt(j))) - { - throw new SyntaxError(sLine, sCol, "bad name: ${" + group + "}"); - } - } - - val = evaluate.get(name); - - if (i < group.length()) - { - c = group.charAt(i++); - if (':' == c) - { - c = (i < group.length() ? group.charAt(i++) : EOT); - } - - CharSequence word = group.subSequence(i, group.length()); - - switch (c) - { - case '-': - case '=': - if (null == val) - { - val = expand(word, evaluate, false); - if ('=' == c) - { - evaluate.put(name, val); - } - } - break; - - case '+': - if (null != val) - { - val = expand(word, evaluate, false); - } - break; - - case '?': - if (null == val) - { - val = expand(word, evaluate, false); - if (null == val || val.toString().length() == 0) - { - val = "parameter not set"; - } - throw new IllegalArgumentException(name + ": " + val); - } - break; - - default: - throw new SyntaxError(sLine, sCol, "bad substitution: ${" + group - + "}"); - } - } - getch(); - } - - return val; + this.pushed = token; } - /** - * returns true if getch() will return EOT - * @return - */ - private boolean eot() + public void skip(int length) { - return index >= text.length(); - } - - private char getch() - { - return ch = getch(false); - } - - private char peek() - { - return getch(true); - } - - private char getch(boolean peek) - { - if (eot()) - { - if (!peek) - { - ++index; - ch = EOT; - } - return EOT; - } - - int current = index; - char c = text.charAt(index++); - - if (('\r' == c) && !eot() && (text.charAt(index) == '\n')) - c = text.charAt(index++); - - if (peek) + while (--length >= 0) { - index = current; - } - else if ('\n' == c) - { - ++line; - column = 0; + getch(); } - else - ++column; - - return c; } }
Modified: felix/trunk/gogo/runtime/src/main/java/org/apache/felix/service/command/CommandSession.java URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/service/command/CommandSession.java?rev=1735994&r1=1735993&r2=1735994&view=diff ============================================================================== --- felix/trunk/gogo/runtime/src/main/java/org/apache/felix/service/command/CommandSession.java (original) +++ felix/trunk/gogo/runtime/src/main/java/org/apache/felix/service/command/CommandSession.java Mon Mar 21 16:52:52 2016 @@ -74,7 +74,7 @@ public interface CommandSession * @param name Name of the variable. * @param value Value of the variable */ - void put(String name, Object value); + Object put(String name, Object value); /** * Convert an object to string form (CharSequence). The level is defined in Copied: felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/AbstractParserTest.java (from r1735204, felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/BaseTestCase.java) URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/AbstractParserTest.java?p2=felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/AbstractParserTest.java&p1=felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/BaseTestCase.java&r1=1735204&r2=1735994&rev=1735994&view=diff ============================================================================== --- felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/BaseTestCase.java (original) +++ felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/AbstractParserTest.java Mon Mar 21 16:52:52 2016 @@ -16,27 +16,32 @@ * specific language governing permissions and limitations * under the License. */ - package org.apache.felix.gogo.runtime; import junit.framework.TestCase; +import org.apache.felix.gogo.runtime.threadio.ThreadIOImpl; -/** - * @author <a href="mailto:[email protected]">Felix Project Team</a> - */ -abstract class BaseTestCase extends TestCase -{ - protected Context m_ctx; +public abstract class AbstractParserTest extends TestCase { + + private ThreadIOImpl threadIO; @Override - protected final void setUp() throws Exception - { - m_ctx = new Context(true); + protected void setUp() throws Exception { + super.setUp(); + threadIO = new ThreadIOImpl(); + threadIO.start(); } @Override - protected final void tearDown() throws Exception - { - m_ctx.stop(); + protected void tearDown() throws Exception { + threadIO.stop(); + super.tearDown(); } + + public class Context extends org.apache.felix.gogo.runtime.Context { + public Context() { + super(AbstractParserTest.this.threadIO); + } + } + } Modified: felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/Context.java URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/Context.java?rev=1735994&r1=1735993&r2=1735994&view=diff ============================================================================== --- felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/Context.java (original) +++ felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/Context.java Mon Mar 21 16:52:52 2016 @@ -18,42 +18,22 @@ */ package org.apache.felix.gogo.runtime; -import org.apache.felix.gogo.runtime.threadio.ThreadIOImpl; import org.apache.felix.service.command.CommandSession; +import org.apache.felix.service.threadio.ThreadIO; public class Context extends CommandProcessorImpl { public static final String EMPTY = ""; - - private static final ThreadIOImpl threadio; + private final CommandSession session; - static - { - threadio = new ThreadIOImpl(); - threadio.start(); - } - - public Context(boolean foo) + public Context(ThreadIO threadio) { super(threadio); addCommand("osgi", this, "addCommand"); addCommand("osgi", this, "removeCommand"); addCommand("osgi", this, "eval"); - session = (CommandSessionImpl) createSession(System.in, System.out, System.err); - } - - @Override - public void stop() - { - try - { - super.stop(); - } - finally - { - threadio.stop(); - } + session = createSession(System.in, System.out, System.err); } public Object execute(CharSequence source) throws Exception @@ -76,9 +56,9 @@ public class Context extends CommandProc addCommand("test", target, function); } - public void set(String name, Object value) + public Object set(String name, Object value) { - session.put(name, value); + return session.put(name, value); } public Object get(String name) Modified: felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestCoercion.java URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestCoercion.java?rev=1735994&r1=1735993&r2=1735994&view=diff ============================================================================== --- felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestCoercion.java (original) +++ felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestCoercion.java Mon Mar 21 16:52:52 2016 @@ -22,7 +22,7 @@ import org.apache.felix.service.command. import org.apache.felix.service.command.Descriptor; import org.apache.felix.service.command.Parameter; -public class TestCoercion extends BaseTestCase +public class TestCoercion extends AbstractParserTest { public boolean fBool(boolean t) { @@ -57,31 +57,32 @@ public class TestCoercion extends BaseTe public void testSimpleTypes() throws Exception { - m_ctx.addCommand("fBool", this); - m_ctx.addCommand("fDouble", this); - m_ctx.addCommand("fInt", this); - m_ctx.addCommand("fLong", this); - m_ctx.addCommand("fString", this); - - assertEquals("fBool true", true, m_ctx.execute("fBool true")); - assertEquals("fBool 'false'", false, m_ctx.execute("fBool 'false'")); - - assertEquals("fDouble 11", 11.0, m_ctx.execute("fDouble 11")); - assertEquals("fDouble '11'", 11.0, m_ctx.execute("fDouble '11'")); - - assertEquals("fInt 22", 22, m_ctx.execute("fInt 22")); - assertEquals("fInt '23'", 23, m_ctx.execute("fInt '23'")); - assertEquals("fInt 1 2", "array", m_ctx.execute("fInt 1 2")); - - assertEquals("fLong 33", 33L, m_ctx.execute("fLong 33")); - assertEquals("fLong '34'", 34L, m_ctx.execute("fLong '34'")); - - assertEquals("fString wibble", "wibble", m_ctx.execute("fString wibble")); - assertEquals("fString 'wibble'", "wibble", m_ctx.execute("fString 'wibble'")); + Context c = new Context(); + c.addCommand("fBool", this); + c.addCommand("fDouble", this); + c.addCommand("fInt", this); + c.addCommand("fLong", this); + c.addCommand("fString", this); + + assertEquals("fBool true", true, c.execute("fBool true")); + assertEquals("fBool 'false'", false, c.execute("fBool 'false'")); + + assertEquals("fDouble 11", 11.0, c.execute("fDouble 11")); + assertEquals("fDouble '11'", 11.0, c.execute("fDouble '11'")); + + assertEquals("fInt 22", 22, c.execute("fInt 22")); + assertEquals("fInt '23'", 23, c.execute("fInt '23'")); + assertEquals("fInt 1 2", "array", c.execute("fInt 1 2")); + + assertEquals("fLong 33", 33L, c.execute("fLong 33")); + assertEquals("fLong '34'", 34L, c.execute("fLong '34'")); + + assertEquals("fString wibble", "wibble", c.execute("fString wibble")); + assertEquals("fString 'wibble'", "wibble", c.execute("fString 'wibble'")); try { - Object r = m_ctx.execute("fString "); + Object r = c.execute("fString "); fail("too few args: expected IllegalArgumentException, got: " + r); } catch (IllegalArgumentException e) @@ -90,7 +91,7 @@ public class TestCoercion extends BaseTe try { - Object r = m_ctx.execute("fString a b"); + Object r = c.execute("fString a b"); fail("too many args: expected IllegalArgumentException, got: " + r); } catch (IllegalArgumentException e) @@ -99,7 +100,7 @@ public class TestCoercion extends BaseTe try { - Object r = m_ctx.execute("fLong string"); + Object r = c.execute("fLong string"); fail("wrong arg type: expected IllegalArgumentException, got: " + r); } catch (IllegalArgumentException e) @@ -119,11 +120,12 @@ public class TestCoercion extends BaseTe public void testBestCoercion() throws Exception { - m_ctx.addCommand("bundles", this); + Context c = new Context(); + c.addCommand("bundles", this); - assertEquals("bundles myloc", "string", m_ctx.execute("bundles myloc")); - assertEquals("bundles 1", "long", m_ctx.execute("bundles 1")); - assertEquals("bundles '1'", "string", m_ctx.execute("bundles '1'")); + assertEquals("bundles myloc", "string", c.execute("bundles myloc")); + assertEquals("bundles 1", "long", c.execute("bundles 1")); + assertEquals("bundles '1'", "string", c.execute("bundles '1'")); } @Descriptor("list all installed bundles") @@ -145,23 +147,24 @@ public class TestCoercion extends BaseTe public void testParameter0() throws Exception { - m_ctx.addCommand("p0", this); - m_ctx.addCommand("p01", this); - - assertEquals("p0", "false:false", m_ctx.execute("p0")); - assertEquals("p0 -l", "true:false", m_ctx.execute("p0 -l")); - assertEquals("p0 --location", "true:false", m_ctx.execute("p0 --location")); - assertEquals("p0 -l -s", "true:true", m_ctx.execute("p0 -l -s")); - assertEquals("p0 -s -l", "true:true", m_ctx.execute("p0 -s -l")); + Context c = new Context(); + c.addCommand("p0", this); + c.addCommand("p01", this); + + assertEquals("p0", "false:false", c.execute("p0")); + assertEquals("p0 -l", "true:false", c.execute("p0 -l")); + assertEquals("p0 --location", "true:false", c.execute("p0 --location")); + assertEquals("p0 -l -s", "true:true", c.execute("p0 -l -s")); + assertEquals("p0 -s -l", "true:true", c.execute("p0 -s -l")); try { - Object r = m_ctx.execute("p0 wibble"); + Object r = c.execute("p0 wibble"); fail("too many args: expected IllegalArgumentException, got: " + r); } catch (IllegalArgumentException e) { } - assertEquals("p01 -f", true, m_ctx.execute("p01 -f")); + assertEquals("p01 -f", true, c.execute("p01 -f")); } public String p1( @@ -172,16 +175,17 @@ public class TestCoercion extends BaseTe public void testParameter1() throws Exception { - m_ctx.addCommand("p1", this); + Context c = new Context(); + c.addCommand("p1", this); - assertEquals("no parameter", "absent", m_ctx.execute("p1")); + assertEquals("no parameter", "absent", c.execute("p1")); // FELIX-2894 - assertEquals("correct parameter", "wibble", m_ctx.execute("p1 -p wibble")); + assertEquals("correct parameter", "wibble", c.execute("p1 -p wibble")); try { - Object r = m_ctx.execute("p1 -p"); + Object r = c.execute("p1 -p"); fail("missing parameter: expected IllegalArgumentException, got: " + r); } catch (IllegalArgumentException e) @@ -190,11 +194,12 @@ public class TestCoercion extends BaseTe try { - Object r = m_ctx.execute("p1 -X"); + Object r = c.execute("p1 -X"); fail("wrong parameter: expected IllegalArgumentException, got: " + r); } catch (IllegalArgumentException e) { } } + } Modified: felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestParser.java URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestParser.java?rev=1735994&r1=1735993&r2=1735994&view=diff ============================================================================== --- felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestParser.java (original) +++ felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestParser.java Mon Mar 21 16:52:52 2016 @@ -27,28 +27,34 @@ import java.util.Collection; import java.util.List; import java.util.regex.Pattern; +import org.apache.felix.gogo.runtime.Parser.Pipeline; +import org.apache.felix.gogo.runtime.Parser.Program; +import org.apache.felix.gogo.runtime.Parser.Sequence; +import org.apache.felix.gogo.runtime.Parser.Statement; import org.apache.felix.service.command.CommandSession; import org.apache.felix.service.command.Function; -public class TestParser extends BaseTestCase +public class TestParser extends AbstractParserTest { int beentheredonethat = 0; public void testEvaluatation() throws Exception { - m_ctx.addCommand("echo", this); - m_ctx.addCommand("capture", this); - - assertEquals("a", m_ctx.execute("echo a | capture")); - assertEquals("a", m_ctx.execute("(echo a) | capture")); - assertEquals("a", m_ctx.execute("((echo a)) | capture")); + Context c = new Context(); + c.addCommand("echo", this); + c.addCommand("capture", this); + + assertEquals("a", c.execute("echo a | capture")); + assertEquals("a", c.execute("(echo a) | capture")); + assertEquals("a", c.execute("((echo a)) | capture")); } public void testUnknownCommand() throws Exception { + Context c = new Context(); try { - m_ctx.execute("echo"); + c.execute("echo"); fail("Execution should have failed due to missing command"); } catch (IllegalArgumentException e) @@ -59,120 +65,129 @@ public class TestParser extends BaseTest public void testSpecialValues() throws Exception { - assertEquals(false, m_ctx.execute("false")); - assertEquals(true, m_ctx.execute("true")); - assertEquals(null, m_ctx.execute("null")); + Context c = new Context(); + assertEquals(false, c.execute("false")); + assertEquals(true, c.execute("true")); + assertEquals(null, c.execute("null")); } public void testQuotes() throws Exception { - m_ctx.addCommand("echo", this); - m_ctx.set("c", "a"); - - assertEquals("a b", m_ctx.execute("echo a b")); - assertEquals("a b", m_ctx.execute("echo 'a b'")); - assertEquals("a b", m_ctx.execute("echo \"a b\"")); - assertEquals("a b", m_ctx.execute("echo a b")); - assertEquals("a b", m_ctx.execute("echo 'a b'")); - assertEquals("a b", m_ctx.execute("echo \"a b\"")); - assertEquals("a b", m_ctx.execute("echo $c b")); - assertEquals("$c b", m_ctx.execute("echo '$c b'")); - assertEquals("a b", m_ctx.execute("echo \"$c b\"")); - assertEquals("a b", m_ctx.execute("echo ${c} b")); - assertEquals("${c} b", m_ctx.execute("echo '${c} b'")); - assertEquals("a b", m_ctx.execute("echo \"${c} b\"")); - assertEquals("aa", m_ctx.execute("echo $c$c")); - assertEquals("a ;a", m_ctx.execute("echo a\\ \\;a")); - assertEquals("baabab", m_ctx.execute("echo b${c}${c}b${c}b")); + Context c = new Context(); + c.addCommand("echo", this); + c.set("c", "a"); + + assertEquals("a b", c.execute("echo a b")); + assertEquals("a b", c.execute("echo 'a b'")); + assertEquals("a b", c.execute("echo \"a b\"")); + assertEquals("a b", c.execute("echo a b")); + assertEquals("a b", c.execute("echo 'a b'")); + assertEquals("a b", c.execute("echo \"a b\"")); + assertEquals("a b", c.execute("echo $c b")); + assertEquals("$c b", c.execute("echo '$c b'")); + assertEquals("a b", c.execute("echo \"$c b\"")); + assertEquals("a b", c.execute("echo ${c} b")); + assertEquals("${c} b", c.execute("echo '${c} b'")); + assertEquals("a b", c.execute("echo \"${c} b\"")); + assertEquals("aa", c.execute("echo $c$c")); + assertEquals("a ;a", c.execute("echo a\\ \\;a")); + assertEquals("baabab", c.execute("echo b${c}${c}b${c}b")); - m_ctx.set("d", "a b "); - assertEquals("a b ", m_ctx.execute("echo \"$d\"")); + c.set("d", "a b "); + assertEquals("a b ", c.execute("echo \"$d\"")); } public void testScope() throws Exception { - m_ctx.addCommand("echo", this); - assertEquals("$a", m_ctx.execute("test:echo \\$a")); - assertEquals("file://poo", m_ctx.execute("test:echo file://poo")); + Context c = new Context(); + c.addCommand("echo", this); + assertEquals("$a", c.execute("test:echo \\$a")); + assertEquals("file://poo", c.execute("test:echo file://poo")); } public void testPipe() throws Exception { - m_ctx.addCommand("echo", this); - m_ctx.addCommand("capture", this); - m_ctx.addCommand("grep", this); - m_ctx.addCommand("echoout", this); - m_ctx.execute("myecho = { echoout $args }"); - assertEquals("def", m_ctx.execute("echo def|grep d.*|capture")); - assertEquals("def", m_ctx.execute("echoout def|grep d.*|capture")); - assertEquals("def", m_ctx.execute("myecho def|grep d.*|capture")); + Context c = new Context(); + c.addCommand("echo", this); + c.addCommand("capture", this); + c.addCommand("grep", this); + c.addCommand("echoout", this); + c.execute("myecho = { echoout $args }"); + assertEquals("def", c.execute("echo def|grep d.*|capture")); + assertEquals("def", c.execute("echoout def|grep d.*|capture")); + assertEquals("def", c.execute("myecho def|grep d.*|capture")); assertEquals("def", - m_ctx.execute("(echoout abc; echoout def; echoout ghi)|grep d.*|capture")); - assertEquals("", m_ctx.execute("echoout def; echoout ghi | grep d.* | capture")); - assertEquals("hello world", m_ctx.execute("echo hello world|capture")); + c.execute("(echoout abc; echoout def; echoout ghi)|grep d.*|capture")); + assertEquals("", c.execute("echoout def; echoout ghi | grep d.* | capture")); + assertEquals("hello world", c.execute("echo hello world|capture")); assertEquals("defghi", - m_ctx.execute("(echoout abc; echoout def; echoout ghi)|grep 'def|ghi'|capture")); + c.execute("(echoout abc; echoout def; echoout ghi)|grep 'def|ghi'|capture")); } public void testAssignment() throws Exception { - m_ctx.addCommand("echo", this); - m_ctx.addCommand("grep", this); - assertEquals("a", m_ctx.execute("a = a; echo ${$a}")); - - assertEquals("hello", m_ctx.execute("echo hello")); - assertEquals("hello", m_ctx.execute("a = (echo hello)")); - //assertEquals("a", m_ctx.execute("a = a; echo $(echo a)")); // #p2 - no eval in var expansion - assertEquals("3", m_ctx.execute("a=3; echo $a")); - assertEquals("3", m_ctx.execute("a = 3; echo $a")); - assertEquals("a", m_ctx.execute("a = a; echo ${$a}")); + Context c = new Context(); + c.addCommand("echo", this); + c.addCommand("grep", this); + assertEquals("a", c.execute("a = a; echo ${$a}")); + + assertEquals("hello", c.execute("echo hello")); + assertEquals("hello", c.execute("a = (echo hello)")); + //assertEquals("a", c.execute("a = a; echo $(echo a)")); // #p2 - no eval in var expansion + assertEquals("3", c.execute("a=3; echo $a")); + assertEquals("3", c.execute("a = 3; echo $a")); + assertEquals("a", c.execute("a = a; echo ${$a}")); } public void testComment() throws Exception { - m_ctx.addCommand("echo", this); - assertEquals("1", m_ctx.execute("echo 1 // hello")); + Context c = new Context(); + c.addCommand("echo", this); + assertEquals("1", c.execute("echo 1 // hello")); } public void testClosure() throws Exception { - m_ctx.addCommand("echo", this); - m_ctx.addCommand("capture", this); - - assertEquals("a", m_ctx.execute("e = { echo $1 } ; e a b")); - assertEquals("b", m_ctx.execute("e = { echo $2 } ; e a b")); - assertEquals("b", m_ctx.execute("e = { eval $args } ; e echo b")); - assertEquals("ca b", m_ctx.execute("e = { echo c$args } ; e a b")); - assertEquals("c a b", m_ctx.execute("e = { echo c $args } ; e a b")); - assertEquals("ca b", m_ctx.execute("e = { echo c$args } ; e 'a b'")); + Context c = new Context(); + c.addCommand("echo", this); + c.addCommand("capture", this); + + assertEquals("a", c.execute("e = { echo $1 } ; e a b")); + assertEquals("b", c.execute("e = { echo $2 } ; e a b")); + assertEquals("b", c.execute("e = { eval $args } ; e echo b")); + assertEquals("ca b", c.execute("e = { echo c$args } ; e a b")); + assertEquals("c a b", c.execute("e = { echo c $args } ; e a b")); + assertEquals("ca b", c.execute("e = { echo c$args } ; e 'a b'")); } public void testArray() throws Exception { - m_ctx.set("echo", this); + Context c = new Context(); + c.set("echo", true); assertEquals("http://www.aqute.biz?com=2&biz=1", - m_ctx.execute("['http://www.aqute.biz?com=2&biz=1'] get 0")); - assertEquals("{a=2, b=3}", m_ctx.execute("[a=2 b=3]").toString()); - assertEquals(3L, m_ctx.execute("[a=2 b=3] get b")); - assertEquals("[3, 4]", m_ctx.execute("[1 2 [3 4] 5 6] get 2").toString()); - assertEquals(5, m_ctx.execute("[1 2 [3 4] 5 6] size")); + c.execute("['http://www.aqute.biz?com=2&biz=1'] get 0")); + assertEquals("{a=2, b=3}", c.execute("[a=2 b=3]").toString()); + assertEquals(3L, c.execute("[a=2 b=3] get b")); + assertEquals("[3, 4]", c.execute("[1 2 [3 4] 5 6] get 2").toString()); + assertEquals(5, c.execute("[1 2 [3 4] 5 6] size")); } public void testParentheses() { Parser parser = new Parser("(a|b)|(d|f)"); - List<List<List<Token>>> p = parser.program(); - assertEquals("a|b", p.get(0).get(0).get(0).toString()); + Program p = parser.program(); + assertEquals("a|b", ((Sequence) ((Statement) ((Pipeline) p.tokens().get(0)).tokens().get(0)).tokens().get(0)).program().toString()); parser = new Parser("grep (d.*)|grep (d|f)"); p = parser.program(); - assertEquals("d.*", p.get(0).get(0).get(1).toString()); + assertEquals("d.*", ((Sequence)((Statement) ((Pipeline) p.tokens().get(0)).tokens().get(0)).tokens().get(1)).program().toString()); } public void testEcho() throws Exception { - m_ctx.addCommand("echo", this); - m_ctx.execute("echo peter"); + Context c = new Context(); + c.addCommand("echo", this); + c.execute("echo peter"); } public void grep(String match) throws IOException @@ -205,20 +220,22 @@ public class TestParser extends BaseTest public void testVars() throws Exception { - m_ctx.addCommand("echo", this); + Context c = new Context(); + c.addCommand("echo", this); - assertEquals("", m_ctx.execute("echo ${very.likely.that.this.does.not.exist}")); - assertNotNull(m_ctx.execute("echo ${java.shell.name}")); - assertEquals("a", m_ctx.execute("a = a; echo ${a}")); + assertEquals("", c.execute("echo ${very.likely.that.this.does.not.exist}")); + assertNotNull(c.execute("echo ${java.shell.name}")); + assertEquals("a", c.execute("a = a; echo ${a}")); } public void testFunny() throws Exception { - m_ctx.addCommand("echo", this); - assertEquals("a", m_ctx.execute("echo a") + ""); - assertEquals("a", m_ctx.execute("eval (echo echo) a") + ""); - //assertEquals("a", m_ctx.execute("((echo echo) echo) (echo a)") + ""); - assertEquals("3", m_ctx.execute("[a=2 (echo b)=(echo 3)] get b").toString()); + Context c = new Context(); + c.addCommand("echo", this); + assertEquals("a", c.execute("echo a") + ""); + assertEquals("a", c.execute("eval (echo echo) a") + ""); + //assertEquals("a", c.execute("((echo echo) echo) (echo a)") + ""); + assertEquals("3", c.execute("[a=2 (echo b)=(echo 3)] get b").toString()); } public CharSequence echo(Object args[]) @@ -248,21 +265,22 @@ public class TestParser extends BaseTest public void testContext() throws Exception { - m_ctx.addCommand("ls", this); + Context c = new Context(); + c.addCommand("ls", this); beentheredonethat = 0; - m_ctx.execute("ls"); + c.execute("ls"); assertEquals(1, beentheredonethat); beentheredonethat = 0; - m_ctx.execute("ls 10"); + c.execute("ls 10"); assertEquals(10, beentheredonethat); beentheredonethat = 0; - m_ctx.execute("ls a b c d e f g h i j"); + c.execute("ls a b c d e f g h i j"); assertEquals(10, beentheredonethat); beentheredonethat = 0; - Integer result = (Integer) m_ctx.execute("ls (ls 5)"); + Integer result = (Integer) c.execute("ls (ls 5)"); assertEquals(10, beentheredonethat); assertEquals((Integer) 5, result); } @@ -293,40 +311,52 @@ public class TestParser extends BaseTest public void testProgram() { - List<List<List<Token>>> x = new Parser("abc def|ghi jkl;mno pqr|stu vwx").program(); - assertEquals("abc", x.get(0).get(0).get(0).toString()); - assertEquals("def", x.get(0).get(0).get(1).toString()); - assertEquals("ghi", x.get(0).get(1).get(0).toString()); - assertEquals("jkl", x.get(0).get(1).get(1).toString()); - assertEquals("mno", x.get(1).get(0).get(0).toString()); - assertEquals("pqr", x.get(1).get(0).get(1).toString()); - assertEquals("stu", x.get(1).get(1).get(0).toString()); - assertEquals("vwx", x.get(1).get(1).get(1).toString()); + Program x = new Parser("abc def|ghi jkl;mno pqr|stu vwx").program(); + Pipeline p0 = (Pipeline) x.tokens().get(0); + Statement s00 = (Statement) p0.tokens().get(0); + Statement s01 = (Statement) p0.tokens().get(1); + Pipeline p1 = (Pipeline) x.tokens().get(1); + Statement s10 = (Statement) p1.tokens().get(0); + Statement s11 = (Statement) p1.tokens().get(1); + assertEquals("abc", s00.tokens().get(0).toString()); + assertEquals("def", s00.tokens().get(1).toString()); + assertEquals("ghi", s01.tokens().get(0).toString()); + assertEquals("jkl", s01.tokens().get(1).toString()); + assertEquals("mno", s10.tokens().get(0).toString()); + assertEquals("pqr", s10.tokens().get(1).toString()); + assertEquals("stu", s11.tokens().get(0).toString()); + assertEquals("vwx", s11.tokens().get(1).toString()); } public void testStatements() { - List<List<Token>> x = new Parser("abc def|ghi jkl|mno pqr").program().get(0); - assertEquals("abc", x.get(0).get(0).toString()); - assertEquals("def", x.get(0).get(1).toString()); - assertEquals("ghi", x.get(1).get(0).toString()); - assertEquals("jkl", x.get(1).get(1).toString()); - assertEquals("mno", x.get(2).get(0).toString()); - assertEquals("pqr", x.get(2).get(1).toString()); + Program x = new Parser("abc def|ghi jkl|mno pqr").program(); + Pipeline p0 = (Pipeline) x.tokens().get(0); + Statement s00 = (Statement) p0.tokens().get(0); + Statement s01 = (Statement) p0.tokens().get(1); + Statement s02 = (Statement) p0.tokens().get(2); + assertEquals("abc", s00.tokens().get(0).toString()); + assertEquals("def", s00.tokens().get(1).toString()); + assertEquals("ghi", s01.tokens().get(0).toString()); + assertEquals("jkl", s01.tokens().get(1).toString()); + assertEquals("mno", s02.tokens().get(0).toString()); + assertEquals("pqr", s02.tokens().get(1).toString()); } public void testSimpleValue() { - List<Token> x = new Parser( - "abc def.ghi http://www.osgi.org?abc=&x=1 [1,2,3] {{{{{{{xyz}}}}}}} (immediate) {'{{{{{'} {\\{} 'abc{}'").program().get(0).get(0); + Program p = new Parser( + "abc def.ghi http://www.osgi.org?abc=&x=1 [1,2,3] {{{{{{{xyz}}}}}}} (immediate) {'{{{{{'} {\\{} 'abc{}'") + .program(); + List<Token> x = ((Statement) p.tokens().get(0)).tokens(); assertEquals("abc", x.get(0).toString()); assertEquals("def.ghi", x.get(1).toString()); assertEquals("http://www.osgi.org?abc=&x=1", x.get(2).toString()); - assertEquals("1,2,3", x.get(3).toString()); - assertEquals("{{{{{{xyz}}}}}}", x.get(4).toString()); - assertEquals("immediate", x.get(5).toString()); - assertEquals("'{{{{{'", x.get(6).toString()); - assertEquals("\\{", x.get(7).toString()); + assertEquals("[1,2,3]", x.get(3).toString()); + assertEquals("{{{{{{{xyz}}}}}}}", x.get(4).toString()); + assertEquals("(immediate)", x.get(5).toString()); + assertEquals("{'{{{{{'}", x.get(6).toString()); + assertEquals("{\\{}", x.get(7).toString()); assertEquals("'abc{}'", x.get(8).toString()); } Modified: felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestParser2.java URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestParser2.java?rev=1735994&r1=1735993&r2=1735994&view=diff ============================================================================== --- felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestParser2.java (original) +++ felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestParser2.java Mon Mar 21 16:52:52 2016 @@ -24,19 +24,20 @@ import java.io.EOFException; * Test features of the new parser/tokenizer, many of which are not supported * by the original parser. */ -public class TestParser2 extends BaseTestCase +public class TestParser2 extends AbstractParserTest { public void testComment() throws Exception { - m_ctx.addCommand("echo", this); + Context c = new Context(); + c.addCommand("echo", this); - assertEquals("file://wibble#tag", m_ctx.execute("echo file://wibble#tag")); - assertEquals("file:", m_ctx.execute("echo file: //wibble#tag")); + assertEquals("file://wibble#tag", c.execute("echo file://wibble#tag")); + assertEquals("file:", c.execute("echo file: //wibble#tag")); - assertEquals("PWD/*.java", m_ctx.execute("echo PWD/*.java")); + assertEquals("PWD/*.java", c.execute("echo PWD/*.java")); try { - m_ctx.execute("echo PWD /*.java"); + c.execute("echo PWD /*.java"); fail("expected EOFException"); } catch (EOFException e) @@ -44,42 +45,44 @@ public class TestParser2 extends BaseTes // expected } - assertEquals("ok", m_ctx.execute("// can't quote\necho ok\n")); + assertEquals("ok", c.execute("// can't quote\necho ok\n")); // quote in comment in closure - assertEquals("ok", m_ctx.execute("x = { // can't quote\necho ok\n}; x")); - assertEquals("ok", m_ctx.execute("x = {\n// can't quote\necho ok\n}; x")); - assertEquals("ok", m_ctx.execute("x = {// can't quote\necho ok\n}; x")); + assertEquals("ok", c.execute("x = { // can't quote\necho ok\n}; x")); + assertEquals("ok", c.execute("x = {\n// can't quote\necho ok\n}; x")); + assertEquals("ok", c.execute("x = {// can't quote\necho ok\n}; x")); } public void testCoercion() throws Exception { - m_ctx.addCommand("echo", this); + Context c = new Context(); + c.addCommand("echo", this); // FELIX-2432 - assertEquals("null x", m_ctx.execute("echo $expandsToNull x")); + assertEquals("null x", c.execute("echo $expandsToNull x")); } public void testStringExecution() throws Exception { - m_ctx.addCommand("echo", this); - m_ctx.addCommand("new", this); + Context c = new Context(); + c.addCommand("echo", this); + c.addCommand("new", this); // FELIX-2433 - assertEquals("helloworld", m_ctx.execute("echo \"$(echo hello)world\"")); + assertEquals("helloworld", c.execute("echo \"$(echo hello)world\"")); // FELIX-1473 - allow method calls on String objects - assertEquals("hello", m_ctx.execute("cmd = echo; eval $cmd hello")); - assertEquals(4, m_ctx.execute("'four' length")); + assertEquals("hello", c.execute("cmd = echo; eval $cmd hello")); + assertEquals(4, c.execute("'four' length")); try { - m_ctx.execute("four length"); + c.execute("four length"); fail("expected: command not found: four"); } catch (IllegalArgumentException e) { } // check CharSequence types are preserved - Object b = m_ctx.execute("b = new java.lang.StringBuilder"); + Object b = c.execute("b = new java.lang.StringBuilder"); assertTrue(b instanceof StringBuilder); - assertEquals(b, m_ctx.execute("c = $b")); + assertEquals(b, c.execute("c = $b")); } public CharSequence echo(Object args[]) Modified: felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestParser3.java URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestParser3.java?rev=1735994&r1=1735993&r2=1735994&view=diff ============================================================================== --- felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestParser3.java (original) +++ felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestParser3.java Mon Mar 21 16:52:52 2016 @@ -22,25 +22,19 @@ package org.apache.felix.gogo.runtime; * Test features of the new parser/tokenizer, many of which are not supported * by the original parser. */ -public class TestParser3 extends BaseTestCase +public class TestParser3 extends AbstractParserTest { public void testArithmetic() throws Exception { - m_ctx.addCommand("echo", this); + Context c = new Context(); + c.addCommand("echo", this); - try - { - assertEquals("10d", m_ctx.execute("echo %(2*(3+2))d")); - assertEquals(3l, m_ctx.execute("%(1+2)")); + assertEquals("10d", c.execute("echo %(2*(3+2))d")); + assertEquals(3l, c.execute("%(1+2)")); - m_ctx.set("a", 2l); - assertEquals(3l, m_ctx.execute("%(a+=1)")); - assertEquals(3l, m_ctx.get("a")); - } - finally - { - m_ctx.stop(); - } + c.set("a", 2l); + assertEquals(3l, c.execute("%(a+=1)")); + assertEquals(3l, c.get("a")); } public CharSequence echo(Object args[]) Modified: felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestTokenizer.java URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestTokenizer.java?rev=1735994&r1=1735993&r2=1735994&view=diff ============================================================================== --- felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestTokenizer.java (original) +++ felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestTokenizer.java Mon Mar 21 16:52:52 2016 @@ -18,6 +18,9 @@ */ package org.apache.felix.gogo.runtime; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -28,11 +31,15 @@ import java.net.URI; import java.util.HashMap; import java.util.Map; -import org.apache.felix.gogo.runtime.Tokenizer.Type; +import junit.framework.TestCase; + +import org.apache.felix.gogo.runtime.threadio.ThreadIOImpl; +import org.junit.Ignore; +import org.junit.Test; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; -public class TestTokenizer extends BaseTestCase +public class TestTokenizer { private final Map<String, Object> vars = new HashMap<String, Object>(); private final Evaluate evaluate; @@ -55,9 +62,14 @@ public class TestTokenizer extends BaseT { return vars.put(key, value); } + + public Object expr(Token t) { + throw new UnsupportedOperationException("expr not implemented."); + } }; } + @Test public void testHello() throws Exception { testHello("hello world\n"); @@ -91,14 +103,13 @@ public class TestTokenizer extends BaseT private void testHello(CharSequence text) throws Exception { Tokenizer t = new Tokenizer(text); - assertEquals(Type.WORD, t.next()); - assertEquals("hello", t.value().toString()); - assertEquals(Type.WORD, t.next()); - assertEquals("world", t.value().toString()); - assertEquals(Type.NEWLINE, t.next()); - assertEquals(Type.EOT, t.next()); + assertEquals("hello", t.next().toString()); + assertEquals("world", t.next().toString()); + assertEquals("\n", t.next().toString()); + assertNull(t.next()); } - + + @Test public void testString() throws Exception { testString("'single $quote' \"double $quote\"\n"); @@ -108,45 +119,52 @@ public class TestTokenizer extends BaseT private void testString(CharSequence text) throws Exception { Tokenizer t = new Tokenizer(text); - assertEquals(Type.WORD, t.next()); - assertEquals("'single $quote'", t.value().toString()); - assertEquals(Type.WORD, t.next()); - assertEquals("\"double $quote\"", t.value().toString()); - assertEquals(Type.NEWLINE, t.next()); - assertEquals(Type.EOT, t.next()); + assertEquals("'single $quote'", t.next().toString()); + assertEquals("\"double $quote\"", t.next().toString()); + assertEquals("\n", t.next().toString()); + assertNull(t.next()); } + @Test public void testClosure() throws Exception { testClosure2("x = { echo '}' $args //comment's\n}\n"); testClosure2("x={ echo '}' $args //comment's\n}\n"); - assertEquals(Type.CLOSURE, token1("{ echo \\{ $args \n}")); - assertEquals(Type.CLOSURE, token1("{ echo \\} $args \n}")); + token1("{ echo \\{ $args \n}"); + token1("{ echo \\} $args \n}"); } - /* - * x = {echo $args}; - */ + // + // x = {echo $args}; + // private void testClosure2(CharSequence text) throws Exception { Tokenizer t = new Tokenizer(text); - assertEquals(Type.WORD, t.next()); - assertEquals("x", t.value().toString()); - assertEquals(Type.ASSIGN, t.next()); - assertEquals(Type.CLOSURE, t.next()); - assertEquals(" echo '}' $args //comment's\n", t.value().toString()); - assertEquals(Type.NEWLINE, t.next()); - assertEquals(Type.EOT, t.next()); + assertEquals("x", t.next().toString()); + assertEquals("=", t.next().toString()); + assertEquals("{", t.next().toString()); + assertEquals("echo", t.next().toString()); + assertEquals("'}'", t.next().toString()); + assertEquals("$args", t.next().toString()); + assertEquals("\n", t.next().toString()); + assertEquals("}", t.next().toString()); + assertEquals("\n", t.next().toString()); + assertEquals(null, t.next()); } - private Type token1(CharSequence text) throws Exception + private void token1(CharSequence text) throws Exception { Tokenizer t = new Tokenizer(text); - Type type = t.next(); - assertEquals(Type.EOT, t.next()); - return type; + assertEquals("{", t.next().toString()); + assertEquals("echo", t.next().toString()); + t.next(); + assertEquals("$args", t.next().toString()); + assertEquals("\n", t.next().toString()); + assertEquals("}", t.next().toString()); + assertNull(t.next()); } + @Test public void testExpand() throws Exception { final URI home = new URI("/home/derek"); @@ -160,7 +178,7 @@ public class TestTokenizer extends BaseT vars.put(user, "Derek Baum"); // quote removal - assertEquals("hello", expand("hello")); + assertEquals("hello", expand("hello").toString()); assertEquals("hello", expand("'hello'")); assertEquals("\"hello\"", expand("'\"hello\"'")); assertEquals("hello", expand("\"hello\"")); @@ -266,33 +284,46 @@ public class TestTokenizer extends BaseT private Object expand(CharSequence word) throws Exception { - return Tokenizer.expand(word, evaluate); + return Expander.expand(word, evaluate); } + @Test public void testParser() throws Exception { new Parser("// comment\n" + "a=\"who's there?\"; ps -ef;\n" + "ls | \n grep y\n").program(); String p1 = "a=1 \\$b=2 c={closure}\n"; new Parser(p1).program(); - new Parser(new Token(Type.ARRAY, p1, (short) 0, (short) 0)).program(); + new Parser("[" + p1 + "]").program(); } - /** - * FELIX-4679 / FELIX-4671. - */ + // + // FELIX-4679 / FELIX-4671. + // + @Test public void testScriptFelix4679() throws Exception { String script = "addcommand system (((${.context} bundles) 0) loadclass java.lang.System)"; - BundleContext bc = createMockContext(); + ThreadIOImpl tio = new ThreadIOImpl(); + tio.start(); + + try + { + BundleContext bc = createMockContext(); - m_ctx.addCommand("gogo", m_ctx, "addcommand"); - m_ctx.addConstant(".context", bc); + CommandProcessorImpl processor = new CommandProcessorImpl(tio); + processor.addCommand("gogo", processor, "addcommand"); + processor.addConstant(".context", bc); - CommandSessionImpl session = new CommandSessionImpl(m_ctx, new ByteArrayInputStream(script.getBytes()), System.out, System.err); + CommandSessionImpl session = new CommandSessionImpl(processor, new ByteArrayInputStream(script.getBytes()), System.out, System.err); - Closure c = new Closure(session, null, script); - assertNull(c.execute(session, null)); + Closure c = new Closure(session, null, script); + assertNull(c.execute(session, null)); + } + finally + { + tio.stop(); + } } private BundleContext createMockContext() throws ClassNotFoundException Modified: felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/threadio/TestThreadIO.java URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/threadio/TestThreadIO.java?rev=1735994&r1=1735993&r2=1735994&view=diff ============================================================================== --- felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/threadio/TestThreadIO.java (original) +++ felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/threadio/TestThreadIO.java Mon Mar 21 16:52:52 2016 @@ -38,7 +38,6 @@ public class TestThreadIO extends TestCa { ThreadIOImpl tio = new ThreadIOImpl(); tio.start(); - List<ByteArrayOutputStream> list = new ArrayList<ByteArrayOutputStream>(); for (int i = 0; i < 10; i++) { @@ -67,7 +66,6 @@ public class TestThreadIO extends TestCa { ThreadIOImpl tio = new ThreadIOImpl(); tio.start(); - System.out.println("Hello World"); ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream();
