aboutsummaryrefslogtreecommitdiffstats
path: root/api/src/test/java/org/semver/jardiff/ClassInheritanceTest.java
blob: 06820327cb1dc2a2530fda6b6c3ef053d44049d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
 * Copyright 2012-2014 Julien Eluard and contributors
 * This project includes software developed by Julien Eluard: https://github.com/jeluard/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.semver.jardiff;
import java.util.HashMap;
import java.util.Map;

import junit.framework.Assert;
import org.junit.Test;
import org.objectweb.asm.ClassReader;
import org.osjava.jardiff.ClassInfo;
import org.osjava.jardiff.JarDiff;
import org.osjava.jardiff.SimpleDiffCriteria;
import org.semver.Delta;
import org.semver.Dumper;
import org.semver.Delta.Change;

public class ClassInheritanceTest {

  public static abstract class InheritanceRoot {
    public abstract void aMethod();
  }

  public static class DirectDescendant extends InheritanceRoot {
    @Override
    public void aMethod() {}
  }

  public static class ClassA extends InheritanceRoot {
    @Override
    public void aMethod() {}
  }

  public static class ClassB extends DirectDescendant {
  }

  @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).
     */
    final Map<String, ClassInfo> oldClassInfoMap = new HashMap<String, ClassInfo>();
    final Map<String, ClassInfo> newClassInfoMap = new HashMap<String, ClassInfo>();
    final 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
    final ClassInfo a = oldClassInfoMap.get("org/semver/jardiff/ClassInheritanceTest$ClassA");
    final 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());
    final DifferenceAccumulatingHandler handler = new DifferenceAccumulatingHandler();
    jd.diff(handler, new SimpleDiffCriteria(true),
        "0.1.0", "0.2.0", oldClassInfoMap, newClassInfoMap);

    Dumper.dumpFullStats(handler.getDelta(), 4, System.out);

    for (final 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());
  }

  private void addClassInfo(final Map<String, ClassInfo> classMap, final Class klass, final JarDiff jd) throws Exception {
    final ClassInfo classInfo = jd.loadClassInfo(new ClassReader(klass.getName()));
    classMap.put(classInfo.getName(), classInfo);
  }

}