Skip to content

Commit d7803f6

Browse files
authored
#383 Add EnumSetProperty (#441)
* 383 -> add EnumSetProperty.java * 383 -> code climate edits * 383 -> edit by review comments * 383 -> edit by review comments * 383 -> Fix constructor call
1 parent 1937fa4 commit d7803f6

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package ch.jalu.configme.properties;
2+
3+
import ch.jalu.configme.properties.types.EnumSetPropertyType;
4+
import org.jetbrains.annotations.NotNull;
5+
6+
import java.util.Arrays;
7+
import java.util.EnumSet;
8+
import java.util.Set;
9+
10+
/**
11+
* EnumSet property.
12+
*
13+
* @param <E> the enum type
14+
*/
15+
public class EnumSetProperty<E extends Enum<E>> extends SetProperty<E> {
16+
17+
public EnumSetProperty(@NotNull String path, @NotNull Class<E> enumClass, @NotNull EnumSet<E> defaultValue) {
18+
super(new EnumSetPropertyType(enumClass), path, defaultValue);
19+
}
20+
21+
public EnumSetProperty(@NotNull String path, @NotNull Class<E> enumClass, @NotNull E @NotNull... defaultValue) {
22+
super(new EnumSetPropertyType(enumClass), path, newEnumSet(enumClass, defaultValue));
23+
}
24+
25+
private static <E extends Enum<E>> @NotNull Set<E> newEnumSet(@NotNull Class<E> enumClass,
26+
E @NotNull [] defaultValue) {
27+
EnumSet<E> enumSet = EnumSet.noneOf(enumClass);
28+
enumSet.addAll(Arrays.asList(defaultValue));
29+
return enumSet;
30+
}
31+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package ch.jalu.configme.properties;
2+
3+
import ch.jalu.configme.properties.convertresult.PropertyValue;
4+
import ch.jalu.configme.resource.PropertyReader;
5+
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.api.extension.ExtendWith;
7+
import org.mockito.Mock;
8+
import org.mockito.junit.jupiter.MockitoExtension;
9+
10+
import java.util.EnumSet;
11+
import java.util.Set;
12+
13+
import static ch.jalu.configme.TestUtils.isErrorValueOf;
14+
import static org.hamcrest.MatcherAssert.assertThat;
15+
import static org.mockito.BDDMockito.given;
16+
17+
/**
18+
* Test for {@link EnumSetProperty}.
19+
*/
20+
@ExtendWith(MockitoExtension.class)
21+
class EnumSetPropertyTest {
22+
23+
@Mock
24+
private PropertyReader reader;
25+
26+
@Test
27+
void shouldReturnEnumSetDefaultValue() {
28+
// given
29+
EnumSet<TestEnum> set = EnumSet.of(TestEnum.ENTRY_A);
30+
EnumSetProperty<TestEnum> property =
31+
new EnumSetProperty<>("enum.path", TestEnum.class, set);
32+
given(reader.getObject(property.getPath()))
33+
.willReturn(null);
34+
35+
// when
36+
PropertyValue<Set<TestEnum>> result = property.determineValue(reader);
37+
38+
// then
39+
assertThat(result, isErrorValueOf(EnumSet.of(TestEnum.ENTRY_A)));
40+
}
41+
42+
@Test
43+
void shouldReturnEnumSetDefaultValueFromArray() {
44+
// given
45+
EnumSetProperty<TestEnum> property =
46+
new EnumSetProperty<>("enum.path", TestEnum.class, new TestEnum[]{TestEnum.ENTRY_B, TestEnum.ENTRY_C});
47+
given(reader.getObject(property.getPath()))
48+
.willReturn(null);
49+
50+
// when
51+
PropertyValue<Set<TestEnum>> result = property.determineValue(reader);
52+
53+
// then
54+
assertThat(result, isErrorValueOf(EnumSet.of(TestEnum.ENTRY_B, TestEnum.ENTRY_C)));
55+
}
56+
57+
private enum TestEnum {
58+
59+
ENTRY_A,
60+
61+
ENTRY_B,
62+
63+
ENTRY_C
64+
65+
}
66+
}

0 commit comments

Comments
 (0)