|
| 1 | +/* |
| 2 | + * Copyright 2012-present 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.autoconfigure; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.FileOutputStream; |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Properties; |
| 24 | +import java.util.function.Consumer; |
| 25 | +import java.util.stream.Stream; |
| 26 | + |
| 27 | +import org.gradle.api.Project; |
| 28 | +import org.gradle.api.file.ConfigurableFileCollection; |
| 29 | +import org.gradle.testfixtures.ProjectBuilder; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | +import org.junit.jupiter.api.io.TempDir; |
| 32 | + |
| 33 | +import static org.assertj.core.api.Assertions.assertThat; |
| 34 | + |
| 35 | +/** |
| 36 | + * Tests for {@link DocumentAutoConfigurationClasses}. |
| 37 | + * |
| 38 | + * @author Andy Wilkinson |
| 39 | + */ |
| 40 | +class DocumentAutoConfigurationClassesTests { |
| 41 | + |
| 42 | + @TempDir |
| 43 | + private File temp; |
| 44 | + |
| 45 | + @Test |
| 46 | + void classesAreDocumented() throws IOException { |
| 47 | + File output = documentAutoConfigurationClasses((metadataDir) -> { |
| 48 | + writeAutoConfigurationMetadata("spring-boot-one", List.of("org.springframework.boot.one.AAutoConfiguration", |
| 49 | + "org.springframework.boot.one.BAutoConfiguration"), metadataDir); |
| 50 | + writeAutoConfigurationMetadata("spring-boot-two", List.of("org.springframework.boot.two.CAutoConfiguration", |
| 51 | + "org.springframework.boot.two.DAutoConfiguration"), metadataDir); |
| 52 | + }); |
| 53 | + assertThat(output).isNotEmptyDirectory(); |
| 54 | + assertThat(output.listFiles()).extracting(File::getName) |
| 55 | + .containsExactlyInAnyOrder("spring-boot-one.adoc", "spring-boot-two.adoc", "nav.adoc"); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void whenMetadataIsRemovedThenOutputForThatMetadataIsNoLongerPresent() throws IOException { |
| 60 | + documentAutoConfigurationClasses((metadataDir) -> { |
| 61 | + writeAutoConfigurationMetadata("spring-boot-one", List.of("org.springframework.boot.one.AAutoConfiguration", |
| 62 | + "org.springframework.boot.one.BAutoConfiguration"), metadataDir); |
| 63 | + writeAutoConfigurationMetadata("spring-boot-two", List.of("org.springframework.boot.two.CAutoConfiguration", |
| 64 | + "org.springframework.boot.two.DAutoConfiguration"), metadataDir); |
| 65 | + }); |
| 66 | + File output = documentAutoConfigurationClasses( |
| 67 | + (metadataDir) -> assertThat(new File(metadataDir, "spring-boot-two.properties").delete()).isTrue()); |
| 68 | + assertThat(output).isNotEmptyDirectory(); |
| 69 | + assertThat(output.listFiles()).extracting(File::getName) |
| 70 | + .containsExactlyInAnyOrder("spring-boot-one.adoc", "nav.adoc"); |
| 71 | + } |
| 72 | + |
| 73 | + private File documentAutoConfigurationClasses(Consumer<File> metadataDir) throws IOException { |
| 74 | + Project project = ProjectBuilder.builder().build(); |
| 75 | + DocumentAutoConfigurationClasses task = project.getTasks() |
| 76 | + .register("documentAutoConfigurationClasses", DocumentAutoConfigurationClasses.class) |
| 77 | + .get(); |
| 78 | + File output = new File(this.temp, "output"); |
| 79 | + File input = new File(this.temp, "input"); |
| 80 | + input.mkdirs(); |
| 81 | + metadataDir.accept(input); |
| 82 | + ConfigurableFileCollection autoConfiguration = project.files(); |
| 83 | + Stream.of(input.listFiles()).forEach(autoConfiguration::from); |
| 84 | + task.getOutputDir().set(output); |
| 85 | + task.setAutoConfiguration(autoConfiguration); |
| 86 | + task.documentAutoConfigurationClasses(); |
| 87 | + return output; |
| 88 | + } |
| 89 | + |
| 90 | + private void writeAutoConfigurationMetadata(String module, List<String> classes, File outputDir) { |
| 91 | + File metadata = new File(outputDir, module + ".properties"); |
| 92 | + Properties properties = new Properties(); |
| 93 | + properties.setProperty("autoConfigurationClassNames", String.join(",", classes)); |
| 94 | + properties.setProperty("module", module); |
| 95 | + try (FileOutputStream out = new FileOutputStream(metadata)) { |
| 96 | + properties.store(out, null); |
| 97 | + } |
| 98 | + catch (IOException ex) { |
| 99 | + throw new java.io.UncheckedIOException(ex); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments