Skip to content

Commit 5d7c3b7

Browse files
committed
Move the multimodule harnessing to the only place where it is used.
1 parent 8b73372 commit 5d7c3b7

File tree

2 files changed

+99
-93
lines changed

2 files changed

+99
-93
lines changed

plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java

Lines changed: 2 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
package com.diffplug.spotless.maven;
1717

1818
import static com.diffplug.common.base.Strings.isNullOrEmpty;
19-
import static java.util.Arrays.asList;
2019
import static java.util.Arrays.stream;
21-
import static java.util.Collections.emptyMap;
2220
import static java.util.Collections.singletonMap;
2321
import static java.util.stream.Collectors.toList;
2422
import static org.junit.jupiter.api.Assertions.fail;
@@ -59,7 +57,6 @@ public class MavenIntegrationHarness extends ResourceHarness {
5957
private static final String MODULES = "modules";
6058
private static final String DEPENDENCIES = "dependencies";
6159
private static final String MODULE_NAME = "name";
62-
private static final String CHILD_ID = "childId";
6360
private static final int REMOTE_DEBUG_PORT = 5005;
6461

6562
private final MustacheFactory mustacheFactory = new DefaultMustacheFactory();
@@ -190,10 +187,6 @@ protected MavenRunner mavenRunnerWithRemoteDebug() throws IOException {
190187
return mavenRunner().withRemoteDebug(REMOTE_DEBUG_PORT);
191188
}
192189

193-
protected MultiModuleProjectCreator multiModuleProject() {
194-
return new MultiModuleProjectCreator();
195-
}
196-
197190
protected String createPomXmlContent(String pluginVersion, String[] executions, String[] configuration, String[] dependencies) throws IOException {
198191
return createPomXmlContent("/pom-test.xml.mustache", pluginVersion, executions, configuration, dependencies);
199192
}
@@ -207,7 +200,7 @@ protected String createPomXmlContent(String pluginVersion, String[] executions,
207200
return createPomXmlContent(pluginVersion, executions, configuration, null);
208201
}
209202

210-
private String createPomXmlContent(String pomTemplate, Map<String, Object> params) throws IOException {
203+
protected String createPomXmlContent(String pomTemplate, Map<String, Object> params) throws IOException {
211204
URL url = MavenIntegrationHarness.class.getResource(pomTemplate);
212205
try (BufferedReader reader = Resources.asCharSource(url, StandardCharsets.UTF_8).openBufferedStream()) {
213206
Mustache mustache = mustacheFactory.compile(reader, "pom");
@@ -217,7 +210,7 @@ private String createPomXmlContent(String pomTemplate, Map<String, Object> param
217210
}
218211
}
219212

220-
private static Map<String, Object> buildPomXmlParams(String pluginVersion, String[] executions, String[] configuration, String[] modules, String[] dependencies) {
213+
protected static Map<String, Object> buildPomXmlParams(String pluginVersion, String[] executions, String[] configuration, String[] modules, String[] dependencies) {
221214
Map<String, Object> params = new HashMap<>();
222215
params.put(SPOTLESS_MAVEN_PLUGIN_VERSION, pluginVersion == null ? getSystemProperty(SPOTLESS_MAVEN_PLUGIN_VERSION) : pluginVersion);
223216

@@ -276,86 +269,4 @@ private static String[] including(String... includes) {
276269
private static String[] formats(String... formats) {
277270
return groupWithSteps("formats", formats);
278271
}
279-
280-
protected class MultiModuleProjectCreator {
281-
282-
private String configSubProject;
283-
private SubProjectFile[] configSubProjectFiles;
284-
private String[] configuration;
285-
private final Map<String, List<SubProjectFile>> subProjects = new LinkedHashMap<>();
286-
287-
protected MultiModuleProjectCreator withConfigSubProject(String name, SubProjectFile... files) {
288-
configSubProject = name;
289-
configSubProjectFiles = files;
290-
return this;
291-
}
292-
293-
protected MultiModuleProjectCreator withConfiguration(String... lines) {
294-
configuration = lines;
295-
return this;
296-
}
297-
298-
protected MultiModuleProjectCreator addSubProject(String name, SubProjectFile... files) {
299-
subProjects.put(name, asList(files));
300-
return this;
301-
}
302-
303-
protected void create() throws IOException {
304-
createRootPom();
305-
createConfigSubProject();
306-
createSubProjects();
307-
}
308-
309-
private void createRootPom() throws IOException {
310-
List<Object> modulesList = new ArrayList<>();
311-
modulesList.add(configSubProject);
312-
modulesList.addAll(subProjects.keySet());
313-
String[] modules = modulesList.toArray(new String[0]);
314-
315-
Map<String, Object> rootPomParams = buildPomXmlParams(null, null, configuration, modules, null);
316-
setFile("pom.xml").toContent(createPomXmlContent("/multi-module/pom-parent.xml.mustache", rootPomParams));
317-
}
318-
319-
private void createConfigSubProject() throws IOException {
320-
if (configSubProject != null) {
321-
String content = createPomXmlContent("/multi-module/pom-config.xml.mustache", emptyMap());
322-
setFile(configSubProject + "/pom.xml").toContent(content);
323-
324-
createSubProjectFiles(configSubProject, asList(configSubProjectFiles));
325-
}
326-
}
327-
328-
private void createSubProjects() throws IOException {
329-
for (Map.Entry<String, List<SubProjectFile>> entry : subProjects.entrySet()) {
330-
String subProjectName = entry.getKey();
331-
List<SubProjectFile> subProjectFiles = entry.getValue();
332-
333-
String content = createPomXmlContent("/multi-module/pom-child.xml.mustache", singletonMap(CHILD_ID, subProjectName));
334-
setFile(subProjectName + "/pom.xml").toContent(content);
335-
336-
createSubProjectFiles(subProjectName, subProjectFiles);
337-
}
338-
}
339-
340-
private void createSubProjectFiles(String subProjectName, List<SubProjectFile> subProjectFiles) throws IOException {
341-
for (SubProjectFile file : subProjectFiles) {
342-
setFile(subProjectName + '/' + file.to).toResource(file.from);
343-
}
344-
}
345-
}
346-
347-
protected static class SubProjectFile {
348-
349-
private final String from;
350-
private final String to;
351-
352-
private SubProjectFile(String from, String to) {
353-
this.from = from;
354-
this.to = to;
355-
}
356-
357-
protected static SubProjectFile file(String from, String to) {
358-
return new SubProjectFile(from, to);
359-
}
360-
}
361272
}

plugin-maven/src/test/java/com/diffplug/spotless/maven/MultiModuleProjectTest.java

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 DiffPlug
2+
* Copyright 2016-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,7 +15,15 @@
1515
*/
1616
package com.diffplug.spotless.maven;
1717

18-
import static com.diffplug.spotless.maven.MavenIntegrationHarness.SubProjectFile.file;
18+
import static java.util.Arrays.asList;
19+
import static java.util.Collections.emptyMap;
20+
import static java.util.Collections.singletonMap;
21+
22+
import java.io.IOException;
23+
import java.util.ArrayList;
24+
import java.util.LinkedHashMap;
25+
import java.util.List;
26+
import java.util.Map;
1927

2028
import org.junit.jupiter.api.Test;
2129

@@ -96,4 +104,91 @@ void testConfigurationDependency() throws Exception {
96104
assertFile("three/src/main/scala/test1.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf_3.0.0");
97105
assertFile("three/src/test/scala/test2.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf_3.0.0");
98106
}
107+
108+
private static final String CHILD_ID = "childId";
109+
110+
protected MultiModuleProjectCreator multiModuleProject() {
111+
return new MultiModuleProjectCreator();
112+
}
113+
114+
class MultiModuleProjectCreator {
115+
private String configSubProject;
116+
private SubProjectFile[] configSubProjectFiles;
117+
private String[] configuration;
118+
private final Map<String, List<SubProjectFile>> subProjects = new LinkedHashMap<>();
119+
120+
protected MultiModuleProjectCreator withConfigSubProject(String name, SubProjectFile... files) {
121+
configSubProject = name;
122+
configSubProjectFiles = files;
123+
return this;
124+
}
125+
126+
protected MultiModuleProjectCreator withConfiguration(String... lines) {
127+
configuration = lines;
128+
return this;
129+
}
130+
131+
protected MultiModuleProjectCreator addSubProject(String name, SubProjectFile... files) {
132+
subProjects.put(name, asList(files));
133+
return this;
134+
}
135+
136+
protected void create() throws IOException {
137+
createRootPom();
138+
createConfigSubProject();
139+
createSubProjects();
140+
}
141+
142+
private void createRootPom() throws IOException {
143+
List<String> modulesList = new ArrayList<>();
144+
modulesList.add(configSubProject);
145+
modulesList.addAll(subProjects.keySet());
146+
String[] modules = modulesList.toArray(new String[0]);
147+
148+
Map<String, Object> rootPomParams = buildPomXmlParams(null, null, configuration, modules, null);
149+
setFile("pom.xml").toContent(createPomXmlContent("/multi-module/pom-parent.xml.mustache", rootPomParams));
150+
}
151+
152+
private void createConfigSubProject() throws IOException {
153+
if (configSubProject != null) {
154+
String content = createPomXmlContent("/multi-module/pom-config.xml.mustache", emptyMap());
155+
setFile(configSubProject + "/pom.xml").toContent(content);
156+
157+
createSubProjectFiles(configSubProject, asList(configSubProjectFiles));
158+
}
159+
}
160+
161+
private void createSubProjects() throws IOException {
162+
for (Map.Entry<String, List<SubProjectFile>> entry : subProjects.entrySet()) {
163+
String subProjectName = entry.getKey();
164+
List<SubProjectFile> subProjectFiles = entry.getValue();
165+
166+
String content = createPomXmlContent("/multi-module/pom-child.xml.mustache", singletonMap(CHILD_ID, subProjectName));
167+
setFile(subProjectName + "/pom.xml").toContent(content);
168+
169+
createSubProjectFiles(subProjectName, subProjectFiles);
170+
}
171+
}
172+
173+
private void createSubProjectFiles(String subProjectName, List<SubProjectFile> subProjectFiles) {
174+
for (SubProjectFile file : subProjectFiles) {
175+
setFile(subProjectName + '/' + file.to).toResource(file.from);
176+
}
177+
}
178+
}
179+
180+
static class SubProjectFile {
181+
182+
private final String from;
183+
private final String to;
184+
185+
private SubProjectFile(String from, String to) {
186+
this.from = from;
187+
this.to = to;
188+
}
189+
}
190+
191+
static SubProjectFile file(String from, String to) {
192+
return new SubProjectFile(from, to);
193+
}
99194
}

0 commit comments

Comments
 (0)