TextPatternParser { public static TextPattern parse( String pattern ) throws TextPatternParsingException { List<TextPatternSegment> segments = new ArrayList<>(); boolean segment = false; boolean invalidExpression = true; Matcher m; if ( pattern != null && !pattern.isEmpty() ) { m = EXPRESSION_REGEX.matcher( pattern ); } else { throw new TextPatternParsingException( "Supplied expression was null or empty.", -1 ); } while ( m.find() ) { invalidExpression = false; String method = m.group( "Method" ); if ( method != null ) { String methodName = m.group( "MethodName" ); if ( methodName == null ) { if ( TextPatternMethod.TEXT.getType().validatePattern( method ) ) { segment = true; segments.add( new TextPatternSegment( TextPatternMethod.TEXT, method ) ); continue; } } else { try { TextPatternMethod textPatternMethod = TextPatternMethod.valueOf( methodName ); if ( textPatternMethod.getType().validatePattern( method ) ) { segment = true; segments.add( new TextPatternSegment( textPatternMethod, method ) ); continue; } } catch ( Exception e ) { } } throw new TextPatternParsingException( "Failed to parse the following method: '" + method + "'", m.start( "Method" ) ); } else if ( m.group( "Join" ) != null ) { if ( !segment ) { throw new TextPatternParsingException( "Unexpected '+'", m.start( "JoinValue" ) ); } else { segment = false; } } } if ( invalidExpression ) { throw new TextPatternParsingException( "The expression is invalid", -1 ); } if ( !segment ) { throw new TextPatternParsingException( "Unexpected '+' at the end of the expression", -1 ); } return new TextPattern( segments ); }  static TextPattern parse( String pattern );  }
@Test public void testParseNullExpressionThrowsException() throws TextPatternParser.TextPatternParsingException { thrown.expect( ParsingException ); TextPatternParser.parse( null ); }
@Test public void testParseEmptyExpressionThrowsException() throws TextPatternParser.TextPatternParsingException { thrown.expect( ParsingException ); TextPatternParser.parse( "" ); }
@Test public void testParseWhitespaceOnlyExpressionThrowsException() throws TextPatternParser.TextPatternParsingException { thrown.expect( ParsingException ); TextPatternParser.parse( " " ); }
@Test public void testParseWithUnexpectedPlusThrowsException() throws TextPatternParser.TextPatternParsingException { thrown.expect( ParsingException ); TextPatternParser.parse( "+" ); }
@Test public void testParseWithInvalidInputThrowsException() throws TextPatternParser.TextPatternParsingException { thrown.expect( ParsingException ); TextPatternParser.parse( "Z" ); }
@Test public void testParseBadTextSegment() throws TextPatternParser.TextPatternParsingException { thrown.expect( ParsingException ); TextPatternParser.parse( "\"This segment has no end" ); }
@Test public void testParseFullValidExpression() throws TextPatternParser.TextPatternParsingException { String TEXT_1 = "\"ABC\""; String SEPARATOR = "\"-\""; String SEQUENTIAL = "SEQUENTIAL(###)"; String expression = String.format( " %s + %s + %s", TEXT_1, SEPARATOR, SEQUENTIAL ); TextPattern textPattern = TextPatternParser.parse( expression ); assertNotNull( textPattern ); List<TextPatternSegment> segments = textPattern.getSegments(); assertEquals( segments.size(), 3 ); assertEquals( segments.get( 0 ).getMethod(), TextPatternMethod.TEXT ); assertEquals( segments.get( 1 ).getMethod(), TextPatternMethod.TEXT ); assertEquals( segments.get( 2 ).getMethod(), TextPatternMethod.SEQUENTIAL ); }
@Test public void testParsePatternEndWithJoinThrowsException() throws TextPatternParser.TextPatternParsingException { String pattern = "RANDOM(#) + "; thrown.expect( ParsingException ); TextPatternParser.parse( pattern ); }
@Test public void testCompletePatternOK() throws TextPatternParser.TextPatternParsingException { String pattern = "ORG_UNIT_CODE() + CURRENT_DATE(yyyy) + RANDOM(###) + \"-OK\""; TextPattern textPattern = TextPatternParser.parse( pattern ); List<TextPatternSegment> segments = textPattern.getSegments(); assertEquals( 4, segments.size() ); assertEquals( segments.get( 0 ).getMethod(), TextPatternMethod.ORG_UNIT_CODE ); assertEquals( segments.get( 1 ).getMethod(), TextPatternMethod.CURRENT_DATE ); assertEquals( segments.get( 2 ).getMethod(), TextPatternMethod.RANDOM ); assertEquals( segments.get( 3 ).getMethod(), TextPatternMethod.TEXT ); }