From a459b332ab7034a8c0a1a6c7353caab16a04de1c Mon Sep 17 00:00:00 2001 From: Stepan Miakchilo Date: Sat, 1 Jun 2024 00:38:32 +0300 Subject: [PATCH 1/3] [EAK-527] Enhanced the string transformation routine; provided a unit test --- .../meta/StringTransformation.java | 64 ++++++++++++++++++- .../meta/StringTransformationTest.java | 48 ++++++++++++++ 2 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 core/src/test/java/com/exadel/aem/toolkit/api/annotations/meta/StringTransformationTest.java diff --git a/core/src/main/java/com/exadel/aem/toolkit/api/annotations/meta/StringTransformation.java b/core/src/main/java/com/exadel/aem/toolkit/api/annotations/meta/StringTransformation.java index d45e24c05..d47ff7c9e 100644 --- a/core/src/main/java/com/exadel/aem/toolkit/api/annotations/meta/StringTransformation.java +++ b/core/src/main/java/com/exadel/aem/toolkit/api/annotations/meta/StringTransformation.java @@ -14,21 +14,49 @@ package com.exadel.aem.toolkit.api.annotations.meta; import java.util.function.UnaryOperator; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.text.WordUtils; import com.google.common.base.CaseFormat; +import com.exadel.aem.toolkit.core.CoreConstants; + /** * Enumerates transformations of a string value that can be applied when rendering as an attribute of a Granite UI entity */ public enum StringTransformation { + /** + * No transformation is applied (default value) + */ NONE(null), + + /** + * The string is converted to uppercase + */ UPPERCASE(String::toUpperCase), + + /** + * The string is converted to lowercase + */ LOWERCASE(String::toLowerCase), - CAMELCASE(string -> CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, string.toLowerCase())), - CAPITALIZE(string -> WordUtils.capitalizeFully(string, ' ', '-')); + + /** + * The string is split into words delimited by a space, a hyphen, or an underscore. The words are then merged + * into a {@code camelCase} string + */ + CAMELCASE(StringTransformation::toCamelCase), + + /** + * The string is split into words delimited by a space, a hyphen, or an underscore. The first letter of each + * word is capitalized. The words are then merged with a space between them + */ + CAPITALIZE(StringTransformation::capitalize); + + private static final char CHAR_SPACE = ' '; + private static final char CHAR_HYPHEN = '-'; + private static final char CHAR_UNDERSCORE = '_'; private final UnaryOperator transformation; @@ -52,4 +80,34 @@ public String apply(String value) { return transformation.apply(value); } + /* --------------- + Transformations + --------------- */ + + /** + * Converts a string to camelCase + * @param value The string to be transformed + * @return Resulting string value + */ + private static String toCamelCase(String value) { + if (StringUtils.isBlank(value)) { + return value; + } + return value.contains(CoreConstants.SEPARATOR_HYPHEN) + ? CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, value.toLowerCase().replace(CHAR_SPACE, CHAR_HYPHEN)) + : CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, value.toLowerCase().replace(CHAR_SPACE, CHAR_UNDERSCORE)); + } + + /** + * Capitalizes the first letter of each word in the provided string + * @param value The string to be transformed + * @return Resulting string value + */ + private static String capitalize(String value) { + if (StringUtils.isBlank(value)) { + return value; + } + String[] words = StringUtils.split(value, " -_"); + return Stream.of(words).map(StringUtils::capitalize).collect(Collectors.joining(StringUtils.SPACE)); + } } diff --git a/core/src/test/java/com/exadel/aem/toolkit/api/annotations/meta/StringTransformationTest.java b/core/src/test/java/com/exadel/aem/toolkit/api/annotations/meta/StringTransformationTest.java new file mode 100644 index 000000000..1e503ca9f --- /dev/null +++ b/core/src/test/java/com/exadel/aem/toolkit/api/annotations/meta/StringTransformationTest.java @@ -0,0 +1,48 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.exadel.aem.toolkit.api.annotations.meta; + +import org.junit.Assert; +import org.junit.Test; + +public class StringTransformationTest { + + private static final String SAMPLE1 = "L0rem Ipsum! Do1or Sit Amet"; + private static final String SAMPLE2 = "Lorem Ipsum dolor-sit-Amet"; + private static final String SAMPLE3 = "Lorem_Ipsum__dolor_Sit_Amet"; + + @Test + public void shouldConvertToLowerCase() { + Assert.assertEquals("l0rem ipsum! do1or sit amet", StringTransformation.LOWERCASE.apply(SAMPLE1)); + } + + @Test + public void shouldConvertToUpperCase() { + Assert.assertEquals("L0REM IPSUM! DO1OR SIT AMET", StringTransformation.UPPERCASE.apply(SAMPLE1)); + } + + @Test + public void shouldConvertToCamelCase() { + Assert.assertEquals("l0remIpsum!Do1orSitAmet", StringTransformation.CAMELCASE.apply(SAMPLE1)); + Assert.assertEquals("loremIpsumDolorSitAmet", StringTransformation.CAMELCASE.apply(SAMPLE2)); + Assert.assertEquals("loremIpsumDolorSitAmet", StringTransformation.CAMELCASE.apply(SAMPLE3)); + } + + @Test + public void shouldCapitalize() { + Assert.assertEquals("L0rem Ipsum! Do1or Sit Amet", StringTransformation.CAPITALIZE.apply(SAMPLE1)); + Assert.assertEquals("Lorem Ipsum Dolor Sit Amet", StringTransformation.CAPITALIZE.apply(SAMPLE2)); + Assert.assertEquals("Lorem Ipsum Dolor Sit Amet", StringTransformation.CAPITALIZE.apply(SAMPLE3)); + } +} From 250029dd5a9b3eb803695559585f210655d975a3 Mon Sep 17 00:00:00 2001 From: Stepan Miakchilo Date: Sat, 1 Jun 2024 00:39:35 +0300 Subject: [PATCH 2/3] [EAK-527] Moved the AllTests class --- .../test/java/com/exadel/aem/toolkit/{core => }/AllTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename core/src/test/java/com/exadel/aem/toolkit/{core => }/AllTests.java (98%) diff --git a/core/src/test/java/com/exadel/aem/toolkit/core/AllTests.java b/core/src/test/java/com/exadel/aem/toolkit/AllTests.java similarity index 98% rename from core/src/test/java/com/exadel/aem/toolkit/core/AllTests.java rename to core/src/test/java/com/exadel/aem/toolkit/AllTests.java index 58ba3d663..bf1caba03 100644 --- a/core/src/test/java/com/exadel/aem/toolkit/core/AllTests.java +++ b/core/src/test/java/com/exadel/aem/toolkit/AllTests.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.exadel.aem.toolkit.core; +package com.exadel.aem.toolkit; import org.junit.runner.RunWith; import org.junit.runners.Suite; From ddcb3252a934d8c2a93b71405348ab6f7765766d Mon Sep 17 00:00:00 2001 From: Stepan Miakchilo Date: Sat, 1 Jun 2024 00:41:55 +0300 Subject: [PATCH 3/3] [EAK-527] Registered a new test class --- core/src/test/java/com/exadel/aem/toolkit/AllTests.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/src/test/java/com/exadel/aem/toolkit/AllTests.java b/core/src/test/java/com/exadel/aem/toolkit/AllTests.java index bf1caba03..0a75a4a1b 100644 --- a/core/src/test/java/com/exadel/aem/toolkit/AllTests.java +++ b/core/src/test/java/com/exadel/aem/toolkit/AllTests.java @@ -17,6 +17,7 @@ import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; +import com.exadel.aem.toolkit.api.annotations.meta.StringTransformationTest; import com.exadel.aem.toolkit.core.injectors.ChildInjectorTest; import com.exadel.aem.toolkit.core.injectors.ChildrenInjectorTest; import com.exadel.aem.toolkit.core.injectors.EToolboxListInjectorTest; @@ -45,6 +46,8 @@ */ @RunWith(Suite.class) @SuiteClasses({ + StringTransformationTest.class, + ListHelperTest.class, ListPageUtilTest.class, ListResourceUtilTest.class,