|
| 1 | +/* |
| 2 | + * Copyright 2012-2017 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.test.autoconfigure.orm.jpa; |
| 18 | + |
| 19 | +import javax.sql.DataSource; |
| 20 | + |
| 21 | +import org.junit.After; |
| 22 | +import org.junit.Test; |
| 23 | +import org.junit.runner.RunWith; |
| 24 | + |
| 25 | +import org.springframework.beans.factory.BeanCreationException; |
| 26 | +import org.springframework.boot.test.util.EnvironmentTestUtils; |
| 27 | +import org.springframework.boot.testutil.ClassPathExclusions; |
| 28 | +import org.springframework.boot.testutil.FilteredClassPathRunner; |
| 29 | +import org.springframework.context.ConfigurableApplicationContext; |
| 30 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 31 | +import org.springframework.context.annotation.Bean; |
| 32 | +import org.springframework.context.annotation.Configuration; |
| 33 | + |
| 34 | +import static org.assertj.core.api.Assertions.assertThat; |
| 35 | +import static org.mockito.Mockito.mock; |
| 36 | + |
| 37 | +/** |
| 38 | + * Specific tests for {@link TestDatabaseAutoConfiguration} when no embedded database is |
| 39 | + * available. |
| 40 | + * |
| 41 | + * @author Stephane Nicoll |
| 42 | + */ |
| 43 | +@RunWith(FilteredClassPathRunner.class) |
| 44 | +@ClassPathExclusions({ "h2-*.jar", "hsqldb-*.jar", "derby-*.jar" }) |
| 45 | +public class TestDatabaseAutoConfigurationNoEmbeddedTests { |
| 46 | + |
| 47 | + private ConfigurableApplicationContext context; |
| 48 | + |
| 49 | + @After |
| 50 | + public void closeContext() { |
| 51 | + if (this.context != null) { |
| 52 | + this.context.close(); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void applyAnyReplace() { |
| 58 | + try { |
| 59 | + load(ExistingDataSourceConfiguration.class); |
| 60 | + } |
| 61 | + catch (BeanCreationException ex) { |
| 62 | + String message = ex.getMessage(); |
| 63 | + assertThat(message).contains( |
| 64 | + "Failed to replace DataSource with an embedded database for tests."); |
| 65 | + assertThat(message).contains( |
| 66 | + "If you want an embedded database please put a supported one on the " |
| 67 | + + "classpath"); |
| 68 | + assertThat(message).contains( |
| 69 | + "or tune the replace attribute of @AutoconfigureTestDatabase."); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void applyNoReplace() { |
| 75 | + load(ExistingDataSourceConfiguration.class, "spring.test.database.replace=NONE"); |
| 76 | + assertThat(this.context.getBeansOfType(DataSource.class)).hasSize(1); |
| 77 | + assertThat(this.context.getBean(DataSource.class)).isSameAs( |
| 78 | + this.context.getBean("myCustomDataSource")); |
| 79 | + } |
| 80 | + |
| 81 | + public void load(Class<?> config, String... environment) { |
| 82 | + this.context = doLoad(config, environment); |
| 83 | + } |
| 84 | + |
| 85 | + public ConfigurableApplicationContext doLoad(Class<?> config, String... environment) { |
| 86 | + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
| 87 | + if (config != null) { |
| 88 | + ctx.register(config); |
| 89 | + } |
| 90 | + ctx.register(TestDatabaseAutoConfiguration.class); |
| 91 | + EnvironmentTestUtils.addEnvironment(ctx, environment); |
| 92 | + ctx.refresh(); |
| 93 | + return ctx; |
| 94 | + } |
| 95 | + |
| 96 | + @Configuration |
| 97 | + static class ExistingDataSourceConfiguration { |
| 98 | + |
| 99 | + @Bean |
| 100 | + public DataSource myCustomDataSource() { |
| 101 | + return mock(DataSource.class); |
| 102 | + } |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments