Search in sources :

Example 1 with Report

use of org.revapi.Report in project revapi by revapi.

the class JavaElementDifferenceAnalyzer method endAnalysis.

@Override
public Report endAnalysis(@Nullable Element oldElement, @Nullable Element newElement) {
    if (oldElement == nonExistenceOldRoot && newElement == nonExistenceNewRoot) {
        nonExistenceMode = false;
        nonExistenceOldRoot = null;
        nonExistenceNewRoot = null;
    }
    if (conforms(oldElement, newElement, AnnotationElement.class)) {
        // the annotations are always reported at the parent element
        return new Report(Collections.emptyList(), oldElement, newElement);
    }
    List<Difference> differences = new ArrayList<>();
    CheckType lastInterest = checkTypeStack.pop();
    if (lastInterest.isConcrete()) {
        for (Check c : checksStack.pop()) {
            List<Difference> p = c.visitEnd();
            if (p != null) {
                differences.addAll(p);
            }
        }
    }
    if (lastAnnotationResults != null && !lastAnnotationResults.isEmpty()) {
        differences.addAll(lastAnnotationResults);
        lastAnnotationResults.clear();
    }
    if (!differences.isEmpty()) {
        LOG.trace("Detected following problems: {}", differences);
    }
    Timing.LOG.trace("Ended analysis of {} and {}.", oldElement, newElement);
    ListIterator<Difference> it = differences.listIterator();
    while (it.hasNext()) {
        Difference d = it.next();
        if (analysisConfiguration.reportUseForAllDifferences() || analysisConfiguration.getUseReportingCodes().contains(d.code)) {
            StringBuilder oldUseChain = null;
            StringBuilder newUseChain = null;
            if (oldElement != null) {
                oldUseChain = new StringBuilder();
                appendUses(oldEnvironment, oldElement, oldUseChain);
            }
            if (newElement != null) {
                newUseChain = new StringBuilder();
                appendUses(newEnvironment, newElement, newUseChain);
            }
            Map<String, String> atts = new HashMap<>(d.attachments);
            if (oldUseChain != null) {
                atts.put("exampleUseChainInOldApi", oldUseChain.toString());
            }
            if (newUseChain != null) {
                atts.put("exampleUseChainInNewApi", newUseChain.toString());
            }
            d = Difference.builder().addAttachments(atts).addClassifications(d.classification).withCode(d.code).withName(d.name).withDescription(d.description).build();
        }
        it.set(d);
    }
    return new Report(differences, oldElement, newElement);
}
Also used : Report(org.revapi.Report) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Check(org.revapi.java.spi.Check) Difference(org.revapi.Difference)

Example 2 with Report

use of org.revapi.Report 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 3 with Report

use of org.revapi.Report in project revapi by revapi.

the class MissingClassReportingTest method testIgnoresMissingClasses.

@Test
public void testIgnoresMissingClasses() throws Exception {
    AnalysisResult res = revapi.analyze(AnalysisContext.builder(revapi).withOldAPI(API.of(new ShrinkwrapArchive(apiV1)).build()).withNewAPI(API.of(new ShrinkwrapArchive(apiV2)).build()).withConfigurationFromJSON("{\"revapi\" : { \"java\" : { \"missing-classes\" : {\"behavior\" : \"ignore\" }}}}").build());
    List<Report> allReports = res.getExtensions().getFirstExtension(CollectingReporter.class, null).getReports();
    Assert.assertEquals(1, allReports.size());
    Assert.assertTrue(containsDifference(allReports, null, "field A.f3", Code.FIELD_ADDED.code()));
}
Also used : Report(org.revapi.Report) AnalysisResult(org.revapi.AnalysisResult) Test(org.junit.Test)

Example 4 with Report

use of org.revapi.Report 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)

Example 5 with Report

use of org.revapi.Report in project revapi by revapi.

the class SupplementaryJarsTest method testExcludedClassesDontDragUsedTypesIntoAPI.

@Test
public void testExcludedClassesDontDragUsedTypesIntoAPI() 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()).withConfigurationFromJSON("{\"revapi\": {\"java\": {" + "\"filter\": {\"classes\": {\"exclude\": [\"C\"]}}}}}").build();
    try (AnalysisResult res = revapi.analyze(ctx)) {
        allReports = res.getExtensions().getFirstExtension(CollectingReporter.class, null).getReports();
    }
    Assert.assertEquals(6, 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.assertFalse(containsDifference(allReports, "class B.UsedByIgnoredClass", "class B.UsedByIgnoredClass", Code.CLASS_KIND_CHANGED.code()));
    Assert.assertFalse(containsDifference(allReports, "method void B.UsedByIgnoredClass::<init>()", null, Code.METHOD_REMOVED.code()));
}
Also used : Revapi(org.revapi.Revapi) Report(org.revapi.Report) AnalysisContext(org.revapi.AnalysisContext) AnalysisResult(org.revapi.AnalysisResult) Test(org.junit.Test)

Aggregations

Report (org.revapi.Report)12 Test (org.junit.Test)7 AnalysisContext (org.revapi.AnalysisContext)6 Difference (org.revapi.Difference)5 AnalysisResult (org.revapi.AnalysisResult)4 Element (org.revapi.Element)3 Revapi (org.revapi.Revapi)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 API (org.revapi.API)2 ClassTemplateLoader (freemarker.cache.ClassTemplateLoader)1 MultiTemplateLoader (freemarker.cache.MultiTemplateLoader)1 TemplateLoader (freemarker.cache.TemplateLoader)1 Configuration (freemarker.template.Configuration)1 DefaultObjectWrapperBuilder (freemarker.template.DefaultObjectWrapperBuilder)1 Template (freemarker.template.Template)1 TemplateException (freemarker.template.TemplateException)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1