Search in sources :

Example 11 with AnalysisContext

use of org.revapi.AnalysisContext 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 12 with AnalysisContext

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

the class BuildTimeReporterTest method testJSONEscapedInIgnoreHint.

@Test
public void testJSONEscapedInIgnoreHint() {
    BuildTimeReporter reporter = new BuildTimeReporter();
    API oldApi = API.builder().build();
    API newApi = API.builder().build();
    AnalysisContext ctx = AnalysisContext.builder().withOldAPI(oldApi).withNewAPI(newApi).withData(BuildTimeReporter.BREAKING_SEVERITY_KEY, DifferenceSeverity.EQUIVALENT).build();
    reporter.initialize(ctx);
    Element oldEl = new SimpleElement() {

        @Nonnull
        @Override
        public API getApi() {
            return oldApi;
        }

        @Nullable
        @Override
        public Archive getArchive() {
            return null;
        }

        @Override
        public int compareTo(Element o) {
            return 0;
        }

        @Override
        public String toString() {
            return "old element";
        }
    };
    Element newEl = new SimpleElement() {

        @Nonnull
        @Override
        public API getApi() {
            return newApi;
        }

        @Nullable
        @Override
        public Archive getArchive() {
            return null;
        }

        @Override
        public int compareTo(Element o) {
            return 0;
        }

        @Override
        public String toString() {
            return "new element";
        }
    };
    Report report = Report.builder().withNew(newEl).withOld(oldEl).addProblem().withCode("diffs\\myDiff").withDescription("the problem").addClassification(CompatibilityType.BINARY, DifferenceSeverity.BREAKING).addAttachment("shouldBeEscaped", "{\"a\", \"b\"}").done().build();
    reporter.report(report);
    String resultMessage = reporter.getAllProblemsMessage();
    Assert.assertNotNull(resultMessage);
    int start = resultMessage.indexOf('{');
    int end = resultMessage.lastIndexOf('}');
    String json = resultMessage.substring(start, end + 1);
    json = json.replace("<<<<< ADD YOUR EXPLANATION FOR THE NECESSITY OF THIS CHANGE >>>>>", "\"\"");
    ModelNode parsed = ModelNode.fromJSONString(json);
    Assert.assertEquals("diffs\\myDiff", parsed.get("code").asString());
    Assert.assertEquals("{\"a\", \"b\"}", parsed.get("shouldBeEscaped").asString());
}
Also used : SimpleElement(org.revapi.simple.SimpleElement) Report(org.revapi.Report) Element(org.revapi.Element) SimpleElement(org.revapi.simple.SimpleElement) API(org.revapi.API) AnalysisContext(org.revapi.AnalysisContext) ModelNode(org.jboss.dmr.ModelNode) Test(org.junit.Test)

Example 13 with AnalysisContext

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

the class ConfigurationValidatorTest method test.

private ValidationResult test(String object, final String extensionId, final String schema) {
    ConfigurationValidator validator = new ConfigurationValidator();
    ModelNode fullConfig = ModelNode.fromJSONString(object);
    Configurable fakeConfigurable = new Configurable() {

        @Nullable
        @Override
        public String getExtensionId() {
            return extensionId;
        }

        @Nullable
        @Override
        public Reader getJSONSchema() {
            return new StringReader(schema);
        }

        @Override
        public void initialize(@Nonnull AnalysisContext analysisContext) {
        }
    };
    return validator.validate(fullConfig, fakeConfigurable);
}
Also used : Nonnull(javax.annotation.Nonnull) StringReader(java.io.StringReader) AnalysisContext(org.revapi.AnalysisContext) ModelNode(org.jboss.dmr.ModelNode)

Example 14 with AnalysisContext

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

the class ConfigurationValidatorTest method testRevapiValidation.

@Test
public void testRevapiValidation() throws Exception {
    String config = "[" + "{\"extension\": \"my-config\", \"configuration\": {\"id\": 3, \"kachna\": \"duck\"}}," + "{\"extension\": \"my-config\", \"configuration\": {\"id\": 4, \"kachna\": \"no duck\"}}," + "{\"extension\": \"other-config\", \"configuration\": 1}" + "]";
    Revapi revapi = Revapi.builder().withFilters(TestFilter.class).withReporters(TestReporter.class).withAnalyzers(DummyApiAnalyzer.class).build();
    AnalysisContext ctx = AnalysisContext.builder(revapi).withConfigurationFromJSON(config).build();
    ValidationResult res = revapi.validateConfiguration(ctx);
    Assert.assertFalse(res.isSuccessful());
    Assert.assertNotNull(res.getErrors());
    Assert.assertEquals(1, res.getErrors().length);
    Assert.assertEquals("/[2]/configuration", res.getErrors()[0].dataPath);
}
Also used : Revapi(org.revapi.Revapi) AnalysisContext(org.revapi.AnalysisContext) Test(org.junit.Test)

Example 15 with AnalysisContext

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

the class ConfigurationValidatorTest method testRevapiValidation_mergeWithoutIds.

@Test
public void testRevapiValidation_mergeWithoutIds() throws Exception {
    // partial config of the extension
    String config1 = "[" + "{\"extension\": \"my-config\", \"configuration\": {\"id\": 3,}}" + "]";
    // complete the config of the extension and add another config for another extension
    String config2 = "[" + "{\"extension\": \"my-config\", \"configuration\": {\"kachna\": \"no duck\"}}," + "{\"extension\": \"other-config\", \"configuration\": 1}" + "]";
    Revapi revapi = Revapi.builder().withFilters(TestFilter.class).withReporters(TestReporter.class).withAnalyzers(DummyApiAnalyzer.class).build();
    AnalysisContext ctx = AnalysisContext.builder(revapi).withConfigurationFromJSON(config1).mergeConfigurationFromJSON(config2).build();
    ValidationResult res = revapi.validateConfiguration(ctx);
    Assert.assertFalse(res.isSuccessful());
    Assert.assertNotNull(res.getErrors());
    // we merged "my-config" from the second config into the first, so that should be ok. Only other-config should error out.
    Assert.assertEquals(1, res.getErrors().length);
    Assert.assertEquals("/[1]/configuration", res.getErrors()[0].dataPath);
}
Also used : Revapi(org.revapi.Revapi) AnalysisContext(org.revapi.AnalysisContext) 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