|
| 1 | +/* |
| 2 | + * Copyright 2012-2023 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.experimental.boot.server.exec.imports; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 21 | +import org.mockito.ArgumentCaptor; |
| 22 | +import org.mockito.Mock; |
| 23 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 24 | + |
| 25 | +import org.springframework.beans.factory.support.RootBeanDefinition; |
| 26 | +import org.springframework.context.support.GenericApplicationContext; |
| 27 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 31 | +import static org.mockito.ArgumentMatchers.eq; |
| 32 | +import static org.mockito.BDDMockito.given; |
| 33 | +import static org.mockito.Mockito.verify; |
| 34 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 35 | + |
| 36 | +/** |
| 37 | + * Tests {@link RegisterBeanDefinitionsInitializer}. |
| 38 | + * |
| 39 | + * @author Rob Winch |
| 40 | + */ |
| 41 | +@ExtendWith(MockitoExtension.class) |
| 42 | +class RegisterBeanDefinitionsInitializerTests { |
| 43 | + |
| 44 | + @Mock |
| 45 | + GenericApplicationContext context; |
| 46 | + |
| 47 | + @Mock |
| 48 | + ConfigurableEnvironment environment; |
| 49 | + |
| 50 | + @Mock |
| 51 | + ClassLoader loader; |
| 52 | + |
| 53 | + RegisterBeanDefinitionsInitializer initializer = new RegisterBeanDefinitionsInitializer(); |
| 54 | + |
| 55 | + @Test |
| 56 | + void initializeWhenNullEnvironmentEntry() { |
| 57 | + given(this.context.getEnvironment()).willReturn(this.environment); |
| 58 | + |
| 59 | + this.initializer.initialize(this.context); |
| 60 | + |
| 61 | + verify(this.context).getEnvironment(); |
| 62 | + verifyNoMoreInteractions(this.context); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void initializeWhenEmptyEnvironmentEntry() { |
| 67 | + given(this.context.getEnvironment()).willReturn(this.environment); |
| 68 | + given(this.environment.getProperty(RegisterBeanDefinitionsInitializer.ADDITIONAL_BEAN_CLASS_NAMES)) |
| 69 | + .willReturn(""); |
| 70 | + |
| 71 | + this.initializer.initialize(this.context); |
| 72 | + |
| 73 | + verify(this.context).getEnvironment(); |
| 74 | + verifyNoMoreInteractions(this.context); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void initializeWhenClassNotFoundException() throws Exception { |
| 79 | + String classNames = ClassA.class.getName(); |
| 80 | + given(this.context.getEnvironment()).willReturn(this.environment); |
| 81 | + given(this.environment.getProperty(RegisterBeanDefinitionsInitializer.ADDITIONAL_BEAN_CLASS_NAMES)) |
| 82 | + .willReturn(classNames); |
| 83 | + given(this.context.getClassLoader()).willReturn(this.loader); |
| 84 | + given(this.loader.loadClass(ClassA.class.getName())).willThrow(new ClassNotFoundException("Not found")); |
| 85 | + |
| 86 | + assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> this.initializer.initialize(this.context)) |
| 87 | + .withMessageContaining("Unable to register bean of type " + ClassA.class.getName()); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void initializeWhenSingleClass() throws Exception { |
| 92 | + String classNames = ClassA.class.getName(); |
| 93 | + given(this.context.getEnvironment()).willReturn(this.environment); |
| 94 | + given(this.environment.getProperty(RegisterBeanDefinitionsInitializer.ADDITIONAL_BEAN_CLASS_NAMES)) |
| 95 | + .willReturn(classNames); |
| 96 | + given(this.context.getClassLoader()).willReturn(this.loader); |
| 97 | + Class beanClass = ClassA.class; |
| 98 | + given(this.loader.loadClass(ClassA.class.getName())).willReturn(beanClass); |
| 99 | + ArgumentCaptor<RootBeanDefinition> beanDefCaptor = ArgumentCaptor.forClass(RootBeanDefinition.class); |
| 100 | + |
| 101 | + this.initializer.initialize(this.context); |
| 102 | + |
| 103 | + verify(this.context).registerBeanDefinition(eq("testjars_" + ClassA.class.getName()), beanDefCaptor.capture()); |
| 104 | + assertThat(beanDefCaptor.getValue().getBeanClass()).isEqualTo(ClassA.class); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void initializeWhenMultiClass() throws Exception { |
| 109 | + String classNames = ClassA.class.getName() + "," + ClassB.class.getName(); |
| 110 | + RegisterBeanDefinitionsInitializer initializer = new RegisterBeanDefinitionsInitializer(); |
| 111 | + given(this.context.getEnvironment()).willReturn(this.environment); |
| 112 | + given(this.environment.getProperty(RegisterBeanDefinitionsInitializer.ADDITIONAL_BEAN_CLASS_NAMES)) |
| 113 | + .willReturn(classNames); |
| 114 | + given(this.context.getClassLoader()).willReturn(this.loader); |
| 115 | + Class beanClassA = ClassA.class; |
| 116 | + given(this.loader.loadClass(ClassA.class.getName())).willReturn(beanClassA); |
| 117 | + Class beanClassB = ClassB.class; |
| 118 | + given(this.loader.loadClass(ClassB.class.getName())).willReturn(beanClassB); |
| 119 | + ArgumentCaptor<RootBeanDefinition> beanDefCaptor = ArgumentCaptor.forClass(RootBeanDefinition.class); |
| 120 | + |
| 121 | + initializer.initialize(this.context); |
| 122 | + |
| 123 | + verify(this.context).registerBeanDefinition(eq("testjars_" + ClassA.class.getName()), beanDefCaptor.capture()); |
| 124 | + assertThat(beanDefCaptor.getValue().getBeanClass()).isEqualTo(ClassA.class); |
| 125 | + verify(this.context).registerBeanDefinition(eq("testjars_" + ClassB.class.getName()), beanDefCaptor.capture()); |
| 126 | + assertThat(beanDefCaptor.getValue().getBeanClass()).isEqualTo(ClassB.class); |
| 127 | + } |
| 128 | + |
| 129 | + static class ClassA { |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + static class ClassB { |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments