Consistent bracket alignment
This commit is contained in:
@@ -535,7 +535,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
|
||||
@Override
|
||||
public void setStatus(int status) {
|
||||
if(!this.isCommitted()) {
|
||||
if (!this.isCommitted()) {
|
||||
this.status = status;
|
||||
}
|
||||
}
|
||||
@@ -543,7 +543,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
@Override
|
||||
@Deprecated
|
||||
public void setStatus(int status, String errorMessage) {
|
||||
if(!this.isCommitted()) {
|
||||
if (!this.isCommitted()) {
|
||||
this.status = status;
|
||||
this.errorMessage = errorMessage;
|
||||
}
|
||||
|
||||
@@ -78,6 +78,7 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
|
||||
*/
|
||||
protected abstract SmartContextLoader getAnnotationConfigLoader();
|
||||
|
||||
|
||||
// --- SmartContextLoader --------------------------------------------------
|
||||
|
||||
private static String name(SmartContextLoader loader) {
|
||||
@@ -94,6 +95,7 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
|
||||
|
||||
private static ApplicationContext delegateLoading(SmartContextLoader loader, MergedContextConfiguration mergedConfig)
|
||||
throws Exception {
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(String.format("Delegating to %s to load context from %s.", name(loader), mergedConfig));
|
||||
}
|
||||
@@ -103,7 +105,8 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
|
||||
private boolean supports(SmartContextLoader loader, MergedContextConfiguration mergedConfig) {
|
||||
if (loader == getAnnotationConfigLoader()) {
|
||||
return ObjectUtils.isEmpty(mergedConfig.getLocations()) && !ObjectUtils.isEmpty(mergedConfig.getClasses());
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
return !ObjectUtils.isEmpty(mergedConfig.getLocations()) && ObjectUtils.isEmpty(mergedConfig.getClasses());
|
||||
}
|
||||
}
|
||||
@@ -111,12 +114,10 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
|
||||
/**
|
||||
* Delegates to candidate {@code SmartContextLoaders} to process the supplied
|
||||
* {@link ContextConfigurationAttributes}.
|
||||
*
|
||||
* <p>Delegation is based on explicit knowledge of the implementations of the
|
||||
* default loaders for {@link #getXmlLoader() XML configuration files} and
|
||||
* {@link #getAnnotationConfigLoader() annotated classes}. Specifically, the
|
||||
* delegation algorithm is as follows:
|
||||
*
|
||||
* <ul>
|
||||
* <li>If the resource locations or annotated classes in the supplied
|
||||
* {@code ContextConfigurationAttributes} are not empty, the appropriate
|
||||
@@ -131,7 +132,6 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
|
||||
* If the annotation-based loader detects default configuration
|
||||
* classes, an {@code info} message will be logged.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param configAttributes the context configuration attributes to process
|
||||
* @throws IllegalArgumentException if the supplied configuration attributes are
|
||||
* {@code null}, or if the supplied configuration attributes include both
|
||||
@@ -147,17 +147,19 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
|
||||
|
||||
Assert.notNull(configAttributes, "configAttributes must not be null");
|
||||
Assert.isTrue(!(configAttributes.hasLocations() && configAttributes.hasClasses()), String.format(
|
||||
"Cannot process locations AND classes for context "
|
||||
+ "configuration %s; configure one or the other, but not both.", configAttributes));
|
||||
"Cannot process locations AND classes for context configuration %s; configure one or the other, but not both.",
|
||||
configAttributes));
|
||||
|
||||
// If the original locations or classes were not empty, there's no
|
||||
// need to bother with default detection checks; just let the
|
||||
// appropriate loader process the configuration.
|
||||
if (configAttributes.hasLocations()) {
|
||||
delegateProcessing(getXmlLoader(), configAttributes);
|
||||
} else if (configAttributes.hasClasses()) {
|
||||
}
|
||||
else if (configAttributes.hasClasses()) {
|
||||
delegateProcessing(getAnnotationConfigLoader(), configAttributes);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
// Else attempt to detect defaults...
|
||||
|
||||
// Let the XML loader process the configuration.
|
||||
@@ -198,15 +200,15 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
|
||||
// throw an exception.
|
||||
if (!configAttributes.hasResources() && ObjectUtils.isEmpty(configAttributes.getInitializers())) {
|
||||
throw new IllegalStateException(String.format(
|
||||
"Neither %s nor %s was able to detect defaults, and no ApplicationContextInitializers "
|
||||
+ "were declared for context configuration %s", name(getXmlLoader()),
|
||||
"Neither %s nor %s was able to detect defaults, and no ApplicationContextInitializers " +
|
||||
"were declared for context configuration %s", name(getXmlLoader()),
|
||||
name(getAnnotationConfigLoader()), configAttributes));
|
||||
}
|
||||
|
||||
if (configAttributes.hasLocations() && configAttributes.hasClasses()) {
|
||||
String message = String.format(
|
||||
"Configuration error: both default locations AND default configuration classes "
|
||||
+ "were detected for context configuration %s; configure one or the other, but not both.",
|
||||
"Configuration error: both default locations AND default configuration classes " +
|
||||
"were detected for context configuration %s; configure one or the other, but not both.",
|
||||
configAttributes);
|
||||
logger.error(message);
|
||||
throw new IllegalStateException(message);
|
||||
@@ -217,12 +219,10 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
|
||||
/**
|
||||
* Delegates to an appropriate candidate {@code SmartContextLoader} to load
|
||||
* an {@link ApplicationContext}.
|
||||
*
|
||||
* <p>Delegation is based on explicit knowledge of the implementations of the
|
||||
* default loaders for {@link #getXmlLoader() XML configuration files} and
|
||||
* {@link #getAnnotationConfigLoader() annotated classes}. Specifically, the
|
||||
* delegation algorithm is as follows:
|
||||
*
|
||||
* <ul>
|
||||
* <li>If the resource locations in the supplied {@code MergedContextConfiguration}
|
||||
* are not empty and the annotated classes are empty,
|
||||
@@ -231,7 +231,6 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
|
||||
* are not empty and the resource locations are empty,
|
||||
* the annotation-based loader will load the {@code ApplicationContext}.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param mergedConfig the merged context configuration to use to load the application context
|
||||
* @throws IllegalArgumentException if the supplied merged configuration is {@code null}
|
||||
* @throws IllegalStateException if neither candidate loader is capable of loading an
|
||||
@@ -240,7 +239,6 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
|
||||
@Override
|
||||
public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
|
||||
Assert.notNull(mergedConfig, "mergedConfig must not be null");
|
||||
|
||||
List<SmartContextLoader> candidates = Arrays.asList(getXmlLoader(), getAnnotationConfigLoader());
|
||||
|
||||
for (SmartContextLoader loader : candidates) {
|
||||
@@ -262,6 +260,7 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
|
||||
name(getAnnotationConfigLoader()), mergedConfig));
|
||||
}
|
||||
|
||||
|
||||
// --- ContextLoader -------------------------------------------------------
|
||||
|
||||
/**
|
||||
@@ -272,8 +271,8 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
|
||||
*/
|
||||
@Override
|
||||
public final String[] processLocations(Class<?> clazz, String... locations) {
|
||||
throw new UnsupportedOperationException("DelegatingSmartContextLoaders do not support the ContextLoader SPI. "
|
||||
+ "Call processContextConfiguration(ContextConfigurationAttributes) instead.");
|
||||
throw new UnsupportedOperationException("DelegatingSmartContextLoaders do not support the ContextLoader SPI. " +
|
||||
"Call processContextConfiguration(ContextConfigurationAttributes) instead.");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -284,8 +283,8 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
|
||||
*/
|
||||
@Override
|
||||
public final ApplicationContext loadContext(String... locations) throws Exception {
|
||||
throw new UnsupportedOperationException("DelegatingSmartContextLoaders do not support the ContextLoader SPI. "
|
||||
+ "Call loadContext(MergedContextConfiguration) instead.");
|
||||
throw new UnsupportedOperationException("DelegatingSmartContextLoaders do not support the ContextLoader SPI. " +
|
||||
"Call loadContext(MergedContextConfiguration) instead.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.SmartContextLoader;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -38,45 +39,12 @@ public abstract class AnnotationConfigContextLoaderUtils {
|
||||
private static final Log logger = LogFactory.getLog(AnnotationConfigContextLoaderUtils.class);
|
||||
|
||||
|
||||
private AnnotationConfigContextLoaderUtils() {
|
||||
/* no-op */
|
||||
}
|
||||
|
||||
private static boolean isStaticNonPrivateAndNonFinal(Class<?> clazz) {
|
||||
Assert.notNull(clazz, "Class must not be null");
|
||||
int modifiers = clazz.getModifiers();
|
||||
return (Modifier.isStatic(modifiers) && !Modifier.isPrivate(modifiers) && !Modifier.isFinal(modifiers));
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the supplied {@link Class} meets the criteria for being
|
||||
* considered a <em>default configuration class</em> candidate.
|
||||
*
|
||||
* <p>Specifically, such candidates:
|
||||
*
|
||||
* <ul>
|
||||
* <li>must not be {@code null}</li>
|
||||
* <li>must not be {@code private}</li>
|
||||
* <li>must not be {@code final}</li>
|
||||
* <li>must be {@code static}</li>
|
||||
* <li>must be annotated with {@code @Configuration}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param clazz the class to check
|
||||
* @return {@code true} if the supplied class meets the candidate criteria
|
||||
*/
|
||||
private static boolean isDefaultConfigurationClassCandidate(Class<?> clazz) {
|
||||
return clazz != null && isStaticNonPrivateAndNonFinal(clazz) && clazz.isAnnotationPresent(Configuration.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect the default configuration classes for the supplied test class.
|
||||
*
|
||||
* <p>The returned class array will contain all static inner classes of
|
||||
* the supplied class that meet the requirements for {@code @Configuration}
|
||||
* class implementations as specified in the documentation for
|
||||
* {@link Configuration @Configuration}.
|
||||
*
|
||||
* <p>The implementation of this method adheres to the contract defined in the
|
||||
* {@link org.springframework.test.context.SmartContextLoader SmartContextLoader}
|
||||
* SPI. Specifically, this method uses introspection to detect default
|
||||
@@ -96,7 +64,8 @@ public abstract class AnnotationConfigContextLoaderUtils {
|
||||
for (Class<?> candidate : declaringClass.getDeclaredClasses()) {
|
||||
if (isDefaultConfigurationClassCandidate(candidate)) {
|
||||
configClasses.add(candidate);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(String.format(
|
||||
"Ignoring class [%s]; it must be static, non-private, non-final, and annotated "
|
||||
@@ -117,4 +86,28 @@ public abstract class AnnotationConfigContextLoaderUtils {
|
||||
return configClasses.toArray(new Class<?>[configClasses.size()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the supplied {@link Class} meets the criteria for being
|
||||
* considered a <em>default configuration class</em> candidate.
|
||||
* <p>Specifically, such candidates:
|
||||
* <ul>
|
||||
* <li>must not be {@code null}</li>
|
||||
* <li>must not be {@code private}</li>
|
||||
* <li>must not be {@code final}</li>
|
||||
* <li>must be {@code static}</li>
|
||||
* <li>must be annotated with {@code @Configuration}</li>
|
||||
* </ul>
|
||||
* @param clazz the class to check
|
||||
* @return {@code true} if the supplied class meets the candidate criteria
|
||||
*/
|
||||
private static boolean isDefaultConfigurationClassCandidate(Class<?> clazz) {
|
||||
return (clazz != null && isStaticNonPrivateAndNonFinal(clazz) && clazz.isAnnotationPresent(Configuration.class));
|
||||
}
|
||||
|
||||
private static boolean isStaticNonPrivateAndNonFinal(Class<?> clazz) {
|
||||
Assert.notNull(clazz, "Class must not be null");
|
||||
int modifiers = clazz.getModifiers();
|
||||
return (Modifier.isStatic(modifiers) && !Modifier.isPrivate(modifiers) && !Modifier.isFinal(modifiers));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ public abstract class AbstractMockMvcBuilder<B extends AbstractMockMvcBuilder<B>
|
||||
public final <T extends B> T addFilters(Filter... filters) {
|
||||
Assert.notNull(filters, "filters cannot be null");
|
||||
|
||||
for(Filter f : filters) {
|
||||
for (Filter f : filters) {
|
||||
Assert.notNull(f, "filters cannot contain null values");
|
||||
this.filters.add(f);
|
||||
}
|
||||
@@ -112,7 +112,7 @@ public abstract class AbstractMockMvcBuilder<B extends AbstractMockMvcBuilder<B>
|
||||
Assert.notNull(filter, "filter cannot be null");
|
||||
Assert.notNull(urlPatterns, "urlPatterns cannot be null");
|
||||
|
||||
if(urlPatterns.length > 0) {
|
||||
if (urlPatterns.length > 0) {
|
||||
filter = new PatternMappingFilterProxy(filter, urlPatterns);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 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
|
||||
@@ -10,12 +10,12 @@
|
||||
* 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 org.springframework.test.web.servlet.setup;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
@@ -61,29 +61,32 @@ final class PatternMappingFilterProxy implements Filter {
|
||||
public PatternMappingFilterProxy(Filter delegate, String... urlPatterns) {
|
||||
Assert.notNull(delegate, "A delegate Filter is required");
|
||||
this.delegate = delegate;
|
||||
for(String urlPattern : urlPatterns) {
|
||||
for (String urlPattern : urlPatterns) {
|
||||
addUrlPattern(urlPattern);
|
||||
}
|
||||
}
|
||||
|
||||
private void addUrlPattern(String urlPattern) {
|
||||
Assert.notNull(urlPattern, "Found null URL Pattern");
|
||||
if(urlPattern.startsWith(EXTENSION_MAPPING_PATTERN)) {
|
||||
if (urlPattern.startsWith(EXTENSION_MAPPING_PATTERN)) {
|
||||
this.endsWithMatches.add(urlPattern.substring(1, urlPattern.length()));
|
||||
} else if(urlPattern.equals(PATH_MAPPING_PATTERN)) {
|
||||
}
|
||||
else if (urlPattern.equals(PATH_MAPPING_PATTERN)) {
|
||||
this.startsWithMatches.add("");
|
||||
}
|
||||
else if (urlPattern.endsWith(PATH_MAPPING_PATTERN)) {
|
||||
this.startsWithMatches.add(urlPattern.substring(0, urlPattern.length() - 1));
|
||||
this.exactMatches.add(urlPattern.substring(0, urlPattern.length() - 2));
|
||||
} else {
|
||||
if("".equals(urlPattern)) {
|
||||
}
|
||||
else {
|
||||
if ("".equals(urlPattern)) {
|
||||
urlPattern = "/";
|
||||
}
|
||||
this.exactMatches.add(urlPattern);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
|
||||
throws IOException, ServletException {
|
||||
@@ -91,29 +94,30 @@ final class PatternMappingFilterProxy implements Filter {
|
||||
HttpServletRequest httpRequest = (HttpServletRequest) request;
|
||||
String requestPath = urlPathHelper.getPathWithinApplication(httpRequest);
|
||||
|
||||
if(matches(requestPath)) {
|
||||
if (matches(requestPath)) {
|
||||
this.delegate.doFilter(request, response, filterChain);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
filterChain.doFilter(request, response);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean matches(String requestPath) {
|
||||
for(String pattern : this.exactMatches) {
|
||||
if(pattern.equals(requestPath)) {
|
||||
for (String pattern : this.exactMatches) {
|
||||
if (pattern.equals(requestPath)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if(!requestPath.startsWith("/")) {
|
||||
if (!requestPath.startsWith("/")) {
|
||||
return false;
|
||||
}
|
||||
for(String pattern : this.endsWithMatches) {
|
||||
if(requestPath.endsWith(pattern)) {
|
||||
for (String pattern : this.endsWithMatches) {
|
||||
if (requestPath.endsWith(pattern)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
for(String pattern : this.startsWithMatches) {
|
||||
if(requestPath.startsWith(pattern)) {
|
||||
for (String pattern : this.startsWithMatches) {
|
||||
if (requestPath.startsWith(pattern)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user