-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
1937fa4
commit d7803f6
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/main/java/ch/jalu/configme/properties/EnumSetProperty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
66
src/test/java/ch/jalu/configme/properties/EnumSetPropertyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} | ||
} |