From 257bc7cc0f49bd96d22021097c44faa853c0a777 Mon Sep 17 00:00:00 2001 From: Shreyash Date: Sat, 12 May 2018 21:24:27 +0530 Subject: [PATCH] BAEL-998:- Java Vavr interoperability --- .../CollectionsInteroperabilityUnitTest.java | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 vavr/src/test/java/com/baeldung/vavr/interoperability/CollectionsInteroperabilityUnitTest.java diff --git a/vavr/src/test/java/com/baeldung/vavr/interoperability/CollectionsInteroperabilityUnitTest.java b/vavr/src/test/java/com/baeldung/vavr/interoperability/CollectionsInteroperabilityUnitTest.java new file mode 100644 index 0000000000..776ef5c56d --- /dev/null +++ b/vavr/src/test/java/com/baeldung/vavr/interoperability/CollectionsInteroperabilityUnitTest.java @@ -0,0 +1,83 @@ +package com.baeldung.vavr.interoperability; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; + +import org.junit.Test; + +import io.vavr.collection.HashMap; +import io.vavr.collection.LinkedHashSet; +import io.vavr.collection.List; +import io.vavr.collection.Map; +import io.vavr.collection.Stream; + +public class CollectionsInteroperabilityUnitTest { + + @Test + public void givenParams_whenVavrList_thenReturnJavaList() { + List vavrStringList = List.of("JAVA", "Javascript", "Scala"); + + java.util.List javaStringList = vavrStringList.toJavaList(); + assertTrue(javaStringList instanceof java.util.List); + } + + @Test + public void givenParams_whenVavrStream_thenReturnJavaStream() { + Stream vavrStream = Stream.of("JAVA", "Javascript", "Scala"); + + java.util.stream.Stream javaParallelStream = vavrStream.toJavaParallelStream(); + assertTrue(javaParallelStream instanceof java.util.stream.Stream); + + java.util.List javaStringList = vavrStream.toJavaList(); + assertTrue(javaStringList instanceof java.util.List); + } + + @Test + public void givenParams_whenVavrMap_thenReturnJavaMap() { + Map vavrMap = HashMap.of("1", "a", "2", "b", "3", "c"); + + java.util.Map javaMap = vavrMap.toJavaMap(); + assertTrue(javaMap instanceof java.util.Map); + } + + @Test + public void givenParams_whenJavaList_thenReturnVavrListUsingOfAll() { + java.util.List javaList = Arrays.asList("Java", "Haskell", "Scala"); + List vavrList = List.ofAll(javaList); + assertTrue(vavrList instanceof io.vavr.collection.List); + } + + @Test + public void givenParams_whenJavaStream_thenReturnVavrListUsingOfAll() { + java.util.stream.Stream javaStream = Arrays.asList("Java", "Haskell", "Scala") + .stream(); + Stream vavrStream = Stream.ofAll(javaStream); + assertTrue(vavrStream instanceof io.vavr.collection.Stream); + } + + @Test(expected = UnsupportedOperationException.class) + public void givenParams_whenVavrListConverted_thenException() { + java.util.List javaList = List.of("Java", "Haskell", "Scala") + .asJava(); + javaList.add("Python"); + assertEquals(4, javaList.size()); + } + + @Test + public void givenParams_whenVavrListConvertedToMutable_thenRetunMutableList() { + java.util.List javaList = List.of("Java", "Haskell", "Scala") + .asJavaMutable(); + javaList.add("Python"); + assertEquals(4, javaList.size()); + } + + @Test + public void givenParams_WhenVavarListConvertedToLinkedSet_thenReturnLinkedSet() { + List vavrList = List.of("Java", "Haskell", "Scala", "Java"); + LinkedHashSet linkedSet = (LinkedHashSet) vavrList.toLinkedSet(); + assertEquals(3, linkedSet.size()); + } + +}