Skip to content

Commit

Permalink
Fix #60 Add property for Double values
Browse files Browse the repository at this point in the history
  • Loading branch information
ljacqu committed Sep 1, 2018
1 parent 494c841 commit e6817ed
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 7 deletions.
20 changes: 20 additions & 0 deletions src/main/java/ch/jalu/configme/properties/DoubleProperty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ch.jalu.configme.properties;

import ch.jalu.configme.resource.PropertyReader;

public class DoubleProperty extends BaseProperty<Double> {

public DoubleProperty(String path, double defaultValue) {
super(path, defaultValue);
}

@Override
protected Double getFromResource(PropertyReader reader) {
return reader.getDouble(getPath());
}

@Override
public Object toExportValue(Double value) {
return value;
}
}
11 changes: 11 additions & 0 deletions src/main/java/ch/jalu/configme/properties/PropertyInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ public static Property<Integer> newProperty(String path, int defaultValue) {
return new IntegerProperty(path, defaultValue);
}

/**
* Creates a new double property.
*
* @param path the property's path
* @param defaultValue the default value
* @return the created property
*/
public static Property<Double> newProperty(String path, double defaultValue) {
return new DoubleProperty(path, defaultValue);
}

/**
* Creates a new String property.
*
Expand Down
60 changes: 60 additions & 0 deletions src/test/java/ch/jalu/configme/properties/DoublePropertyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package ch.jalu.configme.properties;

import ch.jalu.configme.resource.PropertyReader;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;

/**
* Test for {@link DoubleProperty}.
*/
@RunWith(MockitoJUnitRunner.class)
public class DoublePropertyTest {

@Mock
private PropertyReader reader;

@Test
public void shouldReturnDoubleFromResource() {
// given
Property<Double> property = new DoubleProperty("test.path", 3.4);
given(reader.getDouble("test.path")).willReturn(-2508.346);

// when
double result = property.determineValue(reader);

// then
assertThat(result, equalTo(-2508.346));
}

@Test
public void shouldReturnDefaultValue() {
// given
Property<Double> property = new DoubleProperty("property.path", 5.9);
given(reader.getDouble("property.path")).willReturn(null);

// when
double result = property.determineValue(reader);

// then
assertThat(result, equalTo(5.9));
}

@Test
public void shouldReturnValueAsExportValue() {
// given
Property<Double> property = new DoubleProperty("property.path", 4);
double givenValue = 4.3456;

// when
Object exportValue = property.toExportValue(givenValue);

// then
assertThat(exportValue, equalTo(givenValue));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class PropertyInitializerTest {
public void shouldInstantiateProperties() {
assertThat(newProperty("my.path", true), instanceOf(BooleanProperty.class));
assertThat(newProperty("my.path", 12), instanceOf(IntegerProperty.class));
assertThat(newProperty("my.path", -8.4), instanceOf(DoubleProperty.class));
assertThat(newProperty("my.path", "default"), instanceOf(StringProperty.class));
assertThat(newProperty(TestEnum.class, "my.path", TestEnum.FIRST), instanceOf(EnumProperty.class));
assertThat(newListProperty("path", "default", "entries"), instanceOf(StringListProperty.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void shouldReadAllProperties() {
expected.put(TestConfiguration.VERSION_NUMBER, 2492);
expected.put(TestConfiguration.SKIP_BORING_FEATURES, false);
expected.put(TestConfiguration.BORING_COLORS, Arrays.asList("beige", "gray"));
expected.put(TestConfiguration.DUST_LEVEL, 2);
expected.put(TestConfiguration.DUST_LEVEL, 2.4);
expected.put(TestConfiguration.USE_COOL_FEATURES, true);
expected.put(TestConfiguration.COOL_OPTIONS, Arrays.asList("Dinosaurs", "Explosions", "Big trucks"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void shouldWriteMissingProperties() {
expected.put(TestConfiguration.VERSION_NUMBER, 32046);
expected.put(TestConfiguration.SKIP_BORING_FEATURES, false);
expected.put(TestConfiguration.BORING_COLORS, Collections.EMPTY_LIST);
expected.put(TestConfiguration.DUST_LEVEL, -1);
expected.put(TestConfiguration.DUST_LEVEL, -1.1);
expected.put(TestConfiguration.USE_COOL_FEATURES, false);
expected.put(TestConfiguration.COOL_OPTIONS, asList("Dinosaurs", "Explosions", "Big trucks"));
for (Map.Entry<Property<?>, Object> entry : expected.entrySet()) {
Expand Down Expand Up @@ -117,7 +117,7 @@ public void shouldProperlyExportAnyValues() {
expected.put(TestConfiguration.VERSION_NUMBER, -1337);
expected.put(TestConfiguration.SKIP_BORING_FEATURES, false);
expected.put(TestConfiguration.BORING_COLORS, asList("it's a difficult string!", "gray\nwith new lines\n"));
expected.put(TestConfiguration.DUST_LEVEL, -1);
expected.put(TestConfiguration.DUST_LEVEL, -1.1);
expected.put(TestConfiguration.USE_COOL_FEATURES, true);
expected.put(TestConfiguration.COOL_OPTIONS, Collections.EMPTY_LIST);
expected.put(properties.get(0), properties.get(0).getDefaultValue());
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/ch/jalu/configme/samples/TestConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public final class TestConfiguration implements SettingsHolder {
newListProperty("features.boring.colors");

// No comment
public static final Property<Integer> DUST_LEVEL =
newProperty("features.boring.dustLevel", -1);
public static final Property<Double> DUST_LEVEL =
newProperty("features.boring.dustLevel", -1.1);

@Comment("Enable cool features?")
public static final Property<Boolean> USE_COOL_FEATURES =
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/config-export-expected.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ features:
colors:
- beige
- gray
dustLevel: 2
dustLevel: 2.4
# Cool features
# Contains cool settings
cool:
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/config-sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ features:
colors:
- 'beige'
- 'gray'
dustLevel: 2
dustLevel: 2.4
cool:
enabled: true
options:
Expand Down

0 comments on commit e6817ed

Please sign in to comment.