-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ issue #7 ] Unit tests for configuration package (still missing
negative and classpath strategies)
- Loading branch information
agazzarini
committed
Aug 17, 2014
1 parent
d71b723
commit b0b2bba
Showing
2 changed files
with
151 additions
and
2 deletions.
There are no files selected for viewing
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
124 changes: 124 additions & 0 deletions
124
...k/src/test/java/org/gazzax/labs/jena/nosql/fwk/configuration/TestDefaultConfigurator.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,124 @@ | ||
package org.gazzax.labs.jena.nosql.fwk.configuration; | ||
|
||
import static org.gazzax.labs.jena.nosql.fwk.TestUtility.createSampleConfiguration; | ||
import static org.gazzax.labs.jena.nosql.fwk.TestUtility.randomString; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertSame; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
import static org.mockito.Matchers.anyString; | ||
|
||
import java.io.File; | ||
import java.util.Map; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Test case for {@link DefaultConfigurator}. | ||
* | ||
* @author Andrea Gazzarini | ||
* @since 1.0 | ||
*/ | ||
public class TestDefaultConfigurator { | ||
|
||
private DefaultConfigurator cut; | ||
private Configurable dummyTarget; | ||
|
||
/** | ||
* Setup fixture for this test case. | ||
*/ | ||
@Before | ||
public void setUp() { | ||
cut = new DefaultConfigurator(); | ||
dummyTarget = new Configurable() { | ||
@Override | ||
public void accept( | ||
final Configuration<Map<String, Object>> configuration) { | ||
// Nothing, this is a stupid stub. | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Just after creating the instance, the active state must correspond to | ||
* strategy one. | ||
*/ | ||
@Test | ||
public void initialStateMustBeStrategyOne() { | ||
assertSame(cut.tryWithConfigurationFile, cut.currentState); | ||
} | ||
|
||
/** | ||
* A call to getParameter cannot be issued before intialisation. | ||
*/ | ||
@Test | ||
public void callInInvalidState() { | ||
try { | ||
cut.getParameter(anyString(), anyString()); | ||
fail(); | ||
} catch (final IllegalStateException expected) { | ||
// Nothing, this is the expected behaviour. | ||
} | ||
} | ||
|
||
/** | ||
* Strategy #1 (configuration file in a system property). | ||
*/ | ||
@Test | ||
public void strategyOneSucceeds() throws Exception { | ||
final File configurationFile = File.createTempFile(randomString(), ".yaml"); | ||
final Map<String, Object> configuration = createSampleConfiguration(configurationFile); | ||
|
||
System.setProperty( | ||
DefaultConfigurator.CONFIGURATION_FILE_SYSTEM_PROPERTY, | ||
configurationFile.getAbsolutePath()); | ||
|
||
cut.configure(dummyTarget); | ||
|
||
assertSame(cut.configurationHasBeenLoaded, cut.currentState); | ||
checkConfiguration(configuration, cut.parameters); | ||
} | ||
|
||
/** | ||
* Strategy #2 (configuration directory in a system property with a fixed filename). | ||
*/ | ||
@Test | ||
public void strategyTwoSucceeds() throws Exception { | ||
final File tmpConfigurationFile = File.createTempFile(randomString(), ".yaml"); | ||
final File configurationFile = new File(tmpConfigurationFile.getParent(), DefaultConfigurator.CONFIGURATION_FILENAME); | ||
configurationFile.renameTo(configurationFile); | ||
final Map<String, Object> configuration = createSampleConfiguration(configurationFile); | ||
|
||
|
||
System.clearProperty(DefaultConfigurator.CONFIGURATION_FILE_SYSTEM_PROPERTY); | ||
System.setProperty( | ||
DefaultConfigurator.ETC_DIR_SYSTEM_PROPERTY, | ||
configurationFile.getParentFile().getAbsolutePath()); | ||
|
||
cut.configure(dummyTarget); | ||
|
||
assertSame(cut.configurationHasBeenLoaded, cut.currentState); | ||
checkConfiguration(configuration, cut.parameters); | ||
} | ||
|
||
/** | ||
* Internal method used for comparing two configurations. | ||
* | ||
* @param src the first configuration. | ||
* @param trgt the second configuration. | ||
*/ | ||
private void checkConfiguration(final Map<String, Object> src, final Map<String, Object> trgt) { | ||
assertTrue(src.size() > 1); | ||
assertEquals(src.size(), trgt.size()); | ||
|
||
for (String key : src.keySet()) { | ||
final Integer k = Integer.parseInt(key); | ||
assertTrue("Key " + key + " found in src but not in target", trgt.containsKey(k)); | ||
trgt.remove(k); | ||
} | ||
|
||
assertTrue(trgt.isEmpty()); | ||
} | ||
|
||
} |