Search in sources :

Example 6 with AnalysisContext

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);
    }
}
Also used : Difference(org.revapi.Difference) AnalysisContext(org.revapi.AnalysisContext) Test(org.junit.Test)

Example 7 with AnalysisContext

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);
    }
}
Also used : JavaElementForest(org.revapi.java.model.JavaElementForest) Revapi(org.revapi.Revapi) Element(org.revapi.Element) MethodParameterElement(org.revapi.java.model.MethodParameterElement) MethodElement(org.revapi.java.model.MethodElement) AnnotatedElement(java.lang.reflect.AnnotatedElement) API(org.revapi.API) AnalysisContext(org.revapi.AnalysisContext) AnnotatedElementFilter(org.revapi.java.filters.AnnotatedElementFilter)

Example 8 with AnalysisContext

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);
    }
}
Also used : ElementForest(org.revapi.ElementForest) Revapi(org.revapi.Revapi) JavaModelElement(org.revapi.java.spi.JavaModelElement) JavaModelElement(org.revapi.java.spi.JavaModelElement) Element(org.revapi.Element) ArrayList(java.util.ArrayList) ArchiveAnalyzer(org.revapi.ArchiveAnalyzer) AnalysisContext(org.revapi.AnalysisContext) API(org.revapi.API)

Example 9 with AnalysisContext

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);
}
Also used : Report(org.revapi.Report) AnalysisContext(org.revapi.AnalysisContext) Difference(org.revapi.Difference) Test(org.junit.Test)

Example 10 with AnalysisContext

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());
}
Also used : API(org.revapi.API) ShrinkWrap(org.jboss.shrinkwrap.api.ShrinkWrap) Code(org.revapi.java.spi.Code) AnalysisContext(org.revapi.AnalysisContext) Report(org.revapi.Report) JavaModelElement(org.revapi.java.spi.JavaModelElement) AnalysisResult(org.revapi.AnalysisResult) Test(org.junit.Test) Revapi(org.revapi.Revapi) List(java.util.List) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive) After(org.junit.After) Assert(org.junit.Assert) Before(org.junit.Before) Revapi(org.revapi.Revapi) Report(org.revapi.Report) JavaModelElement(org.revapi.java.spi.JavaModelElement) JavaModelElement(org.revapi.java.spi.JavaModelElement) AnalysisContext(org.revapi.AnalysisContext) AnalysisResult(org.revapi.AnalysisResult) Test(org.junit.Test)

Aggregations

AnalysisContext (org.revapi.AnalysisContext)25 Test (org.junit.Test)15 Revapi (org.revapi.Revapi)13 Difference (org.revapi.Difference)7 API (org.revapi.API)6 AnalysisResult (org.revapi.AnalysisResult)6 Report (org.revapi.Report)6 ModelNode (org.jboss.dmr.ModelNode)4 File (java.io.File)3 PrintWriter (java.io.PrintWriter)3 Element (org.revapi.Element)3 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 Nonnull (javax.annotation.Nonnull)2 JavaModelElement (org.revapi.java.spi.JavaModelElement)2 FileArchive (org.revapi.simple.FileArchive)2 ClassTemplateLoader (freemarker.cache.ClassTemplateLoader)1 MultiTemplateLoader (freemarker.cache.MultiTemplateLoader)1 TemplateLoader (freemarker.cache.TemplateLoader)1 Configuration (freemarker.template.Configuration)1