Keep track of original ProjectDescription

See gh-1023
This commit is contained in:
bono007
2019-10-21 21:56:30 -05:00
committed by Stephane Nicoll
parent abf301a7e4
commit 161fccbc75
16 changed files with 866 additions and 1 deletions

View File

@@ -28,6 +28,8 @@ import io.spring.initializr.generator.io.SimpleIndentStrategy;
import io.spring.initializr.generator.io.template.MustacheTemplateRenderer;
import io.spring.initializr.generator.io.template.TemplateRenderer;
import io.spring.initializr.generator.project.ProjectDirectoryFactory;
import io.spring.initializr.generator.project.diff.DefaultProjectDescriptionDiffFactory;
import io.spring.initializr.generator.project.diff.ProjectDescriptionDiffFactory;
import io.spring.initializr.metadata.DependencyMetadataProvider;
import io.spring.initializr.metadata.InitializrMetadata;
import io.spring.initializr.metadata.InitializrMetadataBuilder;
@@ -129,6 +131,12 @@ public class InitializrAutoConfiguration {
return new DefaultDependencyMetadataProvider();
}
@Bean
@ConditionalOnMissingBean
ProjectDescriptionDiffFactory projectDescriptionDiffFactory() {
return new DefaultProjectDescriptionDiffFactory();
}
/**
* Initializr web configuration.
*/

View File

@@ -17,6 +17,7 @@
package io.spring.initializr.web.autoconfigure;
import io.spring.initializr.generator.io.template.TemplateRenderer;
import io.spring.initializr.generator.project.diff.ProjectDescriptionDiffFactory;
import io.spring.initializr.metadata.DependencyMetadataProvider;
import io.spring.initializr.metadata.InitializrMetadataProvider;
import io.spring.initializr.web.controller.CommandLineMetadataController;
@@ -63,6 +64,11 @@ class InitializrAutoConfigurationTests {
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(TemplateRenderer.class));
}
@Test
void autoConfigRegistersDiffFactory() {
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(ProjectDescriptionDiffFactory.class));
}
@Test
void autoConfigWhenTemplateRendererBeanPresentDoesNotRegisterTemplateRenderer() {
this.contextRunner.withUserConfiguration(CustomTemplateRendererConfiguration.class).run((context) -> {

View File

@@ -44,6 +44,7 @@ class CustomProjectContributor implements ProjectContributor {
&& ((CustomProjectDescription) this.description).isCustomFlag()) {
Files.createFile(projectRoot.resolve("custom.txt"));
}
}
}

View File

@@ -28,6 +28,20 @@ class CustomProjectDescription extends MutableProjectDescription {
private boolean customFlag;
CustomProjectDescription() {
super();
}
CustomProjectDescription(final CustomProjectDescription source) {
super(source);
setCustomFlag(source.isCustomFlag());
}
@Override
public CustomProjectDescription createCopy() {
return new CustomProjectDescription(this);
}
boolean isCustomFlag() {
return this.customFlag;
}

View File

@@ -0,0 +1,48 @@
/*
* 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.web.controller.custom;
import java.util.function.BiConsumer;
import io.spring.initializr.generator.project.diff.ProjectDescriptionDiff;
/**
* Extends the base {@link ProjectDescriptionDiff} to provide convenient diff methods on
* {@link CustomProjectDescription}.
*
* @author cbono
*/
class CustomProjectDescriptionDiff extends ProjectDescriptionDiff {
CustomProjectDescriptionDiff(final CustomProjectDescription original) {
super(original);
}
/**
* Optionally calls the specified consumer if the {@code customFlag} is different on
* the original source description and the specified current description.
* @param current the description to test against
* @param consumer to call if the property has changed
*/
void ifCUstomFlagChanged(final CustomProjectDescription current, final BiConsumer<Boolean, Boolean> consumer) {
final CustomProjectDescription original = (CustomProjectDescription) super.getOriginal();
if (original.isCustomFlag() != current.isCustomFlag()) {
consumer.accept(original.isCustomFlag(), current.isCustomFlag());
}
}
}

View File

@@ -0,0 +1,35 @@
/*
* 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.web.controller.custom;
import io.spring.initializr.generator.project.diff.ProjectDescriptionDiff;
import io.spring.initializr.generator.project.diff.ProjectDescriptionDiffFactory;
/**
* A custom {@link ProjectDescriptionDiffFactory} implementation that creates custom
* {@link CustomProjectDescriptionDiff} instances.
*
* @author cbono
*/
public class CustomProjectDescriptionDiffFactory implements ProjectDescriptionDiffFactory<CustomProjectDescription> {
@Override
public ProjectDescriptionDiff create(final CustomProjectDescription description) {
return new CustomProjectDescriptionDiff(description);
}
}

View File

@@ -0,0 +1,81 @@
/*
* 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.web.controller.custom;
import io.spring.initializr.generator.buildsystem.BuildSystem;
import io.spring.initializr.generator.buildsystem.maven.MavenBuildSystem;
import io.spring.initializr.generator.language.Language;
import io.spring.initializr.generator.language.java.JavaLanguage;
import io.spring.initializr.generator.packaging.Packaging;
import io.spring.initializr.generator.version.Version;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
/**
* Simple sanity test around the custom diff extension model.
*/
class CustomProjectDescriptionDiffTest {
@Test
void sanityCheck() {
CustomProjectDescription description = customProjectDescription();
CustomProjectDescriptionDiffFactory diffFactory = new CustomProjectDescriptionDiffFactory();
CustomProjectDescriptionDiff diff = (CustomProjectDescriptionDiff) diffFactory.create(description);
// copied
assertThat(diff.getOriginal()).usingRecursiveComparison().isEqualTo(description);
assertThat(diff.getOriginal()).isNotSameAs(description);
// no changes
diff.ifCUstomFlagChanged(description, (v1, v2) -> fail("Values should not have changed"));
// changes
boolean originalValue = description.isCustomFlag();
description.setCustomFlag(!originalValue);
// TODO could use the CallTrackingBiConsumer that I used in initializr-generator
// tests but then where to put it?
final boolean[] called = { false };
diff.ifCUstomFlagChanged(description, (prev, curr) -> {
assertThat(prev).isEqualTo(originalValue);
assertThat(curr).isEqualTo(description.isCustomFlag());
called[0] = true;
});
assertThat(called[0]).isTrue();
}
private CustomProjectDescription customProjectDescription() {
CustomProjectDescription description = new CustomProjectDescription();
description.setBuildSystem(BuildSystem.forId(MavenBuildSystem.ID));
description.setLanguage(Language.forId(JavaLanguage.ID, "11"));
description.setPlatformVersion(Version.parse("2.2.0.RELEASE"));
description.setGroupId("com.example");
description.setArtifactId("demo");
description.setName("demo");
description.setVersion("0.0.8");
description.setApplicationName("DemoApplication");
description.setPackageName("com.example.demo");
description.setPackaging(Packaging.forId("jar"));
description.setBaseDirectory(".");
description.setCustomFlag(true);
return description;
}
}

View File

@@ -79,6 +79,11 @@ public class ProjectGenerationControllerCustomRequestIntegrationTests
return new CustomProjectGenerationController(metadataProvider, projectGenerationInvoker);
}
@Bean
CustomProjectDescriptionDiffFactory customProjectDescriptionDiffFactory() {
return new CustomProjectDescriptionDiffFactory();
}
}
static class CustomProjectRequestToDescriptionConverter