Skip to content

Commit

Permalink
Split modules into common and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
stijn-dejongh committed Sep 22, 2024
1 parent ecdd792 commit 3d874eb
Show file tree
Hide file tree
Showing 30 changed files with 260 additions and 80 deletions.
51 changes: 51 additions & 0 deletions commons-testing/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<project 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">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>be.sddevelopment</groupId>
<artifactId>code-utils</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>commons-testing</artifactId>

<dependencies>
<!-- BEGIN UTIL SCOPE -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>

<!-- BEGIN TEST SCOPE -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit-junit5-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@
package be.sddevelopment.commons.testing;

import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;

import be.sddevelopment.commons.access.AccessProtectionUtils;
import be.sddevelopment.commons.access.Utility;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

Expand All @@ -50,11 +48,11 @@
* @created 25.03.21, Thursday
* @since 1.0.0
*/
@Utility
public final class ReflectionAssertionUtils {

private ReflectionAssertionUtils() {
AccessProtectionUtils.utilityClassConstructor();
throw new UnsupportedOperationException(
"Utility classes should not have a public or default constructor");
}

public static void assertPrivateMemberReflectionProtection(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import static org.apache.commons.lang3.StringUtils.isAlpha;
import static org.assertj.core.api.Assertions.assertThat;

import be.sddevelopment.commons.access.AccessProtectionUtils;
import be.sddevelopment.commons.access.Utility;
import java.util.Optional;
import java.util.function.Supplier;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -41,13 +39,13 @@
* @version $Id: $Id
* @created 18.10.20, Sunday
*/
@Utility
public final class TextualAssertions {

private static final String FIELD_NOT_EMPTY = "Field expected to not be empty";

private TextualAssertions() {
AccessProtectionUtils.utilityClassConstructor();
throw new UnsupportedOperationException(
"Utility classes should not have a public or default constructor");
}

@SuppressWarnings("OptionalGetWithoutIsPresent")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;

import be.sddevelopment.commons.constants.Strings;
import java.util.Optional;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DisplayNameGeneration;
Expand All @@ -50,26 +49,34 @@ class EmptyInputTests {

@Test
void whenAssertingANumericalOptional_andTheStringIsEmpty_thenTheAssertFails() {
assertThatThrownBy(() -> TextualAssertions.assertThatNumber(this::emptyInputSupplier)).isInstanceOf(AssertionError.class).hasMessageContaining("to be numeric");
assertThatThrownBy(() -> TextualAssertions.assertThatNumber(this::emptyInputSupplier))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("to be numeric");
}

@Test
void whenAssertingAnAlphaNumericalOptional_andTheStringIsEmpty_thenTheAssertFails() {
assertThatThrownBy(() -> TextualAssertions.assertThatAlphanumeric(this::emptyInputSupplier)).isInstanceOf(AssertionError.class).hasMessageContaining("to be alphanumeric");
assertThatThrownBy(() -> TextualAssertions.assertThatAlphanumeric(this::emptyInputSupplier))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("to be alphanumeric");
}

@Test
void whenAssertingAnAlphaOptional_andTheStringIsEmpty_thenTheAssertFails() {
assertThatThrownBy(() -> TextualAssertions.assertThatAlpha(this::emptyInputSupplier)).isInstanceOf(AssertionError.class).hasMessageContaining("to be alphabetical");
assertThatThrownBy(() -> TextualAssertions.assertThatAlpha(this::emptyInputSupplier))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("to be alphabetical");
}

@Test
void whenAssertingAnEmptyInput_andTheStringIsEmpty_thenTheAssertFails() {
assertThatThrownBy(() -> TextualAssertions.assertEmpty(this::emptyInputSupplier)).isInstanceOf(AssertionError.class).hasMessageContaining("Expecting empty Optional");
assertThatThrownBy(() -> TextualAssertions.assertEmpty(this::emptyInputSupplier))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expecting empty Optional");
}

private Optional<String> emptyInputSupplier() {
return of(Strings.EMPTY_STRING);
return of("");
}
}

Expand All @@ -79,17 +86,23 @@ class NullInputTests {

@Test
void whenAssertingANumericalOptional_andTheInputIsNull_thenTheAssertFails() {
assertThatThrownBy(() -> TextualAssertions.assertThatAlphanumeric(Optional::empty)).isInstanceOf(AssertionError.class).hasMessageContaining("Field expected to not be empty");
assertThatThrownBy(() -> TextualAssertions.assertThatAlphanumeric(Optional::empty))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Field expected to not be empty");
}

@Test
void whenAssertingAnAlphaNumericalOptional_andTheInputIsNull_thenTheAssertFails() {
assertThatThrownBy(() -> TextualAssertions.assertThatAlphanumeric(Optional::empty)).isInstanceOf(AssertionError.class).hasMessageContaining("Field expected to not be empty");
assertThatThrownBy(() -> TextualAssertions.assertThatAlphanumeric(Optional::empty))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Field expected to not be empty");
}

@Test
void whenAssertingAnAlphaOptional_andTheInputIsNull_thenTheAssertFails() {
assertThatThrownBy(() -> TextualAssertions.assertThatAlpha(Optional::empty)).isInstanceOf(AssertionError.class).hasMessageContaining("Field expected to not be empty");
assertThatThrownBy(() -> TextualAssertions.assertThatAlpha(Optional::empty))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Field expected to not be empty");
}
}

Expand All @@ -99,17 +112,20 @@ class CorrectInputTests {

@Test
void whenAssertingANumericalOptional_andTheInputContainsOnlyDigits_thenTheAssertFails() {
assertThatCode(() -> TextualAssertions.assertThatNumber(() -> of("1000290"))).doesNotThrowAnyException();
assertThatCode(
() -> TextualAssertions.assertThatNumber(() -> of("1000290"))).doesNotThrowAnyException();
}

@Test
void whenAssertingAnAlphaNumericalOptional_andTheInputContainsLettersAndDigits_thenTheAssertFails() {
assertThatCode(() -> TextualAssertions.assertThatAlphanumeric(() -> of("abcd123jajaja"))).doesNotThrowAnyException();
assertThatCode(() -> TextualAssertions.assertThatAlphanumeric(
() -> of("abcd123jajaja"))).doesNotThrowAnyException();
}

@Test
void whenAssertingAnAlphaOptional_andTheInputIsNull_thenTheAssertFails() {
assertThatCode(() -> TextualAssertions.assertThatAlpha(() -> of("aaaaahjhjahzjahzjahzjahzj"))).doesNotThrowAnyException();
assertThatCode(() -> TextualAssertions.assertThatAlpha(
() -> of("aaaaahjhjahzjahzjahzjahzj"))).doesNotThrowAnyException();
}
}

Expand Down
61 changes: 61 additions & 0 deletions commons/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<project 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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>be.sddevelopment</groupId>
<artifactId>code-utils</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>commons</artifactId>

<dependencies>
<!-- BEGIN UTIL SCOPE -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>

<!-- BEGIN TEST SCOPE -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>
<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit-junit5-api</artifactId>
<version>${archunit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>be.sddevelopment</groupId>
<artifactId>commons-testing</artifactId>
<version>${parent.version}</version>
</dependency>
</dependencies>


</project>
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ void setUp() throws ClassNotFoundException {
*/
@Test
void givenAUtilityClassWithAPrivateDefaultConstructor_whenInstantiatingAndOverwritingAccessModifier_anExceptionIsThrown() {
ReflectionAssertionUtils.assertPrivateMemberReflectionProtection(classToTest.getDeclaredConstructors()[0]);
ReflectionAssertionUtils.assertPrivateMemberReflectionProtection(
classToTest.getDeclaredConstructors()[0]);
}

@Test
void givenAUtilityClassWithAPrivateConstructor_whenInstantiating_anIllegalExceptionIsThrown() {
assertThatThrownBy(() -> classToTest.getDeclaredConstructor().newInstance()).isInstanceOf(IllegalAccessException.class);
assertThatThrownBy(() -> classToTest.getDeclaredConstructor().newInstance()).isInstanceOf(
IllegalAccessException.class);
}

private Class<?> utilityClass() throws ClassNotFoundException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@
@DisplayNameGeneration(ReplaceUnderscoredCamelCasing.class)
class ExceptionSuppressorTest {

public final Condition<Throwable> messageOfOriginalException = new Condition<>(c -> org.apache.commons.lang3.StringUtils.equalsIgnoreCase(c.getMessage(), TestMethods.EXCEPTION_MESSAGE),
"Precondition: Throwable contains the original exception message"
);
public final Condition<Throwable> messageOfOriginalException = new Condition<>(
c -> org.apache.commons.lang3.StringUtils.equalsIgnoreCase(c.getMessage(),
TestMethods.EXCEPTION_MESSAGE
), "Precondition: Throwable contains the original exception message");

private static final class TestMethods {

Expand All @@ -82,31 +83,45 @@ static String throwExceptionIfNotBLank(String url) throws MalformedURLException

@Test
void whenAnExceptionIsSuppressed_usingDefaultSupress_thenChainInterpretsItAsEmptyResult() {
Optional<String> result = Optional.of(Strings.NON_EMPTY_STRING).map(ignore(TestMethods::throwExceptionIfNotBLank));
Optional<String> result = Optional
.of(Strings.NON_EMPTY_STRING)
.map(ignore(TestMethods::throwExceptionIfNotBLank));

assertThat(result).isNotPresent();
}

@Test
void whenAnExceptionIsNotSuppressed_usingDefaultSupress_thenChainUsesMethodReturnObject() {
Optional<String> result = Optional.of(EMPTY_STRING).map(ignore(TestMethods::throwExceptionIfNotBLank));
Optional<String> result = Optional
.of(EMPTY_STRING)
.map(ignore(TestMethods::throwExceptionIfNotBLank));

assertThat(result).contains(TestMethods.NO_THROW);
}

@Test
void whenAnExceptionIsSupressed_ByConvertingToRuntime_thenItIsThrown() {
Optional<String> nonEmptyString = Optional.of(Strings.NON_EMPTY_STRING);
assertThatThrownBy(() -> nonEmptyString.map(ExceptionSuppressor.uncheck(TestMethods::throwExceptionIfNotBLank))).isInstanceOf(WrappedException.class).hasCauseInstanceOf(MalformedURLException.class);
assertThatThrownBy(() -> nonEmptyString.map(
ExceptionSuppressor.uncheck(TestMethods::throwExceptionIfNotBLank)))
.isInstanceOf(WrappedException.class)
.hasCauseInstanceOf(MalformedURLException.class);
}

@Test
void whenAnExceptionIsSupressed_ByConvertingToRuntime_thenTheThrownExceptionContainsTheOriginalOne() {
assertThatThrownBy(() -> Optional.of(Strings.NON_EMPTY_STRING).map(ExceptionSuppressor.uncheck(TestMethods::throwExceptionIfNotBLank))).satisfies(messageOfOriginalException);
assertThatThrownBy(() -> Optional
.of(Strings.NON_EMPTY_STRING)
.map(ExceptionSuppressor.uncheck(
TestMethods::throwExceptionIfNotBLank))).satisfies(
messageOfOriginalException);
}

@Test
void whenAnExceptionIsSupressed_usingIgnoreAsOptional_givenAMethodThatThrowsAnException_TheResultIsAnEmptyOptional() {
assertThat(Optional.of(Strings.NON_EMPTY_STRING).flatMap(ExceptionSuppressor.ignoreAsOptional(TestMethods::throwExceptionIfNotBLank))).isEmpty();
assertThat(Optional
.of(Strings.NON_EMPTY_STRING)
.flatMap(ExceptionSuppressor.ignoreAsOptional(
TestMethods::throwExceptionIfNotBLank))).isEmpty();
}
}
Loading

0 comments on commit 3d874eb

Please sign in to comment.