mirror of
https://gitee.com/dcren/initializr.git
synced 2025-12-02 11:24:04 +08:00
Reformat code
Format code using a pre-release version of the spring-javaformat plugin.
This commit is contained in:
@@ -39,8 +39,7 @@ public class InitializrMetricsConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnSingleCandidate(MeterRegistry.class)
|
||||
public ProjectGenerationMetricsListener metricsListener(
|
||||
MeterRegistry meterRegistry) {
|
||||
public ProjectGenerationMetricsListener metricsListener(MeterRegistry meterRegistry) {
|
||||
return new ProjectGenerationMetricsListener(meterRegistry);
|
||||
}
|
||||
|
||||
|
||||
@@ -25,8 +25,8 @@ import org.springframework.boot.actuate.info.Info;
|
||||
import org.springframework.boot.actuate.info.InfoContributor;
|
||||
|
||||
/**
|
||||
* An {@link InfoContributor} that exposes the actual ranges used by each bom
|
||||
* defined in the project.
|
||||
* An {@link InfoContributor} that exposes the actual ranges used by each bom defined in
|
||||
* the project.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@@ -45,7 +45,8 @@ public class BomRangesInfoContributor implements InfoContributor {
|
||||
if (v.getMappings() != null && !v.getMappings().isEmpty()) {
|
||||
Map<String, Object> bom = new LinkedHashMap<>();
|
||||
v.getMappings().forEach(it -> {
|
||||
String requirement = "Spring Boot " + it.determineVersionRangeRequirement();
|
||||
String requirement = "Spring Boot "
|
||||
+ it.determineVersionRangeRequirement();
|
||||
bom.put(it.getVersion(), requirement);
|
||||
});
|
||||
details.put(k, bom);
|
||||
|
||||
@@ -28,8 +28,8 @@ import org.springframework.boot.actuate.info.InfoContributor;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* An {@link InfoContributor} that exposes the actual ranges used by dependencies
|
||||
* defined in the project that have an explicit version (i.e. not relying on a bom).
|
||||
* An {@link InfoContributor} that exposes the actual ranges used by dependencies defined
|
||||
* in the project that have an explicit version (i.e. not relying on a bom).
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@@ -55,8 +55,8 @@ public class DependencyRangesInfoContributor implements InfoContributor {
|
||||
});
|
||||
if (!dep.isEmpty()) {
|
||||
if (d.getRange() == null) {
|
||||
boolean openRange = dep.values().stream().anyMatch(
|
||||
v -> v.getHigherVersion() == null);
|
||||
boolean openRange = dep.values().stream()
|
||||
.anyMatch(v -> v.getHigherVersion() == null);
|
||||
if (!openRange) {
|
||||
Version higher = null;
|
||||
for (VersionRange versionRange : dep.values()) {
|
||||
@@ -67,7 +67,8 @@ public class DependencyRangesInfoContributor implements InfoContributor {
|
||||
else if (candidate.compareTo(higher) > 0) {
|
||||
higher = candidate;
|
||||
}
|
||||
} ;
|
||||
}
|
||||
;
|
||||
dep.put("managed", new VersionRange(higher));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,9 +44,13 @@ public class ProjectGenerationStatPublisher {
|
||||
.getLogger(ProjectGenerationStatPublisher.class);
|
||||
|
||||
private final ProjectRequestDocumentFactory documentFactory;
|
||||
|
||||
private final StatsProperties statsProperties;
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
private final RetryTemplate retryTemplate;
|
||||
|
||||
public ProjectGenerationStatPublisher(ProjectRequestDocumentFactory documentFactory,
|
||||
@@ -58,10 +62,10 @@ public class ProjectGenerationStatPublisher {
|
||||
StatsProperties.Elastic elastic = statsProperties.getElastic();
|
||||
if (StringUtils.hasText(elastic.getUsername())) {
|
||||
this.restTemplate = restTemplateBuilder
|
||||
.basicAuthorization(elastic.getUsername(),
|
||||
elastic.getPassword())
|
||||
.basicAuthorization(elastic.getUsername(), elastic.getPassword())
|
||||
.build();
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
this.restTemplate = restTemplateBuilder.build();
|
||||
}
|
||||
this.retryTemplate = retryTemplate;
|
||||
@@ -82,10 +86,11 @@ public class ProjectGenerationStatPublisher {
|
||||
.post(this.statsProperties.getElastic().getEntityUrl())
|
||||
.contentType(MediaType.APPLICATION_JSON).body(json);
|
||||
|
||||
this.retryTemplate.execute((RetryCallback<Void, RuntimeException>) context -> {
|
||||
restTemplate.exchange(request, String.class);
|
||||
return null;
|
||||
});
|
||||
this.retryTemplate
|
||||
.execute((RetryCallback<Void, RuntimeException>) context -> {
|
||||
restTemplate.exchange(request, String.class);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
catch (Exception ex) {
|
||||
log.warn(String.format(
|
||||
|
||||
@@ -29,27 +29,45 @@ public class ProjectRequestDocument {
|
||||
private long generationTimestamp;
|
||||
|
||||
private String requestIp;
|
||||
|
||||
private String requestIpv4;
|
||||
|
||||
private String requestCountry;
|
||||
|
||||
private String clientId;
|
||||
|
||||
private String clientVersion;
|
||||
|
||||
private String groupId;
|
||||
|
||||
private String artifactId;
|
||||
|
||||
private String packageName;
|
||||
|
||||
private String bootVersion;
|
||||
|
||||
private String javaVersion;
|
||||
|
||||
private String language;
|
||||
|
||||
private String packaging;
|
||||
|
||||
private String type;
|
||||
|
||||
private final List<String> dependencies = new ArrayList<>();
|
||||
|
||||
private String errorMessage;
|
||||
|
||||
private boolean invalid;
|
||||
|
||||
private boolean invalidJavaVersion;
|
||||
|
||||
private boolean invalidLanguage;
|
||||
|
||||
private boolean invalidPackaging;
|
||||
|
||||
private boolean invalidType;
|
||||
|
||||
private final List<String> invalidDependencies = new ArrayList<>();
|
||||
|
||||
public long getGenerationTimestamp() {
|
||||
|
||||
@@ -37,8 +37,8 @@ import org.springframework.util.StringUtils;
|
||||
*/
|
||||
public class ProjectRequestDocumentFactory {
|
||||
|
||||
private static final Pattern IP_PATTERN =
|
||||
Pattern.compile("[0-9]*\\.[0-9]*\\.[0-9]*\\.[0-9]*");
|
||||
private static final Pattern IP_PATTERN = Pattern
|
||||
.compile("[0-9]*\\.[0-9]*\\.[0-9]*\\.[0-9]*");
|
||||
|
||||
private final InitializrMetadataProvider metadataProvider;
|
||||
|
||||
|
||||
@@ -124,7 +124,8 @@ public class StatsProperties {
|
||||
return new URI(string);
|
||||
}
|
||||
catch (URISyntaxException ex) {
|
||||
throw new IllegalStateException("Cannot create entity URL: " + string, ex);
|
||||
throw new IllegalStateException("Cannot create entity URL: " + string,
|
||||
ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,15 +38,14 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@ActiveProfiles("test-default")
|
||||
@SpringBootTest(classes = Config.class, webEnvironment = RANDOM_PORT,
|
||||
properties = "management.endpoints.web.exposure.include=info,metrics")
|
||||
@SpringBootTest(classes = Config.class, webEnvironment = RANDOM_PORT, properties = "management.endpoints.web.exposure.include=info,metrics")
|
||||
public class ActuatorIntegrationTests
|
||||
extends AbstractFullStackInitializrIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void infoHasExternalProperties() {
|
||||
String body = getRestTemplate().getForObject(
|
||||
createUrl("/actuator/info"), String.class);
|
||||
String body = getRestTemplate().getForObject(createUrl("/actuator/info"),
|
||||
String.class);
|
||||
assertTrue("Wrong body:\n" + body, body.contains("\"spring-boot\""));
|
||||
assertTrue("Wrong body:\n" + body,
|
||||
body.contains("\"version\":\"1.1.4.RELEASE\""));
|
||||
@@ -87,13 +86,13 @@ public class ActuatorIntegrationTests
|
||||
}
|
||||
|
||||
private JsonNode metricsEndpoint() {
|
||||
return parseJson(getRestTemplate().getForObject(
|
||||
createUrl("/actuator/metrics"), String.class));
|
||||
return parseJson(getRestTemplate().getForObject(createUrl("/actuator/metrics"),
|
||||
String.class));
|
||||
}
|
||||
|
||||
private int metricValue(String metric) {
|
||||
JsonNode root = parseJson(getRestTemplate().getForObject(
|
||||
createUrl("/actuator/metrics/" + metric), String.class));
|
||||
JsonNode root = parseJson(getRestTemplate()
|
||||
.getForObject(createUrl("/actuator/metrics/" + metric), String.class));
|
||||
JsonNode measurements = root.get("measurements");
|
||||
assertThat(measurements.isArray());
|
||||
assertThat(measurements.size()).isEqualTo(1);
|
||||
|
||||
@@ -50,13 +50,13 @@ public class InitializrStatsAutoConfigurationTests {
|
||||
this.contextRunner.withUserConfiguration(CustomRestTemplateConfiguration.class)
|
||||
.withPropertyValues("initializr.stats.elastic.uri=http://localhost:9200")
|
||||
.run((context) -> {
|
||||
assertThat(context).hasSingleBean(
|
||||
ProjectGenerationStatPublisher.class);
|
||||
assertThat(context)
|
||||
.hasSingleBean(ProjectGenerationStatPublisher.class);
|
||||
RestTemplate restTemplate = (RestTemplate) new DirectFieldAccessor(
|
||||
context.getBean(ProjectGenerationStatPublisher.class))
|
||||
.getPropertyValue("restTemplate");
|
||||
assertThat(restTemplate.getErrorHandler()).isSameAs(
|
||||
CustomRestTemplateConfiguration.errorHandler);
|
||||
.getPropertyValue("restTemplate");
|
||||
assertThat(restTemplate.getErrorHandler())
|
||||
.isSameAs(CustomRestTemplateConfiguration.errorHandler);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -67,13 +67,15 @@ public class InitializrStatsAutoConfigurationTests {
|
||||
public InitializrMetadataProvider initializrMetadataProvider() {
|
||||
return mock(InitializrMetadataProvider.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import(InfrastructureConfiguration.class)
|
||||
static class CustomRestTemplateConfiguration {
|
||||
|
||||
private static final ResponseErrorHandler errorHandler = mock(ResponseErrorHandler.class);
|
||||
private static final ResponseErrorHandler errorHandler = mock(
|
||||
ResponseErrorHandler.class);
|
||||
|
||||
@Bean
|
||||
public RestTemplateCustomizer testRestTemplateCustomizer() {
|
||||
|
||||
@@ -55,7 +55,6 @@ public class DependencyRangesInfoContributorTests {
|
||||
assertThat(info.getDetails()).doesNotContainKeys("dependency-ranges");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void dependencyWithRangeOnArtifact() {
|
||||
Dependency dependency = Dependency.withId("foo", "com.example", "foo",
|
||||
@@ -77,8 +76,7 @@ public class DependencyRangesInfoContributorTests {
|
||||
.create("[1.1.0.RELEASE, 1.2.0.RELEASE)", null, null, "0.1.0.RELEASE"));
|
||||
dependency.setBom("bom");
|
||||
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
|
||||
.addBom("bom", bom)
|
||||
.addDependencyGroup("foo", dependency).build();
|
||||
.addBom("bom", bom).addDependencyGroup("foo", dependency).build();
|
||||
Info info = getInfo(metadata);
|
||||
assertThat(info.getDetails()).doesNotContainKeys("dependency-ranges");
|
||||
}
|
||||
@@ -107,8 +105,8 @@ public class DependencyRangesInfoContributorTests {
|
||||
Dependency dependency = Dependency.withId("foo", null, null, "0.3.0.RELEASE");
|
||||
dependency.getMappings().add(Dependency.Mapping
|
||||
.create("[1.1.0.RELEASE, 1.2.0.RELEASE)", null, null, "0.1.0.RELEASE"));
|
||||
dependency.getMappings().add(Dependency.Mapping
|
||||
.create("1.2.0.RELEASE", null, null, "0.2.0.RELEASE"));
|
||||
dependency.getMappings().add(
|
||||
Dependency.Mapping.create("1.2.0.RELEASE", null, null, "0.2.0.RELEASE"));
|
||||
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
|
||||
.addDependencyGroup("test", dependency).build();
|
||||
Info info = getInfo(metadata);
|
||||
@@ -146,7 +144,7 @@ public class DependencyRangesInfoContributorTests {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Map<String,Object> getDependencyRangeInfo(Info info, String id) {
|
||||
private Map<String, Object> getDependencyRangeInfo(Info info, String id) {
|
||||
assertThat(info.getDetails()).containsKeys("dependency-ranges");
|
||||
Map<String, Object> ranges = (Map<String, Object>) info.getDetails()
|
||||
.get("dependency-ranges");
|
||||
@@ -155,8 +153,8 @@ public class DependencyRangesInfoContributorTests {
|
||||
|
||||
private static Info getInfo(InitializrMetadata metadata) {
|
||||
Info.Builder builder = new Info.Builder();
|
||||
new DependencyRangesInfoContributor(new SimpleInitializrMetadataProvider(metadata))
|
||||
.contribute(builder);
|
||||
new DependencyRangesInfoContributor(
|
||||
new SimpleInitializrMetadataProvider(metadata)).contribute(builder);
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,7 @@ public class ProjectGenerationMetricsListenerTests {
|
||||
.addDependencyGroup("core", "web", "security", "spring-data").build();
|
||||
|
||||
private ProjectGenerationMetricsListener listener;
|
||||
|
||||
private MetricsAssert metricsAssert;
|
||||
|
||||
@Before
|
||||
@@ -224,11 +225,10 @@ public class ProjectGenerationMetricsListenerTests {
|
||||
|
||||
request.resolve(metadata);
|
||||
fireProjectGeneratedEvent(request);
|
||||
metricsAssert.hasValue(1, "initializr.requests",
|
||||
"initializr.dependency.web", "initializr.dependency.security",
|
||||
"initializr.type.gradle-project", "initializr.packaging.jar",
|
||||
"initializr.java_version.1_6", "initializr.language.groovy",
|
||||
"initializr.boot_version.1_0_2_RELEASE",
|
||||
metricsAssert.hasValue(1, "initializr.requests", "initializr.dependency.web",
|
||||
"initializr.dependency.security", "initializr.type.gradle-project",
|
||||
"initializr.packaging.jar", "initializr.java_version.1_6",
|
||||
"initializr.language.groovy", "initializr.boot_version.1_0_2_RELEASE",
|
||||
"initializr.client_id.spring").metricsCount(9);
|
||||
}
|
||||
|
||||
@@ -238,8 +238,8 @@ public class ProjectGenerationMetricsListenerTests {
|
||||
request.getStyle().addAll(Arrays.asList("security", "spring-data"));
|
||||
request.resolve(metadata);
|
||||
fireProjectGeneratedEvent(request);
|
||||
metricsAssert.hasValue(1, "initializr.requests",
|
||||
"initializr.dependency.security", "initializr.dependency.spring-data");
|
||||
metricsAssert.hasValue(1, "initializr.requests", "initializr.dependency.security",
|
||||
"initializr.dependency.spring-data");
|
||||
|
||||
ProjectRequest anotherRequest = initialize();
|
||||
anotherRequest.getStyle().addAll(Arrays.asList("web", "spring-data"));
|
||||
|
||||
@@ -28,12 +28,10 @@ import io.spring.initializr.test.metadata.InitializrMetadataTestBuilder;
|
||||
abstract class AbstractInitializrStatTests {
|
||||
|
||||
private final InitializrMetadata metadata = InitializrMetadataTestBuilder
|
||||
.withDefaults()
|
||||
.addDependencyGroup("core", "security", "validation", "aop")
|
||||
.withDefaults().addDependencyGroup("core", "security", "validation", "aop")
|
||||
.addDependencyGroup("web", "web", "data-rest", "jersey")
|
||||
.addDependencyGroup("data", "data-jpa", "jdbc")
|
||||
.addDependencyGroup("database", "h2", "mysql")
|
||||
.build();
|
||||
.addDependencyGroup("database", "h2", "mysql").build();
|
||||
|
||||
protected InitializrMetadataProvider createProvider(InitializrMetadata metadata) {
|
||||
return new SimpleInitializrMetadataProvider(metadata);
|
||||
|
||||
@@ -193,6 +193,7 @@ public class MainControllerStatsIntegrationTests
|
||||
public static class Content {
|
||||
|
||||
private final String authorization;
|
||||
|
||||
private final String json;
|
||||
|
||||
Content(String authorization, String body) {
|
||||
|
||||
@@ -44,20 +44,21 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
|
||||
public class ProjectGenerationStatPublisherTests extends AbstractInitializrStatTests {
|
||||
|
||||
private RetryTemplate retryTemplate;
|
||||
private ProjectGenerationStatPublisher statPublisher;
|
||||
private MockRestServiceServer mockServer;
|
||||
|
||||
private ProjectGenerationStatPublisher statPublisher;
|
||||
|
||||
private MockRestServiceServer mockServer;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
StatsProperties properties = createProperties();
|
||||
ProjectRequestDocumentFactory documentFactory =
|
||||
new ProjectRequestDocumentFactory(createProvider(getMetadata()));
|
||||
ProjectRequestDocumentFactory documentFactory = new ProjectRequestDocumentFactory(
|
||||
createProvider(getMetadata()));
|
||||
this.retryTemplate = new RetryTemplate();
|
||||
this.statPublisher = new ProjectGenerationStatPublisher(documentFactory,
|
||||
properties, new RestTemplateBuilder(), retryTemplate);
|
||||
mockServer = MockRestServiceServer.createServer(
|
||||
this.statPublisher.getRestTemplate());
|
||||
mockServer = MockRestServiceServer
|
||||
.createServer(this.statPublisher.getRestTemplate());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -119,8 +120,10 @@ public class ProjectGenerationStatPublisherTests extends AbstractInitializrStatT
|
||||
}
|
||||
|
||||
private static String mockResponse(String id, boolean created) {
|
||||
return "{\"_index\":\"initializr\",\"_type\":\"request\",\"_id\":\"" + id + "\",\"_version\":1,\"_shards\"" +
|
||||
":{\"total\":1,\"successful\":1,\"failed\":0},\"created\":" + created + "}";
|
||||
return "{\"_index\":\"initializr\",\"_type\":\"request\",\"_id\":\"" + id
|
||||
+ "\",\"_version\":1,\"_shards\""
|
||||
+ ":{\"total\":1,\"successful\":1,\"failed\":0},\"created\":" + created
|
||||
+ "}";
|
||||
}
|
||||
|
||||
private static StatsProperties createProperties() {
|
||||
|
||||
@@ -29,13 +29,12 @@ import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class ProjectRequestDocumentFactoryTests extends AbstractInitializrStatTests {
|
||||
|
||||
private final ProjectRequestDocumentFactory factory =
|
||||
new ProjectRequestDocumentFactory(createProvider(getMetadata()));
|
||||
private final ProjectRequestDocumentFactory factory = new ProjectRequestDocumentFactory(
|
||||
createProvider(getMetadata()));
|
||||
|
||||
@Test
|
||||
public void createDocumentForSimpleProject() {
|
||||
|
||||
@@ -37,15 +37,16 @@ public class MetricsAssert {
|
||||
}
|
||||
|
||||
public MetricsAssert hasValue(long value, String... metrics) {
|
||||
Arrays.asList(metrics).forEach(metric ->
|
||||
assertThat(meterRegistry.get(metric).counter().count()).isEqualTo(value));
|
||||
Arrays.asList(metrics)
|
||||
.forEach(metric -> assertThat(meterRegistry.get(metric).counter().count())
|
||||
.isEqualTo(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public MetricsAssert hasNoValue(String... metrics) {
|
||||
Arrays.asList(metrics).forEach(metric ->
|
||||
assertThat(Search.in(this.meterRegistry).name(n -> n.startsWith(metric))
|
||||
.counter()).isNull());
|
||||
Arrays.asList(metrics).forEach(metric -> assertThat(
|
||||
Search.in(this.meterRegistry).name(n -> n.startsWith(metric)).counter())
|
||||
.isNull());
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user