This is an automated email from the ASF dual-hosted git repository. rfscholte pushed a commit to branch MNG-7063 in repository https://gitbox.apache.org/repos/asf/maven.git
commit fd978e17fab1800cfc9ffb655af61305294d1b0c Author: Guillaume Nodet <gno...@gmail.com> AuthorDate: Mon Jan 18 11:46:19 2021 +0100 Use stock junit 5 parameterized tests --- .../maven/graph/DefaultGraphBuilderTest.java | 51 +++--- .../main/java/org/apache/maven/test/Parameter.java | 32 ---- .../java/org/apache/maven/test/Parameterized.java | 37 ----- .../apache/maven/test/ParameterizedExtension.java | 182 --------------------- .../java/org/apache/maven/test/Parameters.java | 35 ---- .../src/main/java/org/apache/maven/test/Test.java | 34 ---- .../maven/xml/sax/ext/CommentRenormalizerTest.java | 31 +--- 7 files changed, 29 insertions(+), 373 deletions(-) diff --git a/maven-core/src/test/java/org/apache/maven/graph/DefaultGraphBuilderTest.java b/maven-core/src/test/java/org/apache/maven/graph/DefaultGraphBuilderTest.java index d2f29e2..4a8f9c0 100644 --- a/maven-core/src/test/java/org/apache/maven/graph/DefaultGraphBuilderTest.java +++ b/maven-core/src/test/java/org/apache/maven/graph/DefaultGraphBuilderTest.java @@ -37,12 +37,11 @@ import org.apache.maven.project.collector.MultiModuleCollectionStrategy; import org.apache.maven.project.collector.PomlessCollectionStrategy; import org.apache.maven.project.collector.ProjectsSelector; import org.apache.maven.project.collector.RequestPomCollectionStrategy; -import org.apache.maven.test.Parameter; -import org.apache.maven.test.Parameterized; -import org.apache.maven.test.Parameters; -import org.apache.maven.test.Test; import org.codehaus.plexus.util.StringUtils; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import java.io.File; import java.util.ArrayList; @@ -60,11 +59,12 @@ import static org.apache.maven.execution.MavenExecutionRequest.REACTOR_MAKE_DOWN import static org.apache.maven.execution.MavenExecutionRequest.REACTOR_MAKE_UPSTREAM; import static org.apache.maven.graph.DefaultGraphBuilderTest.ScenarioBuilder.scenario; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.ArgumentMatchers.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -@Parameterized public class DefaultGraphBuilderTest { /* @@ -103,26 +103,9 @@ public class DefaultGraphBuilderTest private Map<String, MavenProject> artifactIdProjectMap; - // Parameters for the test - @Parameter( 0 ) - private String parameterDescription; - @Parameter( 1 ) - private List<String> parameterSelectedProjects; - @Parameter( 2 ) - private List<String> parameterExcludedProjects; - @Parameter( 3 ) - private String parameterResumeFrom; - @Parameter( 4 ) - private String parameterMakeBehavior; - @Parameter( 5 ) - private List<String> parameterExpectedResult; - @Parameter( 6 ) - private File parameterRequestedPom; - - @Parameters(name = "{index}. {0}") - public static Collection<Object[]> parameters() + public static Stream<Arguments> parameters() { - return asList( + return Stream.of( scenario( "Full reactor in order" ) .expectResult( PARENT_MODULE, MODULE_C, MODULE_C_1, MODULE_A, MODULE_B, MODULE_C_2, INDEPENDENT_MODULE ), scenario( "Selected project" ) @@ -198,8 +181,16 @@ public class DefaultGraphBuilderTest ); } - @Test - public void testGetReactorProjects() + @ParameterizedTest + @MethodSource("parameters") + public void testGetReactorProjects( + String parameterDescription, + List<String> parameterSelectedProjects, + List<String> parameterExcludedProjects, + String parameterResumeFrom, + String parameterMakeBehavior, + List<String> parameterExpectedResult, + File parameterRequestedPom) { // Given List<String> selectedProjects = parameterSelectedProjects.stream().map( p -> ":" + p ).collect( Collectors.toList() ); @@ -350,11 +341,11 @@ public class DefaultGraphBuilderTest return this; } - public Object[] expectResult( String... expectedReactorProjects ) + public Arguments expectResult( String... expectedReactorProjects ) { - return new Object[] { + return Arguments.arguments( description, selectedProjects, excludedProjects, resumeFrom, makeBehavior, asList( expectedReactorProjects ), requestedPom - }; + ); } } } \ No newline at end of file diff --git a/maven-test-support/src/main/java/org/apache/maven/test/Parameter.java b/maven-test-support/src/main/java/org/apache/maven/test/Parameter.java deleted file mode 100644 index f5164c5..0000000 --- a/maven-test-support/src/main/java/org/apache/maven/test/Parameter.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.apache.maven.test; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ - -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; - -/** - * Parameter - */ -@Retention( RetentionPolicy.RUNTIME ) -public @interface Parameter -{ - int value() default 0; -} diff --git a/maven-test-support/src/main/java/org/apache/maven/test/Parameterized.java b/maven-test-support/src/main/java/org/apache/maven/test/Parameterized.java deleted file mode 100644 index a15fbaf..0000000 --- a/maven-test-support/src/main/java/org/apache/maven/test/Parameterized.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.apache.maven.test; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import org.junit.jupiter.api.extension.ExtendWith; - -/** - * Parameterized - */ -@Target( { ElementType.ANNOTATION_TYPE, ElementType.TYPE } ) -@Retention( RetentionPolicy.RUNTIME ) -@ExtendWith( ParameterizedExtension.class ) -public @interface Parameterized -{ -} diff --git a/maven-test-support/src/main/java/org/apache/maven/test/ParameterizedExtension.java b/maven-test-support/src/main/java/org/apache/maven/test/ParameterizedExtension.java deleted file mode 100644 index 5741db4..0000000 --- a/maven-test-support/src/main/java/org/apache/maven/test/ParameterizedExtension.java +++ /dev/null @@ -1,182 +0,0 @@ -package org.apache.maven.test; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ - -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.util.Arrays; -import java.util.Comparator; -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import org.junit.jupiter.api.extension.Extension; -import org.junit.jupiter.api.extension.ExtensionContext; -import org.junit.jupiter.api.extension.TestInstancePostProcessor; -import org.junit.jupiter.api.extension.TestInstantiationException; -import org.junit.jupiter.api.extension.TestTemplateInvocationContext; -import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider; -import org.junit.jupiter.params.converter.DefaultArgumentConverter; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.platform.commons.util.CollectionUtils; -import org.junit.platform.commons.util.ReflectionUtils; - -import static org.junit.jupiter.params.provider.Arguments.arguments; -import static org.junit.platform.commons.util.AnnotationUtils.isAnnotated; - -/** - * ParameterizedExtension - */ -public class ParameterizedExtension implements TestTemplateInvocationContextProvider -{ - - @Override - public boolean supportsTestTemplate( ExtensionContext context ) - { - return context.getTestMethod() - .map( m -> isAnnotated( m, Test.class ) ) - .orElse( false ); - } - - @Override - public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts( - ExtensionContext extensionContext ) - { - Class<?> testClass = extensionContext.getRequiredTestClass(); - try - { - List<Method> parameters = getParametersMethods( testClass ); - if ( parameters.size() != 1 ) - { - throw new IllegalStateException( - "Class " + testClass.getName() + " should provide a single method annotated with @" - + Parameters.class.getSimpleName() ); - } - Object params = parameters.iterator().next().invoke( null ); - return CollectionUtils.toStream( params ) - .map( ParameterizedExtension::toArguments ) - .map( Arguments::get ) - .map( ParameterizedTemplate::new ); - } - catch ( Exception e ) - { - throw new IllegalStateException( "Unable to generate test templates for class " + testClass.getName(), e ); - } - } - - private List<Method> getParametersMethods( Class<?> testClass ) - { - List<Method> parameters = Stream.of( testClass.getDeclaredMethods() ) - .filter( m -> Modifier.isStatic( m.getModifiers() ) ) - .filter( m -> m.getAnnotation( Parameters.class ) != null ) - .collect( Collectors.toList() ); - if ( parameters.isEmpty() && testClass != null ) - { - return getParametersMethods( testClass.getSuperclass() ); - } - else - { - return parameters; - } - } - - private static Arguments toArguments( Object item ) - { - // Nothing to do except cast. - if ( item instanceof Arguments ) - { - return ( Arguments ) item; - } - // Pass all multidimensional arrays "as is", in contrast to Object[]. - // See https://github.com/junit-team/junit5/issues/1665 - if ( ReflectionUtils.isMultidimensionalArray( item ) ) - { - return arguments( item ); - } - // Special treatment for one-dimensional reference arrays. - // See https://github.com/junit-team/junit5/issues/1665 - if ( item instanceof Object[] ) - { - return arguments( ( Object[] ) item ); - } - // Pass everything else "as is". - return arguments( item ); - } - - /** - * ParameterizedTemplate - */ - public static class ParameterizedTemplate implements TestTemplateInvocationContext - { - - private final Object[] params; - - public ParameterizedTemplate( Object[] params ) - { - this.params = params; - } - - @Override - public String getDisplayName( int invocationIndex ) - { - return "[" + invocationIndex + "] " - + Stream.of( params ).map( Object::toString ).collect( Collectors.joining( ", " ) ); - } - - @Override - public List<Extension> getAdditionalExtensions() - { - return Arrays.asList( - ( TestInstancePostProcessor ) this::postProcessTestInstance ); - } - - protected void postProcessTestInstance( Object testInstance, ExtensionContext context ) throws Exception - { - Class<?> clazz = testInstance.getClass(); - List<Field> fields = hierarchy( clazz ) - .map( Class::getDeclaredFields ) - .flatMap( Stream::of ) - .filter( f -> isAnnotated( f, Parameter.class ) ) - .sorted( Comparator.comparing( f -> ( Integer ) f.getAnnotation( Parameter.class ).value() ) ) - .collect( Collectors.toList() ); - if ( params.length != fields.size() ) - { - throw new TestInstantiationException( - "Expected " + fields.size() + " parameters bug got " + params.length + " when instantiating " - + clazz.getName() ); - } - for ( int i = 0; i < fields.size(); i++ ) - { - Field f = fields.get( i ); - f.setAccessible( true ); - f.set( testInstance, DefaultArgumentConverter.INSTANCE.convert( params[i], f.getType() ) ); - } - } - - protected Stream<Class<?>> hierarchy( Class<?> clazz ) - { - Class<?> superclass = clazz.getSuperclass(); - return Stream.concat( Stream.of( clazz ), superclass != null ? hierarchy( superclass ) : Stream.empty() ); - } - - } - -} diff --git a/maven-test-support/src/main/java/org/apache/maven/test/Parameters.java b/maven-test-support/src/main/java/org/apache/maven/test/Parameters.java deleted file mode 100644 index b643a3a..0000000 --- a/maven-test-support/src/main/java/org/apache/maven/test/Parameters.java +++ /dev/null @@ -1,35 +0,0 @@ -package org.apache.maven.test; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Parameters - */ -@Retention( RetentionPolicy.RUNTIME ) -@Target( ElementType.METHOD ) -public @interface Parameters -{ - String name() default "{index}"; -} diff --git a/maven-test-support/src/main/java/org/apache/maven/test/Test.java b/maven-test-support/src/main/java/org/apache/maven/test/Test.java deleted file mode 100644 index 5b542b6..0000000 --- a/maven-test-support/src/main/java/org/apache/maven/test/Test.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.apache.maven.test; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ - -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; - -import org.junit.jupiter.api.TestTemplate; - -/** - * Test - */ -@Retention( RetentionPolicy.RUNTIME ) -@TestTemplate -public @interface Test -{ -} diff --git a/maven-xml/src/test/java/org/apache/maven/xml/sax/ext/CommentRenormalizerTest.java b/maven-xml/src/test/java/org/apache/maven/xml/sax/ext/CommentRenormalizerTest.java index dad847e..fba3521 100644 --- a/maven-xml/src/test/java/org/apache/maven/xml/sax/ext/CommentRenormalizerTest.java +++ b/maven-xml/src/test/java/org/apache/maven/xml/sax/ext/CommentRenormalizerTest.java @@ -22,34 +22,18 @@ package org.apache.maven.xml.sax.ext; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; -import java.util.Arrays; -import java.util.Collection; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; -import org.apache.maven.test.Parameter; -import org.apache.maven.test.Parameterized; -import org.apache.maven.test.Parameters; -import org.apache.maven.test.Test; import org.xml.sax.ext.LexicalHandler; -@Parameterized public class CommentRenormalizerTest { private LexicalHandler lexicalHandler = mock( LexicalHandler.class ); - @Parameter - private String lineSeparator; - - @Parameters - public static Collection<Object[]> data() { - return Arrays.asList(new Object[][] { - { "\n" }, - { "\r\n" }, - { "\r" } - }); - } - - @Test - public void singleLine() + @ParameterizedTest + @ValueSource(strings = {"\n", "\r\n", "\r" }) + public void singleLine(String lineSeparator) throws Exception { CommentRenormalizer commentRenormalizer = new CommentRenormalizer( lexicalHandler, lineSeparator ); @@ -61,8 +45,9 @@ public class CommentRenormalizerTest verify( lexicalHandler ).comment( ch, 0, ch.length ); } - @Test - public void multiLine() + @ParameterizedTest + @ValueSource(strings = {"\n", "\r\n", "\r" }) + public void multiLine(String lineSeparator) throws Exception { CommentRenormalizer commentRenormalizer = new CommentRenormalizer( lexicalHandler, lineSeparator );