|
| 1 | +/* |
| 2 | + * Copyright 2019 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 | + * https://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 | +package org.springframework.data.r2dbc.config; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
| 21 | +import io.r2dbc.h2.H2ConnectionConfiguration; |
| 22 | +import io.r2dbc.h2.H2ConnectionFactory; |
| 23 | +import io.r2dbc.spi.ConnectionFactory; |
| 24 | + |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 28 | +import org.springframework.context.annotation.Bean; |
| 29 | +import org.springframework.context.annotation.Configuration; |
| 30 | +import org.springframework.data.r2dbc.function.DatabaseClient; |
| 31 | + |
| 32 | +/** |
| 33 | + * Tests for {@link AbstractR2dbcConfiguration}. |
| 34 | + * |
| 35 | + * @author Mark Paluch |
| 36 | + */ |
| 37 | +public class R2dbcConfigurationIntegrationTests { |
| 38 | + |
| 39 | + @Test // gh-95 |
| 40 | + public void shouldLookupConnectionFactoryThroughLocalCall() { |
| 41 | + |
| 42 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( |
| 43 | + NonBeanConnectionFactoryConfiguration.class); |
| 44 | + |
| 45 | + context.getBean(DatabaseClient.class); |
| 46 | + |
| 47 | + NonBeanConnectionFactoryConfiguration bean = context.getBean(NonBeanConnectionFactoryConfiguration.class); |
| 48 | + |
| 49 | + assertThat(context.getBeanNamesForType(ConnectionFactory.class)).isEmpty(); |
| 50 | + assertThat(bean.callCounter).isGreaterThan(2); |
| 51 | + |
| 52 | + context.stop(); |
| 53 | + } |
| 54 | + |
| 55 | + @Test // gh-95 |
| 56 | + public void shouldLookupConnectionFactoryThroughLocalCallForExistingCustomBeans() { |
| 57 | + |
| 58 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( |
| 59 | + CustomConnectionFactoryBeanNameConfiguration.class); |
| 60 | + |
| 61 | + context.getBean(DatabaseClient.class); |
| 62 | + |
| 63 | + CustomConnectionFactoryBeanNameConfiguration bean = context |
| 64 | + .getBean(CustomConnectionFactoryBeanNameConfiguration.class); |
| 65 | + |
| 66 | + assertThat(context.getBeanNamesForType(ConnectionFactory.class)).hasSize(1).contains("myCustomBean"); |
| 67 | + assertThat(bean.callCounter).isGreaterThan(2); |
| 68 | + |
| 69 | + ConnectionFactoryWrapper wrapper = context.getBean(ConnectionFactoryWrapper.class); |
| 70 | + assertThat(wrapper.connectionFactory).isExactlyInstanceOf(H2ConnectionFactory.class); |
| 71 | + |
| 72 | + context.stop(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test // gh-95 |
| 76 | + public void shouldRegisterConnectionFactory() { |
| 77 | + |
| 78 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( |
| 79 | + BeanConnectionFactoryConfiguration.class); |
| 80 | + |
| 81 | + context.getBean(DatabaseClient.class); |
| 82 | + |
| 83 | + BeanConnectionFactoryConfiguration bean = context.getBean(BeanConnectionFactoryConfiguration.class); |
| 84 | + |
| 85 | + assertThat(bean.callCounter).isEqualTo(1); |
| 86 | + assertThat(context.getBeanNamesForType(ConnectionFactory.class)).hasSize(1); |
| 87 | + |
| 88 | + context.stop(); |
| 89 | + } |
| 90 | + |
| 91 | + @Configuration(proxyBeanMethods = false) |
| 92 | + static class NonBeanConnectionFactoryConfiguration extends AbstractR2dbcConfiguration { |
| 93 | + |
| 94 | + int callCounter; |
| 95 | + |
| 96 | + @Override |
| 97 | + public ConnectionFactory connectionFactory() { |
| 98 | + |
| 99 | + callCounter++; |
| 100 | + return new H2ConnectionFactory( |
| 101 | + H2ConnectionConfiguration.builder().inMemory("foo").username("sa").password("").build()); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Configuration(proxyBeanMethods = false) |
| 106 | + static class CustomConnectionFactoryBeanNameConfiguration extends AbstractR2dbcConfiguration { |
| 107 | + |
| 108 | + int callCounter; |
| 109 | + |
| 110 | + @Bean |
| 111 | + public ConnectionFactory myCustomBean() { |
| 112 | + return mock(ConnectionFactory.class); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public ConnectionFactory connectionFactory() { |
| 117 | + |
| 118 | + callCounter++; |
| 119 | + return new H2ConnectionFactory( |
| 120 | + H2ConnectionConfiguration.builder().inMemory("foo").username("sa").password("").build()); |
| 121 | + } |
| 122 | + |
| 123 | + @Bean |
| 124 | + ConnectionFactoryWrapper wrapper() { |
| 125 | + return new ConnectionFactoryWrapper(lookupConnectionFactory()); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + static class ConnectionFactoryWrapper { |
| 130 | + ConnectionFactory connectionFactory; |
| 131 | + |
| 132 | + ConnectionFactoryWrapper(ConnectionFactory connectionFactory) { |
| 133 | + this.connectionFactory = connectionFactory; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Configuration(proxyBeanMethods = false) |
| 138 | + static class BeanConnectionFactoryConfiguration extends NonBeanConnectionFactoryConfiguration { |
| 139 | + |
| 140 | + @Override |
| 141 | + @Bean |
| 142 | + public ConnectionFactory connectionFactory() { |
| 143 | + return super.connectionFactory(); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments