diff options
author | Martín Schonaker <[email protected]> | 2014-04-10 11:44:54 -0300 |
---|---|---|
committer | Martín Schonaker <[email protected]> | 2014-04-10 11:44:54 -0300 |
commit | e73cc974125ba252eb361983142a95f857bbd5ba (patch) | |
tree | 06e8f0cb0e7fc66db1dd80f46679cd87f0f3c2db /api | |
parent | 4b869a60b026b4c6822c42c7656993d0c265ef50 (diff) |
Issue #25: more tests on deprecation detection.
Diffstat (limited to 'api')
-rw-r--r-- | api/src/test/java/org/semver/jardiff/DeprecateDetectionTest.java | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/api/src/test/java/org/semver/jardiff/DeprecateDetectionTest.java b/api/src/test/java/org/semver/jardiff/DeprecateDetectionTest.java new file mode 100644 index 0000000..6cc85d6 --- /dev/null +++ b/api/src/test/java/org/semver/jardiff/DeprecateDetectionTest.java @@ -0,0 +1,118 @@ +/** + * 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.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import junit.framework.Assert; + +import org.junit.Test; +import org.objectweb.asm.ClassReader; +import org.osjava.jardiff.ClassInfo; +import org.osjava.jardiff.DiffCriteria; +import org.osjava.jardiff.DiffHandler; +import org.osjava.jardiff.JarDiff; +import org.osjava.jardiff.SimpleDiffCriteria; +import org.semver.Delta.Deprecate; +import org.semver.Delta.Difference; +import org.semver.Dumper; + +public class DeprecateDetectionTest { + + 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 int aField = 0; + } + + public static class ClassB extends DirectDescendant { + @Override + @Deprecated + public void aMethod() {} + + @Deprecated + public int aField = 0; + } + + @Test + public void shouldInheritedMethodMatchImplementedMethod() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, IOException { + /** + * 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(); + Method loadInfoMethod = JarDiff.class.getDeclaredMethod("loadClassInfo", ClassReader.class); + Method diffMethod = JarDiff.class.getDeclaredMethod("diff", DiffHandler.class, DiffCriteria.class, + String.class, String.class, + Map.class, Map.class); + diffMethod.setAccessible(true); + loadInfoMethod.setAccessible(true); + addClassInfo(oldClassInfoMap, ClassA.class, jd, loadInfoMethod); + addClassInfo(oldClassInfoMap, DirectDescendant.class, jd, loadInfoMethod); + addClassInfo(oldClassInfoMap, InheritanceRoot.class, jd, loadInfoMethod); + addClassInfo(newClassInfoMap, ClassB.class, jd, loadInfoMethod); + addClassInfo(newClassInfoMap, DirectDescendant.class, jd, loadInfoMethod); + addClassInfo(newClassInfoMap, InheritanceRoot.class, jd, loadInfoMethod); + + // Make B look like A + newClassInfoMap.put(ClassA.class.getName(), newClassInfoMap.get(ClassB.class.getName())); + newClassInfoMap.remove(ClassB.class.getName()); + DifferenceAccumulatingHandler handler = new DifferenceAccumulatingHandler(); + diffMethod.invoke(jd, 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); + } + + private void addClassInfo(Map<String, ClassInfo> classMap, Class klass, JarDiff jd, + Method loadInfoMethod) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, IOException { + ClassInfo classInfo = (ClassInfo) loadInfoMethod.invoke(jd, new ClassReader(klass.getName())); + classMap.put(klass.getName(), classInfo); + } +} |