|
| 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.connectionfactory.init; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
| 21 | +import io.r2dbc.spi.test.MockConnection; |
| 22 | +import io.r2dbc.spi.test.MockConnectionFactory; |
| 23 | +import reactor.core.publisher.Mono; |
| 24 | + |
| 25 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 26 | + |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +/** |
| 30 | + * Unit tests for {@link ConnectionFactoryInitializer}. |
| 31 | + * |
| 32 | + * @author Mark Paluch |
| 33 | + */ |
| 34 | +public class ConnectionFactoryInitializerUnitTests { |
| 35 | + |
| 36 | + AtomicBoolean called = new AtomicBoolean(); |
| 37 | + DatabasePopulator populator = mock(DatabasePopulator.class); |
| 38 | + MockConnection connection = MockConnection.builder().build(); |
| 39 | + MockConnectionFactory connectionFactory = MockConnectionFactory.builder().connection(connection).build(); |
| 40 | + |
| 41 | + @Test // gh-216 |
| 42 | + public void shouldInitializeConnectionFactory() { |
| 43 | + |
| 44 | + when(populator.populate(any())).thenReturn(Mono.<Void> empty().doOnSubscribe(subscription -> called.set(true))); |
| 45 | + |
| 46 | + ConnectionFactoryInitializer initializer = new ConnectionFactoryInitializer(); |
| 47 | + initializer.setConnectionFactory(connectionFactory); |
| 48 | + initializer.setDatabasePopulator(populator); |
| 49 | + |
| 50 | + initializer.afterPropertiesSet(); |
| 51 | + |
| 52 | + assertThat(called).isTrue(); |
| 53 | + } |
| 54 | + |
| 55 | + @Test // gh-216 |
| 56 | + public void shouldCleanConnectionFactory() { |
| 57 | + |
| 58 | + when(populator.populate(any())).thenReturn(Mono.<Void> empty().doOnSubscribe(subscription -> called.set(true))); |
| 59 | + |
| 60 | + ConnectionFactoryInitializer initializer = new ConnectionFactoryInitializer(); |
| 61 | + initializer.setConnectionFactory(connectionFactory); |
| 62 | + initializer.setDatabaseCleaner(populator); |
| 63 | + |
| 64 | + initializer.afterPropertiesSet(); |
| 65 | + initializer.destroy(); |
| 66 | + |
| 67 | + assertThat(called).isTrue(); |
| 68 | + } |
| 69 | +} |
0 commit comments