From 22b4aebd9ddfd0a22447d3e29e8f1861ab05618e Mon Sep 17 00:00:00 2001 From: "matt.rossi" Date: Sat, 16 Nov 2019 18:00:04 +0100 Subject: [PATCH 1/4] Add Jenkinsfile & update gatling version --- testing-modules/gatling/Jenkinsfile | 20 ++ testing-modules/gatling/pom.xml | 230 ++++++++---------- .../gatling/src/test/scala/Engine.scala | 11 +- .../src/test/scala/IDEPathHelper.scala | 9 +- .../gatling/src/test/scala/Recorder.scala | 8 +- .../org/baeldung/RecordedSimulation.scala | 2 +- 6 files changed, 140 insertions(+), 140 deletions(-) create mode 100644 testing-modules/gatling/Jenkinsfile diff --git a/testing-modules/gatling/Jenkinsfile b/testing-modules/gatling/Jenkinsfile new file mode 100644 index 0000000000..0786788406 --- /dev/null +++ b/testing-modules/gatling/Jenkinsfile @@ -0,0 +1,20 @@ +pipeline { + agent any + stages { + stage("Build Maven") { + steps { + sh 'mvn -B clean package' + } + } + stage("Run Gatling") { + steps { + sh 'mvn gatling:test' + } + post { + always { + gatlingArchive() + } + } + } + } +} \ No newline at end of file diff --git a/testing-modules/gatling/pom.xml b/testing-modules/gatling/pom.xml index 37693ebfee..d105cc8b3e 100644 --- a/testing-modules/gatling/pom.xml +++ b/testing-modules/gatling/pom.xml @@ -1,13 +1,13 @@ - 4.0.0 - org.baeldung - gatling - 1.0-SNAPSHOT - gatling - + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + org.baeldung + gatling + 1.0-SNAPSHOT + gatling + com.baeldung parent-modules @@ -15,122 +15,106 @@ ../../ - - - - io.gatling - gatling-app - ${gatling.version} - - - io.gatling - gatling-recorder - ${gatling.version} - - - io.gatling.highcharts - gatling-charts-highcharts - ${gatling.version} - - - org.scala-lang - scala-library - ${scala.version} - - - - - - - io.gatling.highcharts - gatling-charts-highcharts - - - io.gatling - gatling-app - - - io.gatling - gatling-recorder - - - org.scala-lang - scala-library - - - - src/test/scala - - - - net.alchim31.maven - scala-maven-plugin - ${scala-maven-plugin.version} - - - - - - net.alchim31.maven - scala-maven-plugin - - - - testCompile - - - - - -Ydelambdafy:method - -target:jvm-1.8 - -deprecation - -feature - -unchecked - -language:implicitConversions - -language:postfixOps - - - - - - - - - - - simulation - - - - io.gatling - gatling-maven-plugin - ${gatling-maven-plugin.version} - - - test - - execute - - - true - - - - - - - - + + + + io.gatling + gatling-app + ${gatling.version} + + + io.gatling + gatling-recorder + ${gatling.version} + + + io.gatling.highcharts + gatling-charts-highcharts + ${gatling.version} + + + org.scala-lang + scala-library + ${scala.version} + + + - - 1.8 - 1.8 - UTF-8 - 2.12.6 - 2.3.1 - 3.2.2 - 2.2.4 - + + + io.gatling.highcharts + gatling-charts-highcharts + + + io.gatling + gatling-app + + + io.gatling + gatling-recorder + + + org.scala-lang + scala-library + + + + + src/test/scala + + + + net.alchim31.maven + scala-maven-plugin + ${scala-maven-plugin.version} + + + + + + net.alchim31.maven + scala-maven-plugin + + + + testCompile + + + + + -Ydelambdafy:method + -target:jvm-1.8 + -deprecation + -feature + -unchecked + -language:implicitConversions + -language:postfixOps + + + + + + + io.gatling + gatling-maven-plugin + ${gatling-maven-plugin.version} + + org.baeldung.RecordedSimulation + + + + + + + + 1.8 + 1.8 + UTF-8 + 2.12.6 + 3.3.1 + 4.3.0 + 3.0.4 + diff --git a/testing-modules/gatling/src/test/scala/Engine.scala b/testing-modules/gatling/src/test/scala/Engine.scala index c2884fc218..a34d3eaf60 100644 --- a/testing-modules/gatling/src/test/scala/Engine.scala +++ b/testing-modules/gatling/src/test/scala/Engine.scala @@ -3,11 +3,10 @@ import io.gatling.core.config.GatlingPropertiesBuilder object Engine extends App { - val props = new GatlingPropertiesBuilder - props.dataDirectory(IDEPathHelper.dataDirectory.toString) - props.resultsDirectory(IDEPathHelper.resultsDirectory.toString) - props.bodiesDirectory(IDEPathHelper.bodiesDirectory.toString) - props.binariesDirectory(IDEPathHelper.mavenBinariesDirectory.toString) + val props = new GatlingPropertiesBuilder() + .resourcesDirectory(IDEPathHelper.resourcesDirectory.toString) + .resultsDirectory(IDEPathHelper.resultsDirectory.toString) + .binariesDirectory(IDEPathHelper.mavenBinariesDirectory.toString) - Gatling.fromMap(props.build) + Gatling.fromMap(props.build) } diff --git a/testing-modules/gatling/src/test/scala/IDEPathHelper.scala b/testing-modules/gatling/src/test/scala/IDEPathHelper.scala index 9fb1d7d5c8..6aef6707b2 100644 --- a/testing-modules/gatling/src/test/scala/IDEPathHelper.scala +++ b/testing-modules/gatling/src/test/scala/IDEPathHelper.scala @@ -4,7 +4,7 @@ import io.gatling.commons.util.PathHelper._ object IDEPathHelper { - val gatlingConfUrl: Path = getClass.getClassLoader.getResource("gatling.conf").toURI + val gatlingConfUrl: Path = getClass.getClassLoader.getResource("gatling.conf") val projectRootDir = gatlingConfUrl.ancestor(3) val mavenSourcesDirectory = projectRootDir / "src" / "test" / "scala" @@ -12,11 +12,8 @@ object IDEPathHelper { val mavenTargetDirectory = projectRootDir / "target" val mavenBinariesDirectory = mavenTargetDirectory / "test-classes" - val dataDirectory = mavenResourcesDirectory / "data" - val bodiesDirectory = mavenResourcesDirectory / "bodies" - - val recorderOutputDirectory = mavenSourcesDirectory + val resourcesDirectory = mavenResourcesDirectory + val recorderSimulationsDirectory = mavenSourcesDirectory val resultsDirectory = mavenTargetDirectory / "gatling" - val recorderConfigFile = mavenResourcesDirectory / "recorder.conf" } diff --git a/testing-modules/gatling/src/test/scala/Recorder.scala b/testing-modules/gatling/src/test/scala/Recorder.scala index 9c38e52f12..187f566aac 100644 --- a/testing-modules/gatling/src/test/scala/Recorder.scala +++ b/testing-modules/gatling/src/test/scala/Recorder.scala @@ -3,10 +3,10 @@ import io.gatling.recorder.config.RecorderPropertiesBuilder object Recorder extends App { - val props = new RecorderPropertiesBuilder - props.simulationOutputFolder(IDEPathHelper.recorderOutputDirectory.toString) - props.simulationPackage("org.baeldung") - props.bodiesFolder(IDEPathHelper.bodiesDirectory.toString) + val props = new RecorderPropertiesBuilder() + .simulationsFolder(IDEPathHelper.recorderSimulationsDirectory.toString) + .simulationPackage("org.baeldung") + .resourcesFolder(IDEPathHelper.resourcesDirectory.toString) GatlingRecorder.fromMap(props.build, Some(IDEPathHelper.recorderConfigFile)) } diff --git a/testing-modules/gatling/src/test/scala/org/baeldung/RecordedSimulation.scala b/testing-modules/gatling/src/test/scala/org/baeldung/RecordedSimulation.scala index dece393478..9902f91e0f 100644 --- a/testing-modules/gatling/src/test/scala/org/baeldung/RecordedSimulation.scala +++ b/testing-modules/gatling/src/test/scala/org/baeldung/RecordedSimulation.scala @@ -9,7 +9,7 @@ import io.gatling.jdbc.Predef._ class RecordedSimulation extends Simulation { val httpProtocol = http - .baseURL("http://computer-database.gatling.io") + .baseUrl("http://computer-database.gatling.io") .inferHtmlResources(BlackList(""".*\.css""", """.*\.js""", """.*\.ico"""), WhiteList()) .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") .acceptEncodingHeader("gzip, deflate") From 0cebd99e9edee9ae719acdcbade3545b9c6bef9c Mon Sep 17 00:00:00 2001 From: "matt.rossi" Date: Wed, 8 Jan 2020 18:07:13 +0100 Subject: [PATCH 2/4] Update application-mysql.properties for OpenShift issue --- .../src/main/resources/application-mysql.properties | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/spring-boot-bootstrap/src/main/resources/application-mysql.properties b/spring-boot-bootstrap/src/main/resources/application-mysql.properties index a1823b5d7f..069bf2e9d6 100644 --- a/spring-boot-bootstrap/src/main/resources/application-mysql.properties +++ b/spring-boot-bootstrap/src/main/resources/application-mysql.properties @@ -1 +1,4 @@ -spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect \ No newline at end of file +spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect +spring.application.name = spring-boot-bootstrap +spring.datasource.username = ${SPRING_DATASOURCE_USER} +spring.datasource.password = ${SPRING_DATASOURCE_PASSWORD} \ No newline at end of file From 25546574e5b86fdfa2ec457e273d7f53fa5b5163 Mon Sep 17 00:00:00 2001 From: "matt.rossi" Date: Wed, 8 Jan 2020 18:12:18 +0100 Subject: [PATCH 3/4] Revert "Add Jenkinsfile & update gatling version" This reverts commit 22b4aebd9ddfd0a22447d3e29e8f1861ab05618e. --- testing-modules/gatling/Jenkinsfile | 20 -- testing-modules/gatling/pom.xml | 230 ++++++++++-------- .../gatling/src/test/scala/Engine.scala | 11 +- .../src/test/scala/IDEPathHelper.scala | 9 +- .../gatling/src/test/scala/Recorder.scala | 8 +- .../org/baeldung/RecordedSimulation.scala | 2 +- 6 files changed, 140 insertions(+), 140 deletions(-) delete mode 100644 testing-modules/gatling/Jenkinsfile diff --git a/testing-modules/gatling/Jenkinsfile b/testing-modules/gatling/Jenkinsfile deleted file mode 100644 index 0786788406..0000000000 --- a/testing-modules/gatling/Jenkinsfile +++ /dev/null @@ -1,20 +0,0 @@ -pipeline { - agent any - stages { - stage("Build Maven") { - steps { - sh 'mvn -B clean package' - } - } - stage("Run Gatling") { - steps { - sh 'mvn gatling:test' - } - post { - always { - gatlingArchive() - } - } - } - } -} \ No newline at end of file diff --git a/testing-modules/gatling/pom.xml b/testing-modules/gatling/pom.xml index d105cc8b3e..37693ebfee 100644 --- a/testing-modules/gatling/pom.xml +++ b/testing-modules/gatling/pom.xml @@ -1,13 +1,13 @@ - 4.0.0 - org.baeldung - gatling - 1.0-SNAPSHOT - gatling - + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + org.baeldung + gatling + 1.0-SNAPSHOT + gatling + com.baeldung parent-modules @@ -15,106 +15,122 @@ ../../ + + + + io.gatling + gatling-app + ${gatling.version} + + + io.gatling + gatling-recorder + ${gatling.version} + + + io.gatling.highcharts + gatling-charts-highcharts + ${gatling.version} + + + org.scala-lang + scala-library + ${scala.version} + + + + + + + io.gatling.highcharts + gatling-charts-highcharts + + + io.gatling + gatling-app + + + io.gatling + gatling-recorder + + + org.scala-lang + scala-library + + - - - - io.gatling - gatling-app - ${gatling.version} - - - io.gatling - gatling-recorder - ${gatling.version} - - - io.gatling.highcharts - gatling-charts-highcharts - ${gatling.version} - - - org.scala-lang - scala-library - ${scala.version} - - - + + src/test/scala + + + + net.alchim31.maven + scala-maven-plugin + ${scala-maven-plugin.version} + + + + + + net.alchim31.maven + scala-maven-plugin + + + + testCompile + + + + + -Ydelambdafy:method + -target:jvm-1.8 + -deprecation + -feature + -unchecked + -language:implicitConversions + -language:postfixOps + + + + + + + + + + + simulation + + + + io.gatling + gatling-maven-plugin + ${gatling-maven-plugin.version} + + + test + + execute + + + true + + + + + + + + - - - io.gatling.highcharts - gatling-charts-highcharts - - - io.gatling - gatling-app - - - io.gatling - gatling-recorder - - - org.scala-lang - scala-library - - - - - src/test/scala - - - - net.alchim31.maven - scala-maven-plugin - ${scala-maven-plugin.version} - - - - - - net.alchim31.maven - scala-maven-plugin - - - - testCompile - - - - - -Ydelambdafy:method - -target:jvm-1.8 - -deprecation - -feature - -unchecked - -language:implicitConversions - -language:postfixOps - - - - - - - io.gatling - gatling-maven-plugin - ${gatling-maven-plugin.version} - - org.baeldung.RecordedSimulation - - - - - - - - 1.8 - 1.8 - UTF-8 - 2.12.6 - 3.3.1 - 4.3.0 - 3.0.4 - + + 1.8 + 1.8 + UTF-8 + 2.12.6 + 2.3.1 + 3.2.2 + 2.2.4 + diff --git a/testing-modules/gatling/src/test/scala/Engine.scala b/testing-modules/gatling/src/test/scala/Engine.scala index a34d3eaf60..c2884fc218 100644 --- a/testing-modules/gatling/src/test/scala/Engine.scala +++ b/testing-modules/gatling/src/test/scala/Engine.scala @@ -3,10 +3,11 @@ import io.gatling.core.config.GatlingPropertiesBuilder object Engine extends App { - val props = new GatlingPropertiesBuilder() - .resourcesDirectory(IDEPathHelper.resourcesDirectory.toString) - .resultsDirectory(IDEPathHelper.resultsDirectory.toString) - .binariesDirectory(IDEPathHelper.mavenBinariesDirectory.toString) + val props = new GatlingPropertiesBuilder + props.dataDirectory(IDEPathHelper.dataDirectory.toString) + props.resultsDirectory(IDEPathHelper.resultsDirectory.toString) + props.bodiesDirectory(IDEPathHelper.bodiesDirectory.toString) + props.binariesDirectory(IDEPathHelper.mavenBinariesDirectory.toString) - Gatling.fromMap(props.build) + Gatling.fromMap(props.build) } diff --git a/testing-modules/gatling/src/test/scala/IDEPathHelper.scala b/testing-modules/gatling/src/test/scala/IDEPathHelper.scala index 6aef6707b2..9fb1d7d5c8 100644 --- a/testing-modules/gatling/src/test/scala/IDEPathHelper.scala +++ b/testing-modules/gatling/src/test/scala/IDEPathHelper.scala @@ -4,7 +4,7 @@ import io.gatling.commons.util.PathHelper._ object IDEPathHelper { - val gatlingConfUrl: Path = getClass.getClassLoader.getResource("gatling.conf") + val gatlingConfUrl: Path = getClass.getClassLoader.getResource("gatling.conf").toURI val projectRootDir = gatlingConfUrl.ancestor(3) val mavenSourcesDirectory = projectRootDir / "src" / "test" / "scala" @@ -12,8 +12,11 @@ object IDEPathHelper { val mavenTargetDirectory = projectRootDir / "target" val mavenBinariesDirectory = mavenTargetDirectory / "test-classes" - val resourcesDirectory = mavenResourcesDirectory - val recorderSimulationsDirectory = mavenSourcesDirectory + val dataDirectory = mavenResourcesDirectory / "data" + val bodiesDirectory = mavenResourcesDirectory / "bodies" + + val recorderOutputDirectory = mavenSourcesDirectory val resultsDirectory = mavenTargetDirectory / "gatling" + val recorderConfigFile = mavenResourcesDirectory / "recorder.conf" } diff --git a/testing-modules/gatling/src/test/scala/Recorder.scala b/testing-modules/gatling/src/test/scala/Recorder.scala index 187f566aac..9c38e52f12 100644 --- a/testing-modules/gatling/src/test/scala/Recorder.scala +++ b/testing-modules/gatling/src/test/scala/Recorder.scala @@ -3,10 +3,10 @@ import io.gatling.recorder.config.RecorderPropertiesBuilder object Recorder extends App { - val props = new RecorderPropertiesBuilder() - .simulationsFolder(IDEPathHelper.recorderSimulationsDirectory.toString) - .simulationPackage("org.baeldung") - .resourcesFolder(IDEPathHelper.resourcesDirectory.toString) + val props = new RecorderPropertiesBuilder + props.simulationOutputFolder(IDEPathHelper.recorderOutputDirectory.toString) + props.simulationPackage("org.baeldung") + props.bodiesFolder(IDEPathHelper.bodiesDirectory.toString) GatlingRecorder.fromMap(props.build, Some(IDEPathHelper.recorderConfigFile)) } diff --git a/testing-modules/gatling/src/test/scala/org/baeldung/RecordedSimulation.scala b/testing-modules/gatling/src/test/scala/org/baeldung/RecordedSimulation.scala index 9902f91e0f..dece393478 100644 --- a/testing-modules/gatling/src/test/scala/org/baeldung/RecordedSimulation.scala +++ b/testing-modules/gatling/src/test/scala/org/baeldung/RecordedSimulation.scala @@ -9,7 +9,7 @@ import io.gatling.jdbc.Predef._ class RecordedSimulation extends Simulation { val httpProtocol = http - .baseUrl("http://computer-database.gatling.io") + .baseURL("http://computer-database.gatling.io") .inferHtmlResources(BlackList(""".*\.css""", """.*\.js""", """.*\.ico"""), WhiteList()) .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") .acceptEncodingHeader("gzip, deflate") From eb5cd047e1bec7e407d70aefbf9c225a34db338c Mon Sep 17 00:00:00 2001 From: Kai Yuan Date: Sun, 26 Jan 2020 01:22:39 +0100 Subject: [PATCH 4/4] improvement of the article "How to Return Multiple Values From a Java Method" --- core-java-modules/core-java-lang-2/pom.xml | 5 +++ ...pleReturnValuesUsingApacheCommonsPair.java | 20 +++++++++++ ...eReturnValuesUsingApacheCommonsTriple.java | 23 ++++++++++++ ...nValuesUsingApacheCommonsPairUnitTest.java | 34 ++++++++++++++++++ ...aluesUsingApacheCommonsTripleUnitTest.java | 35 +++++++++++++++++++ 5 files changed, 117 insertions(+) create mode 100644 core-java-modules/core-java-lang-2/src/main/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsPair.java create mode 100644 core-java-modules/core-java-lang-2/src/main/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsTriple.java create mode 100644 core-java-modules/core-java-lang-2/src/test/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsPairUnitTest.java create mode 100644 core-java-modules/core-java-lang-2/src/test/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsTripleUnitTest.java diff --git a/core-java-modules/core-java-lang-2/pom.xml b/core-java-modules/core-java-lang-2/pom.xml index 4702b7350b..a5fb5ca859 100644 --- a/core-java-modules/core-java-lang-2/pom.xml +++ b/core-java-modules/core-java-lang-2/pom.xml @@ -15,6 +15,11 @@ + + org.apache.commons + commons-lang3 + 3.9 + commons-beanutils commons-beanutils diff --git a/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsPair.java b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsPair.java new file mode 100644 index 0000000000..2f505443c2 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsPair.java @@ -0,0 +1,20 @@ +package com.baeldung.methodmultiplereturnvalues; + +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.commons.lang3.tuple.Pair; + +import java.util.Comparator; +import java.util.List; + +class MultipleReturnValuesUsingApacheCommonsPair { + + static ImmutablePair getMostDistantPoint( + List coordinatesList, + Coordinates target) { + return coordinatesList.stream() + .map(coordinates -> ImmutablePair.of(coordinates, coordinates.calculateDistance(target))) + .max(Comparator.comparingDouble(Pair::getRight)) + .get(); + + } +} diff --git a/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsTriple.java b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsTriple.java new file mode 100644 index 0000000000..4da297a51f --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsTriple.java @@ -0,0 +1,23 @@ +package com.baeldung.methodmultiplereturnvalues; + +import org.apache.commons.lang3.tuple.ImmutableTriple; + +import java.util.List; +import java.util.stream.Collectors; + +class MultipleReturnValuesUsingApacheCommonsTriple { + + static ImmutableTriple getMinAvgMaxTriple( + List coordinatesList, + Coordinates target) { + + List distanceList = coordinatesList.stream() + .map(coordinates -> coordinates.calculateDistance(target)) + .collect(Collectors.toList()); + Double minDistance = distanceList.stream().mapToDouble(Double::doubleValue).min().getAsDouble(); + Double avgDistance = distanceList.stream().mapToDouble(Double::doubleValue).average().orElse(0.0D); + Double maxDistance = distanceList.stream().mapToDouble(Double::doubleValue).max().getAsDouble(); + + return ImmutableTriple.of(minDistance, avgDistance, maxDistance); + } +} diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsPairUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsPairUnitTest.java new file mode 100644 index 0000000000..ca5736fe50 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsPairUnitTest.java @@ -0,0 +1,34 @@ +package com.baeldung.methodmultiplereturnvalues; + +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class MultipleReturnValuesUsingApacheCommonsPairUnitTest { + + @Test + void whenUsingPair_thenMultipleFieldsAreReturned() { + + List coordinatesList = new ArrayList<>(); + coordinatesList.add(new Coordinates(1, 1, "home")); + coordinatesList.add(new Coordinates(2, 2, "school")); + coordinatesList.add(new Coordinates(3, 3, "hotel")); + + Coordinates target = new Coordinates(5, 5, "gym"); + + ImmutablePair mostDistantPoint = MultipleReturnValuesUsingApacheCommonsPair.getMostDistantPoint(coordinatesList, target); + + assertEquals(1, mostDistantPoint.getLeft().getLongitude()); + assertEquals(1, mostDistantPoint.getLeft().getLatitude()); + assertEquals("home", mostDistantPoint.getLeft().getPlaceName()); + assertEquals(5.66, BigDecimal.valueOf(mostDistantPoint.getRight()).setScale(2, RoundingMode.HALF_UP).doubleValue()); + + } + +} diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsTripleUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsTripleUnitTest.java new file mode 100644 index 0000000000..d23036f5e6 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsTripleUnitTest.java @@ -0,0 +1,35 @@ +package com.baeldung.methodmultiplereturnvalues; + +import org.apache.commons.lang3.tuple.ImmutableTriple; +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class MultipleReturnValuesUsingApacheCommonsTripleUnitTest { + + @Test + void whenUsingTriple_thenMultipleFieldsAreReturned() { + + List coordinatesList = new ArrayList<>(); + coordinatesList.add(new Coordinates(1, 1, "home")); + coordinatesList.add(new Coordinates(2, 2, "school")); + coordinatesList.add(new Coordinates(3, 3, "hotel")); + + Coordinates target = new Coordinates(5, 5, "gym"); + + ImmutableTriple minAvgMax = MultipleReturnValuesUsingApacheCommonsTriple.getMinAvgMaxTriple(coordinatesList, target); + + assertEquals(2.83, scaleDouble(minAvgMax.left)); //min + assertEquals(4.24, scaleDouble(minAvgMax.middle)); //avg + assertEquals(5.66, scaleDouble(minAvgMax.right)); //max + } + + private double scaleDouble(Double d) { + return BigDecimal.valueOf(d).setScale(2, RoundingMode.HALF_UP).doubleValue(); + } +}