|
| 1 | +package com.poiji.util; |
| 2 | + |
| 3 | +import com.poiji.exception.PoijiInstantiationException; |
| 4 | +import org.apache.commons.collections4.MultiValuedMap; |
| 5 | +import org.apache.commons.collections4.multimap.ArrayListValuedHashMap; |
| 6 | +import org.junit.Test; |
| 7 | + |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +import static org.hamcrest.CoreMatchers.is; |
| 12 | +import static org.hamcrest.CoreMatchers.notNullValue; |
| 13 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 14 | +import static org.junit.Assert.fail; |
| 15 | + |
| 16 | +/** |
| 17 | + * Additional tests for ReflectUtil record support edge cases |
| 18 | + */ |
| 19 | +public class ReflectUtilRecordEdgeCaseTest { |
| 20 | + |
| 21 | + // Test record with primitive types |
| 22 | + public record PrimitiveRecord( |
| 23 | + boolean boolValue, |
| 24 | + byte byteValue, |
| 25 | + short shortValue, |
| 26 | + int intValue, |
| 27 | + long longValue, |
| 28 | + float floatValue, |
| 29 | + double doubleValue, |
| 30 | + char charValue |
| 31 | + ) {} |
| 32 | + |
| 33 | + // Test record with nullable types |
| 34 | + public record NullableRecord( |
| 35 | + String name, |
| 36 | + Integer age, |
| 37 | + Double salary |
| 38 | + ) {} |
| 39 | + |
| 40 | + // Test record with MultiValuedMap |
| 41 | + public record MultiValueMapRecord( |
| 42 | + String id, |
| 43 | + MultiValuedMap<String, String> values |
| 44 | + ) {} |
| 45 | + |
| 46 | + @Test |
| 47 | + public void shouldHandleAllPrimitiveTypesWithDefaults() { |
| 48 | + // Test that all primitive types get proper default values |
| 49 | + Map<String, Object> values = new HashMap<>(); |
| 50 | + // Don't provide any values, should use defaults |
| 51 | + |
| 52 | + PrimitiveRecord record = ReflectUtil.newRecordInstance(PrimitiveRecord.class, values); |
| 53 | + |
| 54 | + assertThat(record, notNullValue()); |
| 55 | + assertThat(record.boolValue(), is(false)); |
| 56 | + assertThat(record.byteValue(), is((byte) 0)); |
| 57 | + assertThat(record.shortValue(), is((short) 0)); |
| 58 | + assertThat(record.intValue(), is(0)); |
| 59 | + assertThat(record.longValue(), is(0L)); |
| 60 | + assertThat(record.floatValue(), is(0.0f)); |
| 61 | + assertThat(record.doubleValue(), is(0.0)); |
| 62 | + assertThat(record.charValue(), is('\0')); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void shouldHandleNullableFieldsInRecords() { |
| 67 | + // Test records with null object fields |
| 68 | + Map<String, Object> values = new HashMap<>(); |
| 69 | + values.put("name", null); |
| 70 | + values.put("age", null); |
| 71 | + values.put("salary", null); |
| 72 | + |
| 73 | + NullableRecord record = ReflectUtil.newRecordInstance(NullableRecord.class, values); |
| 74 | + |
| 75 | + assertThat(record, notNullValue()); |
| 76 | + assertThat(record.name() == null, is(true)); |
| 77 | + assertThat(record.age() == null, is(true)); |
| 78 | + assertThat(record.salary() == null, is(true)); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void shouldCreateEmptyMultiValuedMapForNullField() { |
| 83 | + // Test that null MultiValuedMap fields get initialized with empty map |
| 84 | + Map<String, Object> values = new HashMap<>(); |
| 85 | + values.put("id", "test-id"); |
| 86 | + // Don't provide values field - should create empty MultiValuedMap |
| 87 | + |
| 88 | + MultiValueMapRecord record = ReflectUtil.newRecordInstance(MultiValueMapRecord.class, values); |
| 89 | + |
| 90 | + assertThat(record, notNullValue()); |
| 91 | + assertThat(record.id(), is("test-id")); |
| 92 | + assertThat(record.values(), notNullValue()); |
| 93 | + assertThat(record.values().isEmpty(), is(true)); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void shouldHandleProvidedMultiValuedMap() { |
| 98 | + // Test that provided MultiValuedMap is used correctly |
| 99 | + Map<String, Object> values = new HashMap<>(); |
| 100 | + values.put("id", "test-id"); |
| 101 | + |
| 102 | + MultiValuedMap<String, String> providedMap = new ArrayListValuedHashMap<>(); |
| 103 | + providedMap.put("key1", "value1"); |
| 104 | + providedMap.put("key1", "value2"); |
| 105 | + values.put("values", providedMap); |
| 106 | + |
| 107 | + MultiValueMapRecord record = ReflectUtil.newRecordInstance(MultiValueMapRecord.class, values); |
| 108 | + |
| 109 | + assertThat(record, notNullValue()); |
| 110 | + assertThat(record.id(), is("test-id")); |
| 111 | + assertThat(record.values(), notNullValue()); |
| 112 | + assertThat(record.values().get("key1").size(), is(2)); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void shouldHandlePartiallyPopulatedPrimitiveRecord() { |
| 117 | + // Test record with some primitive values provided and others using defaults |
| 118 | + Map<String, Object> values = new HashMap<>(); |
| 119 | + values.put("intValue", 42); |
| 120 | + values.put("longValue", 100L); |
| 121 | + values.put("boolValue", true); |
| 122 | + // Other primitives should use defaults |
| 123 | + |
| 124 | + PrimitiveRecord record = ReflectUtil.newRecordInstance(PrimitiveRecord.class, values); |
| 125 | + |
| 126 | + assertThat(record, notNullValue()); |
| 127 | + assertThat(record.boolValue(), is(true)); |
| 128 | + assertThat(record.intValue(), is(42)); |
| 129 | + assertThat(record.longValue(), is(100L)); |
| 130 | + // Defaults for unprovided fields |
| 131 | + assertThat(record.byteValue(), is((byte) 0)); |
| 132 | + assertThat(record.shortValue(), is((short) 0)); |
| 133 | + assertThat(record.floatValue(), is(0.0f)); |
| 134 | + assertThat(record.doubleValue(), is(0.0)); |
| 135 | + assertThat(record.charValue(), is('\0')); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void shouldHandleEmptyMapForRecord() { |
| 140 | + // Test creating record with completely empty map |
| 141 | + Map<String, Object> values = new HashMap<>(); |
| 142 | + |
| 143 | + try { |
| 144 | + NullableRecord record = ReflectUtil.newRecordInstance(NullableRecord.class, values); |
| 145 | + assertThat(record, notNullValue()); |
| 146 | + // All fields should be null for object types |
| 147 | + assertThat(record.name() == null, is(true)); |
| 148 | + assertThat(record.age() == null, is(true)); |
| 149 | + assertThat(record.salary() == null, is(true)); |
| 150 | + } catch (PoijiInstantiationException e) { |
| 151 | + fail("Should handle empty map for records with nullable fields"); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments