@Override protected void processFiltered(File file, FileText fileText) { contents = new FileContents(fileText); }
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); }
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 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 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 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 setUp() throws Exception { file = new File(getPath("InputXpathFilterSuppressByXpath.java")); fileContents = new FileContents(new FileText(file, StandardCharsets.UTF_8.name())); }
@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 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 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 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 testGetJavadocBefore() { final FileContents fileContents = new FileContents( new FileText(new File("filename"), Collections.singletonList(" "))); final Map<Integer, TextBlock> javadoc = new HashMap<>(); javadoc.put(0, new Comment(new String[] {"// "}, 2, 1, 2)); Whitebox.setInternalState(fileContents, "javadocComments", javadoc); final TextBlock javadocBefore = fileContents.getJavadocBefore(2); assertEquals("Invalid before javadoc", new Comment(new String[] {"// "}, 2, 1, 2).toString(), javadocBefore.toString()); }
@Test public void testHasIntersectionWithBlockComment() { final FileContents fileContents = new FileContents(new FileText(new File("filename"), Arrays.asList(" /* */ ", " ", " /* test ", " */ ", " "))); fileContents.reportCComment(1, 2, 1, 5); fileContents.reportCComment(3, 2, 4, 2); assertTrue("Should return true when comments intersect", fileContents.hasIntersectionWithComment(2, 2, 3, 6)); }
@Test public void testReportJavadocComment() { final FileContents fileContents = new FileContents( new FileText(new File("filename"), Collections.singletonList(" /** */ "))); fileContents.reportCComment(1, 2, 1, 6); final TextBlock comment = fileContents.getJavadocBefore(2); assertEquals("Invalid comment", new Comment(new String[] {"/** *"}, 2, 1, 6).toString(), comment.toString()); }
@Test public void testTextFileName() { final FileContents fileContents = new FileContents("filename", "123", "456"); assertEquals("Invalid file name", "filename", fileContents.getText().getFile().getName()); }
@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 testReportJavadocComment2() { final FileContents fileContents = new FileContents("filename", " /** */ "); fileContents.reportCComment(1, 2, 1, 6); final TextBlock comment = fileContents.getJavadocBefore(2); assertEquals("Invalid comment", new Comment(new String[] {"/** *"}, 2, 1, 6).toString(), comment.toString()); }
@Test public void testFindNearestMatchDontAllowSameColumn() { final SuppressionCommentFilter suppressionCommentFilter = new SuppressionCommentFilter(); final FileContents contents = new FileContents("filename", "//CHECKSTYLE:OFF: ConstantNameCheck", "line2"); contents.reportSingleLineComment(1, 0); final TreeWalkerAuditEvent dummyEvent = new TreeWalkerAuditEvent(contents, "filename", new LocalizedMessage(1, null, null, null, null, Object.class, null), null); final boolean result = suppressionCommentFilter.accept(dummyEvent); assertFalse("Filter should not accept event", result); }