private static DetailAST create(int tokenType, DetailAST child) { final DetailAST ast = create(tokenType); ast.addChild(child); return ast; } }
private static DetailAST createToken(DetailAST root, int type) { final DetailAST result = new DetailAST(); result.setType(type); if (root != null) { root.addChild(result); } return result; }
private static DetailAST getNode(int... nodeTypes) { DetailAST ast = new DetailAST(); ast.setType(nodeTypes[0]); for (int i = 1; i < nodeTypes.length; i++) { final DetailAST astChild = new DetailAST(); astChild.setType(nodeTypes[i]); ast.addChild(astChild); ast = astChild; } return ast; }
@Test public void testVisitTokenSwitchReflection() { //unexpected parent for ARRAY_DECLARATOR token final DetailAST astImport = mockAST(TokenTypes.IMPORT, "import", "mockfile"); final DetailAST astArrayDeclarator = mockAST(TokenTypes.ARRAY_DECLARATOR, "[", "mockfile"); final DetailAST astRightBracket = mockAST(TokenTypes.RBRACK, "[", "mockfile"); astImport.addChild(astArrayDeclarator); astArrayDeclarator.addChild(astRightBracket); final NoWhitespaceAfterCheck check = new NoWhitespaceAfterCheck(); try { check.visitToken(astArrayDeclarator); fail("no intended exception thrown"); } catch (IllegalStateException ex) { assertEquals("Invalid exception message", "unexpected ast syntax import[0x-1]", ex.getMessage()); } }
@Test public void testVisitTokenBeforeExpressionRange() { // Create first ast final DetailAST astIf = mockAST(TokenTypes.LITERAL_IF, "if", "mockfile", 2, 2); final DetailAST astIfLeftParen = mockAST(TokenTypes.LPAREN, "(", "mockfile", 3, 3); astIf.addChild(astIfLeftParen); final DetailAST astIfTrue = mockAST(TokenTypes.LITERAL_TRUE, "true", "mockfile", 3, 3); astIf.addChild(astIfTrue); final DetailAST astIfRightParen = mockAST(TokenTypes.RPAREN, ")", "mockfile", 4, 4); astIf.addChild(astIfRightParen); // Create ternary ast final DetailAST astTernary = mockAST(TokenTypes.QUESTION, "?", "mockfile", 1, 1); final DetailAST astTernaryTrue = mockAST(TokenTypes.LITERAL_TRUE, "true", "mockfile", 1, 2); astTernary.addChild(astTernaryTrue); final NPathComplexityCheck npathComplexityCheckObj = new NPathComplexityCheck(); // visiting first ast, set expressionSpatialRange to [2,2 - 4,4] npathComplexityCheckObj.visitToken(astIf); final SortedSet<LocalizedMessage> messages1 = npathComplexityCheckObj.getMessages(); Assert.assertEquals("No exception messages expected", 0, messages1.size()); //visiting ternary, it lies before expressionSpatialRange npathComplexityCheckObj.visitToken(astTernary); final SortedSet<LocalizedMessage> messages2 = npathComplexityCheckObj.getMessages(); Assert.assertEquals("No exception messages expected", 0, messages2.size()); }
@Test public void testContainsAnnotationTrue() { final DetailAST ast = new DetailAST(); ast.setType(1); final DetailAST ast2 = new DetailAST(); ast2.setType(TokenTypes.MODIFIERS); ast.addChild(ast2); final DetailAST ast3 = new DetailAST(); ast3.setType(TokenTypes.ANNOTATION); ast2.addChild(ast3); assertTrue("AnnotationUtil should contain " + ast, AnnotationUtil.containsAnnotation(ast)); }
@Test public void testContainsAnnotationListWithNoAnnotationNode() { final DetailAST ast = new DetailAST(); final DetailAST modifiersAst = new DetailAST(); modifiersAst.setType(TokenTypes.MODIFIERS); ast.addChild(modifiersAst); final List<String> annotations = Collections.singletonList("Override"); final boolean result = AnnotationUtil.containsAnnotation(ast, annotations); assertFalse("An empty ast should lead to a false result", result); }
private static DetailAST getNodeWithParentScope(int literal, String scope, int parentTokenType) { final DetailAST ast = getNode(parentTokenType, TokenTypes.MODIFIERS, literal); ast.setText(scope); final DetailAST ast2 = getNode(TokenTypes.OBJBLOCK); ast.getParent().getParent().addChild(ast2); return ast; }
@Test public void testIllegalState() { final DetailAST init = new DetailAST(); init.setType(TokenTypes.STATIC_INIT); final DetailAST objBlock = new DetailAST(); objBlock.setType(TokenTypes.OBJBLOCK); objBlock.addChild(init); final DetailAST interfaceAst = new DetailAST(); interfaceAst.setType(TokenTypes.INTERFACE_DEF); interfaceAst.addChild(objBlock); final InterfaceMemberImpliedModifierCheck check = new InterfaceMemberImpliedModifierCheck(); try { check.visitToken(init); Assert.fail("IllegalStateException is expected"); } catch (IllegalStateException ex) { assertEquals("Error message is unexpected", init.toString(), ex.getMessage()); } }
@Test public void testIllegalState() { final DetailAST init = new DetailAST(); init.setType(TokenTypes.STATIC_INIT); final DetailAST objBlock = new DetailAST(); objBlock.setType(TokenTypes.OBJBLOCK); objBlock.addChild(init); final DetailAST interfaceAst = new DetailAST(); interfaceAst.setType(TokenTypes.CLASS_DEF); interfaceAst.addChild(objBlock); final ClassMemberImpliedModifierCheck check = new ClassMemberImpliedModifierCheck(); try { check.visitToken(init); Assert.fail("IllegalStateException is expected"); } catch (IllegalStateException ex) { assertEquals("Error message is unexpected", init.toString(), ex.getMessage()); } }
@Test @SuppressWarnings("unchecked") public void testStatefulFieldsClearedOnBeginTree2() throws Exception { final DetailAST ast = new DetailAST(); ast.setType(TokenTypes.LITERAL_RETURN); ast.setLineNo(5); final DetailAST child = new DetailAST(); child.setType(TokenTypes.SEMI); ast.addChild(child); final NPathComplexityCheck check = new NPathComplexityCheck(); Assert.assertTrue("Stateful field is not cleared after beginTree", TestUtil.isStatefulFieldClearedDuringBeginTree(check, ast, "isAfterValues", isAfterValues -> ((Collection<Context>) isAfterValues).isEmpty())); }
@Test public void testContainsAnnotationFalse2() { final DetailAST ast = new DetailAST(); ast.setType(1); final DetailAST ast2 = new DetailAST(); ast2.setType(TokenTypes.MODIFIERS); ast.addChild(ast2); assertFalse("AnnotationUtil should not contain " + ast, AnnotationUtil.containsAnnotation(ast)); }
@Test public void testContainsAnnotationListWithNoMatchingAnnotation() { final DetailAST ast = new DetailAST(); final DetailAST modifiersAst = create( TokenTypes.MODIFIERS, create( TokenTypes.ANNOTATION, create( TokenTypes.DOT, create( TokenTypes.IDENT, "Override") ) ) ); ast.addChild(modifiersAst); final List<String> annotations = Collections.singletonList("Deprecated"); final boolean result = AnnotationUtil.containsAnnotation(ast, annotations); assertFalse("No matching annotation found", result); }
@Test public void testContainsAnnotationListWithEmptyAnnotationNode() { final DetailAST ast = new DetailAST(); final DetailAST modifiersAst = create( TokenTypes.MODIFIERS, create( TokenTypes.ANNOTATION, create( TokenTypes.DOT, create( TokenTypes.IDENT, "Override") ) ) ); ast.addChild(modifiersAst); final List<String> annotations = Collections.singletonList("Override"); final boolean result = AnnotationUtil.containsAnnotation(ast, annotations); assertTrue("The dot-ident variation should also work", result); }
@Test public void testGetFirstNode1() { final DetailAST child = new DetailAST(); child.setLineNo(5); child.setColumnNo(6); final DetailAST root = new DetailAST(); root.setLineNo(5); root.setColumnNo(6); root.addChild(child); assertEquals("Unexpected node", root, CheckUtil.getFirstNode(root)); }
@Test public void testGetFirstNode2() { final DetailAST child = new DetailAST(); child.setLineNo(6); child.setColumnNo(5); final DetailAST root = new DetailAST(); root.setLineNo(5); root.setColumnNo(6); root.addChild(child); assertEquals("Unexpected node", root, CheckUtil.getFirstNode(root)); }
/** * Create single-line comment from token. * @param token to create the AST * @return DetailAST with SINGLE_LINE_COMMENT type */ private static DetailAST createSlCommentNode(Token token) { final DetailAST slComment = new DetailAST(); slComment.setType(TokenTypes.SINGLE_LINE_COMMENT); slComment.setText("//"); // column counting begins from 0 slComment.setColumnNo(token.getColumn() - 1); slComment.setLineNo(token.getLine()); final DetailAST slCommentContent = new DetailAST(); slCommentContent.setType(TokenTypes.COMMENT_CONTENT); // column counting begins from 0 // plus length of '//' slCommentContent.setColumnNo(token.getColumn() - 1 + 2); slCommentContent.setLineNo(token.getLine()); slCommentContent.setText(token.getText()); slComment.addChild(slCommentContent); return slComment; }
private static FullIdent prepareFullIdentWithCoordinates(int columnNo, int lineNo) { final DetailAST ast = new DetailAST(); ast.setType(TokenTypes.DOT); ast.setColumnNo(1); ast.setLineNo(2); ast.setText("Root"); final DetailAST ast2 = new DetailAST(); ast2.setType(TokenTypes.LE); ast2.setColumnNo(columnNo); ast2.setLineNo(lineNo); ast2.setText("MyTestik"); final DetailAST ast1 = new DetailAST(); ast1.setType(TokenTypes.LITERAL_NEW); ast1.setColumnNo(14); ast1.setLineNo(15); ast1.setText("MyTest"); ast.addChild(ast1); ast.addChild(ast2); return FullIdent.createFullIdent(ast); }
@Test public void testClearChildCountCache() { final DetailAST parent = new DetailAST(); final DetailAST child = new DetailAST(); parent.setFirstChild(child); final List<Consumer<DetailAST>> clearChildCountCacheMethods = Arrays.asList( child::setNextSibling, child::addPreviousSibling, child::addNextSibling ); for (Consumer<DetailAST> method : clearChildCountCacheMethods) { final int startCount = parent.getChildCount(); method.accept(null); final int intermediateCount = Whitebox.getInternalState(parent, "childCount"); final int finishCount = parent.getChildCount(); assertEquals("Child count has changed", startCount, finishCount); assertEquals("Invalid child count", Integer.MIN_VALUE, intermediateCount); } final int startCount = child.getChildCount(); child.addChild(null); final int intermediateCount = Whitebox.getInternalState(child, "childCount"); final int finishCount = child.getChildCount(); assertEquals("Child count has changed", startCount, finishCount); assertEquals("Invalid child count", Integer.MIN_VALUE, intermediateCount); }
@Test(expected = IllegalStateException.class) public void testVisitTokenSwitchReflection() { // Create mock ast final DetailAST astImport = mockAST(TokenTypes.IMPORT, "import", "mockfile", 0, 0); final DetailAST astIdent = mockAST(TokenTypes.IDENT, "myTestImport", "mockfile", 0, 0); astImport.addChild(astIdent); final DetailAST astSemi = mockAST(TokenTypes.SEMI, ";", "mockfile", 0, 0); astIdent.addNextSibling(astSemi); // Set unsupported option final ImportOrderCheck mock = new ImportOrderCheck(); final ImportOrderOption importOrderOptionMock = PowerMockito.mock(ImportOrderOption.class); Whitebox.setInternalState(importOrderOptionMock, "name", "NEW_OPTION_FOR_UT"); Whitebox.setInternalState(importOrderOptionMock, "ordinal", 5); Whitebox.setInternalState(mock, "option", importOrderOptionMock); // expecting IllegalStateException mock.visitToken(astImport); }