Commit edaed415 authored by Phillip Webb's avatar Phillip Webb

Polish

parent 9dec27e7
......@@ -21,6 +21,7 @@ import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.naming.InitialContext;
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;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
......@@ -27,21 +28,25 @@ import javax.naming.spi.InitialContextFactory;
import org.hamcrest.Matcher;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
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.iterableWithSize;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link ConditionalOnJndi}
*
* @author Stephane Nicoll
* @author Phillip Webb
*/
public class ConditionalOnJndiTests {
......@@ -49,11 +54,14 @@ public class ConditionalOnJndiTests {
private ConfigurableApplicationContext context;
private MockableOnJndi condition = new MockableOnJndi();
@After
public void close() {
TestableInitialContextFactory.clearAll();
if (this.initialContextFactory != null) {
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, this.initialContextFactory);
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
this.initialContextFactory);
}
else {
System.clearProperty(Context.INITIAL_CONTEXT_FACTORY);
......@@ -91,13 +99,27 @@ public class ConditionalOnJndiTests {
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() {
this.initialContextFactory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
TestableInitialContextFactory.class.getName());
}
private void assertPresent(boolean expected) {
int expectedNumber = expected ? 1 : 0;
Matcher<Iterable<String>> matcher = iterableWithSize(expectedNumber);
......@@ -113,6 +135,15 @@ public class ConditionalOnJndiTests {
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
@ConditionalOnJndi
static class JndiAvailableConfiguration {
......@@ -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 {
private static TestableContext context;
@Override
public Context getInitialContext(Hashtable<?, ?> environment)
throws NamingException {
return getContext();
......@@ -147,8 +204,8 @@ public class ConditionalOnJndiTests {
try {
getContext().bind(name, obj);
}
catch (NamingException o_O) {
throw new IllegalStateException(o_O);
catch (NamingException ex) {
throw new IllegalStateException(ex);
}
}
......@@ -161,14 +218,13 @@ public class ConditionalOnJndiTests {
try {
context = new TestableContext();
}
catch (NamingException o_O) {
throw new IllegalStateException(o_O);
catch (NamingException ex) {
throw new IllegalStateException(ex);
}
}
return context;
}
private static class TestableContext extends InitialContext {
private final Map<String, Object> bindings = new HashMap<String, Object>();
......@@ -178,8 +234,7 @@ public class ConditionalOnJndiTests {
}
@Override
public void bind(String name, Object obj)
throws NamingException {
public void bind(String name, Object obj) throws NamingException {
this.bindings.put(name, obj);
}
......@@ -190,7 +245,8 @@ public class ConditionalOnJndiTests {
@Override
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() {
......
......@@ -91,13 +91,13 @@ public class JtaAutoConfigurationTests {
@Test
public void disableJtaSupport() {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
"spring.jta.enabled:false");
EnvironmentTestUtils.addEnvironment(this.context, "spring.jta.enabled:false");
this.context.register(JtaAutoConfiguration.class);
this.context.refresh();
assertEquals(0, this.context.getBeansOfType(JtaTransactionManager.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
......
......@@ -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
converters. You can also override default converters that way.
[[boot-features-spring-message-codes]]
==== MessageCodesResolver
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