|
| 1 | +/* |
| 2 | + * Copyright 2018 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 | +package org.springframework.data.convert; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | +import java.util.stream.Collectors; |
| 23 | +import java.util.stream.IntStream; |
| 24 | + |
| 25 | +import org.junit.Test; |
| 26 | +import org.junit.runner.RunWith; |
| 27 | +import org.junit.runners.Parameterized; |
| 28 | +import org.junit.runners.Parameterized.Parameters; |
| 29 | +import org.springframework.data.mapping.PersistentEntity; |
| 30 | +import org.springframework.data.mapping.PreferredConstructor.Parameter; |
| 31 | +import org.springframework.data.mapping.context.SampleMappingContext; |
| 32 | +import org.springframework.data.mapping.context.SamplePersistentProperty; |
| 33 | +import org.springframework.data.mapping.model.BasicPersistentEntity; |
| 34 | +import org.springframework.data.mapping.model.ParameterValueProvider; |
| 35 | +import org.springframework.data.mapping.model.With32Args; |
| 36 | +import org.springframework.data.mapping.model.With33Args; |
| 37 | +import org.springframework.test.util.ReflectionTestUtils; |
| 38 | + |
| 39 | +/** |
| 40 | + * Unit test to verify correct object instantiation using Kotlin defaulting via {@link KotlinClassGeneratingEntityInstantiator}. |
| 41 | + * |
| 42 | + * @author Mark Paluch |
| 43 | + */ |
| 44 | +@RunWith(Parameterized.class) |
| 45 | +public class ParameterizedKotlinInstantiatorUnitTests { |
| 46 | + |
| 47 | + private final String valueToSet = "THE VALUE"; |
| 48 | + private final PersistentEntity<Object, SamplePersistentProperty> entity; |
| 49 | + private final int propertyCount; |
| 50 | + private final int propertyUnderTestIndex; |
| 51 | + private final String propertyUnderTestName; |
| 52 | + private final EntityInstantiator entityInstantiator; |
| 53 | + |
| 54 | + public ParameterizedKotlinInstantiatorUnitTests(PersistentEntity<Object, SamplePersistentProperty> entity, int propertyCount, int propertyUnderTestIndex, String propertyUnderTestName, EntityInstantiator entityInstantiator, String label) { |
| 55 | + this.entity = entity; |
| 56 | + this.propertyCount = propertyCount; |
| 57 | + this.propertyUnderTestIndex = propertyUnderTestIndex; |
| 58 | + this.propertyUnderTestName = propertyUnderTestName; |
| 59 | + this.entityInstantiator = entityInstantiator; |
| 60 | + } |
| 61 | + |
| 62 | + @Parameters(name = "{5}") |
| 63 | + public static List<Object[]> parameters() { |
| 64 | + |
| 65 | + SampleMappingContext context = new SampleMappingContext(); |
| 66 | + |
| 67 | + KotlinClassGeneratingEntityInstantiator generatingInstantiator = new KotlinClassGeneratingEntityInstantiator(); |
| 68 | + ReflectionEntityInstantiator reflectionInstantiator = ReflectionEntityInstantiator.INSTANCE; |
| 69 | + |
| 70 | + List<Object[]> fixtures = new ArrayList<>(); |
| 71 | + fixtures.addAll(createFixture(context, With32Args.class, 32, generatingInstantiator)); |
| 72 | + fixtures.addAll(createFixture(context, With32Args.class, 32, reflectionInstantiator)); |
| 73 | + fixtures.addAll(createFixture(context, With33Args.class, 33, generatingInstantiator)); |
| 74 | + fixtures.addAll(createFixture(context, With33Args.class, 33, reflectionInstantiator)); |
| 75 | + |
| 76 | + return fixtures; |
| 77 | + } |
| 78 | + |
| 79 | + private static List<Object[]> createFixture(SampleMappingContext context, Class<?> entityType, int propertyCount, EntityInstantiator entityInstantiator) { |
| 80 | + |
| 81 | + BasicPersistentEntity<Object, SamplePersistentProperty> persistentEntity = context.getPersistentEntity(entityType); |
| 82 | + |
| 83 | + return IntStream.range(0, propertyCount).mapToObj(i -> { |
| 84 | + |
| 85 | + return new Object[]{persistentEntity, propertyCount, i, Integer.toString(i), entityInstantiator, String.format("Property %d for %s using %s", i, entityType.getSimpleName(), entityInstantiator.getClass().getSimpleName())}; |
| 86 | + }).collect(Collectors.toList()); |
| 87 | + } |
| 88 | + |
| 89 | + @Test // DATACMNS-1402 |
| 90 | + public void shouldCreateInstanceWithSinglePropertySet() { |
| 91 | + |
| 92 | + Object instance = entityInstantiator.createInstance(entity, new SingleParameterValueProvider()); |
| 93 | + |
| 94 | + for (int i = 0; i < propertyCount; i++) { |
| 95 | + |
| 96 | + Object value = ReflectionTestUtils.getField(instance, Integer.toString(i)); |
| 97 | + |
| 98 | + if (propertyUnderTestIndex == i) { |
| 99 | + assertThat(value).describedAs("Property " + i + " of " + entity).isEqualTo(valueToSet); |
| 100 | + } else { |
| 101 | + assertThat(value).describedAs("Property " + i + " of " + entity).isEqualTo(""); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @Test // DATACMNS-1402 |
| 107 | + public void shouldCreateInstanceWithAllExceptSinglePropertySet() { |
| 108 | + |
| 109 | + Object instance = entityInstantiator.createInstance(entity, new AllButParameterValueProvider()); |
| 110 | + |
| 111 | + for (int i = 0; i < propertyCount; i++) { |
| 112 | + |
| 113 | + Object value = ReflectionTestUtils.getField(instance, Integer.toString(i)); |
| 114 | + |
| 115 | + if (propertyUnderTestIndex == i) { |
| 116 | + assertThat(value).describedAs("Property " + i + " of " + entity).isEqualTo(""); |
| 117 | + } else { |
| 118 | + assertThat(value).describedAs("Property " + i + " of " + entity).isEqualTo(Integer.toString(i)); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Return the value to set for the property to test. |
| 125 | + */ |
| 126 | + class SingleParameterValueProvider implements ParameterValueProvider<SamplePersistentProperty> { |
| 127 | + |
| 128 | + @Override |
| 129 | + public <T> T getParameterValue(Parameter<T, SamplePersistentProperty> parameter) { |
| 130 | + |
| 131 | + if (parameter.getName().equals(propertyUnderTestName)) { |
| 132 | + return (T) valueToSet; |
| 133 | + } |
| 134 | + return null; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Return the property name as value for all properties except the one to test. |
| 140 | + */ |
| 141 | + class AllButParameterValueProvider implements ParameterValueProvider<SamplePersistentProperty> { |
| 142 | + |
| 143 | + @Override |
| 144 | + public <T> T getParameterValue(Parameter<T, SamplePersistentProperty> parameter) { |
| 145 | + |
| 146 | + if (!parameter.getName().equals(propertyUnderTestName)) { |
| 147 | + return (T) parameter.getName(); |
| 148 | + } |
| 149 | + return null; |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments