Search in sources :

Example 1 with Change

use of org.semver.Delta.Change in project semantic-versioning by jeluard.

the class Dumper method dumpFullStats.

/**
     * Dumps on <code>out</code> all differences separated by its type in the order
     * <code>remove</code>, <code>change</code>, <code>deprecate</code> and <code>add</code>.
     * <p>
     * Prepends statistics per class regarding difference type.
     * </p>
     * @param delta the delta to be dumped
     * @param iwidth the integer width for formated integer counter
     * @param out the print output stream
     */
public static void dumpFullStats(final Delta delta, final int iwidth, final PrintStream out) {
    final Set<Difference> diffs = delta.getDifferences();
    final List<Difference> diffsAdd = new ArrayList<Difference>();
    final List<Difference> diffsChange = new ArrayList<Difference>();
    final List<Difference> diffsDeprecate = new ArrayList<Difference>();
    final List<Difference> diffsRemove = new ArrayList<Difference>();
    final Map<String, DiffCount> className2DiffCount = new HashMap<String, DiffCount>();
    int maxClassNameLen = 0;
    for (final Iterator<Difference> iter = diffs.iterator(); iter.hasNext(); ) {
        final Difference diff = iter.next();
        final String className = diff.getClassName();
        maxClassNameLen = Math.max(maxClassNameLen, className.length());
        DiffCount dc = className2DiffCount.get(className);
        if (null == dc) {
            dc = new DiffCount(className);
            className2DiffCount.put(className, dc);
        }
        if (diff instanceof Delta.Add) {
            diffsAdd.add(diff);
            dc.additions++;
        } else if (diff instanceof Delta.Change) {
            diffsChange.add(diff);
            dc.changes++;
        } else if (diff instanceof Delta.Deprecate) {
            diffsDeprecate.add(diff);
            dc.deprecates++;
        } else if (diff instanceof Delta.Remove) {
            diffsRemove.add(diff);
            dc.removes++;
        }
    }
    Collections.sort(diffsAdd);
    Collections.sort(diffsChange);
    Collections.sort(diffsDeprecate);
    Collections.sort(diffsRemove);
    final List<String> classNames = new ArrayList<String>(className2DiffCount.keySet());
    Collections.sort(classNames);
    System.err.println("Summary: " + diffs.size() + " differences in " + classNames.size() + " classes:");
    System.err.println("  Remove " + diffsRemove.size() + ", Change " + diffsChange.size() + ", Deprecate " + diffsDeprecate.size() + ", Add " + diffsAdd.size());
    System.err.printf("%n");
    int iterI = 0;
    for (final Iterator<String> iter = classNames.iterator(); iter.hasNext(); iterI++) {
        final String className = iter.next();
        final DiffCount dc = className2DiffCount.get(className);
        System.err.printf("%" + iwidth + "d/%" + iwidth + "d: %-" + maxClassNameLen + "s: %s%n", iterI, classNames.size(), className, dc.format(iwidth));
    }
    System.err.printf("%n%nRemoves%n%n");
    dump(diffsRemove, System.err);
    System.err.printf("%n%nChanges%n%n");
    dump(diffsChange, System.err);
    System.err.printf("%n%nDeprecates%n%n");
    dump(diffsDeprecate, System.err);
    System.err.printf("%n%nAdditions%n%n");
    dump(diffsAdd, System.err);
    System.err.printf("%n%n");
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Difference(org.semver.Delta.Difference) Change(org.semver.Delta.Change)

Example 2 with Change

use of org.semver.Delta.Change 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)

Aggregations

HashMap (java.util.HashMap)2 Change (org.semver.Delta.Change)2 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 ClassInfo (org.osjava.jardiff.ClassInfo)1 JarDiff (org.osjava.jardiff.JarDiff)1 SimpleDiffCriteria (org.osjava.jardiff.SimpleDiffCriteria)1 Delta (org.semver.Delta)1 Difference (org.semver.Delta.Difference)1