From d0c7340da8975b3c220e2bd227f64dfe284a45f9 Mon Sep 17 00:00:00 2001 From: Blue Montag Software Date: Mon, 26 Jun 2017 14:12:02 -0300 Subject: [PATCH 1/5] for-loops changed to while-loops when appropiate (#2163) --- .../orderedmap/OrderedMapUnitTest.java | 36 ++++++++++++------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/libraries/src/test/java/com/baeldung/commons/collections/orderedmap/OrderedMapUnitTest.java b/libraries/src/test/java/com/baeldung/commons/collections/orderedmap/OrderedMapUnitTest.java index 5392daea3b..6b2777c289 100644 --- a/libraries/src/test/java/com/baeldung/commons/collections/orderedmap/OrderedMapUnitTest.java +++ b/libraries/src/test/java/com/baeldung/commons/collections/orderedmap/OrderedMapUnitTest.java @@ -1,5 +1,10 @@ package com.baeldung.commons.collections.orderedmap; +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; + import org.apache.commons.collections4.OrderedMap; import org.apache.commons.collections4.OrderedMapIterator; import org.apache.commons.collections4.map.LinkedMap; @@ -7,11 +12,6 @@ import org.apache.commons.collections4.map.ListOrderedMap; import org.junit.Before; import org.junit.Test; -import java.util.ArrayList; -import java.util.List; - -import static org.junit.Assert.assertEquals; - public class OrderedMapUnitTest { private String[] names = {"Emily", "Mathew", "Rose", "John", "Anna"}; @@ -45,11 +45,12 @@ public class OrderedMapUnitTest { // as defined in the constant arrays of names and ages: OrderedMapIterator runnersIterator = this.runnersLinkedMap.mapIterator(); - for (int i = 0; runnersIterator.hasNext(); i++) { + int i = 0; + while (runnersIterator.hasNext()) { runnersIterator.next(); - assertEquals(runnersIterator.getKey(), this.names[i]); assertEquals(runnersIterator.getValue(), this.ages[i]); + i++; } } @@ -59,11 +60,12 @@ public class OrderedMapUnitTest { // as defined in the constant arrays of names and ages: OrderedMapIterator runnersIterator = this.runnersListOrderedMap.mapIterator(); - for (int i = 0; runnersIterator.hasNext(); i++) { + int i = 0; + while (runnersIterator.hasNext()) { runnersIterator.next(); - assertEquals(runnersIterator.getKey(), this.names[i]); assertEquals(runnersIterator.getValue(), this.ages[i]); + i++; } } @@ -73,9 +75,11 @@ public class OrderedMapUnitTest { // as defined in the constant arrays of names and ages String name = this.runnersLinkedMap.firstKey(); - for (int i = 0; name != null; i++) { + int i = 0; + while (name != null) { assertEquals(name, this.names[i]); name = this.runnersLinkedMap.nextKey(name); + i++; } } @@ -85,9 +89,11 @@ public class OrderedMapUnitTest { // as defined in the constant arrays of names and ages String name = this.runnersListOrderedMap.firstKey(); - for (int i = 0; name != null; i++) { + int i = 0; + while (name != null) { assertEquals(name, this.names[i]); name = this.runnersListOrderedMap.nextKey(name); + i++; } } @@ -97,9 +103,11 @@ public class OrderedMapUnitTest { // as defined in the constant arrays of names and ages String name = this.runnersLinkedMap.lastKey(); - for (int i = RUNNERS_COUNT - 1; name != null; i--) { + int i = RUNNERS_COUNT - 1; + while (name != null) { assertEquals(name, this.names[i]); name = this.runnersLinkedMap.previousKey(name); + i--; } } @@ -109,9 +117,11 @@ public class OrderedMapUnitTest { // as defined in the constant arrays of names and ages String name = this.runnersListOrderedMap.lastKey(); - for (int i = RUNNERS_COUNT - 1; name != null; i--) { + int i = RUNNERS_COUNT - 1; + while (name != null) { assertEquals(name, this.names[i]); name = this.runnersListOrderedMap.previousKey(name); + i--; } } From 75e0473822ae670301991e0a1cf9f689559c73ea Mon Sep 17 00:00:00 2001 From: Ante Pocedulic Date: Mon, 26 Jun 2017 22:07:18 +0200 Subject: [PATCH 2/5] - refactored asciidoctor code (#2164) - refreshed README.md --- asciidoctor/README.md | 3 + asciidoctor/log4j.properties | 0 asciidoctor/pom.xml | 56 +++++++++++++++++++ .../src/docs/asciidoc/test.adoc | 0 .../baeldung/asciidoctor/AsciidoctorDemo.java | 0 .../asciidoctor/AsciidoctorDemoTest.java | 0 libraries/pom.xml | 38 +------------ 7 files changed, 60 insertions(+), 37 deletions(-) create mode 100644 asciidoctor/README.md create mode 100644 asciidoctor/log4j.properties create mode 100644 asciidoctor/pom.xml rename {libraries => asciidoctor}/src/docs/asciidoc/test.adoc (100%) rename {libraries => asciidoctor}/src/main/java/com/baeldung/asciidoctor/AsciidoctorDemo.java (100%) rename {libraries => asciidoctor}/src/test/java/com/baeldung/asciidoctor/AsciidoctorDemoTest.java (100%) diff --git a/asciidoctor/README.md b/asciidoctor/README.md new file mode 100644 index 0000000000..164200d227 --- /dev/null +++ b/asciidoctor/README.md @@ -0,0 +1,3 @@ +### Relevant articles + +- [Introduction to Asciidoctor](http://www.baeldung.com/introduction-to-asciidoctor) \ No newline at end of file diff --git a/asciidoctor/log4j.properties b/asciidoctor/log4j.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/asciidoctor/pom.xml b/asciidoctor/pom.xml new file mode 100644 index 0000000000..989f6e5354 --- /dev/null +++ b/asciidoctor/pom.xml @@ -0,0 +1,56 @@ + + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + 4.0.0 + + asciidoctor + asciidoctor + + + + org.asciidoctor + asciidoctor-maven-plugin + 1.5.5 + + + org.asciidoctor + asciidoctorj-pdf + 1.5.0-alpha.15 + + + + + output-pdf + generate-resources + + process-asciidoc + + + + + src/docs/asciidoc + target/docs/asciidoc + pdf + + + + + + + + org.asciidoctor + asciidoctorj + 1.5.4 + + + org.asciidoctor + asciidoctorj-pdf + 1.5.0-alpha.11 + + + diff --git a/libraries/src/docs/asciidoc/test.adoc b/asciidoctor/src/docs/asciidoc/test.adoc similarity index 100% rename from libraries/src/docs/asciidoc/test.adoc rename to asciidoctor/src/docs/asciidoc/test.adoc diff --git a/libraries/src/main/java/com/baeldung/asciidoctor/AsciidoctorDemo.java b/asciidoctor/src/main/java/com/baeldung/asciidoctor/AsciidoctorDemo.java similarity index 100% rename from libraries/src/main/java/com/baeldung/asciidoctor/AsciidoctorDemo.java rename to asciidoctor/src/main/java/com/baeldung/asciidoctor/AsciidoctorDemo.java diff --git a/libraries/src/test/java/com/baeldung/asciidoctor/AsciidoctorDemoTest.java b/asciidoctor/src/test/java/com/baeldung/asciidoctor/AsciidoctorDemoTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/asciidoctor/AsciidoctorDemoTest.java rename to asciidoctor/src/test/java/com/baeldung/asciidoctor/AsciidoctorDemoTest.java diff --git a/libraries/pom.xml b/libraries/pom.xml index b7a33abbfe..b010ce5636 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -70,32 +70,6 @@ - - org.asciidoctor - asciidoctor-maven-plugin - 1.5.5 - - - org.asciidoctor - asciidoctorj-pdf - 1.5.0-alpha.15 - - - - - output-pdf - generate-resources - - process-asciidoc - - - - - src/docs/asciidoc - target/docs/asciidoc - pdf - - @@ -359,17 +333,7 @@ io.netty netty-all ${netty.version} - - - org.asciidoctor - asciidoctorj - 1.5.4 - - - org.asciidoctor - asciidoctorj-pdf - 1.5.0-alpha.11 - + org.apache.opennlp From 3ae96995ad8ad20f1b2a8ff65da5fa339050190f Mon Sep 17 00:00:00 2001 From: Seun Matt Date: Mon, 26 Jun 2017 23:18:54 +0100 Subject: [PATCH 3/5] example code for BAEL-966 (#2152) * added updated example codes * updated example code StringToCharStream * deleted StringToCharStream.java locally * removed redundant file * added code for apache commons collection SetUtils * refactored example code --- .../string/StringToCharStreamUnitTest.java | 2 - libraries/pom.xml | 15 +++- .../commons/collections/SetUtilsUnitTest.java | 83 +++++++++++++++++++ 3 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 libraries/src/test/java/com/baeldung/commons/collections/SetUtilsUnitTest.java diff --git a/core-java/src/test/java/com/baeldung/string/StringToCharStreamUnitTest.java b/core-java/src/test/java/com/baeldung/string/StringToCharStreamUnitTest.java index 2ad670fe5d..1b02e5d291 100644 --- a/core-java/src/test/java/com/baeldung/string/StringToCharStreamUnitTest.java +++ b/core-java/src/test/java/com/baeldung/string/StringToCharStreamUnitTest.java @@ -12,7 +12,6 @@ import static org.hamcrest.CoreMatchers.instanceOf; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; public class StringToCharStreamUnitTest { @@ -44,7 +43,6 @@ public class StringToCharStreamUnitTest { .mapToObj(c -> (char) c); Stream characterStream1 = testString.codePoints() .mapToObj(c -> (char) c); - assertNotNull("IntStream returned by chars() did not map to Stream", characterStream); assertNotNull("IntStream returned by codePoints() did not map to Stream", characterStream1); } diff --git a/libraries/pom.xml b/libraries/pom.xml index b010ce5636..6872e4ad7c 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -90,6 +90,11 @@ commons-lang3 ${commons-lang.version} + + org.apache.commons + commons-collections4 + ${commons.collections.version} + org.jasypt jasypt @@ -340,13 +345,17 @@ opennlp-tools 1.8.0 + + junit + junit + ${junit.version} + test + info.debatty java-lsh ${java-lsh.version} - - 0.7.0 @@ -373,6 +382,8 @@ 1.4.0 1.1.0 4.1.10.Final + 4.1 + 4.12 0.10 diff --git a/libraries/src/test/java/com/baeldung/commons/collections/SetUtilsUnitTest.java b/libraries/src/test/java/com/baeldung/commons/collections/SetUtilsUnitTest.java new file mode 100644 index 0000000000..4d264e3aea --- /dev/null +++ b/libraries/src/test/java/com/baeldung/commons/collections/SetUtilsUnitTest.java @@ -0,0 +1,83 @@ +package com.baeldung.commons.collections; + +import org.apache.commons.collections4.SetUtils; +import org.apache.commons.collections4.set.TransformedSet; +import org.junit.Test; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Created by smatt on 21/06/2017. + */ +public class SetUtilsUnitTest { + + @Test(expected = IllegalArgumentException.class) + public void givenSetAndPredicate_whenPredicatedSet_thenValidateSet_and_throw_IllegalArgumentException() { + Set sourceSet = new HashSet<>(); + sourceSet.addAll(Arrays.asList("London", "Lagos", "Err Source1")); + Set validatingSet + = SetUtils.predicatedSet(sourceSet, (s) -> s.startsWith("L")); + validatingSet.add("Err Source2"); + } + + @Test + public void givenTwoSets_whenDifference_thenSetView() { + Set a = new HashSet<>(Arrays.asList(1,2,5)); + Set b = new HashSet<>(Arrays.asList(1,2)); + SetUtils.SetView result = SetUtils.difference(a, b); + assertTrue(result.size() == 1 && result.contains(5)); + } + + @Test + public void givenTwoSets_whenUnion_thenUnionResult() { + Set a = new HashSet<>(Arrays.asList(1,2,5)); + Set b = new HashSet<>(Arrays.asList(1,2)); + Set expected = new HashSet<>(Arrays.asList(1,2,5)); + SetUtils.SetView union = SetUtils.union(a, b); + assertTrue(SetUtils.isEqualSet(expected, union)); + } + + @Test + public void givenTwoSets_whenIntersection_thenIntersectionResult() { + Set a = new HashSet<>(Arrays.asList(1,2,5)); + Set b = new HashSet<>(Arrays.asList(1,2)); + Set expected = new HashSet<>(Arrays.asList(1,2)); + SetUtils.SetView intersect = SetUtils.intersection(a, b); + assertTrue(SetUtils.isEqualSet(expected, intersect)); + } + + @Test + public void givenSet_whenTransformedSet_thenTransformedResult() { + Set a = SetUtils.transformedSet(new HashSet<>(), (e) -> e * 2 ); + a.add(2); + assertEquals(a.toArray()[0], 4); + + Set source = new HashSet<>(Arrays.asList(1)); + Set newSet = TransformedSet.transformedSet(source, (e) -> e * 2); + assertEquals(newSet.toArray()[0], 2); + assertEquals(source.toArray()[0], 2); + } + + @Test + public void givenTwoSet_whenDisjunction_thenDisjunctionSet() { + Set a = new HashSet<>(Arrays.asList(1,2,5)); + Set b = new HashSet<>(Arrays.asList(1,2,3)); + SetUtils.SetView result = SetUtils.disjunction(a, b); + assertTrue(result.toSet().contains(5) && result.toSet().contains(3)); + } + + @Test + public void givenSet_when_OrderedSet_thenMaintainElementOrder() { + Set set = new HashSet<>(Arrays.asList(10,1,5)); + System.out.println("unordered set: " + set); + + Set orderedSet = SetUtils.orderedSet(new HashSet<>()); + orderedSet.addAll(Arrays.asList(10,1,5)); + System.out.println("ordered set = " + orderedSet); + } +} \ No newline at end of file From 213a6e2bf2410285e6dd7cf2bf06362a900891de Mon Sep 17 00:00:00 2001 From: Tian Baoqiang Date: Tue, 27 Jun 2017 13:35:41 +0800 Subject: [PATCH 4/5] #BAEL-397: use toMap instead of creating an empty hashmap (#2167) * BAEL-397: use toMap instead of creating an empty hashmap * BAEL-397: static import toMap for cleaner code --- .../com/baeldung/metrics/servo/MetricTypeTest.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/metrics/src/test/java/com/baeldung/metrics/servo/MetricTypeTest.java b/metrics/src/test/java/com/baeldung/metrics/servo/MetricTypeTest.java index 9f7b6bb6f3..68ba23244d 100644 --- a/metrics/src/test/java/com/baeldung/metrics/servo/MetricTypeTest.java +++ b/metrics/src/test/java/com/baeldung/metrics/servo/MetricTypeTest.java @@ -5,10 +5,10 @@ import com.netflix.servo.stats.StatsConfig; import org.junit.Ignore; import org.junit.Test; -import java.util.HashMap; import java.util.Map; import static java.util.concurrent.TimeUnit.SECONDS; +import static java.util.stream.Collectors.toMap; import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; @@ -123,15 +123,14 @@ public class MetricTypeTest { .getTotalTime() .intValue()); - final Map metricMap = new HashMap<>(3); - timer + final Map metricMap = timer .getMonitors() .stream() .filter(monitor -> monitor .getConfig() .getTags() .containsKey("servo.bucket")) - .forEach(monitor -> metricMap.put(getMonitorTagValue(monitor, "servo.bucket"), (Long) monitor.getValue())); + .collect(toMap(monior -> getMonitorTagValue(monior, "servo.bucket"), monitor -> (Long) monitor.getValue())); assertThat(metricMap, allOf(hasEntry("bucket=2s", 0L), hasEntry("bucket=5s", 1L), hasEntry("bucket=overflow", 1L))); } @@ -177,10 +176,10 @@ public class MetricTypeTest { .getValue() .intValue()); - final Map metricMap = new HashMap<>(10); - timer + final Map metricMap = timer .getMonitors() - .forEach(monitor -> metricMap.put(getMonitorTagValue(monitor, "statistic"), (Number) monitor.getValue())); + .stream() + .collect(toMap(monitor -> getMonitorTagValue(monitor, "statistic"), monitor -> (Number) monitor.getValue())); assertThat(metricMap.keySet(), containsInAnyOrder("count", "totalTime", "max", "min", "variance", "stdDev", "avg", "percentile_99", "percentile_95", "percentile_90")); } From 7ee3e57727b9a45b26208adf77ffe5ef5c1ed595 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Tue, 27 Jun 2017 08:20:33 +0200 Subject: [PATCH 5/5] Ascii doctor fix (#2168) * Asciidoctor fix * Reformat --- libraries/pom.xml | 36 ++++++++----------- ...derClassDirtiesContextIntegrationTest.java | 14 ++++---- .../baeldung/serenity/spring/AdderTest.java | 8 ++--- 3 files changed, 24 insertions(+), 34 deletions(-) diff --git a/libraries/pom.xml b/libraries/pom.xml index 6872e4ad7c..0ab240f6d7 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -1,6 +1,6 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> parent-modules com.baeldung @@ -59,7 +59,7 @@ ${basedir}/datanucleus.properties ${basedir}/log4j.properties true - false + false @@ -81,10 +81,10 @@ ${cglib.version} - commons-beanutils - commons-beanutils - ${commons-beanutils.version} - + commons-beanutils + commons-beanutils + ${commons-beanutils.version} + org.apache.commons commons-lang3 @@ -190,12 +190,6 @@ serenity-core ${serenity.version} test - - - org.asciidoctor - asciidoctor-java-integration - - net.serenity-bdd @@ -251,7 +245,7 @@ org.datanucleus datanucleus-maven-plugin 5.0.2 - + org.datanucleus datanucleus-xml @@ -303,11 +297,11 @@ 1.4.195 - pl.pragmatists - JUnitParams - ${jUnitParams.version} - test - + pl.pragmatists + JUnitParams + ${jUnitParams.version} + test + org.quartz-scheduler quartz @@ -338,8 +332,8 @@ io.netty netty-all ${netty.version} - - + + org.apache.opennlp opennlp-tools @@ -361,7 +355,7 @@ 0.7.0 3.2.4 3.5 - 1.9.3 + 1.9.3 1.9.2 1.2 3.21.0-GA diff --git a/libraries/src/test/java/com/baeldung/serenity/spring/AdderClassDirtiesContextIntegrationTest.java b/libraries/src/test/java/com/baeldung/serenity/spring/AdderClassDirtiesContextIntegrationTest.java index bb25186cee..82dbad0f11 100644 --- a/libraries/src/test/java/com/baeldung/serenity/spring/AdderClassDirtiesContextIntegrationTest.java +++ b/libraries/src/test/java/com/baeldung/serenity/spring/AdderClassDirtiesContextIntegrationTest.java @@ -14,23 +14,21 @@ import org.springframework.test.context.ContextConfiguration; import static com.baeldung.serenity.spring.RandomNumberUtil.randomInt; import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_CLASS; -/** - * @author aiet - */ - @RunWith(Suite.class) @Suite.SuiteClasses({ - AdderClassDirtiesContextIntegrationTest.DirtiesContextTest.class, AdderClassDirtiesContextIntegrationTest.AnotherDirtiesContextTest.class - }) + AdderClassDirtiesContextIntegrationTest.DirtiesContextTest.class, AdderClassDirtiesContextIntegrationTest.AnotherDirtiesContextTest.class +}) public class AdderClassDirtiesContextIntegrationTest { @RunWith(SerenityRunner.class) @ContextConfiguration(classes = AdderService.class) public static abstract class Base { - @Steps AdderServiceSteps adderServiceSteps; + @Steps + AdderServiceSteps adderServiceSteps; - @ClassRule public static SpringIntegrationClassRule springIntegrationClassRule = new SpringIntegrationClassRule(); + @ClassRule + public static SpringIntegrationClassRule springIntegrationClassRule = new SpringIntegrationClassRule(); void whenAccumulate_thenSummedUp() { adderServiceSteps.whenAccumulate(); diff --git a/libraries/src/test/java/com/baeldung/serenity/spring/AdderTest.java b/libraries/src/test/java/com/baeldung/serenity/spring/AdderTest.java index a57b924211..771f389cb1 100644 --- a/libraries/src/test/java/com/baeldung/serenity/spring/AdderTest.java +++ b/libraries/src/test/java/com/baeldung/serenity/spring/AdderTest.java @@ -6,13 +6,11 @@ import org.jbehave.core.annotations.BeforeStory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; -/** - * @author aiet - */ -@ContextConfiguration(classes = { AdderController.class, AdderService.class }) +@ContextConfiguration(classes = {AdderController.class, AdderService.class}) public class AdderTest extends SerenityStory { - @Autowired private AdderService adderService; + @Autowired + private AdderService adderService; @BeforeStory public void init() {