Skip to content

Commit

Permalink
Merge pull request #1702 from DamnClin/simple-collections-utility
Browse files Browse the repository at this point in the history
Simple collections utility
  • Loading branch information
pascalgrimaud authored May 11, 2022
2 parents 14c7905 + 0a54170 commit e389321
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -25,14 +27,42 @@ 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)));

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)));
}
}
Original file line number Diff line number Diff line change
@@ -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 <T>
* Type of this collection
* @param collection
* input collection
* @return An immutable collection
*/
public static <T> Collection<T> immutable(Collection<T> collection) {
if (collection == null) {
return Set.of();
}

return Collections.unmodifiableCollection(collection);
}

/**
* Get an immutable set from the given set
*
* @param <T>
* Type of this set
* @param set
* input set
* @return An immutable set
*/
public static <T> Set<T> immutable(Set<T> set) {
if (set == null) {
return Set.of();
}

return Collections.unmodifiableSet(set);
}

/**
* Get an immutable set from the given list
*
* @param <T>
* Type of this list
* @param list
* input list
* @return An immutable list
*/
public static <T> List<T> immutable(List<T> list) {
if (list == null) {
return List.of();
}

return Collections.unmodifiableList(list);
}
}
Original file line number Diff line number Diff line change
@@ -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<Object> input = null;
Collection<Object> collection = {{collectionClass}}Collections.immutable(input);

assertThat(collection).isEmpty();
assertThatThrownBy(() -> collection.clear()).isExactlyInstanceOf(UnsupportedOperationException.class);
}

@Test
void shouldGetImmutableCollectionFromMutableCollection() {
Collection<String> input = new ArrayList<>();
input.add("value");
Collection<String> 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<Object> input = null;
Set<Object> set = {{collectionClass}}Collections.immutable(input);

assertThat(set).isEmpty();
assertThatThrownBy(() -> set.clear()).isExactlyInstanceOf(UnsupportedOperationException.class);
}

@Test
void shouldGetImmutableCollectionFromMutableCollection() {
Set<String> input = new HashSet<>();
input.add("value");
Set<String> 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<Object> input = null;
List<Object> list = {{collectionClass}}Collections.immutable(input);

assertThat(list).isEmpty();
assertThatThrownBy(() -> list.clear()).isExactlyInstanceOf(UnsupportedOperationException.class);
}

@Test
void shouldGetImmutableCollectionFromMutableCollection() {
List<String> input = new ArrayList<>();
input.add("value");
List<String> list = {{collectionClass}}Collections.immutable(input);

assertThat(list).containsExactly("value");
assertThatThrownBy(() -> list.clear()).isExactlyInstanceOf(UnsupportedOperationException.class);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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"));
}
}

0 comments on commit e389321

Please sign in to comment.