From 2cfbd8d91492a2bbf4a7dabbc7b1c6af4fa8b0ed Mon Sep 17 00:00:00 2001 From: Trying something new <50325284+tancafa@users.noreply.github.com> Date: Sat, 18 Sep 2021 08:50:24 +0530 Subject: [PATCH] Formatting Changes for Java 5074 (#11206) * Formatting Changes for Java 5074 Changed line change indentation to 2 spaces * Rename StreamToListComparisonWithCollectorsToListUnitTest to StreamToListComparisonWithCollectorsToListUnitTest.java --- ...omparisonWithCollectorsToListUnitTest.java | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 core-java-modules/core-java-16/src/test/java/com/baeldung/streams/StreamToListComparisonWithCollectorsToListUnitTest.java diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/streams/StreamToListComparisonWithCollectorsToListUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/streams/StreamToListComparisonWithCollectorsToListUnitTest.java new file mode 100644 index 0000000000..e1330bb4f7 --- /dev/null +++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/streams/StreamToListComparisonWithCollectorsToListUnitTest.java @@ -0,0 +1,128 @@ +package com.baeldung.streams; + +import java.util.List; +import java.util.Locale; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class StreamToListComparisonWithCollectorsToListUnitTest { + + @Test + public void whenAddingtoCollectToList_thenSuccess() { + + List result = Stream.of(Locale.getISOCountries()) + .collect(Collectors.toList()); + + Assertions.assertDoesNotThrow(() -> { + result.add("test"); + }); + } + + @Test + public void whenSortingtoCollectToList_thenSuccess() { + + List result = Stream.of(Locale.getISOCountries()) + .collect(Collectors.toList()); + + Assertions.assertDoesNotThrow(() -> { + result.sort(String::compareToIgnoreCase); + }); + } + + @Test + public void whenAddingCollectUnmodifiableToList_thenException() { + + List result = Stream.of(Locale.getISOCountries()) + .collect(Collectors.toUnmodifiableList()); + + Assertions.assertThrows(UnsupportedOperationException.class, () -> { + result.add("test"); + }); + } + + @Test + public void whenSortingCollectUnmodifiableToList_thenSortException() { + + List result = Stream.of(Locale.getISOCountries()) + .collect(Collectors.toUnmodifiableList()); + + Assertions.assertThrows(UnsupportedOperationException.class, () -> { + result.sort(String::compareToIgnoreCase); + }); + } + + @Test + public void whenAddingStreamToList_thenException() { + + List result = Stream.of(Locale.getISOCountries()) + .toList(); + + Assertions.assertThrows(UnsupportedOperationException.class, () -> { + result.add("test"); + }); + } + + @Test + public void whenSortingStreamToList_thenException() { + + List result = Stream.of(Locale.getISOCountries()) + .toList(); + + Assertions.assertThrows(UnsupportedOperationException.class, () -> { + result.sort(String::compareToIgnoreCase); + }); + } + + @Test + public void whenComparingStreamToList_withCollectToList_thenEqual() { + + List streamToList = Stream.of(Locale.getISOCountries()) + .toList(); + List collectToList = Stream.of(Locale.getISOCountries()) + .collect(Collectors.toList()); + + Assertions.assertEquals(streamToList, collectToList); + } + + @Test + public void whenComparingStreamToList_withUnmodifiedCollectToList_thenEqual() { + + List streamToList = Stream.of(Locale.getISOCountries()) + .toList(); + List collectToUnmodifiableList = Stream.of(Locale.getISOCountries()) + .collect(Collectors.toUnmodifiableList()); + + Assertions.assertEquals(streamToList, collectToUnmodifiableList); + } + + @Test + public void whenNullCollectorsToList_thenSuccess() { + + Assertions.assertDoesNotThrow(() -> { + Stream.of(null, null) + .collect(Collectors.toList()); + }); + } + + @Test + public void whenNullCollectorsUnmodifiableToList_thenException() { + + Assertions.assertThrows(NullPointerException.class, () -> { + Stream.of(null, null) + .collect(Collectors.toUnmodifiableList()); + }); + } + + @Test + public void whenNullStreamToList_thenSuccess() { + + Assertions.assertDoesNotThrow(() -> { + Stream.of(null, null) + .toList(); + }); + } + +}