|
| 1 | +/* |
| 2 | + * Copyright 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 | +package org.springframework.data.mapping.model; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.springframework.data.annotation.Transient; |
| 22 | +import org.springframework.data.mapping.context.SampleMappingContext; |
| 23 | +import org.springframework.data.mapping.context.SamplePersistentProperty; |
| 24 | + |
| 25 | +/** |
| 26 | + * Integration tests for {@link EntityInstantiator}. |
| 27 | + * |
| 28 | + * @author Mark Paluch |
| 29 | + */ |
| 30 | +public class EntityInstantiatorIntegrationTests { |
| 31 | + |
| 32 | + SampleMappingContext context = new SampleMappingContext(); |
| 33 | + EntityInstantiators instantiators = new EntityInstantiators(); |
| 34 | + |
| 35 | + @Test // GH-2942 |
| 36 | + void shouldDefaultTransientProperties() { |
| 37 | + |
| 38 | + WithTransientProperty instance = createInstance(WithTransientProperty.class); |
| 39 | + |
| 40 | + assertThat(instance.foo).isEqualTo(null); |
| 41 | + assertThat(instance.bar).isEqualTo(0); |
| 42 | + } |
| 43 | + |
| 44 | + @Test // GH-2942 |
| 45 | + void shouldDefaultTransientRecordProperties() { |
| 46 | + |
| 47 | + RecordWithTransientProperty instance = createInstance(RecordWithTransientProperty.class); |
| 48 | + |
| 49 | + assertThat(instance.foo).isEqualTo(null); |
| 50 | + assertThat(instance.bar).isEqualTo(0); |
| 51 | + } |
| 52 | + |
| 53 | + @Test // GH-2942 |
| 54 | + void shouldDefaultTransientKotlinProperty() { |
| 55 | + |
| 56 | + DataClassWithTransientProperties instance = createInstance(DataClassWithTransientProperties.class); |
| 57 | + |
| 58 | + // Kotlin defaulting |
| 59 | + assertThat(instance.getFoo()).isEqualTo("foo"); |
| 60 | + |
| 61 | + // Our defaulting |
| 62 | + assertThat(instance.getBar()).isEqualTo(0); |
| 63 | + } |
| 64 | + |
| 65 | + @SuppressWarnings("unchecked") |
| 66 | + private <E> E createInstance(Class<E> entityType) { |
| 67 | + |
| 68 | + var entity = context.getRequiredPersistentEntity(entityType); |
| 69 | + var instantiator = instantiators.getInstantiatorFor(entity); |
| 70 | + |
| 71 | + return (E) instantiator.createInstance(entity, |
| 72 | + new PersistentEntityParameterValueProvider<>(entity, new PropertyValueProvider<SamplePersistentProperty>() { |
| 73 | + @Override |
| 74 | + public <T> T getPropertyValue(SamplePersistentProperty property) { |
| 75 | + return null; |
| 76 | + } |
| 77 | + }, null)); |
| 78 | + } |
| 79 | + |
| 80 | + static class WithTransientProperty { |
| 81 | + |
| 82 | + @Transient String foo; |
| 83 | + @Transient int bar; |
| 84 | + |
| 85 | + public WithTransientProperty(String foo, int bar) { |
| 86 | + |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + record RecordWithTransientProperty(@Transient String foo, @Transient int bar) { |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments