Search in sources :

Example 1 with AnalysisResult

use of org.revapi.AnalysisResult 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 2 with AnalysisResult

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

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

Example 4 with AnalysisResult

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

the class CheckMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (skip) {
        return;
    }
    try (AnalysisResult res = analyze(BuildTimeReporter.class, BuildTimeReporter.BREAKING_SEVERITY_KEY, failSeverity.asDifferenceSeverity())) {
        res.throwIfFailed();
        BuildTimeReporter reporter = res.getExtensions().getFirstExtension(BuildTimeReporter.class, null);
        if (reporter != null && reporter.hasBreakingProblems()) {
            if (failBuildOnProblemsFound) {
                throw new MojoFailureException(reporter.getAllProblemsMessage());
            } else {
                getLog().info("API problems found but letting the build pass as configured.");
            }
        } else {
            getLog().info("API checks completed without failures.");
        }
    } catch (MojoFailureException e) {
        throw e;
    } catch (Exception e) {
        throw new MojoExecutionException("Failed to execute the API analysis.", e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) AnalysisResult(org.revapi.AnalysisResult) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 5 with AnalysisResult

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

the class ReportAggregateMojo method executeReport.

@Override
protected void executeReport(Locale locale) throws MavenReportException {
    if (skip) {
        return;
    }
    if (!canGenerateReport()) {
        return;
    }
    List<MavenProject> dependents = mavenSession.getProjectDependencyGraph().getDownstreamProjects(project, true);
    Collections.sort(dependents, (a, b) -> {
        String as = a.getArtifact().toString();
        String bs = b.getArtifact().toString();
        return as.compareTo(bs);
    });
    Map<MavenProject, ProjectVersions> projectVersions = dependents.stream().collect(Collectors.toMap(Function.identity(), this::getRunConfig));
    projectVersions.put(project, getRunConfig(project));
    ResourceBundle messages = getBundle(locale);
    Sink sink = getSink();
    if (generateSiteReport) {
        startReport(sink, messages);
    }
    try {
        Analyzer topAnalyzer = prepareAnalyzer(null, project, locale, projectVersions.get(project));
        Revapi sharedRevapi = topAnalyzer == null ? null : topAnalyzer.getRevapi();
        for (MavenProject p : dependents) {
            Analyzer projectAnalyzer = prepareAnalyzer(sharedRevapi, p, locale, projectVersions.get(p));
            if (projectAnalyzer != null) {
                try (AnalysisResult res = projectAnalyzer.analyze()) {
                    res.throwIfFailed();
                    ReportTimeReporter reporter = res.getExtensions().getFirstExtension(ReportTimeReporter.class, null);
                    if (generateSiteReport && reporter != null) {
                        reportBody(reporter, projectAnalyzer.getResolvedOldApi(), projectAnalyzer.getResolvedNewApi(), sink, messages);
                    }
                }
            }
        }
        if (generateSiteReport) {
            endReport(sink);
        }
    } catch (Exception e) {
        throw new MavenReportException("Failed to generate the report.", e);
    }
}
Also used : Revapi(org.revapi.Revapi) AnalysisResult(org.revapi.AnalysisResult) VersionRangeResolutionException(org.eclipse.aether.resolution.VersionRangeResolutionException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) MavenReportException(org.apache.maven.reporting.MavenReportException) MavenProject(org.apache.maven.project.MavenProject) Sink(org.apache.maven.doxia.sink.Sink) ResourceBundle(java.util.ResourceBundle) MavenReportException(org.apache.maven.reporting.MavenReportException)

Aggregations

AnalysisResult (org.revapi.AnalysisResult)11 Revapi (org.revapi.Revapi)7 AnalysisContext (org.revapi.AnalysisContext)6 Test (org.junit.Test)4 Report (org.revapi.Report)4 List (java.util.List)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)2 MavenReportException (org.apache.maven.reporting.MavenReportException)2 API (org.revapi.API)2 ModifyException (com.ximpleware.ModifyException)1 NavException (com.ximpleware.NavException)1 TranscodeException (com.ximpleware.TranscodeException)1 XPathEvalException (com.ximpleware.XPathEvalException)1 XPathParseException (com.ximpleware.XPathParseException)1 Getopt (gnu.getopt.Getopt)1 LongOpt (gnu.getopt.LongOpt)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1