From 9f78f6273205aa58c13c3161ac71ae0b9e6e9a60 Mon Sep 17 00:00:00 2001 From: Fabio Silva Date: Sun, 8 Dec 2019 15:57:34 -0300 Subject: [PATCH] =?UTF-8?q?Ruy=20Vil=C3=A3o=20suggestions:=20no=20point=20?= =?UTF-8?q?in=20declaring=20the=20variables=20for=20the=20match=20result,?= =?UTF-8?q?=20let's=20put=20them=20directly=20in=20the=20assert=20(to=20be?= =?UTF-8?q?=20fixed=20in=20both=20tutorial=20and=20code).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pattern/PatternJava11UnitTest.java | 16 +++++++------- .../com/baeldung/pattern/PatternUnitTest.java | 21 +++++++------------ 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/core-java-modules/core-java-11/src/test/java/com/baeldung/pattern/PatternJava11UnitTest.java b/core-java-modules/core-java-11/src/test/java/com/baeldung/pattern/PatternJava11UnitTest.java index ebb8065a78..029755820f 100644 --- a/core-java-modules/core-java-11/src/test/java/com/baeldung/pattern/PatternJava11UnitTest.java +++ b/core-java-modules/core-java-11/src/test/java/com/baeldung/pattern/PatternJava11UnitTest.java @@ -13,19 +13,17 @@ import static org.junit.Assert.assertTrue; public class PatternJava11UnitTest { - private static final String VALID_NAME = "Fabio Silva"; - private static final String INVALID_NAME = "Fabio Luis Silva"; - private static final List NAMES_TO_VALIDATE = Arrays.asList(VALID_NAME, INVALID_NAME); - private static final Pattern FIRST_LAST_NAME_PRE_COMPILED_PATTERN = Pattern.compile("[a-zA-Z]{3,} [a-zA-Z]{3,}"); - @Test public void givenPreCompiledPattern_whenCallAsMatchPredicate_thenReturnMatchPredicateToMatchesThePatternInTheListElements() { - Predicate patternAsMatchPredicate = FIRST_LAST_NAME_PRE_COMPILED_PATTERN.asMatchPredicate(); - List validatedNames = NAMES_TO_VALIDATE.stream() + List namesToValidate = Arrays.asList("Fabio Silva", "Fabio Luis Silva"); + Pattern firstLastNamePreCompiledPattern = Pattern.compile("[a-zA-Z]{3,} [a-zA-Z]{3,}"); + + Predicate patternAsMatchPredicate = firstLastNamePreCompiledPattern.asMatchPredicate(); + List validatedNames = namesToValidate.stream() .filter(patternAsMatchPredicate) .collect(Collectors.toList()); - assertTrue(validatedNames.contains(VALID_NAME)); - assertFalse(validatedNames.contains(INVALID_NAME)); + assertTrue(validatedNames.contains("Fabio Silva")); + assertFalse(validatedNames.contains("Fabio Luis Silva")); } } diff --git a/core-java-modules/core-java-text/src/test/java/com/baeldung/pattern/PatternUnitTest.java b/core-java-modules/core-java-text/src/test/java/com/baeldung/pattern/PatternUnitTest.java index df5c7a3b1d..20c98480db 100644 --- a/core-java-modules/core-java-text/src/test/java/com/baeldung/pattern/PatternUnitTest.java +++ b/core-java-modules/core-java-text/src/test/java/com/baeldung/pattern/PatternUnitTest.java @@ -15,17 +15,12 @@ import static org.junit.Assert.*; public class PatternUnitTest { private static final Pattern FIRST_LAST_NAME_PRE_COMPILED_PATTERN = Pattern.compile("[a-zA-Z]{3,} [a-zA-Z]{3,}"); - private static final String VALID_NAME = "Fabio Silva"; - private static final String INVALID_NAME = "Mr. Silva"; - private static final List NAMES_TO_VALIDATE = Arrays.asList(VALID_NAME, INVALID_NAME); - private static final Pattern SPLIT_PRE_COMPILED_PATTERN = Pattern.compile("__"); - private static final String TEXT_TO_SPLIT = "My_Name__is__Fabio_Silva"; @Test public void givenPreCompiledPattern_whenCallMatcher_thenReturnAMatcherToMatches() { - Matcher matcherName1 = FIRST_LAST_NAME_PRE_COMPILED_PATTERN.matcher(VALID_NAME); - Matcher matcherName2 = FIRST_LAST_NAME_PRE_COMPILED_PATTERN.matcher(INVALID_NAME); + Matcher matcherName1 = FIRST_LAST_NAME_PRE_COMPILED_PATTERN.matcher("Fabio Silva"); + Matcher matcherName2 = FIRST_LAST_NAME_PRE_COMPILED_PATTERN.matcher("Mr. Silva"); boolean matchesName1 = matcherName1.matches(); boolean matchesName2 = matcherName2.matches(); @@ -36,19 +31,20 @@ public class PatternUnitTest { @Test public void givenPreCompiledPattern_whenCallAsPredicate_thenReturnPredicateToFindThePatternInTheListElements() { + List namesToValidate = Arrays.asList("Fabio Silva", "Mr. Silva"); Predicate patternsAsPredicate = FIRST_LAST_NAME_PRE_COMPILED_PATTERN.asPredicate(); - List validNames = NAMES_TO_VALIDATE.stream() + List validNames = namesToValidate.stream() .filter(patternsAsPredicate) .collect(Collectors.toList()); - assertEquals(1,validNames.size()); - assertTrue(validNames.contains(VALID_NAME)); + assertEquals(1, validNames.size()); + assertTrue(validNames.contains("Fabio Silva")); } @Test public void givenPreCompiledPattern_whenCallSplit_thenReturnArrayWithValuesSplitByThePattern() { - String[] textSplit = SPLIT_PRE_COMPILED_PATTERN.split(TEXT_TO_SPLIT); + String[] textSplit = SPLIT_PRE_COMPILED_PATTERN.split("My_Name__is__Fabio_Silva"); assertEquals("My_Name", textSplit[0]); assertEquals("is", textSplit[1]); @@ -57,12 +53,11 @@ public class PatternUnitTest { @Test public void givenPreCompiledPattern_whenCallSplitAsStream_thenReturnArrayWithValuesSplitByThePattern() { - Stream textSplitAsStream = SPLIT_PRE_COMPILED_PATTERN.splitAsStream(TEXT_TO_SPLIT); + Stream textSplitAsStream = SPLIT_PRE_COMPILED_PATTERN.splitAsStream("My_Name__is__Fabio_Silva"); String[] textSplit = textSplitAsStream.toArray(String[]::new); assertEquals("My_Name", textSplit[0]); assertEquals("is", textSplit[1]); assertEquals("Fabio_Silva", textSplit[2]); } - }