Introduces java 8 integration test module

* Only for testing pmd-java
* Activated only if the jdk is 1.8
This commit is contained in:
Brice Dutheil
2015-03-01 22:11:51 +01:00
parent 28d1764428
commit 18f393da3c
8 changed files with 378 additions and 17 deletions

View File

@ -0,0 +1,39 @@
package net.sourceforge.pmd.lang.java.bugs;
//import java.util.stream.IntStream;
//import static java.util.stream.Collectors.toList;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.junit.Ignore;
import org.junit.Test;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersionHandler;
import net.sourceforge.pmd.lang.java.JavaLanguageModule;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
import net.sourceforge.pmd.typeresolution.testdata.UsesJavaStreams;
@Ignore
public class InterfaceMethodTest {
@Test
public void should_not_fail() {
ASTCompilationUnit acu = parseAndTypeResolveForClass(UsesJavaStreams.class);
}
// Note: If you're using Eclipse or some other IDE to run this test, you _must_ have the regress folder in
// the classpath. Normally the IDE doesn't put source directories themselves directly in the classpath, only
// the output directories are in the classpath.
private ASTCompilationUnit parseAndTypeResolveForClass(Class<?> clazz) {
String sourceFile = clazz.getName().replace('.', '/') + ".java";
InputStream is = InterfaceMethodTest.class.getClassLoader().getResourceAsStream(sourceFile);
if (is == null) {
throw new IllegalArgumentException("Unable to find source file " + sourceFile + " for " + clazz);
}
LanguageVersionHandler languageVersionHandler = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.8").getLanguageVersionHandler();
ASTCompilationUnit acu = (ASTCompilationUnit)languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new InputStreamReader(is));
languageVersionHandler.getSymbolFacade().start(acu);
languageVersionHandler.getTypeResolutionFacade(InterfaceMethodTest.class.getClassLoader()).start(acu);
return acu;
}
}

View File

@ -0,0 +1,48 @@
package net.sourceforge.pmd.lang.java.bugs;
import java.io.StringReader;
import org.junit.Ignore;
import org.junit.Test;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersionHandler;
import net.sourceforge.pmd.lang.java.JavaLanguageModule;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
import net.sourceforge.pmd.lang.java.symboltable.SymbolFacade;
@Ignore
public class Java8MultipleLambdasTest {
private static final String MULTIPLE_JAVA_8_LAMBDAS =
"public class MultipleLambdas {" + PMD.EOL +
" Observer a = (o, arg) -> System.out.println(\"a_\" + arg);" + PMD.EOL +
" Observer b = (o, arg) -> System.out.println(\"b_\" + arg);" + PMD.EOL +
"}";
@Test
public void should_not_fail() {
parseCode(MULTIPLE_JAVA_8_LAMBDAS);
}
private void parseCode(String code) {
LanguageVersionHandler languageVersionHandler = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler();
ASTCompilationUnit acu = (ASTCompilationUnit) languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new StringReader(code));
SymbolFacade stb = new SymbolFacade();
stb.initializeWith(acu);
}
// Was failing with :
// java.lang.RuntimeException: Variable: image = 'i', line = 3 is already in the symbol table
// at net.AbstractJavaScope.checkForDuplicatedNameDeclaration(AbstractJavaScope.java:27)
// at net.sourceforge.pmd.lang.java.symboltable.AbstractJavaScope.addDeclaration(AbstractJavaScope.java:21)
// at net.sourceforge.pmd.lang.java.symboltable.ScopeAndDeclarationFinder.visit(ScopeAndDeclarationFinder.java:294)
// at net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId.jjtAccept(ASTVariableDeclaratorId.java:30)
// at net.AbstractJavaNode.childrenAccept(AbstractJavaNode.java:55)
// at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:9)
// at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:455)
// at net.sourceforge.pmd.lang.java.ast.ASTLambdaExpression.jjtAccept(ASTLambdaExpression.java:21)
// at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.childrenAccept(AbstractJavaNode.java:55)
// at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:9)
// at net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter.visit(JavaParserVisitorAdapter.java:312)
}

View File

@ -0,0 +1,53 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.typeresolution;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.junit.Test;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersionHandler;
import net.sourceforge.pmd.lang.java.JavaLanguageModule;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
import net.sourceforge.pmd.typeresolution.testdata.UsesJavaStreams;
import net.sourceforge.pmd.typeresolution.testdata.UsesRepeatableAnnotations;
public class ClassTypeResolverJava8Test {
@Test
public void interface_method_should_be_parseable() {
ASTCompilationUnit acu = parseAndTypeResolveForClass18(UsesJavaStreams.class);
}
@Test
public void repeatable_annotations_method_should_be_parseable() {
ASTCompilationUnit acu = parseAndTypeResolveForClass18(UsesRepeatableAnnotations.class);
}
public static junit.framework.Test suite() {
return new junit.framework.JUnit4TestAdapter(ClassTypeResolverJava8Test.class);
}
private ASTCompilationUnit parseAndTypeResolveForClass18(Class<?> clazz) {
return parseAndTypeResolveForClass(clazz, "1.8");
}
// Note: If you're using Eclipse or some other IDE to run this test, you _must_ have the regress folder in
// the classpath. Normally the IDE doesn't put source directories themselves directly in the classpath, only
// the output directories are in the classpath.
private ASTCompilationUnit parseAndTypeResolveForClass(Class<?> clazz, String version) {
String sourceFile = clazz.getName().replace('.', '/') + ".java";
InputStream is = ClassTypeResolverJava8Test.class.getClassLoader().getResourceAsStream(sourceFile);
if (is == null) {
throw new IllegalArgumentException("Unable to find source file " + sourceFile + " for " + clazz);
}
LanguageVersionHandler languageVersionHandler = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion(version).getLanguageVersionHandler();
ASTCompilationUnit acu = (ASTCompilationUnit) languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new InputStreamReader(is));
languageVersionHandler.getSymbolFacade().start(acu);
languageVersionHandler.getTypeResolutionFacade(ClassTypeResolverJava8Test.class.getClassLoader()).start(acu);
return acu;
}
}

View File

@ -0,0 +1,15 @@
package net.sourceforge.pmd.typeresolution.testdata;
public class UsesJavaStreams {
interface WithStaticAndDefaultMethod {
static void performOn() { }
default void myToString() {}
}
class ImplWithStaticAndDefaultMethod implements WithStaticAndDefaultMethod {}
public void performStuff() {
WithStaticAndDefaultMethod.performOn();
new ImplWithStaticAndDefaultMethod().myToString();
}
}

View File

@ -0,0 +1,21 @@
package net.sourceforge.pmd.typeresolution.testdata;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import net.sourceforge.pmd.typeresolution.testdata.UsesRepeatableAnnotations.Multitude;
@Multitude("1")
@Multitude("2")
@Multitude("3")
@Multitude("4")
public class UsesRepeatableAnnotations {
@Repeatable(Multitudes.class)
@Retention(RetentionPolicy.RUNTIME)
@interface Multitude { String value(); }
@Retention(RetentionPolicy.RUNTIME)
@interface Multitudes { Multitude[] value(); }
}