Skip to content

Commit 968b68c

Browse files
committed
Polish
1 parent 10f7031 commit 968b68c

File tree

14 files changed

+107
-98
lines changed

14 files changed

+107
-98
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricFilterAutoConfiguration.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,12 @@ private int getStatus(HttpServletResponse response) {
135135
}
136136

137137
private boolean is4xxClientError(int status) {
138-
HttpStatus httpStatus = HttpStatus.OK;
139138
try {
140-
httpStatus = HttpStatus.valueOf(status);
139+
return HttpStatus.valueOf(status).is4xxClientError();
141140
}
142141
catch (Exception ex) {
143-
// not convertible
142+
return false;
144143
}
145-
return httpStatus.is4xxClientError();
146144
}
147145

148146
private String getKey(String string) {

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/ManagementSecurityAutoConfigurationTests.java

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@
4444
import org.springframework.security.web.FilterChainProxy;
4545
import org.springframework.test.util.ReflectionTestUtils;
4646
import org.springframework.test.web.servlet.MockMvc;
47+
import org.springframework.test.web.servlet.ResultMatcher;
4748
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
4849
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
4950
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
5051
import org.springframework.util.StringUtils;
51-
import org.springframework.web.context.WebApplicationContext;
5252
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
5353

5454
import static org.hamcrest.Matchers.greaterThan;
@@ -209,54 +209,45 @@ public void realmSameForManagement() throws Exception {
209209
PropertyPlaceholderAutoConfiguration.class);
210210
this.context.refresh();
211211

212-
MockMvc mockMvc = MockMvcBuilders
213-
.webAppContextSetup((WebApplicationContext) this.context)
214-
.addFilters(
215-
this.context.getBean("springSecurityFilterChain", Filter.class))
216-
.build();
217-
212+
Filter filter = this.context.getBean("springSecurityFilterChain", Filter.class);
213+
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
214+
.addFilters(filter).build();
218215

219216
// no user (Main)
220-
mockMvc.perform(
221-
MockMvcRequestBuilders.get("/"))
217+
mockMvc.perform(MockMvcRequestBuilders.get("/"))
222218
.andExpect(MockMvcResultMatchers.status().isUnauthorized())
223-
.andExpect(
224-
MockMvcResultMatchers.header().string("www-authenticate",
225-
Matchers.containsString("realm=\"Spring\"")));
219+
.andExpect(springAuthenticateRealmHeader());
226220

227221
// invalid user (Main)
228222
mockMvc.perform(
229223
MockMvcRequestBuilders.get("/").header("authorization", "Basic xxx"))
230224
.andExpect(MockMvcResultMatchers.status().isUnauthorized())
231-
.andExpect(
232-
MockMvcResultMatchers.header().string("www-authenticate",
233-
Matchers.containsString("realm=\"Spring\"")));
225+
.andExpect(springAuthenticateRealmHeader());
234226

235227
// no user (Management)
236-
mockMvc.perform(
237-
MockMvcRequestBuilders.get("/beans"))
228+
mockMvc.perform(MockMvcRequestBuilders.get("/beans"))
238229
.andExpect(MockMvcResultMatchers.status().isUnauthorized())
239-
.andExpect(
240-
MockMvcResultMatchers.header().string("www-authenticate",
241-
Matchers.containsString("realm=\"Spring\"")));
230+
.andExpect(springAuthenticateRealmHeader());
242231

243232
// invalid user (Management)
244233
mockMvc.perform(
245234
MockMvcRequestBuilders.get("/beans").header("authorization", "Basic xxx"))
246235
.andExpect(MockMvcResultMatchers.status().isUnauthorized())
247-
.andExpect(
248-
MockMvcResultMatchers.header().string("www-authenticate",
249-
Matchers.containsString("realm=\"Spring\"")));
236+
.andExpect(springAuthenticateRealmHeader());
237+
}
238+
239+
private ResultMatcher springAuthenticateRealmHeader() {
240+
return MockMvcResultMatchers.header().string("www-authenticate",
241+
Matchers.containsString("realm=\"Spring\""));
250242
}
251243

252244
@EnableGlobalAuthentication
253245
@Configuration
254246
static class AuthenticationConfig {
255247
@Autowired
256248
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
257-
auth
258-
.inMemoryAuthentication()
259-
.withUser("user").password("password").roles("USER");
249+
auth.inMemoryAuthentication().withUser("user").password("password")
250+
.roles("USER");
260251
}
261252
}
262253

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/BeanTypeRegistry.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -116,39 +116,40 @@ private Class<?> getConfigurationClassFactoryBeanGeneric(
116116
definition.getFactoryMethodName());
117117
Class<?> generic = ResolvableType.forMethodReturnType(method)
118118
.as(FactoryBean.class).resolveGeneric();
119-
if (generic == null || generic.equals(Object.class)) {
120-
generic = determineTypeFromDefinitionAttribute(factoryDefinition);
119+
if ((generic == null || generic.equals(Object.class))
120+
&& definition.hasAttribute(FACTORY_BEAN_OBJECT_TYPE)) {
121+
generic = getTypeFromAttribute(definition
122+
.getAttribute(FACTORY_BEAN_OBJECT_TYPE));
121123
}
122124
return generic;
123125
}
124126

125-
private Class<?> determineTypeFromDefinitionAttribute(BeanDefinition definition)
126-
throws ClassNotFoundException, LinkageError {
127-
if (definition.hasAttribute(FACTORY_BEAN_OBJECT_TYPE)) {
128-
Object attributeObject = definition.getAttribute(FACTORY_BEAN_OBJECT_TYPE);
129-
if (attributeObject instanceof Class<?>) {
130-
return (Class<?>) attributeObject;
131-
}
132-
else if (attributeObject instanceof String) {
133-
return ClassUtils.forName((String) attributeObject, null);
134-
}
135-
}
136-
return Object.class;
137-
}
138-
139127
private Class<?> getDirectFactoryBeanGeneric(
140128
ConfigurableListableBeanFactory beanFactory, BeanDefinition definition,
141129
String name) throws ClassNotFoundException, LinkageError {
142130
Class<?> factoryBeanClass = ClassUtils.forName(definition.getBeanClassName(),
143131
beanFactory.getBeanClassLoader());
144132
Class<?> generic = ResolvableType.forClass(factoryBeanClass)
145133
.as(FactoryBean.class).resolveGeneric();
146-
if (generic == null || generic.equals(Object.class)) {
147-
generic = determineTypeFromDefinitionAttribute(definition);
134+
if ((generic == null || generic.equals(Object.class))
135+
&& definition.hasAttribute(FACTORY_BEAN_OBJECT_TYPE)) {
136+
generic = getTypeFromAttribute(definition
137+
.getAttribute(FACTORY_BEAN_OBJECT_TYPE));
148138
}
149139
return generic;
150140
}
151141

142+
private Class<?> getTypeFromAttribute(Object attribute)
143+
throws ClassNotFoundException, LinkageError {
144+
if (attribute instanceof Class<?>) {
145+
return (Class<?>) attribute;
146+
}
147+
if (attribute instanceof String) {
148+
return ClassUtils.forName((String) attribute, null);
149+
}
150+
return null;
151+
}
152+
152153
/**
153154
* Factory method to get the {@link BeanTypeRegistry} for a given {@link BeanFactory}.
154155
* @param beanFactory the source bean factory

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/GzipFilterProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public void setMimeTypes(List<MimeType> mimeTypes) {
163163
}
164164

165165
public List<MimeType> getExcludedMimeTypes() {
166-
return excludedMimeTypes;
166+
return this.excludedMimeTypes;
167167
}
168168

169169
public void setExcludedMimeTypes(List<MimeType> excludedMimeTypes) {

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchRepositoriesAutoConfigurationTests.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,9 +51,7 @@ public void close() {
5151
@Test
5252
public void testDefaultRepositoryConfiguration() throws Exception {
5353
this.context = new AnnotationConfigApplicationContext();
54-
EnvironmentTestUtils.addEnvironment(this.context,
55-
"spring.data.elasticsearch.properties.path.data:target/data",
56-
"spring.data.elasticsearch.properties.path.logs:target/logs");
54+
addElasticsearchProperties(this.context);
5755
this.context.register(TestConfiguration.class,
5856
ElasticsearchAutoConfiguration.class,
5957
ElasticsearchRepositoriesAutoConfiguration.class,
@@ -67,9 +65,7 @@ public void testDefaultRepositoryConfiguration() throws Exception {
6765
@Test
6866
public void testNoRepositoryConfiguration() throws Exception {
6967
this.context = new AnnotationConfigApplicationContext();
70-
EnvironmentTestUtils.addEnvironment(this.context,
71-
"spring.data.elasticsearch.properties.path.data:target/data",
72-
"spring.data.elasticsearch.properties.path.logs:target/logs");
68+
addElasticsearchProperties(this.context);
7369
this.context.register(EmptyConfiguration.class,
7470
ElasticsearchAutoConfiguration.class,
7571
ElasticsearchRepositoriesAutoConfiguration.class,
@@ -82,9 +78,7 @@ public void testNoRepositoryConfiguration() throws Exception {
8278
@Test
8379
public void doesNotTriggerDefaultRepositoryDetectionIfCustomized() {
8480
this.context = new AnnotationConfigApplicationContext();
85-
EnvironmentTestUtils.addEnvironment(this.context,
86-
"spring.data.elasticsearch.properties.path.data:target/data",
87-
"spring.data.elasticsearch.properties.path.logs:target/logs");
81+
addElasticsearchProperties(this.context);
8882
this.context.register(CustomizedConfiguration.class,
8983
ElasticsearchAutoConfiguration.class,
9084
ElasticsearchRepositoriesAutoConfiguration.class,
@@ -94,6 +88,12 @@ public void doesNotTriggerDefaultRepositoryDetectionIfCustomized() {
9488
assertNotNull(this.context.getBean(CityElasticsearchDbRepository.class));
9589
}
9690

91+
private void addElasticsearchProperties(AnnotationConfigApplicationContext context) {
92+
EnvironmentTestUtils.addEnvironment(context,
93+
"spring.data.elasticsearch.properties.path.data:target/data",
94+
"spring.data.elasticsearch.properties.path.logs:target/logs");
95+
}
96+
9797
@Configuration
9898
@TestAutoConfigurationPackage(City.class)
9999
protected static class TestConfiguration {

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchAutoConfigurationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchDataAutoConfigurationTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,4 +54,5 @@ public void templateExists() {
5454
assertEquals(1,
5555
this.context.getBeanNamesForType(ElasticsearchTemplate.class).length);
5656
}
57+
5758
}

spring-boot-cli/src/main/java/org/springframework/boot/cli/command/shell/RunProcessCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
class RunProcessCommand extends AbstractCommand {
3535

3636
private final String[] command;
37+
3738
private volatile RunProcess process;
3839

3940
public RunProcessCommand(String... command) {

spring-boot-samples/spring-boot-sample-data-elasticsearch/src/test/java/sample/data/elasticsearch/SampleElasticsearchApplicationTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,17 +33,18 @@
3333
*/
3434
public class SampleElasticsearchApplicationTests {
3535

36+
private static final String[] PROPERTIES = {
37+
"spring.data.elasticsearch.properties.path.data:target/data",
38+
"spring.data.elasticsearch.properties.path.logs:target/logs" };
39+
3640
@Rule
3741
public OutputCapture outputCapture = new OutputCapture();
3842

3943
@Test
4044
public void testDefaultSettings() throws Exception {
4145
try {
4246
new SpringApplicationBuilder(SampleElasticsearchApplication.class)
43-
.properties(
44-
"spring.data.elasticsearch.properties.path.data:target/data",
45-
"spring.data.elasticsearch.properties.path.logs:target/logs")
46-
.run();
47+
.properties(PROPERTIES).run();
4748
}
4849
catch (IllegalStateException ex) {
4950
if (serverNotRunning(ex)) {

spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarEntryData.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.io.InputStream;
2121
import java.lang.ref.SoftReference;
22+
import java.util.Calendar;
2223
import java.util.GregorianCalendar;
2324
import java.util.zip.ZipEntry;
2425

@@ -31,6 +32,7 @@
3132
* the entry is actually needed.
3233
*
3334
* @author Phillip Webb
35+
* @author Andy Wilkinson
3436
*/
3537
public final class JarEntryData {
3638

@@ -146,20 +148,27 @@ public int getMethod() {
146148
}
147149

148150
public long getTime() {
149-
long time = Bytes.littleEndianValue(this.header, 12, 2);
150-
151-
int seconds = (int) ((time << 1) & 0x3E);
152-
int minutes = (int) ((time >> 5) & 0x3F);
153-
int hours = (int) ((time >> 11) & 0x1F);
154-
155151
long date = Bytes.littleEndianValue(this.header, 14, 2);
152+
long time = Bytes.littleEndianValue(this.header, 12, 2);
153+
return decodeMsDosFormatDateTime(date, time).getTimeInMillis();
154+
}
156155

157-
int day = (int) (date & 0x1F);
158-
int month = (int) ((date >> 5) & 0xF) - 1;
156+
/**
157+
* Decode MSDOS Date Time details. See <a
158+
* href="http://mindprod.com/jgloss/zip.html">mindprod.com/jgloss/zip.html</a> for
159+
* more details of the format.
160+
* @param date the date part
161+
* @param time the time part
162+
* @return a {@link Calendar} containing the decoded date.
163+
*/
164+
private Calendar decodeMsDosFormatDateTime(long date, long time) {
159165
int year = (int) ((date >> 9) & 0x7F) + 1980;
160-
161-
return new GregorianCalendar(year, month, day, hours, minutes, seconds)
162-
.getTimeInMillis();
166+
int month = (int) ((date >> 5) & 0xF) - 1;
167+
int day = (int) (date & 0x1F);
168+
int hours = (int) ((time >> 11) & 0x1F);
169+
int minutes = (int) ((time >> 5) & 0x3F);
170+
int seconds = (int) ((time << 1) & 0x3E);
171+
return new GregorianCalendar(year, month, day, hours, minutes, seconds);
163172
}
164173

165174
public long getCrc() {

spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-boot/src/main/java/org/springframework/boot/bind/PropertySourcesPropertyValues.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,7 @@ private void processEnumerablePropertySource(EnumerablePropertySource<?> source,
121121
.contains(source.getName()) && !includes.matches(propertyName)) {
122122
continue;
123123
}
124-
Object value = null;
125-
try {
126-
value = resolver.getProperty(propertyName, Object.class);
127-
}
128-
catch (RuntimeException ex) {
129-
// Probably could not resolve placeholders, ignore it here
130-
if (value == null) {
131-
value = source.getProperty(propertyName);
132-
}
133-
}
124+
Object value = getEnumerableProperty(source, resolver, propertyName);
134125
if (!this.propertyValues.containsKey(propertyName)) {
135126
this.propertyValues.put(propertyName, new PropertyValue(propertyName,
136127
value));
@@ -139,6 +130,17 @@ private void processEnumerablePropertySource(EnumerablePropertySource<?> source,
139130
}
140131
}
141132

133+
private Object getEnumerableProperty(EnumerablePropertySource<?> source,
134+
PropertySourcesPropertyResolver resolver, String propertyName) {
135+
try {
136+
return resolver.getProperty(propertyName, Object.class);
137+
}
138+
catch (RuntimeException ex) {
139+
// Probably could not resolve placeholders, ignore it here
140+
return source.getProperty(propertyName);
141+
}
142+
}
143+
142144
private void processCompositePropertySource(CompositePropertySource source,
143145
PropertySourcesPropertyResolver resolver,
144146
PropertyNamePatternsMatcher includes, Collection<String> exacts) {

0 commit comments

Comments
 (0)