Skip to content

Commit

Permalink
test: refactor test to be independent from JDK version (#1764)
Browse files Browse the repository at this point in the history
  • Loading branch information
surli authored and monperrus committed Nov 30, 2017
1 parent f8d9745 commit 8f682a4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 46 deletions.
79 changes: 35 additions & 44 deletions src/test/java/spoon/reflect/declaration/CtTypeInformationTest.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,36 @@
package spoon.reflect.declaration;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static spoon.testing.utils.ModelUtils.build;

import java.lang.reflect.Field;
import java.util.AbstractCollection;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.RandomAccess;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import spoon.reflect.declaration.testclasses.ExtendsObject;
import spoon.Launcher;
import spoon.reflect.declaration.testclasses.ExtendsArrayList;
import spoon.reflect.declaration.testclasses.Subclass;
import spoon.reflect.declaration.testclasses.Subinterface;
import spoon.reflect.declaration.testclasses.TestInterface;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtTypeReference;
import spoon.support.visitor.ClassTypingContext;

import java.lang.reflect.Field;
import java.util.AbstractCollection;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.RandomAccess;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static spoon.testing.utils.ModelUtils.build;

public class CtTypeInformationTest {
private Factory factory;

@Before
public void setUp() throws Exception {
factory = build(ExtendsObject.class, Subclass.class, Subinterface.class, TestInterface.class);
factory = build(ExtendsArrayList.class, Subclass.class, Subinterface.class, TestInterface.class);
}

@Test
Expand All @@ -42,7 +39,7 @@ public void testClassTypingContextContinueScanning() throws Exception {
final CtType<?> subClass = this.factory.Type().get(Subclass.class);
final CtTypeReference<?> subinterface = this.factory.Type().createReference(Subinterface.class);
final CtTypeReference<?> testInterface = this.factory.Type().createReference(TestInterface.class);
final CtTypeReference<?> extendObject = this.factory.Type().createReference(ExtendsObject.class);
final CtTypeReference<?> extendObject = this.factory.Type().createReference(ExtendsArrayList.class);
final CtTypeReference<?> arrayList = this.factory.Type().createReference(ArrayList.class);
final CtTypeReference<?> abstractList = this.factory.Type().createReference(AbstractList.class);
final CtTypeReference<?> abstractCollection = this.factory.Type().createReference(AbstractCollection.class);
Expand Down Expand Up @@ -132,42 +129,36 @@ private CtTypeInformation getLastResolvedSuperclass(ClassTypingContext ctc) thro
}

@Test
public void testGetSuperclass() throws Exception {
int expectedNumberInJDK8_151 = 62; // in Java 1.8.0_151
int expectedNumberInJDK8_111 = 61; // in Java 1.8.0_XXX with XXX before 151
int expectedNumberInJDK9 = 81;

int expectedNumber;

if (System.getProperty("java.version").startsWith("1.8.")) {
if (System.getProperty("java.version").endsWith("151")) {
expectedNumber = expectedNumberInJDK8_151;
} else {
expectedNumber = expectedNumberInJDK8_111;
}
} else {
expectedNumber = expectedNumberInJDK9;
}
public void testGetAllMethodsReturnsTheRightNumber() {
Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/noclasspath/ExtendsObject.java");
launcher.buildModel();
int nbMethodsObject = launcher.getFactory().Type().get(Object.class).getAllMethods().size();

final CtType<?> extendsObject = launcher.getFactory().Type().get("test.ExtendsObject");
assertEquals("It should contain only 'oneMethod' and 'toString' but also contains: "+ StringUtils.join(extendsObject.getMethods(),"\n"), 2, extendsObject.getMethods().size());
assertEquals(nbMethodsObject + 1, extendsObject.getAllMethods().size());
}

final CtType<?> extendObject = this.factory.Type().get(ExtendsObject.class);
@Test
public void testGetSuperclass() throws Exception {
final CtType<?> extendsArrayList = this.factory.Type().get(ExtendsArrayList.class);

// only 1 method directly in this class
Assert.assertEquals(1, extendObject.getMethods().size());
Assert.assertEquals(1, extendsArrayList.getMethods().size());

Assert.assertEquals(expectedNumber, extendObject.getAllMethods().size());
int nbMethodExtendedArrayList = extendsArrayList.getAllMethods().size();

final CtType<?> subClass = this.factory.Type().get(Subclass.class);
assertEquals(2, subClass.getMethods().size());



// the abstract method from Comparable which is overridden should not be present in the model
assertEquals(expectedNumber+2, subClass.getAllMethods().size());
assertEquals(nbMethodExtendedArrayList+2, subClass.getAllMethods().size());

CtTypeReference<?> superclass = subClass.getSuperclass();
Assert.assertEquals(ExtendsObject.class.getName(), superclass.getQualifiedName());
Assert.assertEquals(ExtendsArrayList.class.getName(), superclass.getQualifiedName());

Assert.assertEquals(ExtendsObject.class.getName(), superclass.getQualifiedName());
Assert.assertEquals(ExtendsArrayList.class.getName(), superclass.getQualifiedName());

Assert.assertNotNull(superclass.getSuperclass());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

import java.util.ArrayList;

public class ExtendsObject extends ArrayList {
public class ExtendsArrayList extends ArrayList {
void m() {}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package spoon.reflect.declaration.testclasses;

public class Subclass extends ExtendsObject implements Subinterface {
public class Subclass extends ExtendsArrayList implements Subinterface {
@Override
public int compareTo(Object o) {
return 0;
Expand Down
12 changes: 12 additions & 0 deletions src/test/resources/noclasspath/ExtendsObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package test;

public class ExtendsObject {
public void oneMethod() {

}

@Override
public String toString() {
return "Hello";
}
}

0 comments on commit 8f682a4

Please sign in to comment.