4747
4848import com .teragrep .pth_03 .ParserStructureTestingUtility ;
4949import com .teragrep .pth_03 .ParserSyntaxTestingUtility ;
50+ import com .teragrep .pth_03 .antlr .DPLLexer ;
51+ import com .teragrep .pth_03 .antlr .DPLParser ;
52+ import org .antlr .v4 .runtime .BailErrorStrategy ;
53+ import org .antlr .v4 .runtime .CharStream ;
54+ import org .antlr .v4 .runtime .CharStreams ;
55+ import org .antlr .v4 .runtime .CommonTokenStream ;
56+ import org .antlr .v4 .runtime .Token ;
57+ import org .antlr .v4 .runtime .misc .ParseCancellationException ;
5058import org .junit .jupiter .api .Assertions ;
5159import org .junit .jupiter .params .ParameterizedTest ;
5260import org .junit .jupiter .params .provider .ValueSource ;
5361import org .w3c .dom .NodeList ;
5462
63+ import java .util .Arrays ;
64+ import java .util .List ;
65+ import java .util .Locale ;
66+ import java .util .stream .Collectors ;
67+
5568import static org .junit .jupiter .api .Assertions .assertEquals ;
5669
5770public class TeragrepSyntaxTests {
@@ -558,4 +571,72 @@ void testTeragrepGetConfig(String arg) {
558571 Assertions .assertEquals ("get" , configGetNodes .item (0 ).getTextContent ());
559572 Assertions .assertEquals ("config" , configGetNodes .item (1 ).getTextContent ());
560573 }
574+
575+ @ ParameterizedTest (name = "{index} command = ''{0}''" )
576+ @ ValueSource (strings = {
577+ "| teragrep exec migrate epoch" ,
578+ "| teragrep exec MIGRATE EPOCH"
579+ })
580+ void testMigrateCommandTokenStrings (final String command ) {
581+ final CharStream input = CharStreams .fromString (command );
582+ final DPLLexer lexer = new DPLLexer (input );
583+ final CommonTokenStream tokens = new CommonTokenStream (lexer );
584+ tokens .fill ();
585+ final List <String > tokenStrings = tokens .getTokens ().stream ()
586+ .filter (t -> t .getChannel () == Token .DEFAULT_CHANNEL )
587+ .map (Token ::getText )
588+ .map (s -> s .toLowerCase (Locale .ROOT ))
589+ .map (s -> s .replace ("\" " , "" ).replace ("'" , "" )) // remove quotes
590+ .collect (Collectors .toList ());
591+ final List <String > expectedTokenStrings = Arrays .asList ("|" , "teragrep" , "exec" , "migrate" , "epoch" , "<eof>" );
592+ Assertions .assertEquals (expectedTokenStrings , tokenStrings );
593+ }
594+
595+ @ ParameterizedTest (name = "{index} command = ''{0}''" )
596+ @ ValueSource (strings = {
597+ "| teragrep exec migrate epoch" ,
598+ "| teragrep exec MIGRATE EPOCH"
599+ })
600+ public void testMigrateEpochCommand (final String command ) {
601+ final ParserStructureTestingUtility util = new ParserStructureTestingUtility ();
602+ final String hierarchyXPath = "/root/transformStatement/teragrepTransformation/t_execParameter/t_migrateParameter\n " ;
603+ final Object hierarchyResult = Assertions .assertDoesNotThrow (() -> util .xpathQuery (command , hierarchyXPath , false ));
604+ final NodeList hierarchyNodes = (NodeList ) hierarchyResult ;
605+ Assertions .assertEquals (1 , hierarchyNodes .getLength (),
606+ "expected hierarchyNodes length to be 1 for input <" + command + ">" );
607+ }
608+
609+ @ ParameterizedTest (name = "{index} command = ''{0}''" )
610+ @ ValueSource (strings = {
611+ "| teragrep exec migrate epoch"
612+ })
613+ public void adminMigrateEpochSyntaxParseTest (final String command ) {
614+ Assertions .assertDoesNotThrow (() -> {
615+ final CharStream input = CharStreams .fromString (command );
616+ final DPLLexer lexer = new DPLLexer (input );
617+ final DPLParser parser = new DPLParser (new CommonTokenStream (lexer ));
618+ parser .setErrorHandler (new BailErrorStrategy ()); // first syntax error aborts parsing
619+ final DPLParser .RootContext root = parser .root ();
620+ Assertions .assertNotNull (root );
621+ Assertions .assertEquals (0 , parser .getNumberOfSyntaxErrors ());
622+ });
623+ }
624+
625+ @ ParameterizedTest (name = "{index} command = ''{0}''" )
626+ @ ValueSource (strings = {
627+ "| teragrep exec migrate" ,
628+ "| teragrep exec migrate migrate epoch" ,
629+ "| teragrep exec migrate epoch epoch" ,
630+ "| teragrep exec MiGrAtE epoch" ,
631+ "| teragrep exec migrate EpOcH"
632+ })
633+ public void testInvalidRegexMigrateThrowsException (final String command ) {
634+ Assertions .assertThrows (ParseCancellationException .class , () -> {
635+ final CharStream input = CharStreams .fromString (command );
636+ final DPLLexer lexer = new DPLLexer (input );
637+ final DPLParser parser = new DPLParser (new CommonTokenStream (lexer ));
638+ parser .setErrorHandler (new BailErrorStrategy ()); // first syntax error aborts parsing
639+ parser .root ();
640+ });
641+ }
561642}
0 commit comments