Skip to content

Commit

Permalink
Merge pull request #39 from wesamhaboush/master
Browse files Browse the repository at this point in the history
Add comment char escape capability
  • Loading branch information
Jiaqi Guo authored Feb 12, 2017
2 parents 6657bd3 + 379ba64 commit 385df87
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/main/java/org/cyclopsgroup/jmxterm/cc/CommandCenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
public class CommandCenter
{
private static final String COMMAND_DELIMITER = "&&";
static final String ESCAPE_CHAR_REGEX = "(?<!\\\\)#";

/**
* Argument tokenizer that parses arguments
Expand Down Expand Up @@ -124,11 +125,10 @@ private void doExecute( String command )
return;
}
// Truncate command if there's # character
int commandEnds = command.indexOf( '#' );
if ( commandEnds != -1 )
{
command = command.substring( 0, commandEnds );
}
// Note: this allows people to set properties to values with # (e.g.: set AttributeA /a/\\#something)
command = command
.split(ESCAPE_CHAR_REGEX)[0] //take out all commented out sections
.replace("\\#", "#"); //fix escaped to non-escaped comment charaters
// If command includes multiple segments, call them one by one using recursive call
if ( command.indexOf( COMMAND_DELIMITER ) != -1 )
{
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/org/cyclopsgroup/jmxterm/cc/CommandCenterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import org.junit.Before;
import org.junit.Test;

import static org.cyclopsgroup.jmxterm.cc.CommandCenter.ESCAPE_CHAR_REGEX;


/**
* Test case of {@link CommandCenter}
*
Expand Down Expand Up @@ -124,4 +127,21 @@ public void testSingleSimpleArgument()
{
runCommandAndVerifyArguments( "test 1", Arrays.asList( "1" ) );
}

@Test
public void testRegexEscapesCorrectly() {
final String s1 = "".split(ESCAPE_CHAR_REGEX)[0];
final String s2 = "a b c".split(ESCAPE_CHAR_REGEX)[0];
final String s3 = "a #b c".split(ESCAPE_CHAR_REGEX)[0];
final String s4 = "a #b c #".split(ESCAPE_CHAR_REGEX)[0];
final String s5 = "a \\#b c #".split(ESCAPE_CHAR_REGEX)[0];
final String s6 = "a #b c \\# something".split(ESCAPE_CHAR_REGEX)[0];

assertEquals("", s1);
assertEquals("a b c", s2);
assertEquals("a ", s3);
assertEquals("a ", s4);
assertEquals("a \\#b c ", s5);
assertEquals("a ", s6);
}
}

0 comments on commit 385df87

Please sign in to comment.