mirror of
https://gitee.com/dcren/initializr.git
synced 2026-02-26 05:32:58 +08:00
Fix wrong dependency metric
Previously if a project was generated with no defined dependency; a metric with id 'counter.initializr.dependency.' was published. We now make sure to properly identify that default use case and no metrics gets published in such a case. Fixes gh-57
This commit is contained in:
@@ -24,7 +24,6 @@ import groovy.transform.ToString
|
||||
import groovy.util.logging.Slf4j
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties
|
||||
import org.springframework.util.StringUtils
|
||||
|
||||
/**
|
||||
* The metadata using by the initializr, that is:
|
||||
@@ -246,8 +245,10 @@ class InitializrMetadata {
|
||||
*/
|
||||
def asSpringBootStarter(String name) {
|
||||
groupId = 'org.springframework.boot'
|
||||
artifactId = StringUtils.hasText(name) ? 'spring-boot-starter-' + name : 'spring-boot-starter'
|
||||
id = name
|
||||
artifactId = name ? 'spring-boot-starter-' + name : 'spring-boot-starter'
|
||||
if (name) {
|
||||
id = name
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -49,8 +49,10 @@ class ProjectGenerationMetricsListener implements ProjectGenerationListener {
|
||||
|
||||
protected void handleDependencies(ProjectRequest request) {
|
||||
request.resolvedDependencies.each {
|
||||
def id = sanitize(it.id)
|
||||
increment(key("dependency.$id"))
|
||||
if (!ProjectRequest.DEFAULT_STARTER.equals(it.id)) {
|
||||
def id = sanitize(it.id)
|
||||
increment(key("dependency.$id"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,11 @@ import groovy.util.logging.Slf4j
|
||||
@Slf4j
|
||||
class ProjectRequest {
|
||||
|
||||
/**
|
||||
* The id of the starter to use if no dependency is defined.
|
||||
*/
|
||||
static final DEFAULT_STARTER = 'root_starter'
|
||||
|
||||
List<String> style = []
|
||||
List<String> dependencies = []
|
||||
String name
|
||||
@@ -106,7 +111,7 @@ class ProjectRequest {
|
||||
*/
|
||||
protected addDefaultDependency() {
|
||||
def root = new InitializrMetadata.Dependency()
|
||||
root.id = 'root_starter'
|
||||
root.id = DEFAULT_STARTER
|
||||
root.asSpringBootStarter('')
|
||||
resolvedDependencies << root
|
||||
|
||||
|
||||
@@ -33,6 +33,14 @@ class InitializrMetadataTests {
|
||||
|
||||
private final InitializrMetadata metadata = new InitializrMetadata()
|
||||
|
||||
@Test
|
||||
void createRootSpringBootStarter() {
|
||||
InitializrMetadata.Dependency d = new InitializrMetadata.Dependency();
|
||||
d.asSpringBootStarter("")
|
||||
assertEquals 'org.springframework.boot', d.groupId
|
||||
assertEquals 'spring-boot-starter', d.artifactId
|
||||
}
|
||||
|
||||
@Test
|
||||
void setCoordinatesFromId() {
|
||||
def dependency = createDependency('org.foo:bar:1.2.3')
|
||||
|
||||
@@ -59,6 +59,14 @@ class ProjectGenerationMetricsListenerTests {
|
||||
'initializr.dependency.spring-data')
|
||||
}
|
||||
|
||||
@Test
|
||||
void noDependencies() {
|
||||
def request = initialize()
|
||||
request.resolve(metadata)
|
||||
listener.onGeneratedProject(request)
|
||||
metricsAssert.hasNoValue('initializr.dependency.')
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolvedWebDependency() {
|
||||
def request = initialize()
|
||||
|
||||
@@ -131,6 +131,7 @@ class ProjectRequestTests {
|
||||
def expected = new InitializrMetadata.Dependency()
|
||||
expected.asSpringBootStarter(name)
|
||||
assertDependency(actual, expected.groupId, expected.artifactId, expected.version)
|
||||
assertEquals name, actual.id
|
||||
}
|
||||
|
||||
private static InitializrMetadata.Dependency createDependency(String groupId, String artifactId, String version) {
|
||||
|
||||
@@ -47,6 +47,7 @@ class MetricsAssert {
|
||||
metrics.each {
|
||||
assertEquals "Metric '$it' should not be registered", null, counterService.values[it]
|
||||
}
|
||||
this
|
||||
}
|
||||
|
||||
MetricsAssert metricsCount(int count) {
|
||||
|
||||
Reference in New Issue
Block a user