Skip to content

Commit

Permalink
More robust default value parsing; add tests for defaultValues
Browse files Browse the repository at this point in the history
  • Loading branch information
g3force committed Aug 12, 2017
1 parent 809c1a9 commit 9e03e96
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 36 deletions.
4 changes: 4 additions & 0 deletions config/read.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration base="com.github.g3force.configurable.ConfigClass4">
<testStoredDifferent>42</testStoredDifferent>
</configuration>
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.5.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.github.g3force</groupId>
<artifactId>String2ValueConverter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,16 @@ private List<ConfigurableFieldData> read(final Class<?> clazz)
{
defValue = conf.defValueSpezis()[speziId];
}
if (!"".equals(defValue))
{
try
{
defValue = s2vConv.toString(type, s2vConv.parseString(type, defValue));
} catch (IllegalArgumentException err1)
{
log.warn("Could not convert defValue of field " + name + ": " + defValue, err1);
}
}

String value;
if (((field.getModifiers() & Modifier.STATIC) == 0) // non static field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
public class ConfigRegistration
{
@SuppressWarnings("unused")
private static final Logger log = Logger.getLogger(ConfigRegistration.class.getName());
private final Map<String, ConfigClient> configs = new LinkedHashMap<String, ConfigClient>();
private static final Logger log = Logger.getLogger(ConfigRegistration.class.getName());
private final Map<String, ConfigClient> configs = new LinkedHashMap<>();

private static String defPath = "config/";
private static String defPath = "config/";

private static List<IConfigClientsObserver> observers = new CopyOnWriteArrayList<IConfigClientsObserver>();
private static List<IConfigClientsObserver> observers = new CopyOnWriteArrayList<>();

private static final ConfigRegistration INSTANCE = new ConfigRegistration();
static final ConfigRegistration INSTANCE = new ConfigRegistration();


/**
Expand Down Expand Up @@ -99,7 +99,11 @@ public static synchronized void registerConfigClient(final ConfigClient cc)
}


private ConfigClient getConfigClient(final String key)
/**
* @param key the category
* @return the config client of given category
*/
ConfigClient getConfigClient(final String key)
{
ConfigClient cc = configs.get(key);
if (cc == null)
Expand Down
10 changes: 1 addition & 9 deletions src/main/java/log4j.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Log4j config for Sumatra
#log4j.rootLogger=ALL, console
log4j.rootLogger=ALL, console

# set a consol appender to get logs on stdout
Expand All @@ -10,11 +8,5 @@ log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{ABSOLUTE} %-5p [%t|%c{1}] %m%n
log4j.appender.console.Threshold=WARN

# set jinputs output to warn
log4j.logger.net.java.games=WARN
# set commons configuration to warn
log4j.logger.org.apache.commons.configuration=WARN
# set bluetooth to warn
log4j.logger.com.intel.bluetooth=INFO
# external bluetooth lib
log4j.logger.edu.tigers.bluetoothprotobuf=INFO
log4j.logger.org.apache.commons.configuration=WARN
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
* Author(s): Nicolai Ommer <nicolai.ommer@gmail.com>
* *********************************************************
*/
package configurable;

import com.github.g3force.configurable.ConfigRegistration;
import com.github.g3force.configurable.Configurable;
package com.github.g3force.configurable;


/**
Expand All @@ -30,6 +27,12 @@ public class ConfigClass1
@Configurable(comment = "Document this field")
static double testNoDefault = 5;

@Configurable(comment = "Document this field", defValue = "6")
static double testDefaultDifferent = 5;

@Configurable(comment = "Document this field")
static double testStoredDifferent = 5;

static
{
ConfigRegistration.registerClass("default", ConfigClass1.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
* Author(s): Nicolai Ommer <nicolai.ommer@gmail.com>
* *********************************************************
*/
package configurable;

import com.github.g3force.configurable.ConfigRegistration;
import com.github.g3force.configurable.Configurable;
package com.github.g3force.configurable;


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
* Author(s): Nicolai Ommer <nicolai.ommer@gmail.com>
* *********************************************************
*/
package configurable;

import com.github.g3force.configurable.ConfigRegistration;
import com.github.g3force.configurable.Configurable;
package com.github.g3force.configurable;


/**
Expand Down
52 changes: 52 additions & 0 deletions src/test/java/com/github/g3force/configurable/ConfigClass4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* *********************************************************
* Copyright (c) 2009 - 2016, DHBW Mannheim - Tigers Mannheim
* Project: TIGERS - Sumatra
* Date: Mar 14, 2016
* Author(s): Nicolai Ommer <nicolai.ommer@gmail.com>
* *********************************************************
*/
package com.github.g3force.configurable;


/**
* @author Nicolai Ommer <nicolai.ommer@gmail.com>
*/
public class ConfigClass4
{

@Configurable(comment = "Document this field")
static boolean testBoolFalse = false;

@Configurable(comment = "Document this field")
static boolean testBoolTrue = true;

@Configurable(comment = "Document this field")
static ETest testEnum = ETest.ONE;

@Configurable(comment = "Document this field", defValue = "TWO")
static ETest testEnumDefValue = ETest.ONE;

@Configurable(comment = "Document this field")
static double testDouble = 1;

@Configurable(comment = "Document this field", defValue = "2")
static double testDoubleWithDefault = 2;

@Configurable(comment = "Document this field", defValue = "6")
static double testDefaultDifferent = 5;

@Configurable(comment = "Document this field")
static double testStoredDifferent = 5;

static
{
ConfigRegistration.registerClass("read", ConfigClass4.class);
}

enum ETest
{
ONE,
TWO
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
* Author(s): Nicolai Ommer <nicolai.ommer@gmail.com>
* *********************************************************
*/
package configurable;
package com.github.g3force.configurable;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.within;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.github.g3force.configurable.ConfigRegistration;
import com.github.g3force.configurable.IConfigClient;
import com.github.g3force.configurable.IConfigObserver;

import configurable.ConfigClass1.ETest;
import com.github.g3force.configurable.ConfigClass1.ETest;


/**
Expand All @@ -31,17 +31,23 @@
public class ConfigurableTest
{
@SuppressWarnings("unused")
private static final Logger log = Logger.getLogger(ConfigurableTest.class.getName());
private static final Logger log = Logger.getLogger(ConfigurableTest.class.getName());


private static final String CATEGORY = "default";
private final Path configFilePath = Paths.get("config", CATEGORY + ".xml");
private static final String CATEGORY = "default";
private final Path configFilePath = Paths.get("config", CATEGORY + ".xml");


/**
*/
@Before
public void before()
{
deleteConfigFile();
}


private void deleteConfigFile()
{
try
{
Expand All @@ -57,6 +63,13 @@ public void before()
}


@After
public void after()
{
// deleteConfigFile();
}


/**
* @author Nicolai Ommer <nicolai.ommer@gmail.com>
*/
Expand Down Expand Up @@ -157,4 +170,20 @@ public void testOverride()
ConfigRegistration.overrideConfig(ConfigClass1.class, CATEGORY, "testDouble", "42");
Assert.assertEquals(42.0, ConfigClass1.testDouble, 1e-10);
}


@Test
public void testDefValue()
{
ConfigAnnotationProcessor cap = new ConfigAnnotationProcessor("read");
cap.loadClass(ConfigClass4.class, false);
assertThat(ConfigClass4.testBoolFalse).isEqualTo(false);
assertThat(ConfigClass4.testBoolTrue).isEqualTo(true);
assertThat(ConfigClass4.testEnum.name()).isEqualTo("ONE");
assertThat(ConfigClass4.testEnumDefValue.name()).isEqualTo("TWO");
assertThat(ConfigClass4.testDouble).isEqualTo(1.0, within(1e-10));
assertThat(ConfigClass4.testDoubleWithDefault).isEqualTo(2.0, within(1e-10));
assertThat(ConfigClass4.testDefaultDifferent).isEqualTo(6.0);
assertThat(ConfigClass4.testStoredDifferent).isEqualTo(42.0, within(1e-10));
}
}

0 comments on commit 9e03e96

Please sign in to comment.