From 7b836d92116085c166fcdb236f955f166bdd9307 Mon Sep 17 00:00:00 2001 From: bankole salako Date: Tue, 5 Sep 2017 16:51:08 +0100 Subject: [PATCH] Pairs functionality in Java (Core Java, Apache Commons and Vavr) (#2563) * Unit tests for pairs functionality in Java (Core Java, Apache Commons and Vavr) * Remove default file templates * Refactor mustache * BAEL-1123 - Testing Linked List for Cycles - deep20jain@gmail.com (#2525) * Adding node and cycle detection by hashing * Adding implementation for all algorithms for cycle detection and removal * Applying formatting rules * JDeferred (#2553) * moving jmh into libraries module * refactoring jmh * Update pom.xml * manual algorightm * with BM result * fix for space issue * Fixed indentation * change as per suggestion * vavr either * adding unit test and othe rutilities * adding concurrent module * concurrent package description * concurrent package description * Update EitherUnitTest.java * introducing lambda expression * jooby project * jooby project * reducing employee bean content * bootique module * bootique * bootique * undertow module * undertow module * refactoring * using lambda * as per baeldung formatter * easy code added for simplicity * simpliflying * adding JDeferred module * moved jdeferred to librarires module * BAEL-1116 Spring swagger codegen (#2527) * Spring integration with Swagger client code genrator * Refactor * format pom.xml * BAEL-1106 Introduction to javax.measure (#2522) * BAEL-243 Spring Batch using Partitioner * BAEL-1106 Introduction to javax.measure * Unnecessary Committed * BAEL-1106 Move code to libraries from core-java * BAEL-1106 Move code to libraries from core-java * Update pom.xml * Introduced jmh benchmarking (#2549) * Bael 1043/protonpack (#2415) * BAEL-1043/ Introduction to protonpack * Added test cases for windowed() api * Resolving merge issues * Retrofit rx (#2519) * Retrofit with RxJava * Correct spelling mistake * Use spaces for indentation instead of tabs in pom.xml * Use spaces for indentation instead of tabs in pom.xml * Add Retrofit integration with RxJava to libraries module * Remove standalone project for Retrofit integration with RxJava * remove retrofit-rxjava module * Fixed error in pom.xml caused by an issue while merging * Retrofit integration with RxJava * Fix test cases * Fix merge issues * BAEL-1016 Merging master * import source BAEL-1084 (#2560) * Moved to libraries folder from core-java --- .../pairs/ApacheCommonsPairUnitTest.java | 50 +++++++++++++++++++ .../baeldung/pairs/CoreJavaPairUnitTest.java | 17 +++++++ .../com/baeldung/pairs/VavrPairsUnitTest.java | 29 +++++++++++ 3 files changed, 96 insertions(+) create mode 100644 libraries/src/test/java/com/baeldung/pairs/ApacheCommonsPairUnitTest.java create mode 100644 libraries/src/test/java/com/baeldung/pairs/CoreJavaPairUnitTest.java create mode 100644 libraries/src/test/java/com/baeldung/pairs/VavrPairsUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/pairs/ApacheCommonsPairUnitTest.java b/libraries/src/test/java/com/baeldung/pairs/ApacheCommonsPairUnitTest.java new file mode 100644 index 0000000000..0e6242b8a3 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/pairs/ApacheCommonsPairUnitTest.java @@ -0,0 +1,50 @@ +package com.baeldung.pairs; + +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.commons.lang3.tuple.MutablePair; +import org.junit.Assert; +import org.junit.Test; + +public class ApacheCommonsPairUnitTest { + + @Test + public void givenMutablePair_whenGetValue_shouldPass() { + int key = 5; + String value = "Five"; + + MutablePair mutablePair = new MutablePair<>(key, value); + Assert.assertTrue(mutablePair.getKey() == key); + Assert.assertEquals(mutablePair.getValue(), value); + } + + @Test + public void givenMutablePair_whenSetValue_shouldPass() { + int key = 6; + String value = "Six"; + String newValue = "New Six"; + + MutablePair mutablePair = new MutablePair<>(key, value); + Assert.assertTrue(mutablePair.getKey() == key); + Assert.assertEquals(mutablePair.getValue(), value); + mutablePair.setValue(newValue); + Assert.assertEquals(mutablePair.getValue(), newValue); + } + + @Test + public void givenImmutablePair_whenGetValue_shouldPass() { + int key = 2; + String value = "Two"; + + ImmutablePair immutablePair = new ImmutablePair<>(key, value); + Assert.assertTrue(immutablePair.getKey() == key); + Assert.assertEquals(immutablePair.getValue(), value); + } + + @Test(expected = UnsupportedOperationException.class) + public void givenImmutablePair_whenSetValue_shouldFail() { + ImmutablePair immutablePair = new ImmutablePair<>(1, "One"); + immutablePair.setValue("Another One"); + } + + +} diff --git a/libraries/src/test/java/com/baeldung/pairs/CoreJavaPairUnitTest.java b/libraries/src/test/java/com/baeldung/pairs/CoreJavaPairUnitTest.java new file mode 100644 index 0000000000..ac258b9c77 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/pairs/CoreJavaPairUnitTest.java @@ -0,0 +1,17 @@ +package com.baeldung.pairs; + +import javafx.util.Pair; +import org.junit.Assert; +import org.junit.Test; + +public class CoreJavaPairUnitTest { + @Test + public void givenPair_whenGetValue_shouldSucceed() { + String key = "Good Day"; + boolean value = true; + Pair pair = new Pair<>(key, value); + + Assert.assertEquals(key, pair.getKey()); + Assert.assertEquals(value, pair.getValue()); + } +} \ No newline at end of file diff --git a/libraries/src/test/java/com/baeldung/pairs/VavrPairsUnitTest.java b/libraries/src/test/java/com/baeldung/pairs/VavrPairsUnitTest.java new file mode 100644 index 0000000000..9c318c7744 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/pairs/VavrPairsUnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung.pairs; + +import io.vavr.Tuple2; +import org.junit.Assert; +import org.junit.Test; + +public class VavrPairsUnitTest { + @Test + public void givenTuple_whenSetValue_shouldSucceed() { + String key = "Eleven"; + double value = 11.0; + double newValue = 11.1; + + Tuple2 pair = new Tuple2<>(key, value); + + pair = pair.update2(newValue); + Assert.assertTrue(newValue == pair._2()); + } + + @Test + public void givenPair_whenGetValue_shouldSucceed() { + String key = "Twelve"; + double value = 12.0; + + Tuple2 pair = new Tuple2<>(key, value); + + Assert.assertTrue(value == pair._2()); + } +} \ No newline at end of file