use of org.revapi.AnalysisContext in project revapi by revapi.
the class IgnoreDifferenceTransformTest method testAttachmentRegexMatch.
@Test
public void testAttachmentRegexMatch() throws Exception {
DummyElement oldE = new DummyElement("a");
DummyElement newE = new DummyElement("b");
Difference difference = Difference.builder().withCode("c").addAttachment("kachna", "nedobra").build();
Difference anotherDiff = Difference.builder().withCode("d").build();
try (IgnoreDifferenceTransform t = new IgnoreDifferenceTransform()) {
AnalysisContext config = getAnalysisContextFromFullConfig(IgnoreDifferenceTransform.class, "[{\"extension\": \"revapi.ignore\", \"configuration\": [{\"regex\": true, \"code\":\".*\", \"kachna\": \".*dobra$\", \"justification\" : \"because\"}]}]");
t.initialize(config);
difference = t.transform(oldE, newE, difference);
Assert.assertNull(difference);
anotherDiff = t.transform(oldE, newE, anotherDiff);
Assert.assertNotNull(anotherDiff);
}
}
use of org.revapi.AnalysisContext in project revapi by revapi.
the class AnnotatedElementFilterTest method testWith.
private void testWith(String configJSON, Consumer<List<Element>> test) throws Exception {
ArchiveAndCompilationPath archive = createCompiledJar("test.jar", "annotationfilter/NonPublic.java", "annotationfilter/NonPublicClass.java", "annotationfilter/Public.java", "annotationfilter/PublicClass.java", "annotationfilter/UndecisiveClass.java");
try {
JavaArchiveAnalyzer analyzer = new JavaArchiveAnalyzer(new API(Arrays.asList(new ShrinkwrapArchive(archive.archive)), null), Executors.newSingleThreadExecutor(), null, false, InclusionFilter.acceptAll());
JavaElementForest forest = analyzer.analyze();
AnnotatedElementFilter filter = new AnnotatedElementFilter();
Revapi r = new Revapi(emptySet(), emptySet(), emptySet(), singleton(AnnotatedElementFilter.class));
AnalysisContext ctx = AnalysisContext.builder(r).withConfigurationFromJSON(configJSON).build();
AnalysisContext filterCtx = r.prepareAnalysis(ctx).getFirstConfigurationOrNull(AnnotatedElementFilter.class);
filter.initialize(filterCtx);
List<Element> results = forest.search(Element.class, true, filter, null);
analyzer.getCompilationValve().removeCompiledResults();
test.accept(results);
} finally {
deleteDir(archive.compilationPath);
}
}
use of org.revapi.AnalysisContext in project revapi by revapi.
the class ClassFilterTest method testWith.
static void testWith(ArchiveAndCompilationPath archive, String configJSON, Set<String> expectedResults) throws Exception {
try {
JavaApiAnalyzer apiAnalyzer = new JavaApiAnalyzer(Collections.emptyList());
Revapi r = new Revapi(singleton(JavaApiAnalyzer.class), emptySet(), emptySet(), emptySet());
AnalysisContext ctx = AnalysisContext.builder(r).withConfigurationFromJSON(configJSON).build();
AnalysisContext analyzerCtx = r.prepareAnalysis(ctx).getFirstConfigurationOrNull(JavaApiAnalyzer.class);
apiAnalyzer.initialize(analyzerCtx);
ArchiveAnalyzer archiveAnalyzer = apiAnalyzer.getArchiveAnalyzer(new API(Collections.singletonList(new ShrinkwrapArchive(archive.archive)), null));
ElementForest forest = archiveAnalyzer.analyze();
List<Element> results = forest.search(Element.class, true, new AcceptingFilter(), null);
((JavaArchiveAnalyzer) archiveAnalyzer).getCompilationValve().removeCompiledResults();
List<String> expected = new ArrayList<>(expectedResults);
List<String> actual = results.stream().filter(e -> {
if (e.getArchive() == null) {
return false;
}
if (!(e instanceof JavaModelElement)) {
// exclude annotations
return false;
}
JavaModelElement el = (JavaModelElement) e;
return !el.isInherited();
}).map(Element::getFullHumanReadableString).collect(toList());
Collections.sort(expected);
Collections.sort(actual);
Assert.assertEquals(expected, actual);
} finally {
deleteDir(archive.compilationPath);
}
}
use of org.revapi.AnalysisContext in project revapi by revapi.
the class MissingClassReportingTest method testReportsMissingClasses.
@Test
public void testReportsMissingClasses() throws Exception {
AnalysisContext ctx = AnalysisContext.builder(revapi).withOldAPI(API.of(new ShrinkwrapArchive(apiV1)).build()).withNewAPI(API.of(new ShrinkwrapArchive(apiV2)).build()).withConfigurationFromJSON("{\"revapi\" : { \"java\" : { \"missing-classes\" : {\"behavior\" : \"report\" }}}}").build();
revapi.validateConfiguration(ctx);
List<Report> allReports = revapi.analyze(ctx).getExtensions().getFirstExtension(CollectingReporter.class, null).getReports();
Assert.assertEquals(3, allReports.size());
Assert.assertTrue(containsDifference(allReports, "missing-class B.T$2", "missing-class B.T$2", Code.MISSING_IN_NEW_API.code()));
Assert.assertTrue(containsDifference(allReports, "missing-class B.T$2", "missing-class B.T$2", Code.MISSING_IN_OLD_API.code()));
Assert.assertTrue(containsDifference(allReports, null, "missing-class B.T$3", Code.MISSING_IN_NEW_API.code()));
Assert.assertTrue(containsDifference(allReports, null, "field A.f3", Code.FIELD_ADDED.code()));
boolean containsMissingOld = false;
boolean containsMissingNew = false;
for (Difference d : allReports.get(0).getDifferences()) {
if (d.code.equals(Code.MISSING_IN_NEW_API.code())) {
containsMissingNew = true;
}
if (d.code.equals(Code.MISSING_IN_OLD_API.code())) {
containsMissingOld = true;
}
}
Assert.assertTrue(containsMissingOld);
Assert.assertTrue(containsMissingNew);
}
use of org.revapi.AnalysisContext in project revapi by revapi.
the class SupplementaryJarsTest method testSupplementaryJarsAreTakenIntoAccountWhenComputingAPI.
@Test
public void testSupplementaryJarsAreTakenIntoAccountWhenComputingAPI() throws Exception {
List<Report> allReports;
Revapi revapi = createRevapi(CollectingReporter.class);
AnalysisContext ctx = AnalysisContext.builder(revapi).withOldAPI(API.of(new ShrinkwrapArchive(apiV1)).supportedBy(new ShrinkwrapArchive(supV1)).build()).withNewAPI(API.of(new ShrinkwrapArchive(apiV2)).supportedBy(new ShrinkwrapArchive(supV2)).build()).build();
try (AnalysisResult res = revapi.analyze(ctx)) {
Assert.assertTrue(res.isSuccess());
allReports = res.getExtensions().getFirstExtension(CollectingReporter.class, null).getReports();
}
// 11 removed methods when kind of class changes to interface
Assert.assertEquals(8 + 11, allReports.size());
Assert.assertTrue(containsDifference(allReports, null, "class B.T$1.Private", Code.CLASS_NON_PUBLIC_PART_OF_API.code()));
Assert.assertTrue(containsDifference(allReports, null, "field B.T$2.f2", Code.FIELD_ADDED.code()));
Assert.assertTrue(containsDifference(allReports, null, "field A.f3", Code.FIELD_ADDED.code()));
Assert.assertTrue(containsDifference(allReports, "class B.T$2", "class B.T$2", Code.CLASS_NOW_FINAL.code()));
Assert.assertTrue(containsDifference(allReports, null, "class B.T$3", Code.CLASS_ADDED.code()));
Assert.assertTrue(containsDifference(allReports, null, "class B.PrivateUsedClass", Code.CLASS_NON_PUBLIC_PART_OF_API.code()));
Assert.assertTrue(containsDifference(allReports, "class B.UsedByIgnoredClass", "interface B.UsedByIgnoredClass", Code.CLASS_KIND_CHANGED.code()));
Assert.assertTrue(containsDifference(allReports, "method void B.UsedByIgnoredClass::<init>()", null, Code.METHOD_REMOVED.code()));
// eleven methods removed when kind changed, because interface doesn't have the methods of Object
Assert.assertEquals(11, allReports.stream().filter(r -> {
javax.lang.model.element.TypeElement oldType = null;
if (r.getOldElement() == null || !(r.getOldElement() instanceof JavaModelElement)) {
return false;
}
javax.lang.model.element.Element old = ((JavaModelElement) r.getOldElement()).getDeclaringElement();
do {
if (old instanceof javax.lang.model.element.TypeElement) {
oldType = (javax.lang.model.element.TypeElement) old;
break;
}
old = old.getEnclosingElement();
} while (old != null);
if (oldType == null) {
return false;
}
return oldType.getQualifiedName().contentEquals("java.lang.Object");
}).flatMap(r -> r.getDifferences().stream()).count());
}
Aggregations