|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2021 the original author or authors. |
| 2 | + * Copyright 2012-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
21 | 21 | import java.io.FileWriter;
|
22 | 22 | import java.io.IOException;
|
23 | 23 | import java.io.Reader;
|
| 24 | +import java.io.UncheckedIOException; |
24 | 25 | import java.net.MalformedURLException;
|
25 | 26 | import java.net.URL;
|
26 | 27 | import java.net.URLClassLoader;
|
27 | 28 | import java.nio.file.Files;
|
28 | 29 | import java.nio.file.Path;
|
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.List; |
29 | 32 | import java.util.Properties;
|
30 | 33 | import java.util.SortedSet;
|
31 | 34 | import java.util.TreeSet;
|
@@ -102,13 +105,54 @@ private Properties readTestSlices() throws IOException {
|
102 | 105 | MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(classLoader);
|
103 | 106 | Properties springFactories = readSpringFactories(
|
104 | 107 | new File(this.sourceSet.getOutput().getResourcesDir(), "META-INF/spring.factories"));
|
| 108 | + readTestSlicesDirectory(springFactories, |
| 109 | + new File(this.sourceSet.getOutput().getResourcesDir(), "META-INF/spring-boot/")); |
105 | 110 | for (File classesDir : this.sourceSet.getOutput().getClassesDirs()) {
|
106 | 111 | addTestSlices(testSlices, classesDir, metadataReaderFactory, springFactories);
|
107 | 112 | }
|
108 | 113 | }
|
109 | 114 | return testSlices;
|
110 | 115 | }
|
111 | 116 |
|
| 117 | + /** |
| 118 | + * Reads files from the given directory and puts them in springFactories. The key is |
| 119 | + * the file name, the value is the file contents, split by line, delimited with comma. |
| 120 | + * |
| 121 | + * This is done to mimic the spring.factories structure. |
| 122 | + * @param springFactories spring.factories parsed as properties |
| 123 | + * @param directory directory to scan |
| 124 | + */ |
| 125 | + private void readTestSlicesDirectory(Properties springFactories, File directory) { |
| 126 | + File[] files = directory.listFiles(); |
| 127 | + if (files == null) { |
| 128 | + return; |
| 129 | + } |
| 130 | + for (File file : files) { |
| 131 | + try { |
| 132 | + List<String> lines = removeComments(Files.readAllLines(file.toPath())); |
| 133 | + springFactories.setProperty(file.getName(), StringUtils.collectionToCommaDelimitedString(lines)); |
| 134 | + } |
| 135 | + catch (IOException ex) { |
| 136 | + throw new UncheckedIOException("Failed to read file " + file, ex); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private List<String> removeComments(List<String> lines) { |
| 142 | + List<String> result = new ArrayList<>(); |
| 143 | + for (String line : lines) { |
| 144 | + int commentIndex = line.indexOf('#'); |
| 145 | + if (commentIndex > -1) { |
| 146 | + line = line.substring(0, commentIndex); |
| 147 | + } |
| 148 | + line = line.trim(); |
| 149 | + if (!line.isEmpty()) { |
| 150 | + result.add(line); |
| 151 | + } |
| 152 | + } |
| 153 | + return result; |
| 154 | + } |
| 155 | + |
112 | 156 | private URL toURL(File file) {
|
113 | 157 | try {
|
114 | 158 | return file.toURI().toURL();
|
|
0 commit comments