|
| 1 | +package com.baeldung.arraycopy; |
| 2 | + |
| 3 | +import com.baeldung.arraycopy.model.Address; |
| 4 | +import com.baeldung.arraycopy.model.Employee; |
| 5 | +import org.apache.commons.lang3.SerializationUtils; |
| 6 | +import org.junit.Assert; |
| 7 | +import org.junit.BeforeClass; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import java.util.Arrays; |
| 11 | + |
| 12 | +public class ArrayCopyUtilTest { |
| 13 | + private static Employee[] employees; |
| 14 | + private static final int MAX = 2; |
| 15 | + |
| 16 | + @BeforeClass |
| 17 | + public static void setup(){ |
| 18 | + employees = new Employee[MAX]; |
| 19 | + Employee employee; |
| 20 | + for(int i = 0; i < MAX; i++) { |
| 21 | + employee = new Employee(); |
| 22 | + employee.setName("Emp"+i); |
| 23 | + employee.setId(i); |
| 24 | + employees[i] = employee; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void givenArrayOfPrimitiveType_whenCopiedViaSystemsArrayCopy_thenSuccessful(){ |
| 30 | + int[] array = {23, 43, 55}; |
| 31 | + int[] copiedArray = new int[3]; |
| 32 | + |
| 33 | + System.arraycopy(array, 0, copiedArray, 0, 3); |
| 34 | + |
| 35 | + Assert.assertTrue(array.length == copiedArray.length); |
| 36 | + Assert.assertTrue(copiedArray[0] == array[0]); |
| 37 | + Assert.assertTrue(copiedArray[1] == array[1]); |
| 38 | + Assert.assertTrue(copiedArray[2] == array[2]); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void givenArrayOfPrimitiveType_whenCopiedSubSequenceViaSystemsArrayCopy_thenSuccessful(){ |
| 43 | + int[] array = {23, 43, 55, 12, 65, 88, 92}; |
| 44 | + int[] copiedArray = new int[3]; |
| 45 | + |
| 46 | + System.arraycopy(array, 2, copiedArray, 0, 3); |
| 47 | + |
| 48 | + Assert.assertTrue(3 == copiedArray.length); |
| 49 | + Assert.assertTrue(copiedArray[0] == array[2]); |
| 50 | + Assert.assertTrue(copiedArray[1] == array[3]); |
| 51 | + Assert.assertTrue(copiedArray[2] == array[4]); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void givenArrayOfPrimitiveType_whenCopiedSubSequenceViaArraysCopyOfRange_thenSuccessful(){ |
| 56 | + int[] array = {23, 43, 55, 12, 65, 88, 92}; |
| 57 | + |
| 58 | + int[] copiedArray = Arrays.copyOfRange(array, 1, 4); |
| 59 | + |
| 60 | + Assert.assertTrue(3 == copiedArray.length); |
| 61 | + Assert.assertTrue(copiedArray[0] == array[1]); |
| 62 | + Assert.assertTrue(copiedArray[1] == array[2]); |
| 63 | + Assert.assertTrue(copiedArray[2] == array[3]); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void givenArrayOfPrimitiveType_whenCopiedViaArraysCopyOf_thenValueChangeIsSuccessful(){ |
| 68 | + int[] array = {23, 43, 55, 12}; |
| 69 | + int newLength = array.length; |
| 70 | + |
| 71 | + int[] copiedArray = Arrays.copyOf(array, newLength); |
| 72 | + |
| 73 | + Assert.assertNotNull(copiedArray); |
| 74 | + Assert.assertTrue(copiedArray.length == array.length); |
| 75 | + Assert.assertTrue(copiedArray[0] == array[0]); |
| 76 | + Assert.assertTrue(copiedArray[1] == array[1]); |
| 77 | + Assert.assertTrue(copiedArray[2] == array[2]); |
| 78 | + Assert.assertTrue(copiedArray[3] == array[3]); |
| 79 | + array[0] = 9; |
| 80 | + Assert.assertTrue(copiedArray[0] != array[0]); |
| 81 | + copiedArray[1] = 12; |
| 82 | + Assert.assertTrue(copiedArray[1] != array[1]); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void givenArrayOfNonPrimitiveType_whenCopiedViaArraysCopyOf_thenDoShallowCopy(){ |
| 87 | + Employee[] copiedArray = Arrays.copyOf(employees, employees.length); |
| 88 | + |
| 89 | + Assert.assertNotNull(copiedArray); |
| 90 | + Assert.assertTrue(copiedArray.length == employees.length); |
| 91 | + employees[0].setName(employees[0].getName()+"_Changed"); |
| 92 | + //change in employees' element caused change in the copied array |
| 93 | + Assert.assertTrue(copiedArray[0].getName().equals(employees[0].getName())); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void givenArrayOfPrimitiveType_whenCopiedViaArrayClone_thenValueChangeIsSuccessful(){ |
| 98 | + int[] array = {23, 43, 55, 12}; |
| 99 | + |
| 100 | + int[] copiedArray = array.clone(); |
| 101 | + |
| 102 | + Assert.assertNotNull(copiedArray); |
| 103 | + Assert.assertTrue(copiedArray.length == array.length); |
| 104 | + Assert.assertTrue(copiedArray[0] == array[0]); |
| 105 | + Assert.assertTrue(copiedArray[1] == array[1]); |
| 106 | + Assert.assertTrue(copiedArray[2] == array[2]); |
| 107 | + Assert.assertTrue(copiedArray[3] == array[3]); |
| 108 | + array[0] = 9; |
| 109 | + Assert.assertTrue(copiedArray[0] != array[0]); |
| 110 | + copiedArray[1] = 12; |
| 111 | + Assert.assertTrue(copiedArray[1] != array[1]); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void givenArraysOfNonPrimitiveType_whenCopiedViaArrayClone_thenDoShallowCopy(){ |
| 116 | + Employee[] copiedArray = employees.clone(); |
| 117 | + |
| 118 | + Assert.assertNotNull(copiedArray); |
| 119 | + Assert.assertTrue(copiedArray.length == employees.length); |
| 120 | + employees[0].setName(employees[0].getName()+"_Changed"); |
| 121 | + //change in employees' element changed the copied array |
| 122 | + Assert.assertTrue(copiedArray[0].getName().equals(employees[0].getName())); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void givenArraysOfCloneableNonPrimitiveType_whenCopiedViaArrayClone_thenDoShallowCopy(){ |
| 127 | + Address[] addresses = createAddressArray(); |
| 128 | + |
| 129 | + Address[] copiedArray = addresses.clone(); |
| 130 | + |
| 131 | + Assert.assertNotNull(copiedArray); |
| 132 | + Assert.assertTrue(copiedArray.length == addresses.length); |
| 133 | + addresses[0].setCity(addresses[0].getCity()+"_Changed"); |
| 134 | + Assert.assertTrue(copiedArray[0].getCity().equals(addresses[0].getCity())); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void givenArraysOfSerializableNonPrimitiveType_whenCopiedViaSerializationUtils_thenDoDeepCopy(){ |
| 139 | + Employee[] copiedArray = SerializationUtils.clone(employees); |
| 140 | + |
| 141 | + Assert.assertNotNull(copiedArray); |
| 142 | + Assert.assertTrue(copiedArray.length == employees.length); |
| 143 | + employees[0].setName(employees[0].getName()+"_Changed"); |
| 144 | + //change in employees' element didn't change in the copied array |
| 145 | + Assert.assertFalse(copiedArray[0].getName().equals(employees[0].getName())); |
| 146 | + } |
| 147 | + |
| 148 | + private Address[] createAddressArray(){ |
| 149 | + Address[] addresses = new Address[1]; |
| 150 | + addresses[0] = createAddress(); |
| 151 | + return addresses; |
| 152 | + } |
| 153 | + |
| 154 | + private Address createAddress() { |
| 155 | + Address address = new Address(); |
| 156 | + address.setCountry("USA"); |
| 157 | + address.setState("CA"); |
| 158 | + address.setCity("San Francisco"); |
| 159 | + address.setStreet("Street 1"); |
| 160 | + address.setZipcode("59999"); |
| 161 | + return address; |
| 162 | + } |
| 163 | +} |
0 commit comments