@Test public void testRecognizeImport() { String[] validLines = { "import facets.rcf;", "IMPORT facets.rcf;", "import http: "import http: " import facets.rcf ; ", "import facets.rcf; # with an inline comment" }; for( String line : validLines ) { FileDefinitionParser parser = new FileDefinitionParser( null, false ); int code = parser.recognizeImport( line ); FileDefinition def = parser.getFileRelations(); Assert.assertEquals( "Expected a YES code for '" + line + "'", FileDefinitionParser.P_CODE_YES, code ); Assert.assertEquals( "No parsing error was expected for '" + line + "'", 0, def.getParsingErrors().size()); Assert.assertEquals( "Expected ONE block to be registered for '" + line + "'", 1, def.getBlocks().size()); BlockImport imp = (BlockImport) def.getBlocks().iterator().next(); Assert.assertEquals( "Expected ONE import block for '" + line + "'", AbstractBlock.IMPORT, imp.getInstructionType()); Assert.assertFalse( "URI cannot be empty for '" + line + "'", Utils.isEmptyOrWhitespaces( imp.getUri())); Assert.assertEquals( "URI cannot have surrounding spaces '" + line + "'", imp.getUri(), imp.getUri().trim()); Assert.assertNotSame( "URI cannot be the entire line for '" + line + "'", imp.getUri(), line ); Assert.assertTrue( "The URI was not found in '" + line + "'", line.contains( imp.getUri())); } String[][] commentedLines = { { "import facets.rcf;", "" }, { "import facets.rcf;", "# stuck comment" }, { "import facets.rcf;", "#" }, { "import facets.rcf;", "\t# some in-line comment " } }; for( String[] line : commentedLines ) { FileDefinitionParser parser = new FileDefinitionParser( null, false ); String theLine = line[ 0 ] + line[ 1 ]; int code = parser.recognizeImport( theLine ); FileDefinition def = parser.getFileRelations(); Assert.assertEquals( "Expected a YES code for '" + theLine + "'", FileDefinitionParser.P_CODE_YES, code ); Assert.assertEquals( "No parsing error was expected for '" + theLine + "'", 0, def.getParsingErrors().size()); Assert.assertEquals( "Expected ONE block to be registered for '" + theLine + "'", 1, def.getBlocks().size()); BlockImport imp = (BlockImport) def.getBlocks().iterator().next(); Assert.assertEquals( "Expected ONE import block for '" + theLine + "'", AbstractBlock.IMPORT, imp.getInstructionType()); String expected = Utils.isEmptyOrWhitespaces( line[ 1 ]) ? null : line[ 1 ]; Assert.assertEquals( "Invalid in-line comment for '" + theLine + "'", expected, imp.getInlineComment()); } String[] invalidLines = { "imports facets.rcf;", "IPORT facets.rcf;", "facet", "", " #" }; for( String line : invalidLines ) { FileDefinitionParser parser = new FileDefinitionParser( null, false ); int code = parser.recognizeImport( line ); Assert.assertEquals( "Expected a NO code for '" + line + "'", FileDefinitionParser.P_CODE_NO, code ); } Map<String,ErrorCode> invalidLineToErrorCode = new LinkedHashMap<> (); invalidLineToErrorCode.put( "import facets.rcf", ErrorCode.P_IMPORT_ENDS_WITH_SEMI_COLON ); invalidLineToErrorCode.put( "import facets.rcf ; facet toto {", ErrorCode.P_ONE_BLOCK_PER_LINE ); invalidLineToErrorCode.put( "import facets.rcf ; invalid comment", ErrorCode.P_ONE_BLOCK_PER_LINE ); invalidLineToErrorCode.put( "import facets.rcf ;;", ErrorCode.P_ONE_BLOCK_PER_LINE ); for( Map.Entry<String,ErrorCode> entry : invalidLineToErrorCode.entrySet()) { String line = entry.getKey(); FileDefinitionParser parser = new FileDefinitionParser( null, false ); int code = parser.recognizeImport( entry.getKey()); FileDefinition def = parser.getFileRelations(); Assert.assertEquals( "Expected a YES code for '" + line + "'", FileDefinitionParser.P_CODE_YES, code ); Assert.assertEquals( "A parsing error was expected for '" + line + "'", 1, def.getParsingErrors().size()); Assert.assertEquals( "Expected ONE block to be registered for '" + line + "'", 1, def.getBlocks().size()); Assert.assertEquals( "Expected ONE import block for '" + line + "'", AbstractBlock.IMPORT, def.getBlocks().iterator().next().getInstructionType()); ParsingError error = def.getParsingErrors().iterator().next(); Assert.assertEquals( "Expecting parsing error for '" + line + "'", entry.getValue().getCategory(), ErrorCategory.PARSING ); Assert.assertEquals( "Invalid error code for '" + line + "'", entry.getValue(), error.getErrorCode()); } }
int recognizeImport( String line ) { int result = P_CODE_NO; String[] parts = splitFromInlineComment( line ); String realLine = parts[ 0 ].trim(); String regex = ParsingConstants.KEYWORD_IMPORT + "\\s+([^;]*)"; Matcher m = Pattern.compile( regex, Pattern.CASE_INSENSITIVE ).matcher( realLine ); if( m.find()) { result = P_CODE_YES; BlockImport block = new BlockImport( this.definitionFile ); block.setLine( this.currentLineNumber ); block.setUri( m.group( 1 ).trim()); block.setInlineComment( parts[ 1 ]); this.definitionFile.getBlocks().add( block ); realLine = realLine.substring( m.end()); if( ! realLine.startsWith( String.valueOf( SEMI_COLON ))) addModelError( ErrorCode.P_IMPORT_ENDS_WITH_SEMI_COLON ); else if( realLine.indexOf( SEMI_COLON ) < realLine.length() - 1 ) addModelError( ErrorCode.P_ONE_BLOCK_PER_LINE ); } return result; }
FileDefinitionParser { int recognizeImport( String line ) { int result = P_CODE_NO; String[] parts = splitFromInlineComment( line ); String realLine = parts[ 0 ].trim(); String regex = ParsingConstants.KEYWORD_IMPORT + "\\s+([^;]*)"; Matcher m = Pattern.compile( regex, Pattern.CASE_INSENSITIVE ).matcher( realLine ); if( m.find()) { result = P_CODE_YES; BlockImport block = new BlockImport( this.definitionFile ); block.setLine( this.currentLineNumber ); block.setUri( m.group( 1 ).trim()); block.setInlineComment( parts[ 1 ]); this.definitionFile.getBlocks().add( block ); realLine = realLine.substring( m.end()); if( ! realLine.startsWith( String.valueOf( SEMI_COLON ))) addModelError( ErrorCode.P_IMPORT_ENDS_WITH_SEMI_COLON ); else if( realLine.indexOf( SEMI_COLON ) < realLine.length() - 1 ) addModelError( ErrorCode.P_ONE_BLOCK_PER_LINE ); } return result; } }
FileDefinitionParser { int recognizeImport( String line ) { int result = P_CODE_NO; String[] parts = splitFromInlineComment( line ); String realLine = parts[ 0 ].trim(); String regex = ParsingConstants.KEYWORD_IMPORT + "\\s+([^;]*)"; Matcher m = Pattern.compile( regex, Pattern.CASE_INSENSITIVE ).matcher( realLine ); if( m.find()) { result = P_CODE_YES; BlockImport block = new BlockImport( this.definitionFile ); block.setLine( this.currentLineNumber ); block.setUri( m.group( 1 ).trim()); block.setInlineComment( parts[ 1 ]); this.definitionFile.getBlocks().add( block ); realLine = realLine.substring( m.end()); if( ! realLine.startsWith( String.valueOf( SEMI_COLON ))) addModelError( ErrorCode.P_IMPORT_ENDS_WITH_SEMI_COLON ); else if( realLine.indexOf( SEMI_COLON ) < realLine.length() - 1 ) addModelError( ErrorCode.P_ONE_BLOCK_PER_LINE ); } return result; } FileDefinitionParser( File relationsFile, boolean ignoreComments ); }
FileDefinitionParser { int recognizeImport( String line ) { int result = P_CODE_NO; String[] parts = splitFromInlineComment( line ); String realLine = parts[ 0 ].trim(); String regex = ParsingConstants.KEYWORD_IMPORT + "\\s+([^;]*)"; Matcher m = Pattern.compile( regex, Pattern.CASE_INSENSITIVE ).matcher( realLine ); if( m.find()) { result = P_CODE_YES; BlockImport block = new BlockImport( this.definitionFile ); block.setLine( this.currentLineNumber ); block.setUri( m.group( 1 ).trim()); block.setInlineComment( parts[ 1 ]); this.definitionFile.getBlocks().add( block ); realLine = realLine.substring( m.end()); if( ! realLine.startsWith( String.valueOf( SEMI_COLON ))) addModelError( ErrorCode.P_IMPORT_ENDS_WITH_SEMI_COLON ); else if( realLine.indexOf( SEMI_COLON ) < realLine.length() - 1 ) addModelError( ErrorCode.P_ONE_BLOCK_PER_LINE ); } return result; } FileDefinitionParser( File relationsFile, boolean ignoreComments ); FileDefinition read(); }
FileDefinitionParser { int recognizeImport( String line ) { int result = P_CODE_NO; String[] parts = splitFromInlineComment( line ); String realLine = parts[ 0 ].trim(); String regex = ParsingConstants.KEYWORD_IMPORT + "\\s+([^;]*)"; Matcher m = Pattern.compile( regex, Pattern.CASE_INSENSITIVE ).matcher( realLine ); if( m.find()) { result = P_CODE_YES; BlockImport block = new BlockImport( this.definitionFile ); block.setLine( this.currentLineNumber ); block.setUri( m.group( 1 ).trim()); block.setInlineComment( parts[ 1 ]); this.definitionFile.getBlocks().add( block ); realLine = realLine.substring( m.end()); if( ! realLine.startsWith( String.valueOf( SEMI_COLON ))) addModelError( ErrorCode.P_IMPORT_ENDS_WITH_SEMI_COLON ); else if( realLine.indexOf( SEMI_COLON ) < realLine.length() - 1 ) addModelError( ErrorCode.P_ONE_BLOCK_PER_LINE ); } return result; } FileDefinitionParser( File relationsFile, boolean ignoreComments ); FileDefinition read();  }