|
| 1 | +/* |
| 2 | + * Copyright 2025 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.aot; |
| 17 | + |
| 18 | +import org.assertj.core.api.Assertions; |
| 19 | +import org.junit.jupiter.params.ParameterizedTest; |
| 20 | +import org.junit.jupiter.params.provider.CsvSource; |
| 21 | +import org.mockito.Mockito; |
| 22 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 23 | +import org.springframework.core.env.Environment; |
| 24 | +import org.springframework.mock.env.MockEnvironment; |
| 25 | +import org.springframework.util.StringUtils; |
| 26 | + |
| 27 | +/** |
| 28 | + * Tests for {@link AotContext}. |
| 29 | + * |
| 30 | + * @author Christoph Strobl |
| 31 | + */ |
| 32 | +class AotContextUnitTests { |
| 33 | + |
| 34 | + @ParameterizedTest // GH-3322 |
| 35 | + @CsvSource({ // |
| 36 | + "'spring.aot.repositories.enabled', '', '', '', true", // |
| 37 | + "'spring.aot.repositories.enabled', 'true', '', '', true", // |
| 38 | + "'spring.aot.repositories.enabled', 'false', '', '', false", // |
| 39 | + "'spring.aot.repositories.enabled', '', 'commons', 'true', true", // |
| 40 | + "'spring.aot.repositories.enabled', 'true', 'commons', 'true', true", // |
| 41 | + "'spring.aot.repositories.enabled', '', 'commons', 'false', false", // |
| 42 | + "'spring.aot.repositories.enabled', 'false', 'commons', 'true', false" // |
| 43 | + }) |
| 44 | + void considersEnvironmentSettingsForGeneratedRepositories(String generalFlag, String generalValue, String storeName, |
| 45 | + String storeValue, boolean enabled) { |
| 46 | + |
| 47 | + MockAotContext ctx = new MockAotContext(); |
| 48 | + if (StringUtils.hasText(generalFlag) && StringUtils.hasText(generalValue)) { |
| 49 | + ctx.withProperty(generalFlag, generalValue); |
| 50 | + } |
| 51 | + if (StringUtils.hasText(storeName) && StringUtils.hasText(storeValue)) { |
| 52 | + ctx.withProperty("spring.aot.repositories.%s.enabled".formatted(storeName), storeValue); |
| 53 | + } |
| 54 | + |
| 55 | + Assertions.assertThat(ctx.isGeneratedRepositoriesEnabled(storeName)).isEqualTo(enabled); |
| 56 | + } |
| 57 | + |
| 58 | + class MockAotContext implements AotContext { |
| 59 | + |
| 60 | + private final MockEnvironment environment; |
| 61 | + |
| 62 | + public MockAotContext() { |
| 63 | + this.environment = new MockEnvironment(); |
| 64 | + } |
| 65 | + |
| 66 | + MockAotContext withProperty(String key, String value) { |
| 67 | + environment.setProperty(key, value); |
| 68 | + return this; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public ConfigurableListableBeanFactory getBeanFactory() { |
| 73 | + return Mockito.mock(ConfigurableListableBeanFactory.class); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public TypeIntrospector introspectType(String typeName) { |
| 78 | + return Mockito.mock(TypeIntrospector.class); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public IntrospectedBeanDefinition introspectBeanDefinition(String beanName) { |
| 83 | + return Mockito.mock(IntrospectedBeanDefinition.class); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public Environment getEnvironment() { |
| 88 | + return environment; |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments