|
| 1 | +/* |
| 2 | + * Copyright 2019-2021 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 | + |
| 17 | +package org.springframework.aot.test; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.mockito.ArgumentMatchers.any; |
| 21 | +import static org.mockito.BDDMockito.given; |
| 22 | +import static org.mockito.Mockito.mock; |
| 23 | + |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Set; |
| 26 | + |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +import org.springframework.context.ApplicationContext; |
| 30 | +import org.springframework.context.ApplicationContextInitializer; |
| 31 | +import org.springframework.context.annotation.Configuration; |
| 32 | +import org.springframework.context.support.GenericApplicationContext; |
| 33 | +import org.springframework.test.annotation.DirtiesContext.HierarchyMode; |
| 34 | +import org.springframework.test.context.MergedContextConfiguration; |
| 35 | +import org.springframework.test.context.SmartContextLoader; |
| 36 | +import org.springframework.test.context.cache.ContextCache; |
| 37 | +import org.springframework.test.context.cache.DefaultContextCache; |
| 38 | +import org.springframework.test.context.support.DelegatingSmartContextLoader; |
| 39 | + |
| 40 | +/** |
| 41 | + * Integration tests for {@link AotCacheAwareContextLoaderDelegate} that |
| 42 | + * interact with the {@link DefaultContextCache}. |
| 43 | + * |
| 44 | + * @author Sam Brannen |
| 45 | + */ |
| 46 | +class AotCacheAwareContextLoaderDelegateIntegrationTests { |
| 47 | + |
| 48 | + private final ContextCache contextCache = new DefaultContextCache(); |
| 49 | + |
| 50 | + private final ApplicationContext applicationContext = mock(ApplicationContext.class); |
| 51 | + |
| 52 | + private final SmartContextLoader aotSmartContextLoader = mockSmartContextLoader(this.applicationContext); |
| 53 | + |
| 54 | + private final AotCacheAwareContextLoaderDelegate delegate = createDelegate(this.contextCache, this.aotSmartContextLoader); |
| 55 | + |
| 56 | + |
| 57 | + @Test |
| 58 | + void nonAotTestCase() throws Exception { |
| 59 | + MergedContextConfiguration mergedConfig = createMergedContextConfiguration(DemoTestCase.class); |
| 60 | + |
| 61 | + assertThat(contextCache.size()).isEqualTo(0); |
| 62 | + assertThat(delegate.isContextLoaded(mergedConfig)).isFalse(); |
| 63 | + |
| 64 | + ApplicationContext context = delegate.loadContext(mergedConfig); |
| 65 | + assertThat(delegate.isContextLoaded(mergedConfig)).isTrue(); |
| 66 | + assertThat(context).isNotNull(); |
| 67 | + assertThat(contextCache.size()).isEqualTo(1); |
| 68 | + assertThat(contextCache.getHitCount()).isEqualTo(0); |
| 69 | + assertThat(contextCache.contains(mergedConfig)).isTrue(); |
| 70 | + |
| 71 | + // Load again |
| 72 | + context = delegate.loadContext(mergedConfig); |
| 73 | + assertThat(delegate.isContextLoaded(mergedConfig)).isTrue(); |
| 74 | + assertThat(context).isNotNull(); |
| 75 | + assertThat(contextCache.size()).isEqualTo(1); |
| 76 | + assertThat(contextCache.getHitCount()).isEqualTo(1); |
| 77 | + assertThat(contextCache.contains(mergedConfig)).isTrue(); |
| 78 | + |
| 79 | + // Load again |
| 80 | + context = delegate.loadContext(mergedConfig); |
| 81 | + assertThat(delegate.isContextLoaded(mergedConfig)).isTrue(); |
| 82 | + assertThat(context).isNotNull(); |
| 83 | + assertThat(contextCache.size()).isEqualTo(1); |
| 84 | + assertThat(contextCache.getHitCount()).isEqualTo(2); |
| 85 | + assertThat(contextCache.contains(mergedConfig)).isTrue(); |
| 86 | + |
| 87 | + // Remove |
| 88 | + delegate.closeContext(mergedConfig, HierarchyMode.EXHAUSTIVE); |
| 89 | + assertThat(delegate.isContextLoaded(mergedConfig)).isFalse(); |
| 90 | + assertThat(contextCache.size()).isEqualTo(0); |
| 91 | + assertThat(contextCache.contains(mergedConfig)).isFalse(); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void aotTestCase() throws Exception { |
| 96 | + MergedContextConfiguration mergedConfig = createMergedContextConfiguration(AotTestCase.class); |
| 97 | + AotMergedContextConfiguration aotMergedConfig = new AotMergedContextConfiguration(AotTestCase.class, |
| 98 | + DemoApplicationContextInitializer.class, mergedConfig, delegate, null); |
| 99 | + |
| 100 | + assertThat(contextCache.size()).isEqualTo(0); |
| 101 | + assertThat(delegate.isContextLoaded(mergedConfig)).isFalse(); |
| 102 | + |
| 103 | + ApplicationContext context = delegate.loadContext(mergedConfig); |
| 104 | + assertThat(delegate.isContextLoaded(mergedConfig)).isTrue(); |
| 105 | + assertThat(context).isSameAs(applicationContext); |
| 106 | + assertThat(contextCache.size()).isEqualTo(1); |
| 107 | + assertThat(contextCache.getHitCount()).isEqualTo(0); |
| 108 | + // The ContextCache must not contain the original MergedContextConfiguration. |
| 109 | + assertThat(contextCache.contains(mergedConfig)).isFalse(); |
| 110 | + // Instead, it must contain the AotMergedContextConfiguration. |
| 111 | + assertThat(contextCache.contains(aotMergedConfig)).isTrue(); |
| 112 | + |
| 113 | + // Load again |
| 114 | + context = delegate.loadContext(mergedConfig); |
| 115 | + assertThat(delegate.isContextLoaded(mergedConfig)).isTrue(); |
| 116 | + assertThat(context).isSameAs(applicationContext); |
| 117 | + assertThat(contextCache.size()).isEqualTo(1); |
| 118 | + assertThat(contextCache.getHitCount()).isEqualTo(1); |
| 119 | + // The ContextCache must not contain the original MergedContextConfiguration. |
| 120 | + assertThat(contextCache.contains(mergedConfig)).isFalse(); |
| 121 | + // Instead, it must contain the AotMergedContextConfiguration. |
| 122 | + assertThat(contextCache.contains(aotMergedConfig)).isTrue(); |
| 123 | + |
| 124 | + // Load again |
| 125 | + context = delegate.loadContext(mergedConfig); |
| 126 | + assertThat(delegate.isContextLoaded(mergedConfig)).isTrue(); |
| 127 | + assertThat(context).isSameAs(applicationContext); |
| 128 | + assertThat(contextCache.size()).isEqualTo(1); |
| 129 | + assertThat(contextCache.getHitCount()).isEqualTo(2); |
| 130 | + // The ContextCache must not contain the original MergedContextConfiguration. |
| 131 | + assertThat(contextCache.contains(mergedConfig)).isFalse(); |
| 132 | + // Instead, it must contain the AotMergedContextConfiguration. |
| 133 | + assertThat(contextCache.contains(aotMergedConfig)).isTrue(); |
| 134 | + |
| 135 | + // Remove |
| 136 | + delegate.closeContext(mergedConfig, HierarchyMode.EXHAUSTIVE); |
| 137 | + assertThat(delegate.isContextLoaded(mergedConfig)).isFalse(); |
| 138 | + assertThat(contextCache.size()).isEqualTo(0); |
| 139 | + assertThat(contextCache.contains(mergedConfig)).isFalse(); |
| 140 | + assertThat(contextCache.contains(aotMergedConfig)).isFalse(); |
| 141 | + } |
| 142 | + |
| 143 | + private MergedContextConfiguration createMergedContextConfiguration(Class<?> testClass) { |
| 144 | + return new MergedContextConfiguration(testClass, null, new Class<?>[] { DemoConfiguration.class }, |
| 145 | + Set.of(DemoApplicationContextInitializer.class), null, new DelegatingSmartContextLoader()); |
| 146 | + } |
| 147 | + |
| 148 | + private static AotCacheAwareContextLoaderDelegate createDelegate(ContextCache contextCache, SmartContextLoader aotSmartContextLoader) { |
| 149 | + AotTestMappings aotTestMappings = new AotTestMappings( |
| 150 | + Map.of(AotTestCase.class.getName(), () -> aotSmartContextLoader), |
| 151 | + Map.of(AotTestCase.class.getName(), DemoApplicationContextInitializer.class)); |
| 152 | + return new AotCacheAwareContextLoaderDelegate(aotTestMappings, contextCache); |
| 153 | + } |
| 154 | + |
| 155 | + private static SmartContextLoader mockSmartContextLoader(ApplicationContext applicationContext) { |
| 156 | + try { |
| 157 | + SmartContextLoader mock = mock(SmartContextLoader.class); |
| 158 | + given(mock.loadContext(any(MergedContextConfiguration.class))).willReturn(applicationContext); |
| 159 | + return mock; |
| 160 | + } |
| 161 | + catch (Exception ex) { |
| 162 | + throw new IllegalStateException(ex); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + |
| 167 | + static class AotTestCase { |
| 168 | + } |
| 169 | + |
| 170 | + static class DemoTestCase { |
| 171 | + } |
| 172 | + |
| 173 | + @Configuration |
| 174 | + static class DemoConfiguration { |
| 175 | + } |
| 176 | + |
| 177 | + static class DemoApplicationContextInitializer implements ApplicationContextInitializer<GenericApplicationContext> { |
| 178 | + @Override |
| 179 | + public void initialize(GenericApplicationContext applicationContext) { |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | +} |
0 commit comments