|
| 1 | +/* |
| 2 | + * Copyright 2012-2014 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.condition; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import org.junit.Test; |
| 23 | +import org.springframework.core.type.AnnotatedTypeMetadata; |
| 24 | + |
| 25 | +import static org.hamcrest.Matchers.equalTo; |
| 26 | +import static org.junit.Assert.assertThat; |
| 27 | +import static org.mockito.BDDMockito.given; |
| 28 | +import static org.mockito.Mockito.mock; |
| 29 | + |
| 30 | +/** |
| 31 | + * Tests for {@link OnJndiCondition}. |
| 32 | + * |
| 33 | + * @author Phillip Webb |
| 34 | + */ |
| 35 | +public class ConditionOnJndiTests { |
| 36 | + |
| 37 | + private MockableOnJndi condition = new MockableOnJndi(); |
| 38 | + |
| 39 | + @Test |
| 40 | + public void jndiNotAvailable() { |
| 41 | + this.condition.setJndiAvailable(false); |
| 42 | + ConditionOutcome outcome = this.condition.getMatchOutcome(null, mockMetaData()); |
| 43 | + assertThat(outcome.isMatch(), equalTo(false)); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void jndiLocationNotFound() { |
| 48 | + ConditionOutcome outcome = this.condition.getMatchOutcome(null, |
| 49 | + mockMetaData("java:/a")); |
| 50 | + assertThat(outcome.isMatch(), equalTo(false)); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void jndiLocationFound() { |
| 55 | + this.condition.setFoundLocation("java:/b"); |
| 56 | + ConditionOutcome outcome = this.condition.getMatchOutcome(null, |
| 57 | + mockMetaData("java:/a", "java:/b")); |
| 58 | + assertThat(outcome.isMatch(), equalTo(true)); |
| 59 | + } |
| 60 | + |
| 61 | + private AnnotatedTypeMetadata mockMetaData(String... value) { |
| 62 | + AnnotatedTypeMetadata metadata = mock(AnnotatedTypeMetadata.class); |
| 63 | + Map<String, Object> attributes = new HashMap<String, Object>(); |
| 64 | + attributes.put("value", value); |
| 65 | + given(metadata.getAnnotationAttributes(ConditionalOnJndi.class.getName())) |
| 66 | + .willReturn(attributes); |
| 67 | + return metadata; |
| 68 | + } |
| 69 | + |
| 70 | + private static class MockableOnJndi extends OnJndiCondition { |
| 71 | + |
| 72 | + private boolean jndiAvailable = true; |
| 73 | + |
| 74 | + private String foundLocation; |
| 75 | + |
| 76 | + @Override |
| 77 | + protected boolean isJndiAvailable() { |
| 78 | + return this.jndiAvailable; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + protected JndiLocator getJndiLocator(String[] locations) { |
| 83 | + return new JndiLocator(locations) { |
| 84 | + @Override |
| 85 | + public String lookupFirstLocation() { |
| 86 | + return MockableOnJndi.this.foundLocation; |
| 87 | + } |
| 88 | + }; |
| 89 | + } |
| 90 | + |
| 91 | + public void setJndiAvailable(boolean jndiAvailable) { |
| 92 | + this.jndiAvailable = jndiAvailable; |
| 93 | + } |
| 94 | + |
| 95 | + public void setFoundLocation(String foundLocation) { |
| 96 | + this.foundLocation = foundLocation; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments