mirror of
https://gitee.com/dcren/initializr.git
synced 2025-07-15 14:04:30 +08:00
Review TemplateRenderer abstraction
Closes gh-818
This commit is contained in:
parent
6e9f542560
commit
b6657211f6
@ -808,4 +808,7 @@ expiration settings accordingly.
|
||||
|`initializr.project-resources`
|
||||
|Cache resources that are used to generate projects.
|
||||
|
||||
|`initializr.templates`
|
||||
|Cache templates that are used to generate projects.
|
||||
|
||||
|===
|
||||
|
@ -0,0 +1,100 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* http://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.io.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.samskivert.mustache.Mustache;
|
||||
import com.samskivert.mustache.Mustache.Compiler;
|
||||
import com.samskivert.mustache.Mustache.TemplateLoader;
|
||||
import com.samskivert.mustache.Template;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.Cache.ValueRetrievalException;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
|
||||
/**
|
||||
* A {@link TemplateRenderer} using Mustache.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class MustacheTemplateRenderer implements TemplateRenderer {
|
||||
|
||||
private final Compiler mustache;
|
||||
|
||||
private final Function<String, String> keyGenerator;
|
||||
|
||||
private final Cache templateCache;
|
||||
|
||||
public MustacheTemplateRenderer(String resourcePrefix, Cache templateCache) {
|
||||
String prefix = (resourcePrefix.endsWith("/") ? resourcePrefix
|
||||
: resourcePrefix + "/");
|
||||
this.mustache = Mustache.compiler().withLoader(mustacheTemplateLoader(prefix));
|
||||
this.keyGenerator = (name) -> String.format("%s%s", prefix, name);
|
||||
this.templateCache = templateCache;
|
||||
}
|
||||
|
||||
public MustacheTemplateRenderer(String resourcePrefix) {
|
||||
this(resourcePrefix, null);
|
||||
}
|
||||
|
||||
private static TemplateLoader mustacheTemplateLoader(String prefix) {
|
||||
ResourceLoader resourceLoader = new DefaultResourceLoader();
|
||||
return (name) -> {
|
||||
String location = prefix + name + ".mustache";
|
||||
return new InputStreamReader(
|
||||
resourceLoader.getResource(location).getInputStream(),
|
||||
StandardCharsets.UTF_8);
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public String render(String templateName, Map<String, ?> model) throws IOException {
|
||||
Template template = getTemplate(templateName);
|
||||
return template.execute(model);
|
||||
}
|
||||
|
||||
private Template getTemplate(String name) {
|
||||
try {
|
||||
if (this.templateCache != null) {
|
||||
try {
|
||||
return this.templateCache.get(this.keyGenerator.apply(name),
|
||||
() -> loadTemplate(name));
|
||||
}
|
||||
catch (ValueRetrievalException ex) {
|
||||
throw ex.getCause();
|
||||
}
|
||||
}
|
||||
return loadTemplate(name);
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
throw new IllegalStateException("Cannot load template " + name, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private Template loadTemplate(String name) throws Exception {
|
||||
Reader template = this.mustache.loader.getTemplate(name);
|
||||
return this.mustache.compile(template);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* http://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.io.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Template rendering abstraction.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface TemplateRenderer {
|
||||
|
||||
/**
|
||||
* Render the template with the specified name and the specified model.
|
||||
* @param templateName the name of the template
|
||||
* @param model the model to use
|
||||
* @return the rendering result
|
||||
* @throws IOException if rendering the template failed
|
||||
*/
|
||||
String render(String templateName, Map<String, ?> model) throws IOException;
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* http://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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Template rendering abstraction.
|
||||
*/
|
||||
package io.spring.initializr.generator.io.template;
|
@ -1,108 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2018 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
|
||||
*
|
||||
* http://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.util;
|
||||
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
import com.samskivert.mustache.Mustache;
|
||||
import com.samskivert.mustache.Mustache.Compiler;
|
||||
import com.samskivert.mustache.Mustache.TemplateLoader;
|
||||
import com.samskivert.mustache.Template;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.util.ConcurrentReferenceHashMap;
|
||||
|
||||
/**
|
||||
* A template renderer backed by Mustache.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class TemplateRenderer {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(TemplateRenderer.class);
|
||||
|
||||
private boolean cache = true;
|
||||
|
||||
private final Compiler mustache;
|
||||
|
||||
private final ConcurrentMap<String, Template> templateCaches = new ConcurrentReferenceHashMap<>();
|
||||
|
||||
public TemplateRenderer() {
|
||||
this(mustacheCompiler());
|
||||
}
|
||||
|
||||
public TemplateRenderer(Compiler mustache) {
|
||||
this.mustache = mustache;
|
||||
}
|
||||
|
||||
public boolean isCache() {
|
||||
return this.cache;
|
||||
}
|
||||
|
||||
public void setCache(boolean cache) {
|
||||
this.cache = cache;
|
||||
}
|
||||
|
||||
public String process(String name, Map<String, ?> model) {
|
||||
try {
|
||||
Template template = getTemplate(name);
|
||||
return template.execute(model);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
log.error("Cannot render: " + name, ex);
|
||||
throw new IllegalStateException("Cannot render template", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public Template getTemplate(String name) {
|
||||
if (this.cache) {
|
||||
return this.templateCaches.computeIfAbsent(name, this::loadTemplate);
|
||||
}
|
||||
return loadTemplate(name);
|
||||
}
|
||||
|
||||
protected Template loadTemplate(String name) {
|
||||
try {
|
||||
Reader template;
|
||||
template = this.mustache.loader.getTemplate(name);
|
||||
return this.mustache.compile(template);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalStateException("Cannot load template " + name, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static Compiler mustacheCompiler() {
|
||||
return Mustache.compiler().withLoader(mustacheTemplateLoader());
|
||||
}
|
||||
|
||||
private static TemplateLoader mustacheTemplateLoader() {
|
||||
ResourceLoader resourceLoader = new DefaultResourceLoader();
|
||||
String prefix = "classpath:/templates/";
|
||||
Charset charset = Charset.forName("UTF-8");
|
||||
return (name) -> new InputStreamReader(
|
||||
resourceLoader.getResource(prefix + name).getInputStream(), charset);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* http://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.io.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.concurrent.ConcurrentMapCache;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
* Tests for {@link MustacheTemplateRenderer}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class MustacheTemplateRendererTests {
|
||||
|
||||
private final Cache templatesCache = new ConcurrentMapCache("test");
|
||||
|
||||
@Test
|
||||
void renderTemplate() throws IOException {
|
||||
MustacheTemplateRenderer render = new MustacheTemplateRenderer(
|
||||
"classpath:/templates/mustache", this.templatesCache);
|
||||
assertThat(this.templatesCache.get("classpath:/templates/mustache/test"))
|
||||
.isNull();
|
||||
assertThat(render.render("test", Collections.singletonMap("key", "value")))
|
||||
.isEqualTo("value");
|
||||
assertThat(this.templatesCache.get("classpath:/templates/mustache/test"))
|
||||
.isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void renderTemplateWithoutCache() throws IOException {
|
||||
MustacheTemplateRenderer render = new MustacheTemplateRenderer(
|
||||
"classpath:/templates/mustache");
|
||||
assertThat(render.render("test", Collections.singletonMap("key", "value")))
|
||||
.isEqualTo("value");
|
||||
}
|
||||
|
||||
@Test
|
||||
void renderUnknownTemplate() {
|
||||
MustacheTemplateRenderer render = new MustacheTemplateRenderer(
|
||||
"classpath:/templates/mustache", this.templatesCache);
|
||||
assertThatExceptionOfType(IllegalStateException.class)
|
||||
.isThrownBy(() -> render.render("does-not-exist", Collections.emptyMap()))
|
||||
.withMessageContaining("Cannot load template")
|
||||
.withMessageContaining("does-not-exist");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1 @@
|
||||
{{key}}
|
@ -25,13 +25,14 @@ import javax.cache.expiry.Duration;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.spring.initializr.generator.io.IndentingWriterFactory;
|
||||
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.metadata.DependencyMetadataProvider;
|
||||
import io.spring.initializr.metadata.InitializrMetadata;
|
||||
import io.spring.initializr.metadata.InitializrMetadataBuilder;
|
||||
import io.spring.initializr.metadata.InitializrMetadataProvider;
|
||||
import io.spring.initializr.metadata.InitializrProperties;
|
||||
import io.spring.initializr.util.TemplateRenderer;
|
||||
import io.spring.initializr.web.ProjectResourceLocator;
|
||||
import io.spring.initializr.web.project.MainController;
|
||||
import io.spring.initializr.web.project.ProjectGenerationInvoker;
|
||||
@ -40,6 +41,7 @@ import io.spring.initializr.web.support.DefaultDependencyMetadataProvider;
|
||||
import io.spring.initializr.web.support.DefaultInitializrMetadataProvider;
|
||||
import io.spring.initializr.web.ui.UiController;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
@ -50,6 +52,9 @@ import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfigu
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.support.NoOpCache;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@ -84,12 +89,22 @@ public class InitializrAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public TemplateRenderer templateRenderer(Environment environment) {
|
||||
public TemplateRenderer templateRenderer(Environment environment,
|
||||
ObjectProvider<CacheManager> cacheManager) {
|
||||
return new MustacheTemplateRenderer("classpath:/templates",
|
||||
determineCache(environment, cacheManager.getIfAvailable()));
|
||||
}
|
||||
|
||||
private Cache determineCache(Environment environment, CacheManager cacheManager) {
|
||||
if (cacheManager != null) {
|
||||
Binder binder = Binder.get(environment);
|
||||
boolean cache = binder.bind("spring.mustache.cache", Boolean.class).orElse(true);
|
||||
TemplateRenderer templateRenderer = new TemplateRenderer();
|
||||
templateRenderer.setCache(cache);
|
||||
return templateRenderer;
|
||||
boolean cache = binder.bind("spring.mustache.cache", Boolean.class)
|
||||
.orElse(true);
|
||||
if (cache) {
|
||||
return cacheManager.getCache("initializr.templates");
|
||||
}
|
||||
}
|
||||
return new NoOpCache("templates");
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
@ -28,6 +28,7 @@ import java.util.concurrent.TimeUnit;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import com.samskivert.mustache.Mustache;
|
||||
import io.spring.initializr.generator.io.template.TemplateRenderer;
|
||||
import io.spring.initializr.generator.version.Version;
|
||||
import io.spring.initializr.metadata.DependencyMetadata;
|
||||
import io.spring.initializr.metadata.DependencyMetadataProvider;
|
||||
@ -35,7 +36,6 @@ import io.spring.initializr.metadata.InitializrMetadata;
|
||||
import io.spring.initializr.metadata.InitializrMetadataProvider;
|
||||
import io.spring.initializr.util.Agent;
|
||||
import io.spring.initializr.util.Agent.AgentId;
|
||||
import io.spring.initializr.util.TemplateRenderer;
|
||||
import io.spring.initializr.web.mapper.DependencyMetadataV21JsonMapper;
|
||||
import io.spring.initializr.web.mapper.InitializrMetadataJsonMapper;
|
||||
import io.spring.initializr.web.mapper.InitializrMetadataV21JsonMapper;
|
||||
@ -121,7 +121,8 @@ public class MainController extends AbstractInitializrController {
|
||||
|
||||
@RequestMapping(path = "/", produces = "text/plain")
|
||||
public ResponseEntity<String> serviceCapabilitiesText(
|
||||
@RequestHeader(value = HttpHeaders.USER_AGENT, required = false) String userAgent) {
|
||||
@RequestHeader(value = HttpHeaders.USER_AGENT, required = false) String userAgent)
|
||||
throws IOException {
|
||||
String appUrl = generateAppUrl();
|
||||
InitializrMetadata metadata = this.metadataProvider.get();
|
||||
|
||||
|
@ -17,16 +17,17 @@
|
||||
package io.spring.initializr.web.support;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.io.IOException;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import io.spring.initializr.generator.io.template.TemplateRenderer;
|
||||
import io.spring.initializr.metadata.Dependency;
|
||||
import io.spring.initializr.metadata.InitializrMetadata;
|
||||
import io.spring.initializr.metadata.MetadataElement;
|
||||
import io.spring.initializr.metadata.Type;
|
||||
import io.spring.initializr.util.TemplateRenderer;
|
||||
|
||||
import org.springframework.beans.BeanWrapperImpl;
|
||||
|
||||
@ -56,12 +57,13 @@ public class CommandLineHelpGenerator {
|
||||
* @param metadata the initializr metadata
|
||||
* @param serviceUrl the service URL
|
||||
* @return the generic capabilities text document
|
||||
* @throws IOException if rendering the capabilities failed
|
||||
*/
|
||||
public String generateGenericCapabilities(InitializrMetadata metadata,
|
||||
String serviceUrl) {
|
||||
String serviceUrl) throws IOException {
|
||||
Map<String, Object> model = initializeCommandLineModel(metadata, serviceUrl);
|
||||
model.put("hasExamples", false);
|
||||
return this.template.process("cli-capabilities.txt", model);
|
||||
return this.template.render("cli/cli-capabilities", model);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -69,13 +71,14 @@ public class CommandLineHelpGenerator {
|
||||
* @param metadata the initializr metadata
|
||||
* @param serviceUrl the service URL
|
||||
* @return the generic capabilities text document
|
||||
* @throws IOException if rendering the capabilities failed
|
||||
*/
|
||||
public String generateCurlCapabilities(InitializrMetadata metadata,
|
||||
String serviceUrl) {
|
||||
public String generateCurlCapabilities(InitializrMetadata metadata, String serviceUrl)
|
||||
throws IOException {
|
||||
Map<String, Object> model = initializeCommandLineModel(metadata, serviceUrl);
|
||||
model.put("examples", this.template.process("curl-examples.txt", model));
|
||||
model.put("examples", this.template.render("cli/curl-examples", model));
|
||||
model.put("hasExamples", true);
|
||||
return this.template.process("cli-capabilities.txt", model);
|
||||
return this.template.render("cli/cli-capabilities", model);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -83,13 +86,14 @@ public class CommandLineHelpGenerator {
|
||||
* @param metadata the initializr metadata
|
||||
* @param serviceUrl the service URL
|
||||
* @return the generic capabilities text document
|
||||
* @throws IOException if rendering the capabilities failed
|
||||
*/
|
||||
public String generateHttpieCapabilities(InitializrMetadata metadata,
|
||||
String serviceUrl) {
|
||||
String serviceUrl) throws IOException {
|
||||
Map<String, Object> model = initializeCommandLineModel(metadata, serviceUrl);
|
||||
model.put("examples", this.template.process("httpie-examples.txt", model));
|
||||
model.put("examples", this.template.render("cli/httpie-examples", model));
|
||||
model.put("hasExamples", true);
|
||||
return this.template.process("cli-capabilities.txt", model);
|
||||
return this.template.render("cli/cli-capabilities", model);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -98,12 +102,13 @@ public class CommandLineHelpGenerator {
|
||||
* @param metadata the initializr metadata
|
||||
* @param serviceUrl the service URL
|
||||
* @return the generic capabilities text document
|
||||
* @throws IOException if rendering the capabilities failed
|
||||
*/
|
||||
public String generateSpringBootCliCapabilities(InitializrMetadata metadata,
|
||||
String serviceUrl) {
|
||||
String serviceUrl) throws IOException {
|
||||
Map<String, Object> model = initializeSpringBootCliModel(metadata, serviceUrl);
|
||||
model.put("hasExamples", false);
|
||||
return this.template.process("boot-cli-capabilities.txt", model);
|
||||
return this.template.render("cli/boot-cli-capabilities", model);
|
||||
}
|
||||
|
||||
protected Map<String, Object> initializeCommandLineModel(InitializrMetadata metadata,
|
||||
|
@ -16,9 +16,9 @@
|
||||
|
||||
package io.spring.initializr.web.autoconfigure;
|
||||
|
||||
import io.spring.initializr.generator.io.template.TemplateRenderer;
|
||||
import io.spring.initializr.metadata.DependencyMetadataProvider;
|
||||
import io.spring.initializr.metadata.InitializrMetadataProvider;
|
||||
import io.spring.initializr.util.TemplateRenderer;
|
||||
import io.spring.initializr.web.ProjectResourceLocator;
|
||||
import io.spring.initializr.web.project.MainController;
|
||||
import io.spring.initializr.web.project.ProjectGenerationInvoker;
|
||||
|
@ -26,8 +26,8 @@ import org.springframework.test.context.ActiveProfiles;
|
||||
/**
|
||||
* Validate that the "raw" HTTP commands that are described in the command-line help
|
||||
* works. If anything needs to be updated here, please double check the
|
||||
* "curl-examples.txt" as it may need an update as well. This is also a good indicator of
|
||||
* a non backward compatible change.
|
||||
* "cli/curl-examples.mustache" as it may need an update as well. This is also a good
|
||||
* indicator of a non backward compatible change.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
|
@ -16,19 +16,22 @@
|
||||
|
||||
package io.spring.initializr.web.support;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
import io.spring.initializr.generator.io.template.MustacheTemplateRenderer;
|
||||
import io.spring.initializr.generator.spring.test.InitializrMetadataTestBuilder;
|
||||
import io.spring.initializr.metadata.Dependency;
|
||||
import io.spring.initializr.metadata.InitializrMetadata;
|
||||
import io.spring.initializr.metadata.Type;
|
||||
import io.spring.initializr.util.TemplateRenderer;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link CommandLineHelpGenerator}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class CommandLineHelpGeneratorTests {
|
||||
@ -37,11 +40,12 @@ class CommandLineHelpGeneratorTests {
|
||||
|
||||
@BeforeEach
|
||||
public void init() {
|
||||
this.generator = new CommandLineHelpGenerator(new TemplateRenderer());
|
||||
this.generator = new CommandLineHelpGenerator(
|
||||
new MustacheTemplateRenderer("classpath:/templates"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateGenericCapabilities() {
|
||||
void generateGenericCapabilities() throws IOException {
|
||||
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
|
||||
.addDependencyGroup("test", createDependency("id-b", "depB"),
|
||||
createDependency("id-a", "depA", "and some description"))
|
||||
@ -57,7 +61,7 @@ class CommandLineHelpGeneratorTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateCapabilitiesWithTypeDescription() {
|
||||
void generateCapabilitiesWithTypeDescription() throws IOException {
|
||||
Type type = new Type();
|
||||
type.setId("foo");
|
||||
type.setName("foo-name");
|
||||
@ -72,7 +76,7 @@ class CommandLineHelpGeneratorTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateCapabilitiesWithAlias() {
|
||||
void generateCapabilitiesWithAlias() throws IOException {
|
||||
Dependency dependency = createDependency("dep", "some description");
|
||||
dependency.setAliases(Arrays.asList("legacy", "another"));
|
||||
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
|
||||
@ -85,7 +89,7 @@ class CommandLineHelpGeneratorTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateCurlCapabilities() {
|
||||
void generateCurlCapabilities() throws IOException {
|
||||
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
|
||||
.addDependencyGroup("test", createDependency("id-b", "depB"),
|
||||
createDependency("id-a", "depA", "and some description"))
|
||||
@ -101,7 +105,7 @@ class CommandLineHelpGeneratorTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateHttpCapabilities() {
|
||||
void generateHttpCapabilities() throws IOException {
|
||||
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
|
||||
.addDependencyGroup("test", createDependency("id-b", "depB"),
|
||||
createDependency("id-a", "depA", "and some description"))
|
||||
@ -118,7 +122,7 @@ class CommandLineHelpGeneratorTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateSpringBootCliCapabilities() {
|
||||
void generateSpringBootCliCapabilities() throws IOException {
|
||||
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
|
||||
.addDependencyGroup("test", createDependency("id-b", "depB"),
|
||||
createDependency("id-a", "depA", "and some description"))
|
||||
@ -139,7 +143,7 @@ class CommandLineHelpGeneratorTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateCapabilitiesWithVersionRange() {
|
||||
void generateCapabilitiesWithVersionRange() throws IOException {
|
||||
Dependency first = Dependency.withId("first");
|
||||
first.setDescription("first desc");
|
||||
first.setVersionRange("1.2.0.RELEASE");
|
||||
|
Loading…
Reference in New Issue
Block a user