From 0a541706f4faed17143d3cb1c16329eb12593fd7 Mon Sep 17 00:00:00 2001 From: Colin DAMON Date: Wed, 11 May 2022 18:15:56 +0200 Subject: [PATCH] Simple collections utility The main goal here is to init a Collection manipulation utility class for the project. It's always usefull to have this kind of null safe utility --- .../base/domain/JavaBaseDomainService.java | 30 +++++++ .../base/ProjectCollections.java.mustache | 65 ++++++++++++++ .../base/ProjectCollectionsTest.java.mustache | 89 +++++++++++++++++++ .../domain/JavaBaseDomainServiceTest.java | 5 +- 4 files changed, 187 insertions(+), 2 deletions(-) create mode 100644 src/main/resources/generator/server/javatool/base/ProjectCollections.java.mustache create mode 100644 src/main/resources/generator/server/javatool/base/ProjectCollectionsTest.java.mustache diff --git a/src/main/java/tech/jhipster/lite/generator/server/javatool/base/domain/JavaBaseDomainService.java b/src/main/java/tech/jhipster/lite/generator/server/javatool/base/domain/JavaBaseDomainService.java index 8eee18cad3c..e07206dc3cf 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/javatool/base/domain/JavaBaseDomainService.java +++ b/src/main/java/tech/jhipster/lite/generator/server/javatool/base/domain/JavaBaseDomainService.java @@ -5,6 +5,7 @@ import static tech.jhipster.lite.generator.project.domain.Constants.TEST_JAVA; import static tech.jhipster.lite.generator.project.domain.DefaultConfig.PACKAGE_NAME; +import tech.jhipster.lite.common.domain.WordUtils; import tech.jhipster.lite.generator.project.domain.DefaultConfig; import tech.jhipster.lite.generator.project.domain.Project; import tech.jhipster.lite.generator.project.domain.ProjectRepository; @@ -13,6 +14,7 @@ public class JavaBaseDomainService implements JavaBaseService { public static final String SOURCE = "server/javatool/base"; public static final String ERROR_DOMAIN_PATH = "error/domain"; + private static final String COMMON_DOMAIN_PATH = "common/domain"; private final ProjectRepository projectRepository; @@ -25,6 +27,12 @@ public void addJavaBase(Project project) { project.addDefaultConfig(PACKAGE_NAME); String packageNamePath = project.getPackageNamePath().orElse(DefaultConfig.PACKAGE_PATH); + addErrors(project, packageNamePath); + addCollections(project, packageNamePath); + addAnnotations(project, packageNamePath); + } + + private void addErrors(Project project, String packageNamePath) { JavaBase .errorDomainFiles() .forEach(file -> projectRepository.template(project, SOURCE, file, getPath(MAIN_JAVA, packageNamePath, ERROR_DOMAIN_PATH))); @@ -32,7 +40,29 @@ public void addJavaBase(Project project) { JavaBase .errorDomainTestFiles() .forEach(file -> projectRepository.template(project, SOURCE, file, getPath(TEST_JAVA, packageNamePath, ERROR_DOMAIN_PATH))); + } + + private void addCollections(Project project, String packageNamePath) { + String className = WordUtils.upperFirst(project.getBaseName().orElse("jhipster")); + project.addConfig("collectionClass", className); + + projectRepository.template( + project, + SOURCE, + "ProjectCollections.java", + getPath(MAIN_JAVA, packageNamePath, COMMON_DOMAIN_PATH), + className + "Collections.java" + ); + projectRepository.template( + project, + SOURCE, + "ProjectCollectionsTest.java", + getPath(TEST_JAVA, packageNamePath, COMMON_DOMAIN_PATH), + className + "CollectionsTest.java" + ); + } + private void addAnnotations(Project project, String packageNamePath) { JavaBase.annotationsFiles().forEach(file -> projectRepository.template(project, SOURCE, file, getPath(TEST_JAVA, packageNamePath))); } } diff --git a/src/main/resources/generator/server/javatool/base/ProjectCollections.java.mustache b/src/main/resources/generator/server/javatool/base/ProjectCollections.java.mustache new file mode 100644 index 00000000000..5e301310a45 --- /dev/null +++ b/src/main/resources/generator/server/javatool/base/ProjectCollections.java.mustache @@ -0,0 +1,65 @@ +package {{packageName}}.common.domain; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Set; + +/** + * Null safe utility class to manage collections + */ +public final class {{collectionClass}}Collections { + + private {{collectionClass}}Collections() {} + + /** + * Get an immutable collection from the given collection + * + * @param + * Type of this collection + * @param collection + * input collection + * @return An immutable collection + */ + public static Collection immutable(Collection collection) { + if (collection == null) { + return Set.of(); + } + + return Collections.unmodifiableCollection(collection); + } + + /** + * Get an immutable set from the given set + * + * @param + * Type of this set + * @param set + * input set + * @return An immutable set + */ + public static Set immutable(Set set) { + if (set == null) { + return Set.of(); + } + + return Collections.unmodifiableSet(set); + } + + /** + * Get an immutable set from the given list + * + * @param + * Type of this list + * @param list + * input list + * @return An immutable list + */ + public static List immutable(List list) { + if (list == null) { + return List.of(); + } + + return Collections.unmodifiableList(list); + } +} diff --git a/src/main/resources/generator/server/javatool/base/ProjectCollectionsTest.java.mustache b/src/main/resources/generator/server/javatool/base/ProjectCollectionsTest.java.mustache new file mode 100644 index 00000000000..60adff5b3d4 --- /dev/null +++ b/src/main/resources/generator/server/javatool/base/ProjectCollectionsTest.java.mustache @@ -0,0 +1,89 @@ +package {{packageName}}.common.domain; + +import static org.assertj.core.api.Assertions.*; + +import {{packageName}}.UnitTest; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +@UnitTest +class {{collectionClass}}CollectionsTest { + + @Nested + @DisplayName("Collections") + class {{collectionClass}}CollectionsCollectionsTest { + + @Test + void shouldGetEmptyImmutableCollectionFromNullCollection() { + Collection input = null; + Collection collection = {{collectionClass}}Collections.immutable(input); + + assertThat(collection).isEmpty(); + assertThatThrownBy(() -> collection.clear()).isExactlyInstanceOf(UnsupportedOperationException.class); + } + + @Test + void shouldGetImmutableCollectionFromMutableCollection() { + Collection input = new ArrayList<>(); + input.add("value"); + Collection collection = {{collectionClass}}Collections.immutable(input); + + assertThat(collection).containsExactly("value"); + assertThatThrownBy(() -> collection.clear()).isExactlyInstanceOf(UnsupportedOperationException.class); + } + } + + @Nested + @DisplayName("Set") + class {{collectionClass}}CollectionsSetTest { + + @Test + void shouldGetEmptyImmutableCollectionFromNullCollection() { + Set input = null; + Set set = {{collectionClass}}Collections.immutable(input); + + assertThat(set).isEmpty(); + assertThatThrownBy(() -> set.clear()).isExactlyInstanceOf(UnsupportedOperationException.class); + } + + @Test + void shouldGetImmutableCollectionFromMutableCollection() { + Set input = new HashSet<>(); + input.add("value"); + Set set = {{collectionClass}}Collections.immutable(input); + + assertThat(set).containsExactly("value"); + assertThatThrownBy(() -> set.clear()).isExactlyInstanceOf(UnsupportedOperationException.class); + } + } + + @Nested + @DisplayName("List") + class {{collectionClass}}CollectionsListTest { + + @Test + void shouldGetEmptyImmutableCollectionFromNullCollection() { + List input = null; + List list = {{collectionClass}}Collections.immutable(input); + + assertThat(list).isEmpty(); + assertThatThrownBy(() -> list.clear()).isExactlyInstanceOf(UnsupportedOperationException.class); + } + + @Test + void shouldGetImmutableCollectionFromMutableCollection() { + List input = new ArrayList<>(); + input.add("value"); + List list = {{collectionClass}}Collections.immutable(input); + + assertThat(list).containsExactly("value"); + assertThatThrownBy(() -> list.clear()).isExactlyInstanceOf(UnsupportedOperationException.class); + } + } +} diff --git a/src/test/java/tech/jhipster/lite/generator/server/javatool/base/domain/JavaBaseDomainServiceTest.java b/src/test/java/tech/jhipster/lite/generator/server/javatool/base/domain/JavaBaseDomainServiceTest.java index 6e90ec6e6f9..afb15d00f0d 100644 --- a/src/test/java/tech/jhipster/lite/generator/server/javatool/base/domain/JavaBaseDomainServiceTest.java +++ b/src/test/java/tech/jhipster/lite/generator/server/javatool/base/domain/JavaBaseDomainServiceTest.java @@ -1,7 +1,6 @@ package tech.jhipster.lite.generator.server.javatool.base.domain; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static tech.jhipster.lite.TestUtils.tmpProject; @@ -32,5 +31,7 @@ void shouldAddJavaBase() { javaBaseDomainService.addJavaBase(project); verify(projectRepository, times(17)).template(any(Project.class), anyString(), anyString(), anyString()); + verify(projectRepository).template(any(Project.class), anyString(), anyString(), anyString(), eq("JhipsterCollections.java")); + verify(projectRepository).template(any(Project.class), anyString(), anyString(), anyString(), eq("JhipsterCollectionsTest.java")); } }