From c03e141781b75b7eef3ccb437efdd15ebe1312ea Mon Sep 17 00:00:00 2001 From: bhandy Date: Thu, 3 Jun 2021 22:30:13 -0400 Subject: [PATCH 1/4] Unit tests for non-capturing groups. --- .../regex/NonCapturingGroupUnitTest.java | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/NonCapturingGroupUnitTest.java diff --git a/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/NonCapturingGroupUnitTest.java b/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/NonCapturingGroupUnitTest.java new file mode 100644 index 0000000000..40bdd429dd --- /dev/null +++ b/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/NonCapturingGroupUnitTest.java @@ -0,0 +1,105 @@ +package com.baeldung.regex; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class NonCapturingGroupUnitTest { + + private static final Pattern SIMPLE_URL_PATTERN = Pattern.compile("[^:]+://(?:[.a-z]+/?)+"); + private static final Pattern SCOPED_CASE_INSENSITIVE_URL_PATTERN = Pattern.compile("[^:]+://(?i:[.a-z]+/?)+"); + private static final Pattern SIMPLE_URL_PATTERN_WITH_SPECIFIC_ENDING_PATH = Pattern.compile("[^:]+://(?:[.a-z]+/?)+?/ending-path"); + private static final Pattern SCOPED_CASE_INSENSTIIVE_URL_PATTERN_WITH_ENDING_PATH = Pattern.compile("[^:]://(?i:[.a-z]+/?)+?/ending-path"); + private static final Pattern CASE_INSENSITIVE_URL_PATTERN = Pattern.compile("[^:]+://(?:[.a-z]+/?)+?(/ending-path)", Pattern.CASE_INSENSITIVE); + private static final Pattern SCOPED_CASE_SENSITIVE_URL_PATTERN = Pattern.compile("[^:]+://(?-i:[.a-z]+/?)+?(/ending-path)", Pattern.CASE_INSENSITIVE); + private static final Pattern INDEPENDENT_URL_PATTERN_WITH_ENDING_PATH = Pattern.compile("[^:]+://(?>[.a-z]+/?)+/ending-path"); + private static final Pattern INDEPENDENT_URL_PATTERN_WITH_ENDING_PATH_AND_BACKTRACKING = Pattern.compile("[^:]+://(?>(?:[.a-z]+/?)+/)ending-path"); + + @Test + void givenSimpleUrlPattern_whenValidUrlProvided_thenMatches() { + Matcher urlMatcher = SIMPLE_URL_PATTERN.matcher("http://www.microsoft.com/"); + Assertions.assertThat(urlMatcher.matches()).isTrue(); + Assertions.assertThatThrownBy(() -> urlMatcher.group(1)).isInstanceOf(IndexOutOfBoundsException.class); + } + + @Test + void whenSimpleUrlProvidedWithPathProvided_thenMatches() { + Matcher urlMatcher = SIMPLE_URL_PATTERN.matcher("http://www.microsoft.com/live"); + Assertions.assertThat(urlMatcher.matches()).isTrue(); + } + + @Test + void whenSimpleUrlProvidedWithPathEndingWithSlashProvided_thenMatches() { + Matcher urlMatcher = SIMPLE_URL_PATTERN.matcher("http://www.microsoft.com/live/"); + Assertions.assertThat(urlMatcher.matches()).isTrue(); + } + + @Test + void givenSimpleUrlPattern_whenUrlWithMultiplePathSegmentsProvided_thenMatches() { + Matcher urlMatcher = SIMPLE_URL_PATTERN.matcher("http://www.microsoft.com/some/other/url/path"); + Assertions.assertThat(urlMatcher.matches()).isTrue(); + } + + @Test + void whenUrlWithUppercaseCharactersProvided_thenDoesNotMatch() { + Matcher urlMatcher = SIMPLE_URL_PATTERN.matcher("http://www.Microsoft.com/"); + Assertions.assertThat(urlMatcher.matches()).isFalse(); + } + + @Test + void givenPatternWithCaseInsensitiveGroup_whenUrlHasUppercaseCharactersInsideOfScope_thenMatches() { + Matcher urlMatcher = SCOPED_CASE_INSENSITIVE_URL_PATTERN.matcher("http://www.Microsoft.com/"); + Assertions.assertThat(urlMatcher.matches()).isTrue(); + } + + @Test + void givenCaseInsensitivePattern_whenUrlHasUppercaseCharacters_thenMatches() { + Matcher urlMatcher = CASE_INSENSITIVE_URL_PATTERN.matcher("http://www.Microsoft.com/Ending-path"); + Assertions.assertThat(urlMatcher.matches()).isTrue(); + Assertions.assertThat(urlMatcher.group(1)).isEqualTo("/Ending-path"); + } + + @Test + void givenPatternWithCaseInsensitiveGroup_whenUrlHasUppercaseCharactersOutsideOfScope_thenMatchFails() { + Matcher urlMatcher = SCOPED_CASE_INSENSTIIVE_URL_PATTERN_WITH_ENDING_PATH.matcher("http://www.Microsoft.com/Ending-path"); + Assertions.assertThat(urlMatcher.matches()).isFalse(); + } + + @Test + void givenPatternAllowingBacktracking_whenUrlWithEndingPathCausingBacktrackingProvided_thenMatches() { + Matcher urlMatcher = SIMPLE_URL_PATTERN_WITH_SPECIFIC_ENDING_PATH.matcher("http://www.microsoft.com/ending-path"); + Assertions.assertThat(urlMatcher.matches()).isTrue(); + } + + @Test + void givenPatternWithIndependentNonCapturingGroup_whenBacktrackingOccurs_thenDoesNotMatch() { + Matcher independentMatcher = INDEPENDENT_URL_PATTERN_WITH_ENDING_PATH.matcher("http://www.microsoft.com/ending-path"); + Assertions.assertThat(independentMatcher.matches()).isFalse(); + } + + @Test + void givenPatternWithIndependentNonCapturingGroup_whenBacktrackingDoesNotOccur_thenMatches() { + Matcher independentMatcher = INDEPENDENT_URL_PATTERN_WITH_ENDING_PATH.matcher("http://www.microsoft.com//ending-path"); + Assertions.assertThat(independentMatcher.matches()).isTrue(); + } + + @Test + void givenPatternWithIndependentNonCapturingGroup_whenBacktrackingOccursInsideGroup_thenMatches() { + Matcher independentMatcher = INDEPENDENT_URL_PATTERN_WITH_ENDING_PATH_AND_BACKTRACKING.matcher("http://www.microsoft.com/ending-path"); + Assertions.assertThat(independentMatcher.matches()).isTrue(); + } + + @Test + void givenCaseInsensitivePatternWithCaseSensitivieSubPattern_whenUrlWithUppercaseCharactersOutsideOfScopeProvided_thenMatches() { + Matcher urlMatcher = SCOPED_CASE_SENSITIVE_URL_PATTERN.matcher("http://www.microsoft.com/ENDING-PATH"); + Assertions.assertThat(urlMatcher.matches()).isTrue(); + } + + @Test + void givenCaseInsensitivePatternWithCaseSensitivieSubPattern_whenUrlWithUppercaseCharactersInsideOfScopeProvided_thenDoesNotMatch() { + Matcher urlMatcher = SCOPED_CASE_SENSITIVE_URL_PATTERN.matcher("http://www.Microsoft.com/ending-path"); + Assertions.assertThat(urlMatcher.matches()).isFalse(); + } +} From 57cc5fd880738eca61e359dc8e94aeddd2b75e69 Mon Sep 17 00:00:00 2001 From: bhandy Date: Sat, 5 Jun 2021 15:02:04 -0400 Subject: [PATCH 2/4] Updated the formatting. Helps to save the file after formatting it in Eclipse. --- .../regex/NonCapturingGroupUnitTest.java | 105 -------------- .../NonCapturingGroupUnitTest.java | 135 ++++++++++++++++++ 2 files changed, 135 insertions(+), 105 deletions(-) delete mode 100644 core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/NonCapturingGroupUnitTest.java create mode 100644 core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/noncapturinggroups/NonCapturingGroupUnitTest.java diff --git a/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/NonCapturingGroupUnitTest.java b/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/NonCapturingGroupUnitTest.java deleted file mode 100644 index 40bdd429dd..0000000000 --- a/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/NonCapturingGroupUnitTest.java +++ /dev/null @@ -1,105 +0,0 @@ -package com.baeldung.regex; - -import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class NonCapturingGroupUnitTest { - - private static final Pattern SIMPLE_URL_PATTERN = Pattern.compile("[^:]+://(?:[.a-z]+/?)+"); - private static final Pattern SCOPED_CASE_INSENSITIVE_URL_PATTERN = Pattern.compile("[^:]+://(?i:[.a-z]+/?)+"); - private static final Pattern SIMPLE_URL_PATTERN_WITH_SPECIFIC_ENDING_PATH = Pattern.compile("[^:]+://(?:[.a-z]+/?)+?/ending-path"); - private static final Pattern SCOPED_CASE_INSENSTIIVE_URL_PATTERN_WITH_ENDING_PATH = Pattern.compile("[^:]://(?i:[.a-z]+/?)+?/ending-path"); - private static final Pattern CASE_INSENSITIVE_URL_PATTERN = Pattern.compile("[^:]+://(?:[.a-z]+/?)+?(/ending-path)", Pattern.CASE_INSENSITIVE); - private static final Pattern SCOPED_CASE_SENSITIVE_URL_PATTERN = Pattern.compile("[^:]+://(?-i:[.a-z]+/?)+?(/ending-path)", Pattern.CASE_INSENSITIVE); - private static final Pattern INDEPENDENT_URL_PATTERN_WITH_ENDING_PATH = Pattern.compile("[^:]+://(?>[.a-z]+/?)+/ending-path"); - private static final Pattern INDEPENDENT_URL_PATTERN_WITH_ENDING_PATH_AND_BACKTRACKING = Pattern.compile("[^:]+://(?>(?:[.a-z]+/?)+/)ending-path"); - - @Test - void givenSimpleUrlPattern_whenValidUrlProvided_thenMatches() { - Matcher urlMatcher = SIMPLE_URL_PATTERN.matcher("http://www.microsoft.com/"); - Assertions.assertThat(urlMatcher.matches()).isTrue(); - Assertions.assertThatThrownBy(() -> urlMatcher.group(1)).isInstanceOf(IndexOutOfBoundsException.class); - } - - @Test - void whenSimpleUrlProvidedWithPathProvided_thenMatches() { - Matcher urlMatcher = SIMPLE_URL_PATTERN.matcher("http://www.microsoft.com/live"); - Assertions.assertThat(urlMatcher.matches()).isTrue(); - } - - @Test - void whenSimpleUrlProvidedWithPathEndingWithSlashProvided_thenMatches() { - Matcher urlMatcher = SIMPLE_URL_PATTERN.matcher("http://www.microsoft.com/live/"); - Assertions.assertThat(urlMatcher.matches()).isTrue(); - } - - @Test - void givenSimpleUrlPattern_whenUrlWithMultiplePathSegmentsProvided_thenMatches() { - Matcher urlMatcher = SIMPLE_URL_PATTERN.matcher("http://www.microsoft.com/some/other/url/path"); - Assertions.assertThat(urlMatcher.matches()).isTrue(); - } - - @Test - void whenUrlWithUppercaseCharactersProvided_thenDoesNotMatch() { - Matcher urlMatcher = SIMPLE_URL_PATTERN.matcher("http://www.Microsoft.com/"); - Assertions.assertThat(urlMatcher.matches()).isFalse(); - } - - @Test - void givenPatternWithCaseInsensitiveGroup_whenUrlHasUppercaseCharactersInsideOfScope_thenMatches() { - Matcher urlMatcher = SCOPED_CASE_INSENSITIVE_URL_PATTERN.matcher("http://www.Microsoft.com/"); - Assertions.assertThat(urlMatcher.matches()).isTrue(); - } - - @Test - void givenCaseInsensitivePattern_whenUrlHasUppercaseCharacters_thenMatches() { - Matcher urlMatcher = CASE_INSENSITIVE_URL_PATTERN.matcher("http://www.Microsoft.com/Ending-path"); - Assertions.assertThat(urlMatcher.matches()).isTrue(); - Assertions.assertThat(urlMatcher.group(1)).isEqualTo("/Ending-path"); - } - - @Test - void givenPatternWithCaseInsensitiveGroup_whenUrlHasUppercaseCharactersOutsideOfScope_thenMatchFails() { - Matcher urlMatcher = SCOPED_CASE_INSENSTIIVE_URL_PATTERN_WITH_ENDING_PATH.matcher("http://www.Microsoft.com/Ending-path"); - Assertions.assertThat(urlMatcher.matches()).isFalse(); - } - - @Test - void givenPatternAllowingBacktracking_whenUrlWithEndingPathCausingBacktrackingProvided_thenMatches() { - Matcher urlMatcher = SIMPLE_URL_PATTERN_WITH_SPECIFIC_ENDING_PATH.matcher("http://www.microsoft.com/ending-path"); - Assertions.assertThat(urlMatcher.matches()).isTrue(); - } - - @Test - void givenPatternWithIndependentNonCapturingGroup_whenBacktrackingOccurs_thenDoesNotMatch() { - Matcher independentMatcher = INDEPENDENT_URL_PATTERN_WITH_ENDING_PATH.matcher("http://www.microsoft.com/ending-path"); - Assertions.assertThat(independentMatcher.matches()).isFalse(); - } - - @Test - void givenPatternWithIndependentNonCapturingGroup_whenBacktrackingDoesNotOccur_thenMatches() { - Matcher independentMatcher = INDEPENDENT_URL_PATTERN_WITH_ENDING_PATH.matcher("http://www.microsoft.com//ending-path"); - Assertions.assertThat(independentMatcher.matches()).isTrue(); - } - - @Test - void givenPatternWithIndependentNonCapturingGroup_whenBacktrackingOccursInsideGroup_thenMatches() { - Matcher independentMatcher = INDEPENDENT_URL_PATTERN_WITH_ENDING_PATH_AND_BACKTRACKING.matcher("http://www.microsoft.com/ending-path"); - Assertions.assertThat(independentMatcher.matches()).isTrue(); - } - - @Test - void givenCaseInsensitivePatternWithCaseSensitivieSubPattern_whenUrlWithUppercaseCharactersOutsideOfScopeProvided_thenMatches() { - Matcher urlMatcher = SCOPED_CASE_SENSITIVE_URL_PATTERN.matcher("http://www.microsoft.com/ENDING-PATH"); - Assertions.assertThat(urlMatcher.matches()).isTrue(); - } - - @Test - void givenCaseInsensitivePatternWithCaseSensitivieSubPattern_whenUrlWithUppercaseCharactersInsideOfScopeProvided_thenDoesNotMatch() { - Matcher urlMatcher = SCOPED_CASE_SENSITIVE_URL_PATTERN.matcher("http://www.Microsoft.com/ending-path"); - Assertions.assertThat(urlMatcher.matches()).isFalse(); - } -} diff --git a/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/noncapturinggroups/NonCapturingGroupUnitTest.java b/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/noncapturinggroups/NonCapturingGroupUnitTest.java new file mode 100644 index 0000000000..7b4e475b0a --- /dev/null +++ b/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/noncapturinggroups/NonCapturingGroupUnitTest.java @@ -0,0 +1,135 @@ +package com.baeldung.regex.noncapturinggroups; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class NonCapturingGroupUnitTest { + + private static final Pattern SIMPLE_URL_PATTERN = Pattern.compile("[^:]+://(?:[.a-z]+/?)+"); + private static final Pattern SCOPED_CASE_INSENSITIVE_URL_PATTERN = Pattern.compile("[^:]+://(?i:[.a-z]+/?)+"); + private static final Pattern SIMPLE_URL_PATTERN_WITH_SPECIFIC_ENDING_PATH = Pattern.compile("[^:]+://(?:[.a-z]+/?)+?/ending-path"); + private static final Pattern SCOPED_CASE_INSENSTIIVE_URL_PATTERN_WITH_ENDING_PATH = Pattern.compile("[^:]://(?i:[.a-z]+/?)+?/ending-path"); + private static final Pattern CASE_INSENSITIVE_URL_PATTERN = Pattern.compile("[^:]+://(?:[.a-z]+/?)+?(/ending-path)", Pattern.CASE_INSENSITIVE); + private static final Pattern SCOPED_CASE_SENSITIVE_URL_PATTERN = Pattern.compile("[^:]+://(?-i:[.a-z]+/?)+?(/ending-path)", Pattern.CASE_INSENSITIVE); + private static final Pattern INDEPENDENT_URL_PATTERN_WITH_ENDING_PATH = Pattern.compile("[^:]+://(?>[.a-z]+/?)+/ending-path"); + private static final Pattern INDEPENDENT_URL_PATTERN_WITH_ENDING_PATH_AND_BACKTRACKING = Pattern.compile("[^:]+://(?>(?:[.a-z]+/?)+/)ending-path"); + + @Test + void givenSimpleUrlPattern_whenValidUrlProvided_thenMatches() { + Matcher urlMatcher = SIMPLE_URL_PATTERN.matcher("http://www.microsoft.com/"); + + Assertions.assertThat(urlMatcher.matches()) + .isTrue(); + Assertions.assertThatThrownBy(() -> urlMatcher.group(1)) + .isInstanceOf(IndexOutOfBoundsException.class); + } + + @Test + void whenSimpleUrlProvidedWithPathProvided_thenMatches() { + Matcher urlMatcher = SIMPLE_URL_PATTERN.matcher("http://www.microsoft.com/live"); + + Assertions.assertThat(urlMatcher.matches()) + .isTrue(); + } + + @Test + void whenSimpleUrlProvidedWithPathEndingWithSlashProvided_thenMatches() { + Matcher urlMatcher = SIMPLE_URL_PATTERN.matcher("http://www.microsoft.com/live/"); + + Assertions.assertThat(urlMatcher.matches()) + .isTrue(); + } + + @Test + void givenSimpleUrlPattern_whenUrlWithMultiplePathSegmentsProvided_thenMatches() { + Matcher urlMatcher = SIMPLE_URL_PATTERN.matcher("http://www.microsoft.com/some/other/url/path"); + + Assertions.assertThat(urlMatcher.matches()) + .isTrue(); + } + + @Test + void whenUrlWithUppercaseCharactersProvided_thenDoesNotMatch() { + Matcher urlMatcher = SIMPLE_URL_PATTERN.matcher("http://www.Microsoft.com/"); + + Assertions.assertThat(urlMatcher.matches()) + .isFalse(); + } + + @Test + void givenPatternWithCaseInsensitiveGroup_whenUrlHasUppercaseCharactersInsideOfScope_thenMatches() { + Matcher urlMatcher = SCOPED_CASE_INSENSITIVE_URL_PATTERN.matcher("http://www.Microsoft.com/"); + + Assertions.assertThat(urlMatcher.matches()) + .isTrue(); + } + + @Test + void givenCaseInsensitivePattern_whenUrlHasUppercaseCharacters_thenMatches() { + Matcher urlMatcher = CASE_INSENSITIVE_URL_PATTERN.matcher("http://www.Microsoft.com/Ending-path"); + + Assertions.assertThat(urlMatcher.matches()) + .isTrue(); + Assertions.assertThat(urlMatcher.group(1)) + .isEqualTo("/Ending-path"); + } + + @Test + void givenPatternWithCaseInsensitiveGroup_whenUrlHasUppercaseCharactersOutsideOfScope_thenMatchFails() { + Matcher urlMatcher = SCOPED_CASE_INSENSTIIVE_URL_PATTERN_WITH_ENDING_PATH.matcher("http://www.Microsoft.com/Ending-path"); + + Assertions.assertThat(urlMatcher.matches()) + .isFalse(); + } + + @Test + void givenPatternAllowingBacktracking_whenUrlWithEndingPathCausingBacktrackingProvided_thenMatches() { + Matcher urlMatcher = SIMPLE_URL_PATTERN_WITH_SPECIFIC_ENDING_PATH.matcher("http://www.microsoft.com/ending-path"); + + Assertions.assertThat(urlMatcher.matches()) + .isTrue(); + } + + @Test + void givenPatternWithIndependentNonCapturingGroup_whenBacktrackingOccurs_thenDoesNotMatch() { + Matcher independentMatcher = INDEPENDENT_URL_PATTERN_WITH_ENDING_PATH.matcher("http://www.microsoft.com/ending-path"); + + Assertions.assertThat(independentMatcher.matches()) + .isFalse(); + } + + @Test + void givenPatternWithIndependentNonCapturingGroup_whenBacktrackingDoesNotOccur_thenMatches() { + Matcher independentMatcher = INDEPENDENT_URL_PATTERN_WITH_ENDING_PATH.matcher("http://www.microsoft.com//ending-path"); + + Assertions.assertThat(independentMatcher.matches()) + .isTrue(); + } + + @Test + void givenPatternWithIndependentNonCapturingGroup_whenBacktrackingOccursInsideGroup_thenMatches() { + Matcher independentMatcher = INDEPENDENT_URL_PATTERN_WITH_ENDING_PATH_AND_BACKTRACKING.matcher("http://www.microsoft.com/ending-path"); + + Assertions.assertThat(independentMatcher.matches()) + .isTrue(); + } + + @Test + void givenCaseInsensitivePatternWithCaseSensitivieSubPattern_whenUrlWithUppercaseCharactersOutsideOfScopeProvided_thenMatches() { + Matcher urlMatcher = SCOPED_CASE_SENSITIVE_URL_PATTERN.matcher("http://www.microsoft.com/ENDING-PATH"); + + Assertions.assertThat(urlMatcher.matches()) + .isTrue(); + } + + @Test + void givenCaseInsensitivePatternWithCaseSensitivieSubPattern_whenUrlWithUppercaseCharactersInsideOfScopeProvided_thenDoesNotMatch() { + Matcher urlMatcher = SCOPED_CASE_SENSITIVE_URL_PATTERN.matcher("http://www.Microsoft.com/ending-path"); + + Assertions.assertThat(urlMatcher.matches()) + .isFalse(); + } +} From c7326c23caa0f00fd46ffa0305a5923d637bcb9b Mon Sep 17 00:00:00 2001 From: bhandy Date: Mon, 7 Jun 2021 22:41:29 -0400 Subject: [PATCH 3/4] Updated the fluent API calls for the AssertJ assertions. --- .../NonCapturingGroupUnitTest.java | 48 +++++++------------ 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/noncapturinggroups/NonCapturingGroupUnitTest.java b/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/noncapturinggroups/NonCapturingGroupUnitTest.java index 7b4e475b0a..281a550f34 100644 --- a/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/noncapturinggroups/NonCapturingGroupUnitTest.java +++ b/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/noncapturinggroups/NonCapturingGroupUnitTest.java @@ -21,115 +21,99 @@ public class NonCapturingGroupUnitTest { void givenSimpleUrlPattern_whenValidUrlProvided_thenMatches() { Matcher urlMatcher = SIMPLE_URL_PATTERN.matcher("http://www.microsoft.com/"); - Assertions.assertThat(urlMatcher.matches()) - .isTrue(); - Assertions.assertThatThrownBy(() -> urlMatcher.group(1)) - .isInstanceOf(IndexOutOfBoundsException.class); + Assertions.assertThat(urlMatcher.matches()).isTrue(); + Assertions.assertThatThrownBy(() -> urlMatcher.group(1)).isInstanceOf(IndexOutOfBoundsException.class); } @Test void whenSimpleUrlProvidedWithPathProvided_thenMatches() { Matcher urlMatcher = SIMPLE_URL_PATTERN.matcher("http://www.microsoft.com/live"); - Assertions.assertThat(urlMatcher.matches()) - .isTrue(); + Assertions.assertThat(urlMatcher.matches()).isTrue(); } @Test void whenSimpleUrlProvidedWithPathEndingWithSlashProvided_thenMatches() { Matcher urlMatcher = SIMPLE_URL_PATTERN.matcher("http://www.microsoft.com/live/"); - Assertions.assertThat(urlMatcher.matches()) - .isTrue(); + Assertions.assertThat(urlMatcher.matches()).isTrue(); } @Test void givenSimpleUrlPattern_whenUrlWithMultiplePathSegmentsProvided_thenMatches() { Matcher urlMatcher = SIMPLE_URL_PATTERN.matcher("http://www.microsoft.com/some/other/url/path"); - Assertions.assertThat(urlMatcher.matches()) - .isTrue(); + Assertions.assertThat(urlMatcher.matches()).isTrue(); } @Test void whenUrlWithUppercaseCharactersProvided_thenDoesNotMatch() { Matcher urlMatcher = SIMPLE_URL_PATTERN.matcher("http://www.Microsoft.com/"); - Assertions.assertThat(urlMatcher.matches()) - .isFalse(); + Assertions.assertThat(urlMatcher.matches()).isFalse(); } @Test void givenPatternWithCaseInsensitiveGroup_whenUrlHasUppercaseCharactersInsideOfScope_thenMatches() { Matcher urlMatcher = SCOPED_CASE_INSENSITIVE_URL_PATTERN.matcher("http://www.Microsoft.com/"); - Assertions.assertThat(urlMatcher.matches()) - .isTrue(); + Assertions.assertThat(urlMatcher.matches()).isTrue(); } @Test void givenCaseInsensitivePattern_whenUrlHasUppercaseCharacters_thenMatches() { Matcher urlMatcher = CASE_INSENSITIVE_URL_PATTERN.matcher("http://www.Microsoft.com/Ending-path"); - Assertions.assertThat(urlMatcher.matches()) - .isTrue(); - Assertions.assertThat(urlMatcher.group(1)) - .isEqualTo("/Ending-path"); + Assertions.assertThat(urlMatcher.matches()).isTrue(); + Assertions.assertThat(urlMatcher.group(1)).isEqualTo("/Ending-path"); } @Test void givenPatternWithCaseInsensitiveGroup_whenUrlHasUppercaseCharactersOutsideOfScope_thenMatchFails() { Matcher urlMatcher = SCOPED_CASE_INSENSTIIVE_URL_PATTERN_WITH_ENDING_PATH.matcher("http://www.Microsoft.com/Ending-path"); - Assertions.assertThat(urlMatcher.matches()) - .isFalse(); + Assertions.assertThat(urlMatcher.matches()).isFalse(); } @Test void givenPatternAllowingBacktracking_whenUrlWithEndingPathCausingBacktrackingProvided_thenMatches() { Matcher urlMatcher = SIMPLE_URL_PATTERN_WITH_SPECIFIC_ENDING_PATH.matcher("http://www.microsoft.com/ending-path"); - Assertions.assertThat(urlMatcher.matches()) - .isTrue(); + Assertions.assertThat(urlMatcher.matches()).isTrue(); } @Test void givenPatternWithIndependentNonCapturingGroup_whenBacktrackingOccurs_thenDoesNotMatch() { Matcher independentMatcher = INDEPENDENT_URL_PATTERN_WITH_ENDING_PATH.matcher("http://www.microsoft.com/ending-path"); - Assertions.assertThat(independentMatcher.matches()) - .isFalse(); + Assertions.assertThat(independentMatcher.matches()).isFalse(); } @Test void givenPatternWithIndependentNonCapturingGroup_whenBacktrackingDoesNotOccur_thenMatches() { Matcher independentMatcher = INDEPENDENT_URL_PATTERN_WITH_ENDING_PATH.matcher("http://www.microsoft.com//ending-path"); - Assertions.assertThat(independentMatcher.matches()) - .isTrue(); + Assertions.assertThat(independentMatcher.matches()).isTrue(); } @Test void givenPatternWithIndependentNonCapturingGroup_whenBacktrackingOccursInsideGroup_thenMatches() { Matcher independentMatcher = INDEPENDENT_URL_PATTERN_WITH_ENDING_PATH_AND_BACKTRACKING.matcher("http://www.microsoft.com/ending-path"); - Assertions.assertThat(independentMatcher.matches()) - .isTrue(); + Assertions.assertThat(independentMatcher.matches()).isTrue(); } @Test void givenCaseInsensitivePatternWithCaseSensitivieSubPattern_whenUrlWithUppercaseCharactersOutsideOfScopeProvided_thenMatches() { Matcher urlMatcher = SCOPED_CASE_SENSITIVE_URL_PATTERN.matcher("http://www.microsoft.com/ENDING-PATH"); - Assertions.assertThat(urlMatcher.matches()) - .isTrue(); + Assertions.assertThat(urlMatcher.matches()).isTrue(); } @Test void givenCaseInsensitivePatternWithCaseSensitivieSubPattern_whenUrlWithUppercaseCharactersInsideOfScopeProvided_thenDoesNotMatch() { Matcher urlMatcher = SCOPED_CASE_SENSITIVE_URL_PATTERN.matcher("http://www.Microsoft.com/ending-path"); - Assertions.assertThat(urlMatcher.matches()) - .isFalse(); + Assertions.assertThat(urlMatcher.matches()).isFalse(); } } From be18bad873d62b8416fac9f0940f86b65bccfafa Mon Sep 17 00:00:00 2001 From: bhandy Date: Sat, 12 Jun 2021 00:25:19 -0400 Subject: [PATCH 4/4] Moving NonCapturingGroupUnitTestjava to new core-java-regex-2 module. --- core-java-modules/core-java-regex-2/pom.xml | 31 +++++++++++++++++++ .../NonCapturingGroupUnitTest.java | 0 core-java-modules/pom.xml | 3 +- 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 core-java-modules/core-java-regex-2/pom.xml rename core-java-modules/{core-java-regex => core-java-regex-2}/src/test/java/com/baeldung/regex/noncapturinggroups/NonCapturingGroupUnitTest.java (100%) diff --git a/core-java-modules/core-java-regex-2/pom.xml b/core-java-modules/core-java-regex-2/pom.xml new file mode 100644 index 0000000000..45e19ba5a5 --- /dev/null +++ b/core-java-modules/core-java-regex-2/pom.xml @@ -0,0 +1,31 @@ + + + 4.0.0 + core-java-regex-2 + 0.1.0-SNAPSHOT + core-java-regex-2 + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + ../ + + + + + org.assertj + assertj-core + ${assertj-core.version} + test + + + + + 3.15.0 + + + diff --git a/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/noncapturinggroups/NonCapturingGroupUnitTest.java b/core-java-modules/core-java-regex-2/src/test/java/com/baeldung/regex/noncapturinggroups/NonCapturingGroupUnitTest.java similarity index 100% rename from core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/noncapturinggroups/NonCapturingGroupUnitTest.java rename to core-java-modules/core-java-regex-2/src/test/java/com/baeldung/regex/noncapturinggroups/NonCapturingGroupUnitTest.java diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index b801d44a08..e1205c2217 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -114,6 +114,7 @@ core-java-string-operations-3 core-java-sun core-java-regex + core-java-regex-2 pre-jpms @@ -138,4 +139,4 @@ 2.22.2 5.6.2 - \ No newline at end of file +