/** * Get the full text of the file. * @return an object containing the full text of the file */ public FileText getText() { return new FileText(text); }
/** * Get FileText from a file. * @param file the file to get the FileText from. * @return the FileText. * @throws IOException if the file could not be read. */ private static FileText getFileText(File file) throws IOException { return new FileText(file.getAbsoluteFile(), System.getProperty("file.encoding", StandardCharsets.UTF_8.name())); }
/** * Parse a file and return the parse tree. * @param file the file to parse. * @return the root node of the parse tree. * @throws IOException if the file could not be read. */ private static DetailNode parseFile(File file) throws IOException { final FileText text = new FileText(file.getAbsoluteFile(), System.getProperty("file.encoding", StandardCharsets.UTF_8.name())); return parseJavadocAsDetailNode(text.getFullText().toString()); }
@Before public void setUp() throws Exception { file = new File(getPath("InputXpathFilterSuppressByXpath.java")); fileContents = new FileContents(new FileText(file, StandardCharsets.UTF_8.name())); }
@Test public void testNotInPackageInfo() { final FileContents fileContents = new FileContents(new FileText( new File("filename.java"), Collections.singletonList(" // "))); assertFalse("Should return false when not in package info", fileContents.inPackageInfo()); }
@Test public void testInPackageInfo() { final FileContents fileContents = new FileContents(new FileText( new File("filename.package-info.java"), Collections.singletonList(" // "))); assertTrue("Should return true when in package info", fileContents.inPackageInfo()); }
@Test public void testSinglelineCommentIntersect() { // just to make UT coverage 100% final FileContents fileContents = new FileContents( new FileText(new File("filename"), Collections.singletonList(" // "))); fileContents.reportSingleLineComment(1, 2); assertTrue("Should return true when comments intersect", fileContents.hasIntersectionWithComment(1, 5, 1, 6)); }
@Test public void testSinglelineCommentNotIntersect() { // just to make UT coverage 100% final FileContents fileContents = new FileContents( new FileText(new File("filename"), Collections.singletonList(" // "))); fileContents.reportSingleLineComment(1, 2); assertFalse("Should return false when there is no intersection", fileContents.hasIntersectionWithComment(1, 0, 1, 1)); }
@Test public void testHasIntersectionWithSingleLineComment() { final FileContents fileContents = new FileContents( new FileText(new File("filename"), Arrays.asList(" ", " //test ", " //test ", " //test "))); fileContents.reportCppComment(4, 4); assertTrue("Should return true when comments intersect", fileContents.hasIntersectionWithComment(1, 3, 4, 6)); }
@Test public void testHasIntersectionWithBlockComment2() { final FileContents fileContents = new FileContents( new FileText(new File("filename"), Arrays.asList(" /* */ ", " ", " "))); fileContents.reportCComment(1, 2, 1, 5); assertFalse("Should return false when there is no intersection", fileContents.hasIntersectionWithComment(2, 2, 3, 6)); }
@Test public void testPrintAst() throws Exception { final FileText text = new FileText( new File(getPath("InputAstTreeStringPrinterComments.java")).getAbsoluteFile(), System.getProperty("file.encoding", StandardCharsets.UTF_8.name())); final String actual = AstTreeStringPrinter.printAst(text, JavaParser.Options.WITHOUT_COMMENTS); final String expected = new String(Files.readAllBytes(Paths.get( getPath("ExpectedAstTreeStringPrinter.txt"))), StandardCharsets.UTF_8); Assert.assertEquals("Print AST output is invalid", expected, actual); }
@Test public void testExtractBlockComment() { final FileContents fileContents = new FileContents( new FileText(new File("filename"), Arrays.asList(" ", " ", " /* test ", " */ ", " "))); fileContents.reportCComment(3, 2, 4, 2); final Map<Integer, List<TextBlock>> blockComments = fileContents.getBlockComments(); final String[] text = blockComments.get(3).get(0).getText(); assertArrayEquals("Invalid comment text", new String[] {"/* test ", " *"}, text); }
@Before public void init() throws Exception { final File file = new File(getPath("InputXpathQueryGenerator.java")); fileText = new FileText(file, StandardCharsets.UTF_8.name()); rootAst = JavaParser.parseFile(file, JavaParser.Options.WITH_COMMENTS); }
private static TreeWalkerAuditEvent createTreeWalkerAuditEvent(String fileName, LocalizedMessage message) throws Exception { final File file = new File(getPath(fileName)); final FileText fileText = new FileText( file.getAbsoluteFile(), System.getProperty("file.encoding", StandardCharsets.UTF_8.name())); final FileContents fileContents = new FileContents(fileText); final DetailAST rootAst = JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS); return new TreeWalkerAuditEvent(fileContents, fileName, message, rootAst); }
@Test public void testReportComment() { final FileContents fileContents = new FileContents( new FileText(new File("filename"), Collections.singletonList(" // "))); fileContents.reportCComment(1, 2, 1, 2); final Map<Integer, List<TextBlock>> comments = fileContents.getCComments(); assertEquals("Invalid comment", new Comment(new String[] {"/"}, 2, 1, 2).toString(), comments.get(1).get(0).toString()); }
@Test public void testSupportedCharset() throws IOException { final String charsetName = StandardCharsets.ISO_8859_1.name(); final FileText fileText = new FileText(new File(getPath("InputFileTextImportControl.xml")), charsetName); assertEquals("Invalid charset name", charsetName, fileText.getCharset().name()); }
@Test public void testReportCppComment() { final FileContents fileContents = new FileContents( new FileText(new File("filename"), Collections.singletonList(" // "))); fileContents.reportCppComment(1, 2); final Map<Integer, TextBlock> cppComments = fileContents.getCppComments(); assertEquals("Invalid comment", new Comment(new String[] {" // "}, 2, 1, 6).toString(), cppComments.get(1).toString()); }
@Test public void testLineColumnAtTheStartOfFile() throws IOException { final String charsetName = StandardCharsets.ISO_8859_1.name(); final FileText fileText = new FileText(new File(getPath("InputFileTextImportControl.xml")), charsetName); final FileText copy = new FileText(fileText); final LineColumn lineColumn = copy.lineColumn(0); assertEquals("Invalid line", 1, lineColumn.getLine()); assertEquals("Invalid column", 0, lineColumn.getColumn()); }
@Test public void testGetDetailAst() throws Exception { final FileText testFileText = new FileText( new File(getPath("InputFullIdentTestArrayType.java")).getAbsoluteFile(), System.getProperty("file.encoding", StandardCharsets.UTF_8.name())); final DetailAST packageDefinitionNode = JavaParser.parse(new FileContents(testFileText)); final DetailAST packageName = packageDefinitionNode.getFirstChild().getNextSibling(); final FullIdent ident = FullIdent.createFullIdent(packageName); Assert.assertEquals("Invalid full indent", "com[1x8]", ident.getDetailAst().toString()); }
@Test public void testProcessNonJavaFilesWithoutException() throws Exception { final TreeWalker treeWalker = new TreeWalker(); treeWalker.setTabWidth(1); treeWalker.configure(new DefaultConfiguration("default config")); final File file = new File(getPath("InputTreeWalkerNotJava.xml")); final FileText fileText = new FileText(file, StandardCharsets.ISO_8859_1.name()); treeWalker.processFiltered(file, fileText); final Collection<Checks> checks = Whitebox.getInternalState(treeWalker, "ordinaryChecks"); assertTrue("No checks -> No parsing", checks.isEmpty()); }