From 026af2de2acc4a4b7b7ed9b1ca346b96b00f704f Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 15 Jun 2018 16:00:10 +0100 Subject: [PATCH 1/2] Add matrix testing infrastructure --- build.gradle | 2 + .../build/matrix/MatrixTestExtension.groovy | 106 ++++++++++++++++++ .../build/matrix/MatrixTestPlugin.groovy | 28 +++++ .../SampleBuildConfigurer.groovy | 4 +- .../{ => samples}/SamplesExtension.groovy | 4 +- .../build/{ => samples}/SamplesPlugin.groovy | 4 +- .../gradle-plugins/matrixtest.properties | 1 + .../gradle-plugins/samples.properties | 2 +- 8 files changed, 144 insertions(+), 7 deletions(-) create mode 100644 buildSrc/src/main/groovy/org/springframework/restdocs/build/matrix/MatrixTestExtension.groovy create mode 100644 buildSrc/src/main/groovy/org/springframework/restdocs/build/matrix/MatrixTestPlugin.groovy rename buildSrc/src/main/groovy/org/springframework/restdocs/build/{ => samples}/SampleBuildConfigurer.groovy (98%) rename buildSrc/src/main/groovy/org/springframework/restdocs/build/{ => samples}/SamplesExtension.groovy (92%) rename buildSrc/src/main/groovy/org/springframework/restdocs/build/{ => samples}/SamplesPlugin.groovy (90%) create mode 100644 buildSrc/src/main/resources/META-INF/gradle-plugins/matrixtest.properties diff --git a/build.gradle b/build.gradle index 5b3fd3c3..c551e6a2 100644 --- a/build.gradle +++ b/build.gradle @@ -16,6 +16,7 @@ allprojects { group = 'org.springframework.restdocs' repositories { mavenCentral() + maven { url 'https://repo.spring.io/libs-snapshot' } } } @@ -51,6 +52,7 @@ subprojects { apply plugin: 'propdeps-eclipse' apply plugin: 'propdeps-maven' apply plugin: 'maven' + apply plugin: 'matrixtest' sourceCompatibility = 1.8 targetCompatibility = 1.8 diff --git a/buildSrc/src/main/groovy/org/springframework/restdocs/build/matrix/MatrixTestExtension.groovy b/buildSrc/src/main/groovy/org/springframework/restdocs/build/matrix/MatrixTestExtension.groovy new file mode 100644 index 00000000..90d84d1e --- /dev/null +++ b/buildSrc/src/main/groovy/org/springframework/restdocs/build/matrix/MatrixTestExtension.groovy @@ -0,0 +1,106 @@ +/* + * Copyright 2014-2018 the original author or authors. + * + * 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.springframework.restdocs.build.matrix + +import org.gradle.api.Project +import org.gradle.api.Task +import org.gradle.api.plugins.JavaBasePlugin +import org.gradle.api.tasks.testing.Test + +public class MatrixTestExtension { + + private List entries = [] + + MatrixTestExtension(Project project) { + project.afterEvaluate { + configureTestTasks(project) + } + } + + void methodMissing(String name, args) { + Entry entry = new Entry(); + Closure closure = args[0] + closure.delegate = entry + closure.resolveStrategy = Closure.DELEGATE_FIRST + closure.call() + entries << entry + } + + void configureTestTasks(Project project) { + if (!entries.empty) { + cartesianProduct(entries.collect { entry -> + entry.versions.collect { ['group': entry.group, 'version': it] } + }).forEach { configureTestTask(project, it) } + } + } + + void configureTestTask(Project project, List> versionSelectors) { + String identifier = ""; + versionSelectors.forEach { + identifier += "_${it.group}_${it.version}" + } + String description = "Runs the unit tests using " + description += versionSelectors.collect { "${it.group} ${it.version}" }.join(", ") + Test matrixTest = project.tasks.create("matrixTest" + identifier, Test) { test -> + test.setDescription(description); + test.setGroup(JavaBasePlugin.VERIFICATION_GROUP); + def testSourceSet = project.sourceSets.test + def configuration = project.configurations.create(testSourceSet.runtimeClasspathConfigurationName + identifier) { + extendsFrom(project.configurations.getByName(testSourceSet.runtimeClasspathConfigurationName)) + resolutionStrategy.eachDependency { dependency -> + versionSelectors + .findAll{ it.group == dependency.requested.group } + .each { dependency.useVersion it.version } + } + } + } + classpath = project.files(testSourceSet.output, project.sourceSets.main.output, configuration) + } + project.tasks.getByName('check').dependsOn(matrixTest) + } + + List>> cartesianProduct(List>> lists) { + if (lists.size() == 1) { + return lists + } + return cartesianProduct(lists, 0) + } + + List>> cartesianProduct(List>> lists, int index) { + List>> result = []; + if (index == lists.size()) { + result.add([]); + } else { + lists.get(index).each { list -> + cartesianProduct(lists, index + 1).each { product -> + product.add(list) + result.add(product) + } + } + } + return result; + } + + class Entry { + + String group + + List versions + + } + +} \ No newline at end of file diff --git a/buildSrc/src/main/groovy/org/springframework/restdocs/build/matrix/MatrixTestPlugin.groovy b/buildSrc/src/main/groovy/org/springframework/restdocs/build/matrix/MatrixTestPlugin.groovy new file mode 100644 index 00000000..cf2c6472 --- /dev/null +++ b/buildSrc/src/main/groovy/org/springframework/restdocs/build/matrix/MatrixTestPlugin.groovy @@ -0,0 +1,28 @@ +/* + * Copyright 2014-2018 the original author or authors. + * + * 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.springframework.restdocs.build.matrix + +import org.gradle.api.Plugin +import org.gradle.api.Project + +public class MatrixTestPlugin implements Plugin { + + public void apply(Project project) { + project.extensions.create('matrixTest', MatrixTestExtension, project) + } + +} \ No newline at end of file diff --git a/buildSrc/src/main/groovy/org/springframework/restdocs/build/SampleBuildConfigurer.groovy b/buildSrc/src/main/groovy/org/springframework/restdocs/build/samples/SampleBuildConfigurer.groovy similarity index 98% rename from buildSrc/src/main/groovy/org/springframework/restdocs/build/SampleBuildConfigurer.groovy rename to buildSrc/src/main/groovy/org/springframework/restdocs/build/samples/SampleBuildConfigurer.groovy index 0db278aa..84c7888b 100644 --- a/buildSrc/src/main/groovy/org/springframework/restdocs/build/SampleBuildConfigurer.groovy +++ b/buildSrc/src/main/groovy/org/springframework/restdocs/build/samples/SampleBuildConfigurer.groovy @@ -1,5 +1,5 @@ /* - * Copyright 2014-2017 the original author or authors. + * Copyright 2014-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.restdocs.build +package org.springframework.restdocs.build.samples import org.gradle.api.GradleException import org.gradle.api.Project diff --git a/buildSrc/src/main/groovy/org/springframework/restdocs/build/SamplesExtension.groovy b/buildSrc/src/main/groovy/org/springframework/restdocs/build/samples/SamplesExtension.groovy similarity index 92% rename from buildSrc/src/main/groovy/org/springframework/restdocs/build/SamplesExtension.groovy rename to buildSrc/src/main/groovy/org/springframework/restdocs/build/samples/SamplesExtension.groovy index c86a3104..e783714d 100644 --- a/buildSrc/src/main/groovy/org/springframework/restdocs/build/SamplesExtension.groovy +++ b/buildSrc/src/main/groovy/org/springframework/restdocs/build/samples/SamplesExtension.groovy @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.restdocs.build +package org.springframework.restdocs.build.samples import org.gradle.api.Project import org.gradle.api.Task diff --git a/buildSrc/src/main/groovy/org/springframework/restdocs/build/SamplesPlugin.groovy b/buildSrc/src/main/groovy/org/springframework/restdocs/build/samples/SamplesPlugin.groovy similarity index 90% rename from buildSrc/src/main/groovy/org/springframework/restdocs/build/SamplesPlugin.groovy rename to buildSrc/src/main/groovy/org/springframework/restdocs/build/samples/SamplesPlugin.groovy index 09af86d8..43c02316 100644 --- a/buildSrc/src/main/groovy/org/springframework/restdocs/build/SamplesPlugin.groovy +++ b/buildSrc/src/main/groovy/org/springframework/restdocs/build/samples/SamplesPlugin.groovy @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.restdocs.build +package org.springframework.restdocs.build.samples import org.gradle.api.Plugin import org.gradle.api.Project diff --git a/buildSrc/src/main/resources/META-INF/gradle-plugins/matrixtest.properties b/buildSrc/src/main/resources/META-INF/gradle-plugins/matrixtest.properties new file mode 100644 index 00000000..4af96aba --- /dev/null +++ b/buildSrc/src/main/resources/META-INF/gradle-plugins/matrixtest.properties @@ -0,0 +1 @@ +implementation-class: org.springframework.restdocs.build.matrix.MatrixTestPlugin \ No newline at end of file diff --git a/buildSrc/src/main/resources/META-INF/gradle-plugins/samples.properties b/buildSrc/src/main/resources/META-INF/gradle-plugins/samples.properties index ded5899c..062b9c79 100644 --- a/buildSrc/src/main/resources/META-INF/gradle-plugins/samples.properties +++ b/buildSrc/src/main/resources/META-INF/gradle-plugins/samples.properties @@ -1 +1 @@ -implementation-class: org.springframework.restdocs.build.SamplesPlugin \ No newline at end of file +implementation-class: org.springframework.restdocs.build.samples.SamplesPlugin \ No newline at end of file From a60b87990b5e4535c07f3e34d7bd8bc930a52d33 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 15 Jun 2018 16:00:25 +0100 Subject: [PATCH 2/2] Tolerate changes to request body reading in Framework 5.1 Previously, MockHttpServletRequest allowed its body to be ready multiple times using its InputStream. As of recent Spring Framework 5.1 snapshots, this is no longer the case. This commit moves to directly accessing the request's content as a byte array using getContentAsByteArray() which was introduced in Framework 5.0. Closes gh-515 --- spring-restdocs-core/build.gradle | 7 +++++++ spring-restdocs-mockmvc/build.gradle | 7 +++++++ .../restdocs/mockmvc/MockMvcRequestConverter.java | 6 +++--- spring-restdocs-webtestclient/build.gradle | 7 +++++++ 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/spring-restdocs-core/build.gradle b/spring-restdocs-core/build.gradle index 9f0977ae..47aa151b 100644 --- a/spring-restdocs-core/build.gradle +++ b/spring-restdocs-core/build.gradle @@ -61,4 +61,11 @@ artifacts { test { jvmArgs "-javaagent:${configurations.jacoco.asPath}=destfile=${buildDir}/jacoco.exec,includes=org.springframework.restdocs.*,excludes=org.springframework.restdocs.mustache.*" +} + +matrixTest { + springFramework { + group = 'org.springframework' + versions = ['5.1.0.BUILD-SNAPSHOT'] + } } \ No newline at end of file diff --git a/spring-restdocs-mockmvc/build.gradle b/spring-restdocs-mockmvc/build.gradle index 5baca9c2..49c0f19c 100644 --- a/spring-restdocs-mockmvc/build.gradle +++ b/spring-restdocs-mockmvc/build.gradle @@ -16,4 +16,11 @@ dependencies { test { jvmArgs "-javaagent:${configurations.jacoco.asPath}=destfile=${buildDir}/jacoco.exec,includes=org.springframework.restdocs.*" +} + +matrixTest { + springFramework { + group = 'org.springframework' + versions = ['5.1.0.BUILD-SNAPSHOT'] + } } \ No newline at end of file diff --git a/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/MockMvcRequestConverter.java b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/MockMvcRequestConverter.java index 45e0a9ba..bfaeaaff 100644 --- a/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/MockMvcRequestConverter.java +++ b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/MockMvcRequestConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2017 the original author or authors. + * Copyright 2014-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -81,8 +81,8 @@ class MockMvcRequestConverter implements RequestConverter