|
| 1 | +/* |
| 2 | + * Copyright 2012-2016 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.devtools.autoconfigure; |
| 18 | + |
| 19 | +import java.sql.Connection; |
| 20 | +import java.sql.SQLException; |
| 21 | +import java.sql.Statement; |
| 22 | + |
| 23 | +import javax.sql.DataSource; |
| 24 | + |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; |
| 28 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 29 | +import org.springframework.boot.test.EnvironmentTestUtils; |
| 30 | +import org.springframework.context.ConfigurableApplicationContext; |
| 31 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 32 | +import org.springframework.context.annotation.Bean; |
| 33 | +import org.springframework.context.annotation.Configuration; |
| 34 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; |
| 35 | + |
| 36 | +import static org.mockito.BDDMockito.given; |
| 37 | +import static org.mockito.Mockito.mock; |
| 38 | +import static org.mockito.Mockito.times; |
| 39 | +import static org.mockito.Mockito.verify; |
| 40 | + |
| 41 | +/** |
| 42 | + * Tests for {@link DevToolsDataSourceAutoConfiguration}. |
| 43 | + * |
| 44 | + * @author Andy Wilkinson |
| 45 | + */ |
| 46 | +public class DevToolsDataSourceAutoConfigurationTests { |
| 47 | + |
| 48 | + @Test |
| 49 | + public void embeddedDatabaseIsNotShutDown() throws SQLException { |
| 50 | + ConfigurableApplicationContext context = createContext("org.h2.Driver", |
| 51 | + EmbeddedDatabaseConfiguration.class); |
| 52 | + DataSource dataSource = context.getBean(DataSource.class); |
| 53 | + context.close(); |
| 54 | + verify(dataSource, times(0)).getConnection(); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void externalDatabaseIsNotShutDown() throws SQLException { |
| 59 | + ConfigurableApplicationContext context = createContext("org.postgresql.Driver", |
| 60 | + DataSourceConfiguration.class); |
| 61 | + DataSource dataSource = context.getBean(DataSource.class); |
| 62 | + context.close(); |
| 63 | + verify(dataSource, times(0)).getConnection(); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void nonEmbeddedInMemoryDatabaseIsShutDown() throws SQLException { |
| 68 | + ConfigurableApplicationContext context = createContext("org.h2.Driver", |
| 69 | + DataSourceConfiguration.class); |
| 70 | + DataSource dataSource = context.getBean(DataSource.class); |
| 71 | + Connection connection = mock(Connection.class); |
| 72 | + given(dataSource.getConnection()).willReturn(connection); |
| 73 | + Statement statement = mock(Statement.class); |
| 74 | + given(connection.createStatement()).willReturn(statement); |
| 75 | + context.close(); |
| 76 | + verify(statement).execute("SHUTDOWN"); |
| 77 | + } |
| 78 | + |
| 79 | + private ConfigurableApplicationContext createContext(String driver, |
| 80 | + Class<?>... classes) { |
| 81 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); |
| 82 | + context.register(classes); |
| 83 | + context.register(DevToolsDataSourceAutoConfiguration.class); |
| 84 | + EnvironmentTestUtils.addEnvironment(context, |
| 85 | + "spring.datasource.driver-class-name:" + driver); |
| 86 | + context.refresh(); |
| 87 | + return context; |
| 88 | + } |
| 89 | + |
| 90 | + @Configuration |
| 91 | + @EnableConfigurationProperties(DataSourceProperties.class) |
| 92 | + static class EmbeddedDatabaseConfiguration { |
| 93 | + |
| 94 | + @Bean |
| 95 | + public EmbeddedDatabase embeddedDatabase() { |
| 96 | + return mock(EmbeddedDatabase.class); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Configuration |
| 101 | + @EnableConfigurationProperties(DataSourceProperties.class) |
| 102 | + static class DataSourceConfiguration { |
| 103 | + |
| 104 | + @Bean |
| 105 | + public DataSource in() { |
| 106 | + return mock(DataSource.class); |
| 107 | + } |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments