CE-781 Add cases for LinkedHashMap and HashMap in deepCopySimpleMap

This commit is contained in:
2024-01-10 19:57:20 -06:00
parent fed8cbbb45
commit c27a2a986a
2 changed files with 67 additions and 16 deletions

View File

@ -167,6 +167,16 @@ public class QRecord implements Serializable
ArrayList<?> cloneList = new ArrayList<>(arrayList);
clone.put(entry.getKey(), (V) cloneList);
}
else if(entry.getValue() instanceof LinkedHashMap<?, ?> linkedHashMap)
{
LinkedHashMap<?, ?> cloneMap = new LinkedHashMap<>(linkedHashMap);
clone.put(entry.getKey(), (V) cloneMap);
}
else if(entry.getValue() instanceof HashMap<?, ?> hashMap)
{
HashMap<?, ?> cloneMap = new HashMap<>(hashMap);
clone.put(entry.getKey(), (V) cloneMap);
}
else if(entry.getValue() instanceof QRecord otherQRecord)
{
clone.put(entry.getKey(), (V) new QRecord(otherQRecord));

View File

@ -25,6 +25,7 @@ package com.kingsrook.qqq.backend.core.model.data;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.kingsrook.qqq.backend.core.BaseTest;
@ -33,6 +34,7 @@ import com.kingsrook.qqq.backend.core.utils.collections.MapBuilder;
import org.junit.jupiter.api.Test;
import static com.kingsrook.qqq.backend.core.model.data.QRecord.BACKEND_DETAILS_TYPE_HEAVY_FIELD_LENGTHS;
import static com.kingsrook.qqq.backend.core.model.data.QRecord.BACKEND_DETAILS_TYPE_JSON_SOURCE_OBJECT;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
@ -147,10 +149,6 @@ class QRecordTest extends BaseTest
QRecord byteArrayValue = new QRecord().withValue("myBytes", new byte[] { 65, 66, 67, 68 });
assertArrayEquals(new byte[] { 65, 66, 67, 68 }, new QRecord(byteArrayValue).getValueByteArray("myBytes"));
ArrayList<Integer> originalArrayList = new ArrayList<>(List.of(1, 2, 3));
QRecord recordWithArrayListValue = new QRecord().withValue("myList", originalArrayList);
QRecord cloneWithArrayListValue = new QRecord(recordWithArrayListValue);
////////////////////////////////////////////
// qrecord as a value inside another (!?) //
////////////////////////////////////////////
@ -159,18 +157,6 @@ class QRecordTest extends BaseTest
assertEquals(1, ((QRecord) cloneWithNestedQRecord.getValue("myRecord")).getValueInteger("A"));
assertNotSame(cloneWithNestedQRecord.getValue("myRecord"), nestedQRecordValue.getValue("myRecord"));
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// the clone list and original list should be equals (have contents that are equals), but not be the same (reference) //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
assertEquals(List.of(1, 2, 3), cloneWithArrayListValue.getValue("myList"));
assertNotSame(originalArrayList, cloneWithArrayListValue.getValue("myList"));
//////////////////////////////////////////////////////////////////////////////////////////////////////
// make sure a change to the original list doesn't change the cloned list (as it was cloned deeply) //
//////////////////////////////////////////////////////////////////////////////////////////////////////
originalArrayList.add(4);
assertNotEquals(originalArrayList, cloneWithArrayListValue.getValue("myList"));
QRecord emptyRecord = new QRecord();
QRecord emptyClone = new QRecord(emptyRecord);
assertNull(emptyClone.getTableName());
@ -183,4 +169,59 @@ class QRecordTest extends BaseTest
assertEquals(0, emptyClone.getAssociatedRecords().size());
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testListAsValue()
{
ArrayList<Integer> originalArrayList = new ArrayList<>(List.of(1, 2, 3));
QRecord recordWithArrayListValue = new QRecord().withValue("myList", originalArrayList);
QRecord cloneWithArrayListValue = new QRecord(recordWithArrayListValue);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// the clone list and original list should be equals (have contents that are equals), but not be the same (reference) //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
assertEquals(List.of(1, 2, 3), cloneWithArrayListValue.getValue("myList"));
assertNotSame(originalArrayList, cloneWithArrayListValue.getValue("myList"));
//////////////////////////////////////////////////////////////////////////////////////////////////////
// make sure a change to the original list doesn't change the cloned list (as it was cloned deeply) //
//////////////////////////////////////////////////////////////////////////////////////////////////////
originalArrayList.add(4);
assertNotEquals(originalArrayList, cloneWithArrayListValue.getValue("myList"));
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testMapAsValue()
{
LinkedHashMap<String, Integer> originalMap = new LinkedHashMap<>(Map.of("one", 1, "two", 2, "three", 3));
QRecord recordWithMapValue = new QRecord().withValue("myMap", originalMap);
QRecord cloneWithMapValue = new QRecord(recordWithMapValue);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// the clone map and original map should be equals (have contents that are equals), but not be the same (reference) //
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
assertEquals(originalMap, cloneWithMapValue.getValue("myMap"));
assertNotSame(originalMap, cloneWithMapValue.getValue("myMap"));
//////////////////////////////////////////////////////////
// make sure we re-created it as the same subtype (LHM) //
//////////////////////////////////////////////////////////
assertThat(cloneWithMapValue.getValue("myMap")).isInstanceOf(LinkedHashMap.class);
//////////////////////////////////////////////////////////////////////////////////////////////////////
// make sure a change to the original list doesn't change the cloned list (as it was cloned deeply) //
//////////////////////////////////////////////////////////////////////////////////////////////////////
originalMap.put("four", 4);
assertNotEquals(originalMap, cloneWithMapValue.getValue("myMap"));
}
}