Skip to content

Commit

Permalink
#318 PropertyBuilder: account for the fact that some properties might…
Browse files Browse the repository at this point in the history
… be null even if we don't expect them to be
  • Loading branch information
ljacqu committed Jun 20, 2023
1 parent 3f68392 commit 00f0724
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
15 changes: 10 additions & 5 deletions src/main/java/ch/jalu/configme/properties/PropertyBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ch.jalu.configme.properties.inlinearray.InlineArrayConverter;
import ch.jalu.configme.properties.types.PropertyType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.LinkedHashMap;
Expand All @@ -25,14 +26,14 @@ public abstract class PropertyBuilder<K, T, B extends PropertyBuilder<K, T, B>>

private String path;
private T defaultValue;
private PropertyType<K> type;
private final PropertyType<K> type;

/**
* Constructor.
*
* @param type the property type
*/
public PropertyBuilder(@NotNull PropertyType<K> type) {
public PropertyBuilder(@Nullable PropertyType<K> type) {
this.type = type;
}

Expand Down Expand Up @@ -65,15 +66,19 @@ public PropertyBuilder(@NotNull PropertyType<K> type) {
*/
public abstract @NotNull Property<T> build();

protected final @NotNull String getPath() {

// Note: these getters are @Nullable because they may be null only in some implementations, or because a caller
// might have forgotten to set a value (e.g. the path). Some values are never valid as null, but _can_ be null here.

protected final @Nullable String getPath() {
return path;
}

protected final @NotNull T getDefaultValue() {
protected final @Nullable T getDefaultValue() {
return defaultValue;
}

protected final @NotNull PropertyType<K> getType() {
protected final @Nullable PropertyType<K> getType() {
return type;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.Set;

import static ch.jalu.configme.TestUtils.getExceptionTypeForNullArg;
import static ch.jalu.configme.TestUtils.getExceptionTypeForNullField;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anEmptyMap;
import static org.hamcrest.Matchers.arrayContaining;
Expand Down Expand Up @@ -55,7 +54,7 @@ void shouldCreateCommonPropertyWithCustomFunction() {
@Test
void shouldThrowForMissingPathInCommonPropertyBuilder() {
// given / when
Exception e = assertThrows(getExceptionTypeForNullField(),
Exception e = assertThrows(getExceptionTypeForNullArg(),
() -> new PropertyBuilder.TypeBasedPropertyBuilder<>(PrimitivePropertyType.STRING)
.defaultValue("Hello")
.build());
Expand All @@ -69,7 +68,7 @@ void shouldThrowForMissingPathInCommonPropertyBuilder() {
@Test
void shouldThrowForMissingDefaultValueInCommonPropertyBuilder() {
// given / when
Exception e = assertThrows(getExceptionTypeForNullField(),
Exception e = assertThrows(getExceptionTypeForNullArg(),
() -> new PropertyBuilder.TypeBasedPropertyBuilder<>(PrimitivePropertyType.STRING)
.path("a.path")
.createFunction((path, val, type) -> new StringProperty(path, val))
Expand Down Expand Up @@ -127,7 +126,7 @@ void shouldCreateMapPropertyWithEmptyMapAsDefault() {
@Test
void shouldThrowForMissingPathInMapBuilder() {
// given / when
Exception e = assertThrows(getExceptionTypeForNullField(),
Exception e = assertThrows(getExceptionTypeForNullArg(),
() -> new PropertyBuilder.MapPropertyBuilder<>(PrimitivePropertyType.DOUBLE).build());

// then
Expand Down Expand Up @@ -195,7 +194,7 @@ void shouldCreateSetProperty() {
@Test
void shouldThrowForMissingDefaultValue() {
// given / when / then
assertThrows(getExceptionTypeForNullField(),
assertThrows(getExceptionTypeForNullArg(),
() -> new PropertyBuilder.ListPropertyBuilder<>(PrimitivePropertyType.DOUBLE)
.path("defined.path")
.build());
Expand Down

0 comments on commit 00f0724

Please sign in to comment.