From e92ecf211a0cc3f0a83cb207e7d6667efe450cd7 Mon Sep 17 00:00:00 2001 From: Usman Mohyuddin Date: Sat, 29 Aug 2020 22:31:40 +0500 Subject: [PATCH 1/4] Remove prefix from string using groovy (BAEL-4104) Remove prefix from string using groovy (BAEL-4104) --- core-groovy-strings/README.md | 8 +++ core-groovy-strings/pom.xml | 72 +++++++++++++++++++ .../removeprefix/RemovePrefixTest.groovy | 58 +++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 core-groovy-strings/README.md create mode 100644 core-groovy-strings/pom.xml create mode 100644 core-groovy-strings/src/test/groovy/com/baeldung/removeprefix/RemovePrefixTest.groovy diff --git a/core-groovy-strings/README.md b/core-groovy-strings/README.md new file mode 100644 index 0000000000..e3202e2dd0 --- /dev/null +++ b/core-groovy-strings/README.md @@ -0,0 +1,8 @@ +# Core Groovy Strings + +This module contains articles about core Groovy strings concepts + +## Relevant articles: + +- [Remove prefix in Groovy]() +- [[<-- Prev]](/core-groovy-strings) diff --git a/core-groovy-strings/pom.xml b/core-groovy-strings/pom.xml new file mode 100644 index 0000000000..059b53662c --- /dev/null +++ b/core-groovy-strings/pom.xml @@ -0,0 +1,72 @@ + + + com.baeldung + 4.0.0 + core-groovy-strings + 1.0-SNAPSHOT + core-groovy-strings + jar + + + + org.codehaus.groovy + groovy-all + ${groovy.version} + pom + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + + + + + src/main/groovy + src/main/java + + + org.codehaus.groovy + groovy-eclipse-compiler + ${groovy.compiler.version} + true + + + maven-compiler-plugin + ${compiler.plugin.version} + + groovy-eclipse-compiler + ${java.version} + ${java.version} + + + + org.codehaus.groovy + groovy-eclipse-compiler + ${groovy.compiler.version} + + + org.codehaus.groovy + groovy-eclipse-batch + ${groovy.version}-01 + + + + + + + + 1.0.0 + 1.1.3 + 2.5.7 + 2.20.1 + 3.8.0 + 3.3.0-01 + 1.8 + + + \ No newline at end of file diff --git a/core-groovy-strings/src/test/groovy/com/baeldung/removeprefix/RemovePrefixTest.groovy b/core-groovy-strings/src/test/groovy/com/baeldung/removeprefix/RemovePrefixTest.groovy new file mode 100644 index 0000000000..568e5fdde8 --- /dev/null +++ b/core-groovy-strings/src/test/groovy/com/baeldung/removeprefix/RemovePrefixTest.groovy @@ -0,0 +1,58 @@ +package com.baeldung.removeprefix + +import org.junit.Assert +import org.junit.Test + +class RemovePrefixTest { + + + @Test + public void givenWhenCasePrefixIsRemoved_thenReturnTrue() { + def trimPrefix = { + it.startsWith('Groovy') ? it.minus('Groovy') : it + } + + Assert.assertEquals(trimPrefix("GroovyTutorials at Baeldung"), "Tutorials at Baeldung") + } + + @Test + public void givenWhenPrefixIsRemoved_thenReturnTrue() { + + String prefix = "groovy-" + String trimPrefix = "Groovy-Tutorials at Baeldung" + def result; + if(trimPrefix.startsWithIgnoreCase(prefix)) { + result = trimPrefix.substring(prefix.length()) + } + + Assert.assertEquals("Tutorials at Baeldung", result) + } + + @Test + public void givenWhenPrefixIsRemovedUsingRegex_thenReturnTrue() { + + def regex = ~"^([Gg])roovy-" + String trimPrefix = "Groovy-Tutorials at Baeldung" + String result = trimPrefix - regex + + Assert.assertEquals("Tutorials at Baeldung", result) + } + + @Test public void givenWhenPrefixIsRemovedUsingReplaceFirst_thenReturnTrue() { + def regex = ~"^groovy" + String trimPrefix = "groovyTutorials at Baeldung's groovy page" + String result = trimPrefix.replaceFirst(regex, "") + + Assert.assertEquals("Tutorials at Baeldung's groovy page", result) + } + + @Test + public void givenWhenPrefixIsRemovedUsingReplaceAll_thenReturnTrue() { + + String trimPrefix = "groovyTutorials at Baeldung groovy" + String result = trimPrefix.replaceAll(/^groovy/, "") + + Assert.assertEquals("Tutorials at Baeldung groovy", result) + } + +} From 8415ee8096e64e7b7a57fb0fe85d17024a4d6884 Mon Sep 17 00:00:00 2001 From: Usman Mohyuddin Date: Sun, 6 Sep 2020 00:07:33 +0500 Subject: [PATCH 2/4] update the test names as whenX_thenY update the test names as whenX_thenY --- .../com/baeldung/removeprefix/RemovePrefixTest.groovy | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/core-groovy-strings/src/test/groovy/com/baeldung/removeprefix/RemovePrefixTest.groovy b/core-groovy-strings/src/test/groovy/com/baeldung/removeprefix/RemovePrefixTest.groovy index 568e5fdde8..aa9e65d98d 100644 --- a/core-groovy-strings/src/test/groovy/com/baeldung/removeprefix/RemovePrefixTest.groovy +++ b/core-groovy-strings/src/test/groovy/com/baeldung/removeprefix/RemovePrefixTest.groovy @@ -7,7 +7,7 @@ class RemovePrefixTest { @Test - public void givenWhenCasePrefixIsRemoved_thenReturnTrue() { + public void whenCasePrefixIsRemoved_thenReturnTrue() { def trimPrefix = { it.startsWith('Groovy') ? it.minus('Groovy') : it } @@ -16,7 +16,7 @@ class RemovePrefixTest { } @Test - public void givenWhenPrefixIsRemoved_thenReturnTrue() { + public void whenPrefixIsRemovedWithIgnoreCase_thenReturnTrue() { String prefix = "groovy-" String trimPrefix = "Groovy-Tutorials at Baeldung" @@ -29,7 +29,7 @@ class RemovePrefixTest { } @Test - public void givenWhenPrefixIsRemovedUsingRegex_thenReturnTrue() { + public void whenPrefixIsRemovedUsingRegex_thenReturnTrue() { def regex = ~"^([Gg])roovy-" String trimPrefix = "Groovy-Tutorials at Baeldung" @@ -38,7 +38,7 @@ class RemovePrefixTest { Assert.assertEquals("Tutorials at Baeldung", result) } - @Test public void givenWhenPrefixIsRemovedUsingReplaceFirst_thenReturnTrue() { + @Test public void whenPrefixIsRemovedUsingReplaceFirst_thenReturnTrue() { def regex = ~"^groovy" String trimPrefix = "groovyTutorials at Baeldung's groovy page" String result = trimPrefix.replaceFirst(regex, "") @@ -47,7 +47,7 @@ class RemovePrefixTest { } @Test - public void givenWhenPrefixIsRemovedUsingReplaceAll_thenReturnTrue() { + public void whenPrefixIsRemovedUsingReplaceAll_thenReturnTrue() { String trimPrefix = "groovyTutorials at Baeldung groovy" String result = trimPrefix.replaceAll(/^groovy/, "") From 43fb479c815a010c983560885f8b713eefd4347f Mon Sep 17 00:00:00 2001 From: Usman Mohyuddin Date: Thu, 10 Sep 2020 22:39:44 +0500 Subject: [PATCH 3/4] update the PR with removal of README file and update pom.xml files according to comments --- core-groovy-strings/README.md | 8 -- core-groovy-strings/pom.xml | 114 +++++++++++++----- .../removeprefix/RemovePrefixTest.groovy | 37 ++++-- pom.xml | 2 + 4 files changed, 108 insertions(+), 53 deletions(-) delete mode 100644 core-groovy-strings/README.md diff --git a/core-groovy-strings/README.md b/core-groovy-strings/README.md deleted file mode 100644 index e3202e2dd0..0000000000 --- a/core-groovy-strings/README.md +++ /dev/null @@ -1,8 +0,0 @@ -# Core Groovy Strings - -This module contains articles about core Groovy strings concepts - -## Relevant articles: - -- [Remove prefix in Groovy]() -- [[<-- Prev]](/core-groovy-strings) diff --git a/core-groovy-strings/pom.xml b/core-groovy-strings/pom.xml index 059b53662c..1144d748ee 100644 --- a/core-groovy-strings/pom.xml +++ b/core-groovy-strings/pom.xml @@ -3,70 +3,120 @@ xmlns="http://maven.apache.org/POM/4.0.0" 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"> - com.baeldung 4.0.0 core-groovy-strings 1.0-SNAPSHOT core-groovy-strings jar - + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + org.codehaus.groovy - groovy-all + groovy ${groovy.version} + + + org.codehaus.groovy + groovy-all + ${groovy-all.version} pom + + org.codehaus.groovy + groovy-dateutil + ${groovy.version} + + + org.codehaus.groovy + groovy-sql + ${groovy-sql.version} + org.junit.platform junit-platform-runner ${junit.platform.version} test + + org.hsqldb + hsqldb + ${hsqldb.version} + test + + + org.spockframework + spock-core + ${spock-core.version} + test + - src/main/groovy - src/main/java - org.codehaus.groovy - groovy-eclipse-compiler - ${groovy.compiler.version} - true + org.codehaus.gmavenplus + gmavenplus-plugin + ${gmavenplus-plugin.version} + + + + addSources + addTestSources + compile + compileTests + + + - maven-compiler-plugin - ${compiler.plugin.version} - - groovy-eclipse-compiler - ${java.version} - ${java.version} - + maven-failsafe-plugin + ${maven-failsafe-plugin.version} - org.codehaus.groovy - groovy-eclipse-compiler - ${groovy.compiler.version} - - - org.codehaus.groovy - groovy-eclipse-batch - ${groovy.version}-01 + org.junit.platform + junit-platform-surefire-provider + ${junit.platform.version} + + + junit5 + + integration-test + verify + + + + **/*Test5.java + + + + - + + + + central + https://jcenter.bintray.com + + + 1.0.0 - 1.1.3 - 2.5.7 - 2.20.1 - 3.8.0 - 3.3.0-01 - 1.8 + 2.5.6 + 2.5.6 + 2.5.6 + 2.4.0 + 1.1-groovy-2.4 + 1.6 - + \ No newline at end of file diff --git a/core-groovy-strings/src/test/groovy/com/baeldung/removeprefix/RemovePrefixTest.groovy b/core-groovy-strings/src/test/groovy/com/baeldung/removeprefix/RemovePrefixTest.groovy index aa9e65d98d..4cdcb4ddfa 100644 --- a/core-groovy-strings/src/test/groovy/com/baeldung/removeprefix/RemovePrefixTest.groovy +++ b/core-groovy-strings/src/test/groovy/com/baeldung/removeprefix/RemovePrefixTest.groovy @@ -9,23 +9,28 @@ class RemovePrefixTest { @Test public void whenCasePrefixIsRemoved_thenReturnTrue() { def trimPrefix = { - it.startsWith('Groovy') ? it.minus('Groovy') : it + it.startsWith('Groovy-') ? it.minus('Groovy-') : it } + + def actual = trimPrefix("Groovy-Tutorials at Baeldung") + def expected = "Tutorials at Baeldung" - Assert.assertEquals(trimPrefix("GroovyTutorials at Baeldung"), "Tutorials at Baeldung") + Assert.assertEquals(expected, actual) } @Test - public void whenPrefixIsRemovedWithIgnoreCase_thenReturnTrue() { + public void whenPrefixIsRemoved_thenReturnTrue() { String prefix = "groovy-" String trimPrefix = "Groovy-Tutorials at Baeldung" - def result; + def actual; if(trimPrefix.startsWithIgnoreCase(prefix)) { - result = trimPrefix.substring(prefix.length()) + actual = trimPrefix.substring(prefix.length()) } + + def expected = "Tutorials at Baeldung" - Assert.assertEquals("Tutorials at Baeldung", result) + Assert.assertEquals(expected, actual) } @Test @@ -33,26 +38,32 @@ class RemovePrefixTest { def regex = ~"^([Gg])roovy-" String trimPrefix = "Groovy-Tutorials at Baeldung" - String result = trimPrefix - regex + String actual = trimPrefix - regex + + def expected = "Tutorials at Baeldung" - Assert.assertEquals("Tutorials at Baeldung", result) + Assert.assertEquals(expected, actual) } @Test public void whenPrefixIsRemovedUsingReplaceFirst_thenReturnTrue() { def regex = ~"^groovy" String trimPrefix = "groovyTutorials at Baeldung's groovy page" - String result = trimPrefix.replaceFirst(regex, "") + String actual = trimPrefix.replaceFirst(regex, "") - Assert.assertEquals("Tutorials at Baeldung's groovy page", result) + def expected = "Tutorials at Baeldung's groovy page" + + Assert.assertEquals(expected, actual) } @Test public void whenPrefixIsRemovedUsingReplaceAll_thenReturnTrue() { String trimPrefix = "groovyTutorials at Baeldung groovy" - String result = trimPrefix.replaceAll(/^groovy/, "") + String actual = trimPrefix.replaceAll(/^groovy/, "") + + def expected = "Tutorials at Baeldung groovy" - Assert.assertEquals("Tutorials at Baeldung groovy", result) + Assert.assertEquals(expected, actual) } -} +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index ffdfe4cffa..f43318bcaa 100644 --- a/pom.xml +++ b/pom.xml @@ -383,6 +383,7 @@ core-groovy core-groovy-2 core-groovy-collections + core-groovy-strings core-java-modules core-kotlin-modules @@ -898,6 +899,7 @@ core-groovy core-groovy-2 core-groovy-collections + core-groovy-strings core-java-modules core-kotlin-modules From 40ebac5142a360131df2fb1f26bee8cb0b542511 Mon Sep 17 00:00:00 2001 From: Usman Mohyuddin Date: Tue, 15 Sep 2020 15:09:35 +0500 Subject: [PATCH 4/4] Split the @Test annotation in new line --- .../groovy/com/baeldung/removeprefix/RemovePrefixTest.groovy | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core-groovy-strings/src/test/groovy/com/baeldung/removeprefix/RemovePrefixTest.groovy b/core-groovy-strings/src/test/groovy/com/baeldung/removeprefix/RemovePrefixTest.groovy index 4cdcb4ddfa..61b81fe1b2 100644 --- a/core-groovy-strings/src/test/groovy/com/baeldung/removeprefix/RemovePrefixTest.groovy +++ b/core-groovy-strings/src/test/groovy/com/baeldung/removeprefix/RemovePrefixTest.groovy @@ -45,7 +45,8 @@ class RemovePrefixTest { Assert.assertEquals(expected, actual) } - @Test public void whenPrefixIsRemovedUsingReplaceFirst_thenReturnTrue() { + @Test + public void whenPrefixIsRemovedUsingReplaceFirst_thenReturnTrue() { def regex = ~"^groovy" String trimPrefix = "groovyTutorials at Baeldung's groovy page" String actual = trimPrefix.replaceFirst(regex, "")