SourceSnippetHelper { static public SourceSnippet readFromRelativeFilePath(File baseDirectory, String templateRelativePath, int lineFrom, int lineTo) throws IOException { if (baseDirectory != null && templateRelativePath != null) { File templateFile = new File(baseDirectory.getAbsolutePath() + File.separator + templateRelativePath); return readFromFile(templateFile, lineFrom, lineTo); } return null; }  static SourceSnippet readFromQualifiedSourceCodePath(File baseDirectory,
                                                                    String packageName,
                                                                    String fileName,
                                                                    int lineFrom,
                                                                    int lineTo); static SourceSnippet readFromRelativeFilePath(File baseDirectory,
                                                            String templateRelativePath,
                                                            int lineFrom,
                                                            int lineTo); static SourceSnippet readFromFile(File file,
                                            int lineFrom,
                                            int lineTo);  }
@Test public void readFromRelativeFilePath() throws Exception { SourceSnippet snippet; snippet = SourceSnippetHelper.readFromRelativeFilePath( baseDir.toFile(), relativeSourcePath.toString(), 1, 10); URI expectedSourceLocation = baseDir.resolve(relativeSourcePath).toUri(); Assert.assertEquals(expectedSourceLocation, snippet.getSourceLocation()); Assert.assertEquals(3, snippet.getLines().size()); Assert.assertEquals("first line", snippet.getLines().get(0)); Assert.assertEquals("second line", snippet.getLines().get(1)); Assert.assertEquals("third line", snippet.getLines().get(2)); Assert.assertEquals(1, snippet.getLineNumberFrom()); Assert.assertEquals(3, snippet.getLineNumberTo()); snippet = SourceSnippetHelper.readFromRelativeFilePath( baseDir.toFile(), relativeSourcePath.toString(), 1, 1); Assert.assertEquals(expectedSourceLocation, snippet.getSourceLocation()); Assert.assertEquals(1, snippet.getLines().size()); Assert.assertEquals("first line", snippet.getLines().get(0)); Assert.assertEquals(1, snippet.getLineNumberFrom()); Assert.assertEquals(1, snippet.getLineNumberTo()); snippet = SourceSnippetHelper.readFromRelativeFilePath( baseDir.toFile(), relativeSourcePath.toString(), -1, 1); Assert.assertEquals(expectedSourceLocation, snippet.getSourceLocation()); Assert.assertEquals(3, snippet.getLines().size()); Assert.assertEquals("first line", snippet.getLines().get(0)); Assert.assertEquals("second line", snippet.getLines().get(1)); Assert.assertEquals("third line", snippet.getLines().get(2)); Assert.assertEquals(1, snippet.getLineNumberFrom()); Assert.assertEquals(3, snippet.getLineNumberTo()); snippet = SourceSnippetHelper.readFromRelativeFilePath( baseDir.toFile(), relativeSourcePath.toString(), -10, -2); Assert.assertEquals(expectedSourceLocation, snippet.getSourceLocation()); Assert.assertEquals(3, snippet.getLines().size()); Assert.assertEquals("first line", snippet.getLines().get(0)); Assert.assertEquals("second line", snippet.getLines().get(1)); Assert.assertEquals("third line", snippet.getLines().get(2)); Assert.assertEquals(1, snippet.getLineNumberFrom()); Assert.assertEquals(3, snippet.getLineNumberTo()); }
@Test public void readFromUTF8File() throws Exception { SourceSnippet snippet = SourceSnippetHelper.readFromRelativeFilePath( baseDir.toFile(), relativeUTF8SourcePath.toString(), 1, 1); Assert.assertEquals("utf8=✓", snippet.getLines().get(0)); }