Search in sources :

Example 1 with SimpleDiffCriteria

use of org.osjava.jardiff.SimpleDiffCriteria in project semantic-versioning by jeluard.

the class ClassInheritanceTest method shouldInheritedMethodMatchImplementedMethod.

@Test
public void shouldInheritedMethodMatchImplementedMethod() throws Exception {
    /**
     * The situation we are testing is as follows:
     * Abstract class InheritanceRoot is initially implemented directly by ClassA.
     * ClassA is later modified to extend another implementation of InheritanceRoot
     * and the methods required by InheritanceRoot are now removed from ClassA directly,
     * and instead inherited from the new parent, DirectDescendant. For the purposes of
     * this test, this new ClassA is represented by ClassB (as we can't have the same
     * class declared twice in a test -- in real life, this would both be ClassA's,
     * in different jars).
     */
    Map<String, ClassInfo> oldClassInfoMap = new HashMap<String, ClassInfo>();
    Map<String, ClassInfo> newClassInfoMap = new HashMap<String, ClassInfo>();
    JarDiff jd = new JarDiff();
    addClassInfo(oldClassInfoMap, ClassA.class, jd);
    addClassInfo(oldClassInfoMap, DirectDescendant.class, jd);
    addClassInfo(oldClassInfoMap, InheritanceRoot.class, jd);
    addClassInfo(newClassInfoMap, ClassB.class, jd);
    addClassInfo(newClassInfoMap, DirectDescendant.class, jd);
    addClassInfo(newClassInfoMap, InheritanceRoot.class, jd);
    // Make B look like A
    ClassInfo a = oldClassInfoMap.get("org/semver/jardiff/ClassInheritanceTest$ClassA");
    ClassInfo b = newClassInfoMap.get("org/semver/jardiff/ClassInheritanceTest$ClassB");
    newClassInfoMap.put(a.getName(), new ClassInfo(b.getVersion(), b.getAccess(), a.getName(), b.getSignature(), b.getSupername(), b.getInterfaces(), b.getMethodMap(), b.getFieldMap()));
    newClassInfoMap.remove(b.getName());
    DifferenceAccumulatingHandler handler = new DifferenceAccumulatingHandler();
    jd.diff(handler, new SimpleDiffCriteria(), "0.1.0", "0.2.0", oldClassInfoMap, newClassInfoMap);
    for (Delta.Difference d : handler.getDelta().getDifferences()) {
        System.err.println(d.getClassName() + " : " + d.getClass().getName() + " : " + d.getInfo().getName() + " : " + d.getInfo().getAccessType());
        if (d instanceof Change) {
            System.err.println("  : " + ((Change) d).getModifiedInfo().getName());
        }
    }
    Assert.assertEquals("differences found", 1, handler.getDelta().getDifferences().size());
}
Also used : HashMap(java.util.HashMap) Delta(org.semver.Delta) JarDiff(org.osjava.jardiff.JarDiff) SimpleDiffCriteria(org.osjava.jardiff.SimpleDiffCriteria) Change(org.semver.Delta.Change) ClassInfo(org.osjava.jardiff.ClassInfo) Test(org.junit.Test)

Example 2 with SimpleDiffCriteria

use of org.osjava.jardiff.SimpleDiffCriteria in project semantic-versioning by jeluard.

the class Main method main.

public static void main(final String[] args) throws IOException {
    Config config = new Config();
    CmdlineParser cmdlineParser = new CmdlineParser(config);
    // Load translations of command line descriptions
    cmdlineParser.setResourceBundle(Main.class.getPackage().getName() + ".Messages", Main.class.getClassLoader());
    cmdlineParser.setProgramName("semver");
    cmdlineParser.setAboutLine("Semantic Version validator.");
    try {
        cmdlineParser.parse(args);
    } catch (CmdlineParserException e) {
        System.err.println("Error: " + e.getLocalizedMessage() + "\nRun semver --help for help.");
        System.exit(1);
    }
    if (config.help) {
        cmdlineParser.usage();
        System.exit(0);
    }
    final DiffCriteria diffCriteria = config.publicOnly ? new PublicDiffCriteria() : new SimpleDiffCriteria();
    final Comparer comparer = new Comparer(diffCriteria, new File(config.baseJar), new File(config.newJar), config.includes, config.excludes);
    final Delta delta = comparer.diff();
    if (config.diff) {
        Dumper.dump(delta);
    }
    if (config.check) {
        System.out.println(delta.computeCompatibilityType());
    }
    if (config.infer) {
        System.out.println(delta.infer(Version.parse(config.baseVersion)));
    }
    if (config.validate) {
        System.out.println(delta.validate(Version.parse(config.baseVersion), Version.parse(config.newVersion)));
    }
}
Also used : PublicDiffCriteria(org.osjava.jardiff.PublicDiffCriteria) CmdlineParser(de.tototec.cmdoption.CmdlineParser) CmdlineParserException(de.tototec.cmdoption.CmdlineParserException) SimpleDiffCriteria(org.osjava.jardiff.SimpleDiffCriteria) SimpleDiffCriteria(org.osjava.jardiff.SimpleDiffCriteria) PublicDiffCriteria(org.osjava.jardiff.PublicDiffCriteria) DiffCriteria(org.osjava.jardiff.DiffCriteria) File(java.io.File)

Example 3 with SimpleDiffCriteria

use of org.osjava.jardiff.SimpleDiffCriteria in project semantic-versioning by jeluard.

the class AbstractEnforcerRule method compareJars.

private void compareJars(final EnforcerRuleHelper helper, final Version previous, final File previousJar, final Version current, final File currentJar) throws EnforcerRuleException {
    helper.getLog().info("Using <" + previousJar + "> as previous JAR");
    helper.getLog().info("Using <" + currentJar + "> as current JAR");
    try {
        final DiffCriteria diffCriteria = publicOnly ? new PublicDiffCriteria() : new SimpleDiffCriteria();
        final Comparer comparer = new Comparer(diffCriteria, previousJar, currentJar, extractFilters(this.includes), extractFilters(this.excludes));
        final Delta delta = comparer.diff();
        enforce(helper, delta, previous, current);
    } catch (IOException e) {
        throw new EnforcerRuleException("Exception while checking compatibility: " + e.toString(), e);
    }
}
Also used : PublicDiffCriteria(org.osjava.jardiff.PublicDiffCriteria) Delta(org.semver.Delta) Comparer(org.semver.Comparer) EnforcerRuleException(org.apache.maven.enforcer.rule.api.EnforcerRuleException) SimpleDiffCriteria(org.osjava.jardiff.SimpleDiffCriteria) IOException(java.io.IOException) SimpleDiffCriteria(org.osjava.jardiff.SimpleDiffCriteria) PublicDiffCriteria(org.osjava.jardiff.PublicDiffCriteria) DiffCriteria(org.osjava.jardiff.DiffCriteria)

Example 4 with SimpleDiffCriteria

use of org.osjava.jardiff.SimpleDiffCriteria in project semantic-versioning by jeluard.

the class DeprecateDetectionTest method shouldInheritedMethodMatchImplementedMethod.

@Test
public void shouldInheritedMethodMatchImplementedMethod() throws Exception {
    /**
     * The situation we are testing is as follows:
     * Abstract class InheritanceRoot is initially implemented directly by ClassA.
     * ClassA is later modified to extend another implementation of InheritanceRoot
     * and the methods required by InheritanceRoot are now removed from ClassA directly,
     * and instead inherited from the new parent, DirectDescendant. For the purposes of
     * this test, this new ClassA is represented by ClassB (as we can't have the same
     * class declared twice in a test -- in real life, this would both be ClassA's,
     * in different jars).
     */
    Map<String, ClassInfo> oldClassInfoMap = new HashMap<String, ClassInfo>();
    Map<String, ClassInfo> newClassInfoMap = new HashMap<String, ClassInfo>();
    JarDiff jd = new JarDiff();
    addClassInfo(oldClassInfoMap, ClassA.class, jd);
    addClassInfo(oldClassInfoMap, DirectDescendant.class, jd);
    addClassInfo(oldClassInfoMap, InheritanceRoot.class, jd);
    addClassInfo(newClassInfoMap, ClassB.class, jd);
    addClassInfo(newClassInfoMap, DirectDescendant.class, jd);
    addClassInfo(newClassInfoMap, InheritanceRoot.class, jd);
    // Make B look like A
    newClassInfoMap.put("org/semver/jardiff/DeprecateDetectionTest$ClassA", newClassInfoMap.get("org/semver/jardiff/DeprecateDetectionTest$ClassB"));
    newClassInfoMap.remove("org/semver/jardiff/DeprecateDetectionTest$ClassB");
    DifferenceAccumulatingHandler handler = new DifferenceAccumulatingHandler();
    jd.diff(handler, new SimpleDiffCriteria(), "0.1.0", "0.2.0", oldClassInfoMap, newClassInfoMap);
    Dumper.dump(handler.getDelta());
    Set<Difference> differences = handler.getDelta().getDifferences();
    Assert.assertEquals("differences found", 3, differences.size());
    // Naive search for Deprecate.
    boolean hasDeprecate = false;
    for (Difference d : differences) {
        if (d instanceof Deprecate)
            hasDeprecate = true;
    }
    Assert.assertTrue("No Delta.Deprecate found", hasDeprecate);
}
Also used : HashMap(java.util.HashMap) Deprecate(org.semver.Delta.Deprecate) JarDiff(org.osjava.jardiff.JarDiff) SimpleDiffCriteria(org.osjava.jardiff.SimpleDiffCriteria) Difference(org.semver.Delta.Difference) ClassInfo(org.osjava.jardiff.ClassInfo) Test(org.junit.Test)

Aggregations

SimpleDiffCriteria (org.osjava.jardiff.SimpleDiffCriteria)4 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 ClassInfo (org.osjava.jardiff.ClassInfo)2 DiffCriteria (org.osjava.jardiff.DiffCriteria)2 JarDiff (org.osjava.jardiff.JarDiff)2 PublicDiffCriteria (org.osjava.jardiff.PublicDiffCriteria)2 Delta (org.semver.Delta)2 CmdlineParser (de.tototec.cmdoption.CmdlineParser)1 CmdlineParserException (de.tototec.cmdoption.CmdlineParserException)1 File (java.io.File)1 IOException (java.io.IOException)1 EnforcerRuleException (org.apache.maven.enforcer.rule.api.EnforcerRuleException)1 Comparer (org.semver.Comparer)1 Change (org.semver.Delta.Change)1 Deprecate (org.semver.Delta.Deprecate)1 Difference (org.semver.Delta.Difference)1