mirror of
https://gitee.com/dcren/initializr.git
synced 2025-09-18 17:48:14 +08:00
Make DefaultProjectAssertGenerator more flexible
This commits adds a public constructor to take a ProjectDirectoryFactory implementation to make it more obvious it is a required component. In the absence of a configured factory, one is retrieved from the context as before.
This commit is contained in:
@@ -25,18 +25,37 @@ import java.util.stream.Collectors;
|
||||
import io.spring.initializr.generator.project.contributor.ProjectContributor;
|
||||
|
||||
/**
|
||||
* The default {@link ProjectAssetGenerator}. Generates a directory structure with all
|
||||
* available {@link ProjectContributor project contributors}.
|
||||
* A default {@link ProjectAssetGenerator} implementation that generates a directory
|
||||
* structure with all available {@link ProjectContributor project contributors}. Uses a
|
||||
* {@link ProjectDirectoryFactory} to determine the root directory to use based on a
|
||||
* {@link ResolvedProjectDescription}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class DefaultProjectAssetGenerator implements ProjectAssetGenerator<Path> {
|
||||
|
||||
private final ProjectDirectoryFactory projectDirectoryFactory;
|
||||
|
||||
/**
|
||||
* Create a new instance with the {@link ProjectDirectoryFactory} to use.
|
||||
* @param projectDirectoryFactory the project directory factory to use
|
||||
*/
|
||||
public DefaultProjectAssetGenerator(ProjectDirectoryFactory projectDirectoryFactory) {
|
||||
this.projectDirectoryFactory = projectDirectoryFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance without an explicit {@link ProjectDirectoryFactory}. A bean
|
||||
* of that type is expected to be available in the context.
|
||||
*/
|
||||
public DefaultProjectAssetGenerator() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Path generate(ProjectGenerationContext context) throws IOException {
|
||||
ResolvedProjectDescription resolvedProjectDescription = context.getBean(ResolvedProjectDescription.class);
|
||||
Path projectRoot = context.getBean(ProjectDirectoryFactory.class)
|
||||
.createProjectDirectory(resolvedProjectDescription);
|
||||
Path projectRoot = resolveProjectDirectoryFactory(context).createProjectDirectory(resolvedProjectDescription);
|
||||
Path projectDirectory = initializerProjectDirectory(projectRoot, resolvedProjectDescription);
|
||||
List<ProjectContributor> contributors = context.getBeanProvider(ProjectContributor.class).orderedStream()
|
||||
.collect(Collectors.toList());
|
||||
@@ -46,6 +65,11 @@ public class DefaultProjectAssetGenerator implements ProjectAssetGenerator<Path>
|
||||
return projectRoot;
|
||||
}
|
||||
|
||||
private ProjectDirectoryFactory resolveProjectDirectoryFactory(ProjectGenerationContext context) {
|
||||
return (this.projectDirectoryFactory != null) ? this.projectDirectoryFactory
|
||||
: context.getBean(ProjectDirectoryFactory.class);
|
||||
}
|
||||
|
||||
private Path initializerProjectDirectory(Path rootDir, ResolvedProjectDescription description) throws IOException {
|
||||
Path projectDirectory = resolveProjectDirectory(rootDir, description);
|
||||
Files.createDirectories(projectDirectory);
|
||||
|
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.spring.initializr.generator.project;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link DefaultProjectAssetGenerator}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class DefaultProjectAssetGeneratorTests {
|
||||
|
||||
@Test
|
||||
void generationWithExplicitFactoryDoesNotLookupBean(@TempDir Path tempDir) throws IOException {
|
||||
ResolvedProjectDescription description = new ResolvedProjectDescription(new ProjectDescription());
|
||||
ProjectDirectoryFactory factory = mock(ProjectDirectoryFactory.class);
|
||||
Path expected = tempDir.resolve("does-not-exist");
|
||||
assertThat(expected).doesNotExist();
|
||||
given(factory.createProjectDirectory(description)).willReturn(expected);
|
||||
try (ProjectGenerationContext context = new ProjectGenerationContext()) {
|
||||
context.registerBean(ResolvedProjectDescription.class, () -> description);
|
||||
context.refresh();
|
||||
Path rootDir = new DefaultProjectAssetGenerator(factory).generate(context);
|
||||
assertThat(rootDir).isSameAs(expected);
|
||||
assertThat(expected).exists().isDirectory();
|
||||
verify(factory).createProjectDirectory(description);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void generationWithoutExplicitFactoryLookupsBean(@TempDir Path tempDir) throws IOException {
|
||||
ResolvedProjectDescription description = new ResolvedProjectDescription(new ProjectDescription());
|
||||
ProjectDirectoryFactory factory = mock(ProjectDirectoryFactory.class);
|
||||
Path expected = tempDir.resolve("does-not-exist");
|
||||
assertThat(expected).doesNotExist();
|
||||
given(factory.createProjectDirectory(description)).willReturn(expected);
|
||||
try (ProjectGenerationContext context = new ProjectGenerationContext()) {
|
||||
context.registerBean(ResolvedProjectDescription.class, () -> description);
|
||||
context.registerBean(ProjectDirectoryFactory.class, () -> factory);
|
||||
context.refresh();
|
||||
Path rootDir = new DefaultProjectAssetGenerator().generate(context);
|
||||
assertThat(rootDir).isSameAs(expected);
|
||||
assertThat(expected).exists().isDirectory();
|
||||
verify(factory).createProjectDirectory(description);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void generationWithoutExplicitFactoryFailIfBeanIsNotPresent() {
|
||||
ResolvedProjectDescription description = new ResolvedProjectDescription(new ProjectDescription());
|
||||
assertThatThrownBy(() -> {
|
||||
try (ProjectGenerationContext context = new ProjectGenerationContext()) {
|
||||
context.registerBean(ResolvedProjectDescription.class, () -> description);
|
||||
context.refresh();
|
||||
new DefaultProjectAssetGenerator().generate(context);
|
||||
}
|
||||
}).isInstanceOf(NoSuchBeanDefinitionException.class)
|
||||
.hasMessageContaining(ProjectDirectoryFactory.class.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void generationWithBaseDirCreatesBaseDirStructure(@TempDir Path tempDir) throws IOException {
|
||||
ProjectDescription projectDescription = new ProjectDescription();
|
||||
projectDescription.setBaseDirectory("my-project");
|
||||
ResolvedProjectDescription description = new ResolvedProjectDescription(projectDescription);
|
||||
ProjectDirectoryFactory factory = mock(ProjectDirectoryFactory.class);
|
||||
Path expected = tempDir.resolve("does-not-exist");
|
||||
assertThat(expected).doesNotExist();
|
||||
given(factory.createProjectDirectory(description)).willReturn(expected);
|
||||
try (ProjectGenerationContext context = new ProjectGenerationContext()) {
|
||||
context.registerBean(ResolvedProjectDescription.class, () -> description);
|
||||
context.refresh();
|
||||
Path rootDir = new DefaultProjectAssetGenerator(factory).generate(context);
|
||||
assertThat(rootDir).isSameAs(expected);
|
||||
assertThat(expected).exists().isDirectory();
|
||||
assertThat(rootDir.resolve("my-project")).exists().isDirectory();
|
||||
verify(factory).createProjectDirectory(description);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user