Commit edaed415 authored by Phillip Webb's avatar Phillip Webb

Polish

parent 9dec27e7
...@@ -21,6 +21,7 @@ import java.lang.annotation.ElementType; ...@@ -21,6 +21,7 @@ import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import javax.naming.InitialContext; import javax.naming.InitialContext;
import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Conditional;
......
/*
* Copyright 2012-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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on 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.boot.autoconfigure.condition;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.springframework.core.type.AnnotatedTypeMetadata;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link OnJndiCondition}.
*
* @author Phillip Webb
*/
public class ConditionOnJndiTests {
private MockableOnJndi condition = new MockableOnJndi();
@Test
public void jndiNotAvailable() {
this.condition.setJndiAvailable(false);
ConditionOutcome outcome = this.condition.getMatchOutcome(null, mockMetaData());
assertThat(outcome.isMatch(), equalTo(false));
}
@Test
public void jndiLocationNotFound() {
ConditionOutcome outcome = this.condition.getMatchOutcome(null,
mockMetaData("java:/a"));
assertThat(outcome.isMatch(), equalTo(false));
}
@Test
public void jndiLocationFound() {
this.condition.setFoundLocation("java:/b");
ConditionOutcome outcome = this.condition.getMatchOutcome(null,
mockMetaData("java:/a", "java:/b"));
assertThat(outcome.isMatch(), equalTo(true));
}
private AnnotatedTypeMetadata mockMetaData(String... value) {
AnnotatedTypeMetadata metadata = mock(AnnotatedTypeMetadata.class);
Map<String, Object> attributes = new HashMap<String, Object>();
attributes.put("value", value);
given(metadata.getAnnotationAttributes(ConditionalOnJndi.class.getName()))
.willReturn(attributes);
return metadata;
}
private static class MockableOnJndi extends OnJndiCondition {
private boolean jndiAvailable = true;
private String foundLocation;
@Override
protected boolean isJndiAvailable() {
return this.jndiAvailable;
}
@Override
protected JndiLocator getJndiLocator(String[] locations) {
return new JndiLocator(locations) {
@Override
public String lookupFirstLocation() {
return MockableOnJndi.this.foundLocation;
}
};
}
public void setJndiAvailable(boolean jndiAvailable) {
this.jndiAvailable = jndiAvailable;
}
public void setFoundLocation(String foundLocation) {
this.foundLocation = foundLocation;
}
}
}
...@@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.condition; ...@@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.condition;
import java.util.HashMap; import java.util.HashMap;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Map; import java.util.Map;
import javax.naming.Context; import javax.naming.Context;
import javax.naming.InitialContext; import javax.naming.InitialContext;
import javax.naming.NamingException; import javax.naming.NamingException;
...@@ -27,21 +28,25 @@ import javax.naming.spi.InitialContextFactory; ...@@ -27,21 +28,25 @@ import javax.naming.spi.InitialContextFactory;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.type.AnnotatedTypeMetadata;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.iterableWithSize; import static org.hamcrest.Matchers.iterableWithSize;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/** /**
* Tests for {@link ConditionalOnJndi} * Tests for {@link ConditionalOnJndi}
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Phillip Webb
*/ */
public class ConditionalOnJndiTests { public class ConditionalOnJndiTests {
...@@ -49,11 +54,14 @@ public class ConditionalOnJndiTests { ...@@ -49,11 +54,14 @@ public class ConditionalOnJndiTests {
private ConfigurableApplicationContext context; private ConfigurableApplicationContext context;
private MockableOnJndi condition = new MockableOnJndi();
@After @After
public void close() { public void close() {
TestableInitialContextFactory.clearAll(); TestableInitialContextFactory.clearAll();
if (this.initialContextFactory != null) { if (this.initialContextFactory != null) {
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, this.initialContextFactory); System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
this.initialContextFactory);
} }
else { else {
System.clearProperty(Context.INITIAL_CONTEXT_FACTORY); System.clearProperty(Context.INITIAL_CONTEXT_FACTORY);
...@@ -91,13 +99,27 @@ public class ConditionalOnJndiTests { ...@@ -91,13 +99,27 @@ public class ConditionalOnJndiTests {
assertPresent(true); assertPresent(true);
} }
@Test
public void jndiLocationNotFound() {
ConditionOutcome outcome = this.condition.getMatchOutcome(null,
mockMetaData("java:/a"));
assertThat(outcome.isMatch(), equalTo(false));
}
@Test
public void jndiLocationFound() {
this.condition.setFoundLocation("java:/b");
ConditionOutcome outcome = this.condition.getMatchOutcome(null,
mockMetaData("java:/a", "java:/b"));
assertThat(outcome.isMatch(), equalTo(true));
}
private void setupJndi() { private void setupJndi() {
this.initialContextFactory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY); this.initialContextFactory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
TestableInitialContextFactory.class.getName()); TestableInitialContextFactory.class.getName());
} }
private void assertPresent(boolean expected) { private void assertPresent(boolean expected) {
int expectedNumber = expected ? 1 : 0; int expectedNumber = expected ? 1 : 0;
Matcher<Iterable<String>> matcher = iterableWithSize(expectedNumber); Matcher<Iterable<String>> matcher = iterableWithSize(expectedNumber);
...@@ -113,6 +135,15 @@ public class ConditionalOnJndiTests { ...@@ -113,6 +135,15 @@ public class ConditionalOnJndiTests {
this.context = applicationContext; this.context = applicationContext;
} }
private AnnotatedTypeMetadata mockMetaData(String... value) {
AnnotatedTypeMetadata metadata = mock(AnnotatedTypeMetadata.class);
Map<String, Object> attributes = new HashMap<String, Object>();
attributes.put("value", value);
given(metadata.getAnnotationAttributes(ConditionalOnJndi.class.getName()))
.willReturn(attributes);
return metadata;
}
@Configuration @Configuration
@ConditionalOnJndi @ConditionalOnJndi
static class JndiAvailableConfiguration { static class JndiAvailableConfiguration {
...@@ -133,11 +164,37 @@ public class ConditionalOnJndiTests { ...@@ -133,11 +164,37 @@ public class ConditionalOnJndiTests {
} }
} }
private static class MockableOnJndi extends OnJndiCondition {
private boolean jndiAvailable = true;
private String foundLocation;
@Override
protected boolean isJndiAvailable() {
return this.jndiAvailable;
}
@Override
protected JndiLocator getJndiLocator(String[] locations) {
return new JndiLocator(locations) {
@Override
public String lookupFirstLocation() {
return MockableOnJndi.this.foundLocation;
}
};
}
public void setFoundLocation(String foundLocation) {
this.foundLocation = foundLocation;
}
}
public static class TestableInitialContextFactory implements InitialContextFactory { public static class TestableInitialContextFactory implements InitialContextFactory {
private static TestableContext context; private static TestableContext context;
@Override
public Context getInitialContext(Hashtable<?, ?> environment) public Context getInitialContext(Hashtable<?, ?> environment)
throws NamingException { throws NamingException {
return getContext(); return getContext();
...@@ -147,8 +204,8 @@ public class ConditionalOnJndiTests { ...@@ -147,8 +204,8 @@ public class ConditionalOnJndiTests {
try { try {
getContext().bind(name, obj); getContext().bind(name, obj);
} }
catch (NamingException o_O) { catch (NamingException ex) {
throw new IllegalStateException(o_O); throw new IllegalStateException(ex);
} }
} }
...@@ -161,14 +218,13 @@ public class ConditionalOnJndiTests { ...@@ -161,14 +218,13 @@ public class ConditionalOnJndiTests {
try { try {
context = new TestableContext(); context = new TestableContext();
} }
catch (NamingException o_O) { catch (NamingException ex) {
throw new IllegalStateException(o_O); throw new IllegalStateException(ex);
} }
} }
return context; return context;
} }
private static class TestableContext extends InitialContext { private static class TestableContext extends InitialContext {
private final Map<String, Object> bindings = new HashMap<String, Object>(); private final Map<String, Object> bindings = new HashMap<String, Object>();
...@@ -178,8 +234,7 @@ public class ConditionalOnJndiTests { ...@@ -178,8 +234,7 @@ public class ConditionalOnJndiTests {
} }
@Override @Override
public void bind(String name, Object obj) public void bind(String name, Object obj) throws NamingException {
throws NamingException {
this.bindings.put(name, obj); this.bindings.put(name, obj);
} }
...@@ -190,7 +245,8 @@ public class ConditionalOnJndiTests { ...@@ -190,7 +245,8 @@ public class ConditionalOnJndiTests {
@Override @Override
public Hashtable<?, ?> getEnvironment() throws NamingException { public Hashtable<?, ?> getEnvironment() throws NamingException {
return new Hashtable<Object, Object>(); // Used to detect if JNDI is available return new Hashtable<Object, Object>(); // Used to detect if JNDI is
// available
} }
public void clearAll() { public void clearAll() {
......
...@@ -91,13 +91,13 @@ public class JtaAutoConfigurationTests { ...@@ -91,13 +91,13 @@ public class JtaAutoConfigurationTests {
@Test @Test
public void disableJtaSupport() { public void disableJtaSupport() {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, EnvironmentTestUtils.addEnvironment(this.context, "spring.jta.enabled:false");
"spring.jta.enabled:false");
this.context.register(JtaAutoConfiguration.class); this.context.register(JtaAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
assertEquals(0, this.context.getBeansOfType(JtaTransactionManager.class).size()); assertEquals(0, this.context.getBeansOfType(JtaTransactionManager.class).size());
assertEquals(0, this.context.getBeansOfType(XADataSourceWrapper.class).size()); assertEquals(0, this.context.getBeansOfType(XADataSourceWrapper.class).size());
assertEquals(0, this.context.getBeansOfType(XAConnectionFactoryWrapper.class).size()); assertEquals(0, this.context.getBeansOfType(XAConnectionFactoryWrapper.class)
.size());
} }
@Test @Test
......
...@@ -208,7 +208,7 @@ configuration instead. This is possible in both Maven and Gradle. ...@@ -208,7 +208,7 @@ configuration instead. This is possible in both Maven and Gradle.
[[production-ready-application-info-automatic-expansion-maven]] [[production-ready-application-info-automatic-expansion-maven]]
===== Automatic property expansion using Maven ===== Automatic property expansion using Maven
You can automatically expand info properties from the Maven project using resource You can automatically expand info properties from the Maven project using resource
filtering. If you use the `spring-boot-starter-parent` you can then refer to your filtering. If you use the `spring-boot-starter-parent` you can then refer to your
Maven '`project properties`' via `@..@` placeholders, e.g. Maven '`project properties`' via `@..@` placeholders, e.g.
[source,properties,indent=0] [source,properties,indent=0]
...@@ -242,16 +242,16 @@ and (inside `<plugins/>`): ...@@ -242,16 +242,16 @@ and (inside `<plugins/>`):
[source,xml,indent=0] [source,xml,indent=0]
---- ----
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId> <artifactId>maven-resources-plugin</artifactId>
<version>2.6</version> <version>2.6</version>
<configuration> <configuration>
<delimiters> <delimiters>
<delimiter>@</delimiter> <delimiter>@</delimiter>
</delimiters> </delimiters>
</configuration> </configuration>
</plugin> </plugin>
---- ----
[[production-ready-application-info-automatic-expansion-gradle]] [[production-ready-application-info-automatic-expansion-gradle]]
......
...@@ -913,6 +913,8 @@ If you need to add or customize converters you can use Spring Boot's ...@@ -913,6 +913,8 @@ If you need to add or customize converters you can use Spring Boot's
Any `HttpMessageConverter` bean that is present in the context will be added to the list of Any `HttpMessageConverter` bean that is present in the context will be added to the list of
converters. You can also override default converters that way. converters. You can also override default converters that way.
[[boot-features-spring-message-codes]] [[boot-features-spring-message-codes]]
==== MessageCodesResolver ==== MessageCodesResolver
Spring MVC has a strategy for generating error codes for rendering error messages Spring MVC has a strategy for generating error codes for rendering error messages
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment