|
| 1 | +/* |
| 2 | + * Copyright 2012-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.build.test; |
| 18 | + |
| 19 | +import org.gradle.api.Plugin; |
| 20 | +import org.gradle.api.Project; |
| 21 | +import org.gradle.api.plugins.JavaPlugin; |
| 22 | +import org.gradle.api.plugins.JavaPluginConvention; |
| 23 | +import org.gradle.api.tasks.SourceSet; |
| 24 | +import org.gradle.api.tasks.SourceSetContainer; |
| 25 | +import org.gradle.api.tasks.testing.Test; |
| 26 | +import org.gradle.language.base.plugins.LifecycleBasePlugin; |
| 27 | +import org.gradle.plugins.ide.eclipse.EclipsePlugin; |
| 28 | +import org.gradle.plugins.ide.eclipse.model.EclipseModel; |
| 29 | + |
| 30 | +/** |
| 31 | + * A {@link Plugin} to configure system testing support in a {@link Project}. |
| 32 | + * |
| 33 | + * @author Andy Wilkinson |
| 34 | + * @author Scott Frederick |
| 35 | + */ |
| 36 | +public class SystemTestPlugin implements Plugin<Project> { |
| 37 | + |
| 38 | + /** |
| 39 | + * Name of the {@code systemTest} task. |
| 40 | + */ |
| 41 | + public static String SYSTEM_TEST_TASK_NAME = "systemTest"; |
| 42 | + |
| 43 | + /** |
| 44 | + * Name of the {@code systemTest} source set. |
| 45 | + */ |
| 46 | + public static String SYSTEM_TEST_SOURCE_SET_NAME = "systemTest"; |
| 47 | + |
| 48 | + @Override |
| 49 | + public void apply(Project project) { |
| 50 | + project.getPlugins().withType(JavaPlugin.class, (javaPlugin) -> configureSystemTesting(project)); |
| 51 | + } |
| 52 | + |
| 53 | + private void configureSystemTesting(Project project) { |
| 54 | + SourceSet systemTestSourceSet = createSourceSet(project); |
| 55 | + createTestTask(project, systemTestSourceSet); |
| 56 | + project.getPlugins().withType(EclipsePlugin.class, (eclipsePlugin) -> { |
| 57 | + EclipseModel eclipse = project.getExtensions().getByType(EclipseModel.class); |
| 58 | + eclipse.classpath((classpath) -> classpath.getPlusConfigurations().add( |
| 59 | + project.getConfigurations().getByName(systemTestSourceSet.getRuntimeClasspathConfigurationName()))); |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + private SourceSet createSourceSet(Project project) { |
| 64 | + SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets(); |
| 65 | + SourceSet systemTestSourceSet = sourceSets.create(SYSTEM_TEST_SOURCE_SET_NAME); |
| 66 | + SourceSet mainSourceSet = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME); |
| 67 | + systemTestSourceSet |
| 68 | + .setCompileClasspath(systemTestSourceSet.getCompileClasspath().plus(mainSourceSet.getOutput())); |
| 69 | + systemTestSourceSet |
| 70 | + .setRuntimeClasspath(systemTestSourceSet.getRuntimeClasspath().plus(mainSourceSet.getOutput())); |
| 71 | + return systemTestSourceSet; |
| 72 | + } |
| 73 | + |
| 74 | + private void createTestTask(Project project, SourceSet systemTestSourceSet) { |
| 75 | + Test systemTest = project.getTasks().create(SYSTEM_TEST_TASK_NAME, Test.class); |
| 76 | + systemTest.setGroup(LifecycleBasePlugin.VERIFICATION_GROUP); |
| 77 | + systemTest.setDescription("Runs system tests."); |
| 78 | + systemTest.setTestClassesDirs(systemTestSourceSet.getOutput().getClassesDirs()); |
| 79 | + systemTest.setClasspath(systemTestSourceSet.getRuntimeClasspath()); |
| 80 | + systemTest.shouldRunAfter(JavaPlugin.TEST_TASK_NAME); |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments