Skip to content

Commit

Permalink
Polish
Browse files Browse the repository at this point in the history
  • Loading branch information
philwebb committed Jun 2, 2015
1 parent 10f7031 commit 968b68c
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,12 @@ private int getStatus(HttpServletResponse response) {
}

private boolean is4xxClientError(int status) {
HttpStatus httpStatus = HttpStatus.OK;
try {
httpStatus = HttpStatus.valueOf(status);
return HttpStatus.valueOf(status).is4xxClientError();
}
catch (Exception ex) {
// not convertible
return false;
}
return httpStatus.is4xxClientError();
}

private String getKey(String string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
import org.springframework.security.web.FilterChainProxy;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

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

MockMvc mockMvc = MockMvcBuilders
.webAppContextSetup((WebApplicationContext) this.context)
.addFilters(
this.context.getBean("springSecurityFilterChain", Filter.class))
.build();

Filter filter = this.context.getBean("springSecurityFilterChain", Filter.class);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.addFilters(filter).build();

// no user (Main)
mockMvc.perform(
MockMvcRequestBuilders.get("/"))
mockMvc.perform(MockMvcRequestBuilders.get("/"))
.andExpect(MockMvcResultMatchers.status().isUnauthorized())
.andExpect(
MockMvcResultMatchers.header().string("www-authenticate",
Matchers.containsString("realm=\"Spring\"")));
.andExpect(springAuthenticateRealmHeader());

// invalid user (Main)
mockMvc.perform(
MockMvcRequestBuilders.get("/").header("authorization", "Basic xxx"))
.andExpect(MockMvcResultMatchers.status().isUnauthorized())
.andExpect(
MockMvcResultMatchers.header().string("www-authenticate",
Matchers.containsString("realm=\"Spring\"")));
.andExpect(springAuthenticateRealmHeader());

// no user (Management)
mockMvc.perform(
MockMvcRequestBuilders.get("/beans"))
mockMvc.perform(MockMvcRequestBuilders.get("/beans"))
.andExpect(MockMvcResultMatchers.status().isUnauthorized())
.andExpect(
MockMvcResultMatchers.header().string("www-authenticate",
Matchers.containsString("realm=\"Spring\"")));
.andExpect(springAuthenticateRealmHeader());

// invalid user (Management)
mockMvc.perform(
MockMvcRequestBuilders.get("/beans").header("authorization", "Basic xxx"))
.andExpect(MockMvcResultMatchers.status().isUnauthorized())
.andExpect(
MockMvcResultMatchers.header().string("www-authenticate",
Matchers.containsString("realm=\"Spring\"")));
.andExpect(springAuthenticateRealmHeader());
}

private ResultMatcher springAuthenticateRealmHeader() {
return MockMvcResultMatchers.header().string("www-authenticate",
Matchers.containsString("realm=\"Spring\""));
}

@EnableGlobalAuthentication
@Configuration
static class AuthenticationConfig {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
auth.inMemoryAuthentication().withUser("user").password("password")
.roles("USER");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,39 +116,40 @@ private Class<?> getConfigurationClassFactoryBeanGeneric(
definition.getFactoryMethodName());
Class<?> generic = ResolvableType.forMethodReturnType(method)
.as(FactoryBean.class).resolveGeneric();
if (generic == null || generic.equals(Object.class)) {
generic = determineTypeFromDefinitionAttribute(factoryDefinition);
if ((generic == null || generic.equals(Object.class))
&& definition.hasAttribute(FACTORY_BEAN_OBJECT_TYPE)) {
generic = getTypeFromAttribute(definition
.getAttribute(FACTORY_BEAN_OBJECT_TYPE));
}
return generic;
}

private Class<?> determineTypeFromDefinitionAttribute(BeanDefinition definition)
throws ClassNotFoundException, LinkageError {
if (definition.hasAttribute(FACTORY_BEAN_OBJECT_TYPE)) {
Object attributeObject = definition.getAttribute(FACTORY_BEAN_OBJECT_TYPE);
if (attributeObject instanceof Class<?>) {
return (Class<?>) attributeObject;
}
else if (attributeObject instanceof String) {
return ClassUtils.forName((String) attributeObject, null);
}
}
return Object.class;
}

private Class<?> getDirectFactoryBeanGeneric(
ConfigurableListableBeanFactory beanFactory, BeanDefinition definition,
String name) throws ClassNotFoundException, LinkageError {
Class<?> factoryBeanClass = ClassUtils.forName(definition.getBeanClassName(),
beanFactory.getBeanClassLoader());
Class<?> generic = ResolvableType.forClass(factoryBeanClass)
.as(FactoryBean.class).resolveGeneric();
if (generic == null || generic.equals(Object.class)) {
generic = determineTypeFromDefinitionAttribute(definition);
if ((generic == null || generic.equals(Object.class))
&& definition.hasAttribute(FACTORY_BEAN_OBJECT_TYPE)) {
generic = getTypeFromAttribute(definition
.getAttribute(FACTORY_BEAN_OBJECT_TYPE));
}
return generic;
}

private Class<?> getTypeFromAttribute(Object attribute)
throws ClassNotFoundException, LinkageError {
if (attribute instanceof Class<?>) {
return (Class<?>) attribute;
}
if (attribute instanceof String) {
return ClassUtils.forName((String) attribute, null);
}
return null;
}

/**
* Factory method to get the {@link BeanTypeRegistry} for a given {@link BeanFactory}.
* @param beanFactory the source bean factory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void setMimeTypes(List<MimeType> mimeTypes) {
}

public List<MimeType> getExcludedMimeTypes() {
return excludedMimeTypes;
return this.excludedMimeTypes;
}

public void setExcludedMimeTypes(List<MimeType> excludedMimeTypes) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2015 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.
Expand Down Expand Up @@ -51,9 +51,7 @@ public void close() {
@Test
public void testDefaultRepositoryConfiguration() throws Exception {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
"spring.data.elasticsearch.properties.path.data:target/data",
"spring.data.elasticsearch.properties.path.logs:target/logs");
addElasticsearchProperties(this.context);
this.context.register(TestConfiguration.class,
ElasticsearchAutoConfiguration.class,
ElasticsearchRepositoriesAutoConfiguration.class,
Expand All @@ -67,9 +65,7 @@ public void testDefaultRepositoryConfiguration() throws Exception {
@Test
public void testNoRepositoryConfiguration() throws Exception {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
"spring.data.elasticsearch.properties.path.data:target/data",
"spring.data.elasticsearch.properties.path.logs:target/logs");
addElasticsearchProperties(this.context);
this.context.register(EmptyConfiguration.class,
ElasticsearchAutoConfiguration.class,
ElasticsearchRepositoriesAutoConfiguration.class,
Expand All @@ -82,9 +78,7 @@ public void testNoRepositoryConfiguration() throws Exception {
@Test
public void doesNotTriggerDefaultRepositoryDetectionIfCustomized() {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
"spring.data.elasticsearch.properties.path.data:target/data",
"spring.data.elasticsearch.properties.path.logs:target/logs");
addElasticsearchProperties(this.context);
this.context.register(CustomizedConfiguration.class,
ElasticsearchAutoConfiguration.class,
ElasticsearchRepositoriesAutoConfiguration.class,
Expand All @@ -94,6 +88,12 @@ public void doesNotTriggerDefaultRepositoryDetectionIfCustomized() {
assertNotNull(this.context.getBean(CityElasticsearchDbRepository.class));
}

private void addElasticsearchProperties(AnnotationConfigApplicationContext context) {
EnvironmentTestUtils.addEnvironment(context,
"spring.data.elasticsearch.properties.path.data:target/data",
"spring.data.elasticsearch.properties.path.logs:target/logs");
}

@Configuration
@TestAutoConfigurationPackage(City.class)
protected static class TestConfiguration {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2015 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2015 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.
Expand Down Expand Up @@ -54,4 +54,5 @@ public void templateExists() {
assertEquals(1,
this.context.getBeanNamesForType(ElasticsearchTemplate.class).length);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
class RunProcessCommand extends AbstractCommand {

private final String[] command;

private volatile RunProcess process;

public RunProcessCommand(String... command) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2015 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.
Expand Down Expand Up @@ -33,17 +33,18 @@
*/
public class SampleElasticsearchApplicationTests {

private static final String[] PROPERTIES = {
"spring.data.elasticsearch.properties.path.data:target/data",
"spring.data.elasticsearch.properties.path.logs:target/logs" };

@Rule
public OutputCapture outputCapture = new OutputCapture();

@Test
public void testDefaultSettings() throws Exception {
try {
new SpringApplicationBuilder(SampleElasticsearchApplication.class)
.properties(
"spring.data.elasticsearch.properties.path.data:target/data",
"spring.data.elasticsearch.properties.path.logs:target/logs")
.run();
.properties(PROPERTIES).run();
}
catch (IllegalStateException ex) {
if (serverNotRunning(ex)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2015 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.
Expand All @@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.zip.ZipEntry;

Expand All @@ -31,6 +32,7 @@
* the entry is actually needed.
*
* @author Phillip Webb
* @author Andy Wilkinson
*/
public final class JarEntryData {

Expand Down Expand Up @@ -146,20 +148,27 @@ public int getMethod() {
}

public long getTime() {
long time = Bytes.littleEndianValue(this.header, 12, 2);

int seconds = (int) ((time << 1) & 0x3E);
int minutes = (int) ((time >> 5) & 0x3F);
int hours = (int) ((time >> 11) & 0x1F);

long date = Bytes.littleEndianValue(this.header, 14, 2);
long time = Bytes.littleEndianValue(this.header, 12, 2);
return decodeMsDosFormatDateTime(date, time).getTimeInMillis();
}

int day = (int) (date & 0x1F);
int month = (int) ((date >> 5) & 0xF) - 1;
/**
* Decode MSDOS Date Time details. See <a
* href="http://mindprod.com/jgloss/zip.html">mindprod.com/jgloss/zip.html</a> for
* more details of the format.
* @param date the date part
* @param time the time part
* @return a {@link Calendar} containing the decoded date.
*/
private Calendar decodeMsDosFormatDateTime(long date, long time) {
int year = (int) ((date >> 9) & 0x7F) + 1980;

return new GregorianCalendar(year, month, day, hours, minutes, seconds)
.getTimeInMillis();
int month = (int) ((date >> 5) & 0xF) - 1;
int day = (int) (date & 0x1F);
int hours = (int) ((time >> 11) & 0x1F);
int minutes = (int) ((time >> 5) & 0x3F);
int seconds = (int) ((time << 1) & 0x3E);
return new GregorianCalendar(year, month, day, hours, minutes, seconds);
}

public long getCrc() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2015 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,7 @@ private void processEnumerablePropertySource(EnumerablePropertySource<?> source,
.contains(source.getName()) && !includes.matches(propertyName)) {
continue;
}
Object value = null;
try {
value = resolver.getProperty(propertyName, Object.class);
}
catch (RuntimeException ex) {
// Probably could not resolve placeholders, ignore it here
if (value == null) {
value = source.getProperty(propertyName);
}
}
Object value = getEnumerableProperty(source, resolver, propertyName);
if (!this.propertyValues.containsKey(propertyName)) {
this.propertyValues.put(propertyName, new PropertyValue(propertyName,
value));
Expand All @@ -139,6 +130,17 @@ private void processEnumerablePropertySource(EnumerablePropertySource<?> source,
}
}

private Object getEnumerableProperty(EnumerablePropertySource<?> source,
PropertySourcesPropertyResolver resolver, String propertyName) {
try {
return resolver.getProperty(propertyName, Object.class);
}
catch (RuntimeException ex) {
// Probably could not resolve placeholders, ignore it here
return source.getProperty(propertyName);
}
}

private void processCompositePropertySource(CompositePropertySource source,
PropertySourcesPropertyResolver resolver,
PropertyNamePatternsMatcher includes, Collection<String> exacts) {
Expand Down
Loading

0 comments on commit 968b68c

Please sign in to comment.