Skip to content

Instantly share code, notes, and snippets.

@zivce
Created April 28, 2021 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zivce/c1ecf7229c64c8a4e4344f35971db54a to your computer and use it in GitHub Desktop.
Save zivce/c1ecf7229c64c8a4e4344f35971db54a to your computer and use it in GitHub Desktop.
package com.utils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.*;
import java.util.stream.Collectors;
@RunWith(MockitoJUnitRunner.class)
public class ListOps {
@Test
public void testAsList() {
System.out.println("### TESTING asList ###");
List<Character> asList = Arrays.asList('A', 'V');
List<Character> asList2 = Arrays.asList('A', 'V');
doListOps(asList, asList2);
}
@Test
public void testListOf() {
System.out.println("### TESTING List.Of ###");
List<Character> asList = List.of('A', 'V');
List<Character> asList2 = List.of('A', 'V');
doListOps(asList, asList2);
}
@Test
public void testSingletonList() {
System.out.println("### TESTING singletonList ###");
List<Character> asList = Collections.singletonList('A');
List<Character> asList2 = Collections.singletonList('A');
doListOps(asList, asList2);
}
@Test
public void testArrayList() {
System.out.println("### TESTING singletonList ###");
List<Character> asList = new ArrayList<>(Arrays.asList('A', 'V'));
List<Character> asList2 = new ArrayList<>(Arrays.asList('A', 'V'));
doListOps(asList, asList2);
}
@Test
public void testUnmodifiableList() {
System.out.println("### TESTING unmodifiableList ###");
List<Character> asList = Collections.unmodifiableList(List.of('A', 'V'));
List<Character> asList2 = Collections.unmodifiableList(List.of('A', 'V'));
doListOps(asList, asList2);
}
public void doListOps(List<Character> list, List<Character> list2) {
UnsupportedOperationException exc = null;
try {
System.out.println("add operation for list");
list.add('Y');
}
catch (UnsupportedOperationException e) {
exc = e;
}
if (exc == null) {
System.out.println("Exception not thrown for add");
} else {
System.out.println("Exception thrown for add " + exc);
}
printList(list);
exc = null;
System.out.println("========================");
try {
System.out.println("set operation for list");
list.set(0, 'A');
}
catch (UnsupportedOperationException e) {
exc = e;
}
if (exc == null) {
System.out.println("Exception not thrown for set");
} else {
System.out.println("Exception thrown for set " + exc);
}
printList(list);
exc = null;
System.out.println("========================");
try {
System.out.println("addAll operation for list");
list.addAll(0, list2);
}
catch (UnsupportedOperationException e) {
exc = e;
}
if (exc == null) {
System.out.println("Exception not thrown for addAll");
}
else {
System.out.println("Exception thrown for addAll " + exc);
}
printList(list);
exc = null;
System.out.println("========================");
try {
System.out.println("remove operation for list");
list.remove(0);
}
catch (UnsupportedOperationException e) {
exc = e;
}
if (exc == null) {
System.out.println("Exception not thrown for remove");
}
else {
System.out.println("Exception thrown for remove " + exc);
}
printList(list);
exc = null;
System.out.println("========================");
try {
System.out.println("removeAll WITH FULL LIST operation for list");
list.removeAll(list2);
}
catch (UnsupportedOperationException e) {
exc = e;
}
if (exc == null) {
System.out.println("Exception not thrown for removeAll WITH FULL LIST ");
}
else {
System.out.println("Exception thrown for removeAll WITH FULL LIST " + exc);
}
printList(list);
exc = null;
System.out.println("========================");
try {
System.out.println("removeAll WITH EMPTY LIST operation for list");
list.removeAll(Collections.emptyList());
}
catch (UnsupportedOperationException e) {
exc = e;
}
if (exc == null) {
System.out.println("Exception not thrown for removeAll");
}
else {
System.out.println("Exception thrown for removeAll WITH EMPTY LIST " + exc);
}
printList(list);
exc = null;
System.out.println("========================");
try {
System.out.println("retainAll() WITH EMPTY LIST operation for list");
list.retainAll(Collections.emptyList());
}
catch (UnsupportedOperationException e) {
exc = e;
}
if (exc == null) {
System.out.println("Exception not thrown for retainAll() WITH EMPTY LIST ");
}
else {
System.out.println("Exception thrown for retainAll() WITH EMPTY LIST " + exc);
}
printList(list);
exc = null;
System.out.println("========================");
try {
System.out.println("retainAll WITH SAME LIST operation for list");
list.retainAll(list2);
}
catch (UnsupportedOperationException e) {
exc = e;
}
if (exc == null) {
System.out.println("Exception not thrown for retainAll");
}
else {
System.out.println("Exception thrown for retainAll WITH EMPTY LIST " + exc);
}
printList(list);
exc = null;
System.out.println("========================");
try {
System.out.println("replaceAll operation for list");
list.replaceAll(Character::charValue);
}
catch (UnsupportedOperationException e) {
exc = e;
}
if (exc == null) {
System.out.println("Exception not thrown for replaceAll");
}
else {
System.out.println("Exception thrown for replaceAll " + exc);
}
printList(list);
exc = null;
System.out.println("========================");
try {
System.out.println("sort operation for list");
list.sort(Character::compareTo);
}
catch (UnsupportedOperationException e) {
exc = e;
}
if (exc == null) {
System.out.println("Exception not thrown for sort");
}
else {
System.out.println("Exception thrown for sort " + exc);
}
exc = null;
printList(list);
System.out.println("========================");
List<Character> tmp = list;
try {
System.out.println("clear operation for list");
list.clear();
}
catch (UnsupportedOperationException e) {
exc = e;
}
if (exc == null) {
System.out.println("Exception not thrown for clear");
}
else {
System.out.println("Exception thrown for clear " + exc);
}
list = tmp;
printList(list);
exc = null;
System.out.println("========================");
}
private void printList(List<Character> list)
{
System.out.println("========================");
System.out.println("PRINTING LIST");
System.out.println(list.stream().map(Object::toString)
.collect(Collectors.joining(" ")));
System.out.println("========================");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment