|
| 1 | +/* |
| 2 | + * Copyright 2002-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * 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, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.spring.gradle.convention; |
| 18 | + |
| 19 | +import io.spring.gradle.IncludeRepoTask; |
| 20 | +import org.apache.commons.io.FileUtils; |
| 21 | +import org.gradle.api.Project; |
| 22 | +import org.gradle.api.tasks.GradleBuild; |
| 23 | +import org.gradle.testfixtures.ProjectBuilder; |
| 24 | +import org.junit.jupiter.api.AfterEach; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +import java.util.Arrays; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | + |
| 31 | +class IncludeCheckRemotePluginTest { |
| 32 | + |
| 33 | + Project rootProject; |
| 34 | + |
| 35 | + @AfterEach |
| 36 | + public void cleanup() throws Exception { |
| 37 | + if (rootProject != null) { |
| 38 | + FileUtils.deleteDirectory(rootProject.getProjectDir()); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void applyWhenExtensionPropertiesNoTasksThenCreateCheckRemoteTaskWithDefaultTask() { |
| 44 | + this.rootProject = ProjectBuilder.builder().build(); |
| 45 | + this.rootProject.getPluginManager().apply(IncludeCheckRemotePlugin.class); |
| 46 | + this.rootProject.getExtensions().configure(IncludeCheckRemotePlugin.IncludeCheckRemoteExtension.class, |
| 47 | + (includeCheckRemoteExtension) -> { |
| 48 | + includeCheckRemoteExtension.setProperty("repository", "my-project/my-repository"); |
| 49 | + includeCheckRemoteExtension.setProperty("ref", "main"); |
| 50 | + }); |
| 51 | + |
| 52 | + GradleBuild checkRemote = (GradleBuild) this.rootProject.getTasks().named("checkRemote").get(); |
| 53 | + assertThat(checkRemote.getTasks()).containsExactly("check"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void applyWhenExtensionPropertiesTasksThenCreateCheckRemoteWithProvidedTasks() { |
| 58 | + this.rootProject = ProjectBuilder.builder().build(); |
| 59 | + this.rootProject.getPluginManager().apply(IncludeCheckRemotePlugin.class); |
| 60 | + this.rootProject.getExtensions().configure(IncludeCheckRemotePlugin.IncludeCheckRemoteExtension.class, |
| 61 | + (includeCheckRemoteExtension) -> { |
| 62 | + includeCheckRemoteExtension.setProperty("repository", "my-project/my-repository"); |
| 63 | + includeCheckRemoteExtension.setProperty("ref", "main"); |
| 64 | + includeCheckRemoteExtension.setProperty("tasks", Arrays.asList("clean", "build", "test")); |
| 65 | + }); |
| 66 | + |
| 67 | + GradleBuild checkRemote = (GradleBuild) this.rootProject.getTasks().named("checkRemote").get(); |
| 68 | + assertThat(checkRemote.getTasks()).containsExactly("clean", "build", "test"); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void applyWhenExtensionPropertiesThenRegisterIncludeRepoTaskWithExtensionProperties() { |
| 73 | + this.rootProject = ProjectBuilder.builder().build(); |
| 74 | + this.rootProject.getPluginManager().apply(IncludeCheckRemotePlugin.class); |
| 75 | + this.rootProject.getExtensions().configure(IncludeCheckRemotePlugin.IncludeCheckRemoteExtension.class, |
| 76 | + (includeCheckRemoteExtension) -> { |
| 77 | + includeCheckRemoteExtension.setProperty("repository", "my-project/my-repository"); |
| 78 | + includeCheckRemoteExtension.setProperty("ref", "main"); |
| 79 | + }); |
| 80 | + |
| 81 | + IncludeRepoTask includeRepo = (IncludeRepoTask) this.rootProject.getTasks().named("includeRepo").get(); |
| 82 | + assertThat(includeRepo).isNotNull(); |
| 83 | + assertThat(includeRepo.getRepository().get()).isEqualTo("my-project/my-repository"); |
| 84 | + assertThat(includeRepo.getRef().get()).isEqualTo("main"); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void applyWhenRegisterTasksThenCheckRemoteDirSameAsIncludeRepoOutputDir() { |
| 89 | + this.rootProject = ProjectBuilder.builder().build(); |
| 90 | + this.rootProject.getPluginManager().apply(IncludeCheckRemotePlugin.class); |
| 91 | + this.rootProject.getExtensions().configure(IncludeCheckRemotePlugin.IncludeCheckRemoteExtension.class, |
| 92 | + (includeCheckRemoteExtension) -> { |
| 93 | + includeCheckRemoteExtension.setProperty("repository", "my-project/my-repository"); |
| 94 | + includeCheckRemoteExtension.setProperty("ref", "main"); |
| 95 | + }); |
| 96 | + IncludeRepoTask includeRepo = (IncludeRepoTask) this.rootProject.getTasks().named("includeRepo").get(); |
| 97 | + GradleBuild checkRemote = (GradleBuild) this.rootProject.getTasks().named("checkRemote").get(); |
| 98 | + assertThat(checkRemote.getDir()).isEqualTo(includeRepo.getOutputDirectory()); |
| 99 | + } |
| 100 | +} |
0 commit comments