Search in sources :

Example 1 with ASTCompilationUnit

use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project controller by opendaylight.

the class ConcreteModuleGeneratedObjectFactoryTest method test.

@Test
public void test() throws Exception {
    FullyQualifiedName fqn = new FullyQualifiedName("foo.bar", "Baz");
    FullyQualifiedName abstractFQN = new FullyQualifiedName("foo.bar", "AbstractBaz");
    String nullableDescription = null;
    ModuleMXBeanEntry moduleMXBeanEntry = mockModuleMXBeanEntry(fqn, abstractFQN, nullableDescription);
    Optional<String> copyright = Optional.absent();
    Optional<String> header = Optional.absent();
    GeneratedObject go = new ConcreteModuleGeneratedObjectFactory().toGeneratedObject(moduleMXBeanEntry, copyright, header);
    Entry<FullyQualifiedName, File> entry = go.persist(generatorOutputPath).get();
    File dstFile = entry.getValue();
    Node c = parse(dstFile);
    assertEquals(fqn.getPackageName(), ((ASTCompilationUnit) c).getPackageDeclaration().getPackageNameImage());
    assertEquals(fqn.getTypeName(), c.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class).getImage());
    assertHasMethodNamed(c, "customValidation");
    assertHasMethodNamed(c, "createInstance");
}
Also used : ConcreteModuleGeneratedObjectFactory(org.opendaylight.controller.config.yangjmxgenerator.plugin.gofactory.ConcreteModuleGeneratedObjectFactory) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) ModuleMXBeanEntry(org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry) Node(net.sourceforge.pmd.lang.ast.Node) FullyQualifiedName(org.opendaylight.controller.config.yangjmxgenerator.plugin.java.FullyQualifiedName) GeneratedObject(org.opendaylight.controller.config.yangjmxgenerator.plugin.java.GeneratedObject) File(java.io.File) AbstractGeneratedObjectTest(org.opendaylight.controller.config.yangjmxgenerator.plugin.module.AbstractGeneratedObjectTest) Test(org.junit.Test)

Example 2 with ASTCompilationUnit

use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd by pmd.

the class SourceFileScopeTest method testPackageIsEmptyString.

@Test
public void testPackageIsEmptyString() {
    parseCode(TEST1);
    ASTCompilationUnit decl = acu;
    assertEquals(decl.getScope().getEnclosingScope(SourceFileScope.class).getPackageName(), "");
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Test(org.junit.Test)

Example 3 with ASTCompilationUnit

use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd by pmd.

the class ClassTypeResolverTest method testInnerClass.

@Test
public void testInnerClass() throws ClassNotFoundException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(InnerClass.class);
    Class<?> theInnerClass = Class.forName("net.sourceforge.pmd.typeresolution.testdata.InnerClass$TheInnerClass");
    // Outer class
    ASTTypeDeclaration typeDeclaration = acu.getFirstDescendantOfType(ASTTypeDeclaration.class);
    assertEquals(InnerClass.class, typeDeclaration.getType());
    ASTClassOrInterfaceDeclaration outerClassDeclaration = typeDeclaration.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class);
    assertEquals(InnerClass.class, outerClassDeclaration.getType());
    // Inner class
    assertEquals(theInnerClass, outerClassDeclaration.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class).getType());
    // Method parameter as inner class
    ASTFormalParameter formalParameter = typeDeclaration.getFirstDescendantOfType(ASTFormalParameter.class);
    assertEquals(theInnerClass, formalParameter.getType());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) ASTTypeDeclaration(net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) Test(org.junit.Test)

Example 4 with ASTCompilationUnit

use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd by pmd.

the class ClassTypeResolverTest method testMethodTypeInferenceVarargsZeroArity.

@Test
public void testMethodTypeInferenceVarargsZeroArity() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(VarargsZeroArity.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression"), AbstractJavaTypeNode.class);
    int index = 0;
    // int var = aMethod();
    assertEquals(int.class, expressions.get(index++).getType());
    // String var2 = aMethod("");
    assertEquals(String.class, expressions.get(index++).getType());
    // Make sure we got them all
    assertEquals("All expressions not tested", index, expressions.size());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) Test(org.junit.Test)

Example 5 with ASTCompilationUnit

use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd by pmd.

the class ClassTypeResolverTest method testAssignmentOperators.

@Test
public void testAssignmentOperators() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(Operators.class);
    List<ASTStatementExpression> expressions = convertList(acu.findChildNodesWithXPath("//Block[preceding-sibling::MethodDeclarator[@Image = " + "'assignmentOperators']]//StatementExpression"), ASTStatementExpression.class);
    int index = 0;
    assertEquals(Long.TYPE, expressions.get(index++).getType());
    assertEquals(Long.TYPE, expressions.get(index++).getType());
    assertEquals(Long.TYPE, expressions.get(index++).getType());
    assertEquals(Long.TYPE, expressions.get(index++).getType());
    assertEquals(Long.TYPE, expressions.get(index++).getType());
    assertEquals(Long.TYPE, expressions.get(index++).getType());
    assertEquals(Long.TYPE, expressions.get(index++).getType());
    assertEquals(Long.TYPE, expressions.get(index++).getType());
    assertEquals(Long.TYPE, expressions.get(index++).getType());
    assertEquals(Long.TYPE, expressions.get(index++).getType());
    assertEquals(Long.TYPE, expressions.get(index++).getType());
    assertEquals(Long.TYPE, expressions.get(index++).getType());
    // Make sure we got them all.
    assertEquals("All expressions not tested", index, expressions.size());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) Test(org.junit.Test)

Aggregations

ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)79 Test (org.junit.Test)66 Constraint (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)36 AbstractJavaTypeNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)27 StringReader (java.io.StringReader)8 RuleContext (net.sourceforge.pmd.RuleContext)8 LanguageVersionHandler (net.sourceforge.pmd.lang.LanguageVersionHandler)8 ASTImportDeclaration (net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration)7 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)6 Node (net.sourceforge.pmd.lang.ast.Node)5 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)5 ArrayList (java.util.ArrayList)4 ParserOptions (net.sourceforge.pmd.lang.ParserOptions)3 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 Method (java.lang.reflect.Method)2 List (java.util.List)2 LanguageVersion (net.sourceforge.pmd.lang.LanguageVersion)2 Parser (net.sourceforge.pmd.lang.Parser)2 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)2