Merge pull request #10210 from vishal1023/BAEL-4236

Bael 4236
This commit is contained in:
Greg 2020-11-08 13:41:04 -05:00 committed by GitHub
commit 3888cd0af1
10 changed files with 172 additions and 0 deletions

View File

@ -0,0 +1,14 @@
package com.baeldung.exception.indexoutofbounds;
import java.util.ArrayList;
import java.util.List;
public class CopyListUsingAddAllMethodDemo {
static List<Integer> copyList(List<Integer> source) {
List<Integer> destination = new ArrayList<>();
destination.addAll(source);
return destination;
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.exception.indexoutofbounds;
import java.util.Collections;
import java.util.List;
public class CopyListUsingCollectionsCopyMethodDemo {
static void copyList(List<Integer> source, List<Integer> destination) {
Collections.copy(destination, source);
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.exception.indexoutofbounds;
import java.util.ArrayList;
import java.util.List;
public class CopyListUsingConstructorDemo {
static List<Integer> copyList(List<Integer> source) {
return new ArrayList<>(source);
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.exception.indexoutofbounds;
import java.util.List;
import java.util.stream.Collectors;
public class CopyListUsingJava8StreamDemo {
static List<Integer> copyList(List<Integer> source) {
return source
.stream()
.collect(Collectors.toList());
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.exception.indexoutofbounds;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class IndexOutOfBoundsExceptionDemo {
static List<Integer> copyList(List<Integer> source) {
List<Integer> destination = new ArrayList<>(source.size());
Collections.copy(destination, source);
return destination;
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.exception.indexoutofbounds;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class CopyListUsingAddAllMethodDemoUnitTest {
@Test
void whenPassValidArrayList_thenCopyListUsingAddAllMethod() {
List<Integer> source = Arrays.asList(11, 22, 33);
assertEquals(source, CopyListUsingAddAllMethodDemo.copyList(source));
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.exception.indexoutofbounds;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
public class CopyListUsingCollectionsCopyMethodDemoUnitTest {
@Test
void whenCopyListUsingCollectionsCopy_thenOverrideAllDestinationListValues() {
List<Integer> source = Arrays.asList(11, 22, 33);
List<Integer> destination = Arrays.asList(1, 2, 3);
CopyListUsingCollectionsCopyMethodDemo.copyList(source, destination);
assertEquals(source, destination);
}
@Test
void whenCopyListUsingCollectionsCopy_thenOverrideInitialDestinationValuesAndOthersShouldBeUnchanged(){
List<Integer> source = Arrays.asList(11, 22, 33);
List<Integer> destination = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> expectedList = Arrays.asList(11, 22, 33, 4, 5);
CopyListUsingCollectionsCopyMethodDemo.copyList(source, destination);
assertEquals(expectedList, destination);
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.exception.indexoutofbounds;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class CopyListUsingConstructorDemoUnitTest {
@Test
void whenCopyListUsingConstructor_thenMakeACopyOfList() {
List<Integer> source = Arrays.asList(11, 22, 33);
assertEquals(source, CopyListUsingConstructorDemo.copyList(source));
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.exception.indexoutofbounds;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class CopyListUsingJava8StreamDemoUnitTest {
@Test
void whenCopyListUsingStream_thenMakeACopyOfArrayList() {
List<Integer> source = Arrays.asList(11, 22, 33);
assertEquals(source, CopyListUsingJava8StreamDemo.copyList(source));
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.exception.indexoutofbounds;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
class IndexOutOfBoundsExceptionDemoUnitTest {
@Test
void givenDestinationArrayListSizeIsZero_whenCopySourceArrayListToDestination_thenShouldThrowIndexOutOfBoundsException() {
List<Integer> source = Arrays.asList(1, 2, 3, 4, 5);
assertThrows(IndexOutOfBoundsException.class, () -> IndexOutOfBoundsExceptionDemo.copyList(source));
}
@Test
void givenSourceAndDestinationListSizeIsEqual_whenCopySourceArrayListToDestination_thenShouldNotThrowIndexOutOfBoundsException() {
List<Integer> source = Collections.emptyList();
assertEquals(source, IndexOutOfBoundsExceptionDemo.copyList(source));
}
}