Skip to content

Commit 524912b

Browse files
author
Abhishek Malvadkar
committed
GH-1 : Refactored pom file and created StringUtils along with basic functions and unit test for them
1 parent 3bfd50c commit 524912b

File tree

9 files changed

+214
-53
lines changed

9 files changed

+214
-53
lines changed

pom.xml

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,81 @@
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
5-
<parent>
6-
<groupId>org.springframework.boot</groupId>
7-
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>3.4.3</version>
9-
<relativePath/> <!-- lookup parent from repository -->
10-
</parent>
5+
116
<groupId>com.amalvadkar</groupId>
127
<artifactId>am-commons-utils</artifactId>
138
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
1410
<name>am-commons-utils</name>
15-
<description>Common Utilities</description>
16-
<url/>
17-
<licenses>
18-
<license/>
19-
</licenses>
20-
<developers>
21-
<developer/>
22-
</developers>
23-
<scm>
24-
<connection/>
25-
<developerConnection/>
26-
<tag/>
27-
<url/>
28-
</scm>
11+
<description>Common Utility Library</description>
12+
2913
<properties>
14+
<!-- Dependency versions management -->
3015
<java.version>21</java.version>
16+
<junit-jupiter.version>5.10.2</junit-jupiter.version>
17+
<assertj-core.version>3.25.3</assertj-core.version>
18+
19+
<!-- Plugin versions management -->
20+
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
21+
<maven-surefire-plugin.version>3.1.2</maven-surefire-plugin.version>
3122
</properties>
23+
3224
<dependencies>
25+
<!-- JUnit 5 API for writing tests -->
26+
<dependency>
27+
<groupId>org.junit.jupiter</groupId>
28+
<artifactId>junit-jupiter-api</artifactId>
29+
<version>${junit-jupiter.version}</version>
30+
<scope>test</scope>
31+
</dependency>
32+
33+
<!-- JUnit 5 Engine for running tests -->
3334
<dependency>
34-
<groupId>org.springframework.boot</groupId>
35-
<artifactId>spring-boot-starter</artifactId>
35+
<groupId>org.junit.jupiter</groupId>
36+
<artifactId>junit-jupiter-engine</artifactId>
37+
<version>${junit-jupiter.version}</version>
38+
<scope>test</scope>
3639
</dependency>
3740

41+
<!-- AssertJ for fluent assertions -->
3842
<dependency>
39-
<groupId>org.springframework.boot</groupId>
40-
<artifactId>spring-boot-starter-test</artifactId>
43+
<groupId>org.assertj</groupId>
44+
<artifactId>assertj-core</artifactId>
45+
<version>${assertj-core.version}</version>
4146
<scope>test</scope>
4247
</dependency>
48+
4349
</dependencies>
4450

4551
<build>
52+
<finalName>${project.artifactId}</finalName>
4653
<plugins>
54+
<!-- Compiler Plugin to Ensure Java 21 Compatibility -->
55+
<plugin>
56+
<groupId>org.apache.maven.plugins</groupId>
57+
<artifactId>maven-compiler-plugin</artifactId>
58+
<version>${maven-compiler-plugin.version}</version>
59+
<configuration>
60+
<source>${java.version}</source>
61+
<target>${java.version}</target>
62+
</configuration>
63+
</plugin>
64+
65+
<!-- JAR Plugin for Packaging the Library -->
66+
<plugin>
67+
<groupId>org.apache.maven.plugins</groupId>
68+
<artifactId>maven-jar-plugin</artifactId>
69+
<version>3.2.2</version>
70+
</plugin>
71+
72+
<!-- Surefire Plugin for Running JUnit 5 Tests -->
4773
<plugin>
48-
<groupId>org.springframework.boot</groupId>
49-
<artifactId>spring-boot-maven-plugin</artifactId>
74+
<groupId>org.apache.maven.plugins</groupId>
75+
<artifactId>maven-surefire-plugin</artifactId>
76+
<version>${maven-surefire-plugin.version}</version>
77+
<configuration>
78+
<useModulePath>false</useModulePath>
79+
</configuration>
5080
</plugin>
5181
</plugins>
5282
</build>

src/main/java/com/amalvadkar/commons/utils/AmCommonsUtilsApplication.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.amalvadkar.commons.utils.constants;
2+
3+
public class StringConstants {
4+
5+
public static final String EMPTY_STRING = "";
6+
7+
private StringConstants() {
8+
throw new UnsupportedOperationException("Constants class");
9+
}
10+
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.amalvadkar.commons.utils.exceptions;
2+
3+
/**
4+
* Custom exception for invalid string operations.
5+
*/
6+
public class InvalidStringException extends RuntimeException {
7+
public InvalidStringException(String message) {
8+
super(message);
9+
}
10+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.amalvadkar.commons.utils.string;
2+
3+
import com.amalvadkar.commons.utils.constants.StringConstants;
4+
import com.amalvadkar.commons.utils.exceptions.InvalidStringException;
5+
6+
/**
7+
* Utility class for common string operations.
8+
*/
9+
public final class StringUtils {
10+
11+
private StringUtils() {
12+
throw new UnsupportedOperationException("Utility class");
13+
}
14+
15+
/**
16+
* Capitalizes the first letter of the input string.
17+
*
18+
* @param input The string to capitalize.
19+
* @return Capitalized string or an empty string if input is null/empty.
20+
* @throws InvalidStringException if the input is null or empty.
21+
*/
22+
public static String capitalize(String input) {
23+
if (isEmpty(input)) {
24+
throw new InvalidStringException("Input string cannot be null or empty.");
25+
}
26+
return input.substring(0, 1).toUpperCase() + input.substring(1);
27+
}
28+
29+
/**
30+
* Checks if a string is null or empty.
31+
*
32+
* @param input The string to check.
33+
* @return True if the string is null or empty, otherwise false.
34+
*/
35+
public static boolean isEmpty(String input) {
36+
return input == null || input.trim().isEmpty();
37+
}
38+
39+
/**
40+
* Reverses a given string.
41+
*
42+
* @param input The string to reverse.
43+
* @return The reversed string.
44+
*/
45+
public static String reverse(String input) {
46+
if (isEmpty(input)) {
47+
return StringConstants.EMPTY_STRING;
48+
}
49+
return new StringBuilder(input).reverse().toString();
50+
}
51+
52+
/**
53+
* Trims spaces from both ends of the string.
54+
*
55+
* @param input The string to trim.
56+
* @return The trimmed string.
57+
*/
58+
public static String trim(String input) {
59+
return isEmpty(input) ? StringConstants.EMPTY_STRING : input.trim();
60+
}
61+
62+
/**
63+
* Checks if a string contains only letters.
64+
*
65+
* @param input The string to check.
66+
* @return True if the string contains only letters, otherwise false.
67+
*/
68+
public static boolean containsOnlyLetters(String input) {
69+
return !isEmpty(input) && input.matches("[a-zA-Z]+");
70+
}
71+
}

src/main/resources/application.properties

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/test/java/com/amalvadkar/commons/utils/AmCommonsUtilsApplicationTests.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.amalvadkar.commons.utils.string;
2+
3+
import com.amalvadkar.commons.utils.exceptions.InvalidStringException;
4+
import common.AbstractUnitTest;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
9+
10+
class StringUtilsTest extends AbstractUnitTest {
11+
12+
@Test
13+
void capitalize_should_capitalize_first_letter() {
14+
assertThat(StringUtils.capitalize("hello")).isEqualTo("Hello");
15+
}
16+
17+
@Test
18+
void capitalize_should_throw_exception_when_input_is_null() {
19+
assertThatThrownBy(() -> StringUtils.capitalize(null))
20+
.isInstanceOf(InvalidStringException.class)
21+
.hasMessage("Input string cannot be null or empty.");
22+
}
23+
24+
@Test
25+
void is_empty_should_return_true_when_string_is_empty() {
26+
assertThat(StringUtils.isEmpty("")).isTrue();
27+
}
28+
29+
@Test
30+
void is_empty_should_return_false_when_string_is_not_empty() {
31+
assertThat(StringUtils.isEmpty("test")).isFalse();
32+
}
33+
34+
@Test
35+
void reverse_should_return_reversed_string() {
36+
assertThat(StringUtils.reverse("hello")).isEqualTo("olleh");
37+
}
38+
39+
@Test
40+
void reverse_should_return_empty_when_input_is_empty() {
41+
assertThat(StringUtils.reverse("")).isEqualTo("");
42+
}
43+
44+
@Test
45+
void trim_should_remove_leading_and_trailing_spaces() {
46+
assertThat(StringUtils.trim(" hello ")).isEqualTo("hello");
47+
}
48+
49+
@Test
50+
void contains_only_letters_should_return_true_when_only_letters() {
51+
assertThat(StringUtils.containsOnlyLetters("Hello")).isTrue();
52+
}
53+
54+
@Test
55+
void contains_only_letters_should_return_false_when_contains_numbers() {
56+
assertThat(StringUtils.containsOnlyLetters("Hello123")).isFalse();
57+
}
58+
59+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package common;
2+
3+
import org.junit.jupiter.api.Tag;
4+
5+
@Tag("unit")
6+
public class AbstractUnitTest {
7+
}

0 commit comments

Comments
 (0)