diff --git a/libraries/README.md b/libraries/README.md new file mode 100644 index 0000000000..7b4f7c36ac --- /dev/null +++ b/libraries/README.md @@ -0,0 +1,5 @@ +The libraries module contains examples related to small libraries that are relatively easy to use and does not require any separate module of its own. + +The code examples related to different libraries should go in a new package. + +Remember, for advanced libraries like JUnit, Jackson, etc. we already have separate modules. Please make sure to have a look at the existing modules in such cases. \ No newline at end of file diff --git a/libraries/pom.xml b/libraries/pom.xml index bfcc9b66d5..c89efa3f66 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -1,60 +1,71 @@ - - - parent-modules - com.baeldung - 1.0.0-SNAPSHOT - - 4.0.0 + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + 4.0.0 - libraries - libraries - - - - org.apache.maven.plugins - maven-compiler-plugin - - 1.8 - 1.8 - - - - + libraries + libraries + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + - - - - cglib - cglib - ${cglib.version} - - - org.apache.commons - commons-lang3 - ${commons-lang.version} - - - junit - junit - ${junit.version} - test - - - org.jasypt - jasypt - ${jasypt.version} - + + + + cglib + cglib + ${cglib.version} + + + org.apache.commons + commons-lang3 + ${commons-lang.version} + + + junit + junit + ${junit.version} + test + + + org.jasypt + jasypt + ${jasypt.version} + + + org.javatuples + javatuples + ${javatuples.version} + + + + org.assertj + assertj-core + ${assertj.version} + + - - - - 3.2.4 - 3.5 - 4.12 - 1.9.2 - + + 3.2.4 + 3.5 + 4.12 + 1.9.2 + 1.2 + 3.6.2 + diff --git a/libraries/src/test/java/com/baeldung/javatuples/JavaTuplesTest.java b/libraries/src/test/java/com/baeldung/javatuples/JavaTuplesTest.java new file mode 100644 index 0000000000..af4308e188 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/javatuples/JavaTuplesTest.java @@ -0,0 +1,118 @@ +package com.baeldung.javatuples; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Arrays; +import java.util.List; + +import org.javatuples.KeyValue; +import org.javatuples.LabelValue; +import org.javatuples.Pair; +import org.javatuples.Quartet; +import org.javatuples.Triplet; +import org.javatuples.Unit; +import org.junit.Test; + +public class JavaTuplesTest { + + @SuppressWarnings("unused") + @Test + public void whenCreatingTuples_thenCreateTuples() { + Pair pair = new Pair("This is a pair", 55); + Triplet triplet = Triplet.with("hello", 23, 33.2); + + List collectionOfNames = Arrays.asList("john", "doe", "anne", "alex"); + Quartet quartet = Quartet.fromCollection(collectionOfNames); + + Pair pairFromList = Pair.fromIterable(collectionOfNames, 2); + + String[] names = new String[] { "john", "doe", "anne" }; + Triplet triplet2 = Triplet.fromArray(names); + } + + @Test + public void whenGetValuexFromTuples_thenRetriveValueWithType() { + Quartet quartet = Quartet.with("john", 72.5, 32, "1051 SW"); + + String name = quartet.getValue0(); + Integer age = quartet.getValue2(); + assertThat(name).isEqualTo("john"); + assertThat(age).isEqualTo(32); + } + + @Test + public void whenGetKeyValue_thenGetKeyValue() { + KeyValue keyValue = KeyValue.with(5, "F"); + Integer key = keyValue.getKey(); + String value = keyValue.getValue(); + + assertThat(key).isEqualTo(5); + assertThat(value).isEqualTo("F"); + } + + @Test + public void whenGetLabelValue_thenGetLabelValue() { + LabelValue labelValue = LabelValue.with(5, "F"); + Integer key = labelValue.getLabel(); + String value = labelValue.getValue(); + + assertThat(key).isEqualTo(5); + assertThat(value).isEqualTo("F"); + } + + @Test + public void whenGetValueFromTuples_thenRetriveValueWithoutType() { + Quartet quartet = Quartet.with("john", 72.5, 32, "1051 SW"); + + String name = (String) quartet.getValue(0); + Integer age = (Integer) quartet.getValue(2); + assertThat(name).isEqualTo("john"); + assertThat(age).isEqualTo(32); + } + + @Test + public void whenSetValueInTuple_thenGetANewTuple() { + Pair john = Pair.with("john", 32); + Pair alex = john.setAt0("alex"); + assertThat(john.toString()).isNotEqualTo(alex.toString()); + } + + @Test + public void whenAddNewElement_thenCreateNewTuple() { + Pair pair1 = Pair.with("john", 32); + Triplet triplet1 = pair1.add("1051 SW"); + assertThat(triplet1.contains("john")); + assertThat(triplet1.contains(32)); + assertThat(triplet1.contains("1051 SW")); + + Pair pair2 = Pair.with("alex", 45); + Quartet quartet2 = pair1.add(pair2); + assertThat(quartet2.containsAll(pair1)); + assertThat(quartet2.containsAll(pair2)); + + Quartet quartet1 = pair1.add("alex", 45); + assertThat(quartet1.containsAll("alex", "john", 32, 45)); + + Triplet triplet2 = pair1.addAt1("1051 SW"); + assertThat(triplet2.indexOf("john")).isEqualTo(0); + assertThat(triplet2.indexOf("1051 SW")).isEqualTo(1); + assertThat(triplet2.indexOf(32)).isEqualTo(2); + + Unit unit = pair1.removeFrom0(); + assertThat(unit.contains(32)); + } + + @Test + public void whenCallingToList_thenReturnList() { + Quartet quartet = Quartet.with("john", 72.5, 32, "1051 SW"); + List list = quartet.toList(); + assertThat(list.size()).isEqualTo(4); + } + + @Test + public void whenCallingToArray_thenReturnArray() { + Quartet quartet = Quartet.with("john", 72.5, 32, "1051 SW"); + Object[] array = quartet.toArray(); + assertThat(array.length).isEqualTo(4); + } +}