Skip to content

Commit

Permalink
#383 Add EnumSetProperty (#441)
Browse files Browse the repository at this point in the history
* 383 -> add EnumSetProperty.java

* 383 -> code climate edits

* 383 -> edit by review comments

* 383 -> edit by review comments

* 383 -> Fix constructor call
  • Loading branch information
lengeligor authored Oct 8, 2024
1 parent 1937fa4 commit d7803f6
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/main/java/ch/jalu/configme/properties/EnumSetProperty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ch.jalu.configme.properties;

import ch.jalu.configme.properties.types.EnumSetPropertyType;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.EnumSet;
import java.util.Set;

/**
* EnumSet property.
*
* @param <E> the enum type
*/
public class EnumSetProperty<E extends Enum<E>> extends SetProperty<E> {

public EnumSetProperty(@NotNull String path, @NotNull Class<E> enumClass, @NotNull EnumSet<E> defaultValue) {
super(new EnumSetPropertyType(enumClass), path, defaultValue);
}

public EnumSetProperty(@NotNull String path, @NotNull Class<E> enumClass, @NotNull E @NotNull... defaultValue) {
super(new EnumSetPropertyType(enumClass), path, newEnumSet(enumClass, defaultValue));
}

private static <E extends Enum<E>> @NotNull Set<E> newEnumSet(@NotNull Class<E> enumClass,
E @NotNull [] defaultValue) {
EnumSet<E> enumSet = EnumSet.noneOf(enumClass);
enumSet.addAll(Arrays.asList(defaultValue));
return enumSet;
}
}
66 changes: 66 additions & 0 deletions src/test/java/ch/jalu/configme/properties/EnumSetPropertyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package ch.jalu.configme.properties;

import ch.jalu.configme.properties.convertresult.PropertyValue;
import ch.jalu.configme.resource.PropertyReader;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.EnumSet;
import java.util.Set;

import static ch.jalu.configme.TestUtils.isErrorValueOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.BDDMockito.given;

/**
* Test for {@link EnumSetProperty}.
*/
@ExtendWith(MockitoExtension.class)
class EnumSetPropertyTest {

@Mock
private PropertyReader reader;

@Test
void shouldReturnEnumSetDefaultValue() {
// given
EnumSet<TestEnum> set = EnumSet.of(TestEnum.ENTRY_A);
EnumSetProperty<TestEnum> property =
new EnumSetProperty<>("enum.path", TestEnum.class, set);
given(reader.getObject(property.getPath()))
.willReturn(null);

// when
PropertyValue<Set<TestEnum>> result = property.determineValue(reader);

// then
assertThat(result, isErrorValueOf(EnumSet.of(TestEnum.ENTRY_A)));
}

@Test
void shouldReturnEnumSetDefaultValueFromArray() {
// given
EnumSetProperty<TestEnum> property =
new EnumSetProperty<>("enum.path", TestEnum.class, new TestEnum[]{TestEnum.ENTRY_B, TestEnum.ENTRY_C});
given(reader.getObject(property.getPath()))
.willReturn(null);

// when
PropertyValue<Set<TestEnum>> result = property.determineValue(reader);

// then
assertThat(result, isErrorValueOf(EnumSet.of(TestEnum.ENTRY_B, TestEnum.ENTRY_C)));
}

private enum TestEnum {

ENTRY_A,

ENTRY_B,

ENTRY_C

}
}

0 comments on commit d7803f6

Please sign in to comment.