diff --git a/src/test/java/spoon/test/factory/TypeFactoryTest.java b/src/test/java/spoon/test/factory/TypeFactoryTest.java index bd70568c190..bd1b0d3def0 100644 --- a/src/test/java/spoon/test/factory/TypeFactoryTest.java +++ b/src/test/java/spoon/test/factory/TypeFactoryTest.java @@ -1,18 +1,16 @@ /** - * Copyright (C) 2006-2018 INRIA and contributors - * Spoon - http://spoon.gforge.inria.fr/ + * Copyright (C) 2006-2018 INRIA and contributors Spoon - http://spoon.gforge.inria.fr/ * - * This software is governed by the CeCILL-C License under French law and - * abiding by the rules of distribution of free software. You can use, modify - * and/or redistribute the software under the terms of the CeCILL-C license as - * circulated by CEA, CNRS and INRIA at http://www.cecill.info. + * This software is governed by the CeCILL-C License under French law and abiding by the rules of + * distribution of free software. You can use, modify and/or redistribute the software under the + * terms of the CeCILL-C license as circulated by CEA, CNRS and INRIA at http://www.cecill.info. * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details. + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * CeCILL-C License for more details. * - * The fact that you are presently reading this means that you have had - * knowledge of the CeCILL-C license and that you accept its terms. + * The fact that you are presently reading this means that you have had knowledge of the CeCILL-C + * license and that you accept its terms. */ package spoon.test.factory; @@ -34,6 +32,8 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertTrue; public class TypeFactoryTest { @@ -59,11 +59,17 @@ public void testCreateTypeRef() { @Test public void reflectionAPI() { // Spoon can be used as reflection API - CtType s = new TypeFactory().get(String.class); - assertEquals("String", s.getSimpleName()); - assertEquals("java.lang.String", s.getQualifiedName()); - assertEquals(3, s.getSuperInterfaces().size()); - assertEquals(2, s.getMethodsByName("toLowerCase").size()); + CtType s = new TypeFactory().get(String.class); + assertAll( + () -> assertEquals("String", s.getSimpleName()), + () -> assertEquals("java.lang.String", s.getQualifiedName()), + /* + In java 12 string got 2 new interfaces (see https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/lang/String.html) + Constable, ConstantDesc. To support the CI with jdk8 and newer CIs for local testing this assertion is needed. + */ + () -> assertTrue(3 == s.getSuperInterfaces().size() || 5 == s.getSuperInterfaces().size()), + () -> assertEquals(3, s.getSuperInterfaces().size()), + () -> assertEquals(2, s.getMethodsByName("toLowerCase").size())); } @Test diff --git a/src/test/java/spoon/test/model/TypeTest.java b/src/test/java/spoon/test/model/TypeTest.java index a2c9a9e94d3..81f6562c491 100644 --- a/src/test/java/spoon/test/model/TypeTest.java +++ b/src/test/java/spoon/test/model/TypeTest.java @@ -1,18 +1,9 @@ /** - * Copyright (C) 2006-2018 INRIA and contributors - * Spoon - http://spoon.gforge.inria.fr/ + * SPDX-License-Identifier: (MIT OR CECILL-C) * - * This software is governed by the CeCILL-C License under French law and - * abiding by the rules of distribution of free software. You can use, modify - * and/or redistribute the software under the terms of the CeCILL-C license as - * circulated by CEA, CNRS and INRIA at http://www.cecill.info. + * Copyright (C) 2006-2019 INRIA and contributors * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details. - * - * The fact that you are presently reading this means that you have had - * knowledge of the CeCILL-C license and that you accept its terms. + * Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) of the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon. */ package spoon.test.model; @@ -25,11 +16,9 @@ import spoon.reflect.declaration.CtTypeInformation; import spoon.reflect.declaration.CtTypeParameter; import spoon.reflect.factory.TypeFactory; -import spoon.reflect.reference.CtExecutableReference; import spoon.reflect.reference.CtTypeReference; import spoon.reflect.visitor.filter.AllTypeMembersFunction; -import java.util.Collection; import java.util.List; import java.util.Set; @@ -37,38 +26,54 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertAll; import static spoon.testing.utils.ModelUtils.build; import static spoon.testing.utils.ModelUtils.createFactory; public class TypeTest { @Test - public void testGetAllExecutables() throws Exception { + public void testGetDifferentElements() throws Exception { + // contract: All elements of a model must be found. CtClass type = build("spoon.test.model", "Foo"); - assertEquals(1, type.getDeclaredFields().size()); - assertEquals(3, type.getMethods().size()); - assertEquals(4, type.getDeclaredExecutables().size()); - assertEquals(2, type.getAllFields().size()); - assertEquals(1, type.getConstructors().size()); - assertEquals(16, type.getAllMethods().size()); - assertEquals(12, type.getFactory().Type().get(Object.class).getAllMethods().size()); + assertAll( + () -> assertEquals(1, type.getDeclaredFields().size()), + () -> assertEquals(3, type.getMethods().size()), + () -> assertEquals(4, type.getDeclaredExecutables().size()), + () -> assertEquals(2, type.getAllFields().size()), + () -> assertEquals(1, type.getConstructors().size()), + () -> assertEquals(16, type.getAllMethods().size()), + () -> assertEquals(12, type.getFactory().Type().get(Object.class).getAllMethods().size())); // we have 3 methods in Foo + 2 in Baz - 1 common in Foo.bar (m) + 12 in Object + 1 explicit constructor in Foo - Collection> allExecutables = type.getAllExecutables(); - assertEquals(17, allExecutables.size()); + /* + This assertion is needed because in java.lang.object the method registerNative was removed. + See https://bugs.openjdk.java.net/browse/JDK-8232801 for details. + To fit this change and support new jdks and the CI both values are correct. + In jdk8 object has 12 methods and in newer jdk object has 11 + */ + assertTrue(17 == type.getAllExecutables().size() || 16 == type.getAllExecutables().size()); } @Test public void testAllTypeMembersFunctionMode() throws Exception { // contract: AllTypeMembersFunction can be configured to return all members or only internally visible members CtClass type = build("spoon.test.model", "Foo"); - List internallyAccessibleMethods = type.map(new AllTypeMembersFunction(CtMethod.class).setMode(AllTypeMembersFunction.Mode.SKIP_PRIVATE)).list(); - List allMethods = type.map(new AllTypeMembersFunction(CtMethod.class)).list(); - assertEquals(16, internallyAccessibleMethods.size()); - assertEquals(17, allMethods.size()); + List> internallyAccessibleMethods = type.map(new AllTypeMembersFunction(CtMethod.class).setMode(AllTypeMembersFunction.Mode.SKIP_PRIVATE)).list(); + List> allMethods = type.map(new AllTypeMembersFunction(CtMethod.class)).list(); + assertAll( + () -> assertEquals(16, internallyAccessibleMethods.size()), + () -> assertTrue(17 == allMethods.size() || 16 == allMethods.size())); + + /* + This if-clause is needed because in java.lang.object the method registerNative was removed. + See https://bugs.openjdk.java.net/browse/JDK-8232801 for details. + */ allMethods.removeAll(internallyAccessibleMethods); - assertEquals(1, allMethods.size()); - assertEquals("registerNatives()", allMethods.get(0).getSignature()); + if (allMethods.size() == 1) { + assertEquals(1, allMethods.size()); + assertEquals("registerNatives()", allMethods.get(0).getSignature()); + } } @Test diff --git a/src/test/java/spoon/test/reference/ExecutableReferenceTest.java b/src/test/java/spoon/test/reference/ExecutableReferenceTest.java index d927dc9a6e3..b681780cca1 100644 --- a/src/test/java/spoon/test/reference/ExecutableReferenceTest.java +++ b/src/test/java/spoon/test/reference/ExecutableReferenceTest.java @@ -1,23 +1,15 @@ /** - * Copyright (C) 2006-2018 INRIA and contributors - * Spoon - http://spoon.gforge.inria.fr/ + * SPDX-License-Identifier: (MIT OR CECILL-C) * - * This software is governed by the CeCILL-C License under French law and - * abiding by the rules of distribution of free software. You can use, modify - * and/or redistribute the software under the terms of the CeCILL-C license as - * circulated by CEA, CNRS and INRIA at http://www.cecill.info. + * Copyright (C) 2006-2019 INRIA and contributors * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details. - * - * The fact that you are presently reading this means that you have had - * knowledge of the CeCILL-C license and that you accept its terms. + * Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) of the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon. */ package spoon.test.reference; import org.junit.Test; import spoon.Launcher; +import spoon.OutputType; import spoon.reflect.code.CtInvocation; import spoon.reflect.declaration.CtClass; import spoon.reflect.declaration.CtExecutable; @@ -35,8 +27,9 @@ import spoon.test.reference.testclasses.Stream; import spoon.test.reference.testclasses.SuperFoo; +import java.util.ArrayList; +import java.util.Collection; import java.util.List; -import java.util.stream.Collectors; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; @@ -44,6 +37,7 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertAll; public class ExecutableReferenceTest { @Test @@ -124,25 +118,41 @@ public void testSuperClassInGetAllExecutables() { } @Test - public void testSpecifyGetAllExecutablesMethod() { - final Launcher launcher = new Launcher(); - launcher.setArgs(new String[] {"--output-type", "nooutput" }); + public void testGetAllExecutablesMethodForInterface() { + // contract: As interfaces doesn't extend object, the Foo interface must have 1 method and no method from object. + Launcher launcher = new Launcher(); + launcher.getEnvironment().setOutputType(OutputType.NO_OUTPUT); launcher.addInputResource("./src/test/java/spoon/test/reference/testclasses"); launcher.run(); + CtInterface foo = launcher.getFactory().Interface().get(spoon.test.reference.testclasses.Foo.class); + Collection> fooExecutables = foo.getAllExecutables(); + assertAll( + () ->assertEquals(1, fooExecutables.size()), + () ->assertEquals(foo.getSuperInterfaces().iterator().next().getTypeDeclaration().getMethod("m").getReference(), + launcher.getFactory().Interface().get(SuperFoo.class).getMethod("m").getReference())); + } - final CtInterface foo = launcher.getFactory().Interface().get(spoon.test.reference.testclasses.Foo.class); - final List> fooExecutables = foo.getAllExecutables().stream().collect(Collectors.toList()); - assertEquals(1, fooExecutables.size()); - assertEquals(foo.getSuperInterfaces().stream().findFirst().get().getTypeDeclaration().getMethod("m").getReference(), launcher.getFactory().Interface().get(SuperFoo.class).getMethod("m").getReference()); - - final CtClass bar = launcher.getFactory().Class().get(Bar.class); - final List> barExecutables = bar.getAllExecutables().stream().collect(Collectors.toList()); - assertEquals(12 /* object */ + 1 /* constructor */, barExecutables.size()); - - final CtInterface kuu = launcher.getFactory().Interface().get(Kuu.class); - final List> kuuExecutables = kuu.getAllExecutables().stream().collect(Collectors.toList()); - assertEquals(1 /* default method in interface */, kuuExecutables.size()); - assertEquals(kuu.getMethod("m").getReference(), kuuExecutables.get(0)); + @Test + public void testGetAllExecutablesMethodForClasses() { + // contract: As classes extend object and the Bar class has 1 method, getAllExecutables for Bar must return 12/13. + Launcher launcher = new Launcher(); + launcher.getEnvironment().setOutputType(OutputType.NO_OUTPUT); + launcher.addInputResource("./src/test/java/spoon/test/reference/testclasses"); + launcher.run(); + CtClass bar = launcher.getFactory().Class().get(Bar.class); + Collection> barExecutables = bar.getAllExecutables(); + /* + This assertion is needed because in java.lang.object the method registerNative was removed. + See https://bugs.openjdk.java.net/browse/JDK-8232801 for details. + To fit this change and support new jdks and the CI both values are correct. + In jdk8 object has 12 methods and in newer jdk object has 11 + */ + assertTrue(barExecutables.size() == 12 || barExecutables.size() == 13); + CtInterface kuu = launcher.getFactory().Interface().get(Kuu.class); + List> kuuExecutables = new ArrayList<>(kuu.getAllExecutables()); + assertAll( + () -> assertEquals(1 /* default method in interface */, kuuExecutables.size()), + () -> assertEquals(kuu.getMethod("m").getReference(), kuuExecutables.get(0))); } @Test