-
-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Luigi R. Viggiano
committed
Dec 4, 2013
1 parent
9d35039
commit c5da9ec
Showing
5 changed files
with
223 additions
and
1 deletion.
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
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,53 @@ | ||
/* | ||
* Copyright (c) 2013, Luigi R. Viggiano | ||
* All rights reserved. | ||
* | ||
* This software is distributable under the BSD license. | ||
* See the terms of the BSD license in the documentation provided with this software. | ||
*/ | ||
|
||
package org.aeonbits.owner.util; | ||
|
||
import java.util.AbstractMap; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import static java.util.Arrays.asList; | ||
import static java.util.Collections.unmodifiableSet; | ||
|
||
/** | ||
* Utility class to create a Map from a single entry (key-value pair). | ||
* | ||
* <p> | ||
* Example of usage: | ||
* </p> | ||
* | ||
* <pre> | ||
* import static org.aeonbits.owner.util.EntryMap.map; | ||
* | ||
* Map<String, String> myMap = map("foo", "bar"); | ||
* | ||
* String bar = myMap.get("foo"); | ||
* </pre> | ||
* | ||
* @since 1.0.6 | ||
* @author Luigi R. Viggiano | ||
*/ | ||
public class EntryMap<K, V> extends AbstractMap<K, V> { | ||
private final Entry<K, V> entry; | ||
private final Set<Entry<K, V>> entrySet; | ||
|
||
private EntryMap(final K key, final V value) { | ||
entry = new SimpleEntry<K, V>(key, value); | ||
entrySet = unmodifiableSet(new HashSet<Entry<K, V>>(asList(entry))); | ||
} | ||
|
||
@Override | ||
public Set<Entry<K, V>> entrySet() { | ||
return entrySet; | ||
} | ||
|
||
public static <K, V> EntryMap<K, V> map(K key, V value) { | ||
return new EntryMap<K, V>(key, value); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
owner/src/main/java/org/aeonbits/owner/util/package-info.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,12 @@ | ||
/* | ||
* Copyright (c) 2013, Luigi R. Viggiano | ||
* All rights reserved. | ||
* | ||
* This software is distributable under the BSD license. | ||
* See the terms of the BSD license in the documentation provided with this software. | ||
*/ | ||
|
||
/** | ||
* Provides utility interfaces and classes. | ||
*/ | ||
package org.aeonbits.owner.util; |
134 changes: 134 additions & 0 deletions
134
owner/src/test/java/org/aeonbits/owner/variableexpansion/KeyExpansionTest.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,134 @@ | ||
/* | ||
* Copyright (c) 2013, Luigi R. Viggiano | ||
* All rights reserved. | ||
* | ||
* This software is distributable under the BSD license. | ||
* See the terms of the BSD license in the documentation provided with this software. | ||
*/ | ||
|
||
package org.aeonbits.owner.variableexpansion; | ||
|
||
import org.aeonbits.owner.Config; | ||
import org.aeonbits.owner.Config.DisableFeature; | ||
import org.aeonbits.owner.Config.Sources; | ||
import org.aeonbits.owner.ConfigFactory; | ||
import org.junit.Test; | ||
|
||
import static org.aeonbits.owner.Config.DisableableFeature.VARIABLE_EXPANSION; | ||
import static org.aeonbits.owner.util.EntryMap.map; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
|
||
/** | ||
* @author Luigi R. Viggiano | ||
*/ | ||
public class KeyExpansionTest { | ||
|
||
|
||
@Sources("classpath:org/aeonbits/owner/variableexpansion/KeyExpansionExample.xml") | ||
public interface MyConfig extends Config { | ||
@Key("servers.${env}.name") | ||
String name(); | ||
|
||
@Key("servers.${env}.hostname") | ||
String hostname(); | ||
|
||
@Key("servers.${env}.port") | ||
Integer port(); | ||
|
||
@Key("servers.${env}.user") | ||
String user(); | ||
|
||
@DisableFeature(VARIABLE_EXPANSION) | ||
@Key("servers.${env}.password") | ||
String password(); | ||
} | ||
|
||
@Test | ||
public void testKeyExpansion() { | ||
MyConfig cfg = ConfigFactory.create(MyConfig.class, map("env", "dev")); | ||
|
||
assertEquals("DEV", cfg.name()); | ||
assertEquals("devhost", cfg.hostname()); | ||
assertEquals(new Integer(6000), cfg.port()); | ||
assertEquals("myuser1", cfg.user()); | ||
assertNull(cfg.password()); // expansion is disabled on method level | ||
} | ||
|
||
@DisableFeature(VARIABLE_EXPANSION) | ||
@Sources("classpath:org/aeonbits/owner/variableexpansion/KeyExpansionExample.xml") | ||
public interface MyConfigWithExpansionDisabled extends Config { | ||
@Key("servers.${env}.name") | ||
String name(); | ||
|
||
@Key("servers.${env}.hostname") | ||
String hostname(); | ||
|
||
@Key("servers.${env}.port") | ||
Integer port(); | ||
|
||
@Key("servers.${env}.user") | ||
String user(); | ||
|
||
@Key("servers.${env}.password") | ||
String password(); | ||
} | ||
|
||
@Test | ||
public void testKeyExpansionDisabled() { | ||
MyConfigWithExpansionDisabled cfg = | ||
ConfigFactory.create(MyConfigWithExpansionDisabled.class, map("env", "dev")); | ||
|
||
assertNull(cfg.name()); | ||
assertNull(cfg.hostname()); | ||
assertNull(cfg.port()); | ||
assertNull(cfg.user()); | ||
assertNull(cfg.password()); | ||
} | ||
|
||
@Sources("classpath:org/aeonbits/owner/variableexpansion/KeyExpansionExample.xml") | ||
public interface ExpandsFromAnotherKey extends Config { | ||
|
||
@DefaultValue("dev") | ||
String env(); | ||
|
||
@Key("servers.${env}.name") | ||
String name(); | ||
|
||
@Key("servers.${env}.hostname") | ||
String hostname(); | ||
|
||
@Key("servers.${env}.port") | ||
Integer port(); | ||
|
||
@Key("servers.${env}.user") | ||
String user(); | ||
|
||
@DisableFeature(VARIABLE_EXPANSION) | ||
@Key("servers.${env}.password") | ||
String password(); | ||
} | ||
|
||
@Test | ||
public void testKeyExpansionFromAnotherKey() { | ||
ExpandsFromAnotherKey cfg = ConfigFactory.create(ExpandsFromAnotherKey.class); | ||
|
||
assertEquals("DEV", cfg.name()); | ||
assertEquals("devhost", cfg.hostname()); | ||
assertEquals(new Integer(6000), cfg.port()); | ||
assertEquals("myuser1", cfg.user()); | ||
assertNull(cfg.password()); // expansion is disabled on method level | ||
} | ||
|
||
@Test | ||
public void testKeyExpansionFromAnotherKeyWithImportOverriding() { | ||
ExpandsFromAnotherKey cfg = ConfigFactory.create(ExpandsFromAnotherKey.class, map("env", "uat")); | ||
|
||
assertEquals("UAT", cfg.name()); | ||
assertEquals("uathost", cfg.hostname()); | ||
assertEquals(new Integer(60020), cfg.port()); | ||
assertEquals("myuser2", cfg.user()); | ||
assertNull("mypass2", cfg.password()); // expansion is disabled on method level | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
owner/src/test/resources/org/aeonbits/owner/variableexpansion/KeyExpansionExample.xml
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,16 @@ | ||
<servers> | ||
<dev> | ||
<name>DEV</name> | ||
<hostname>devhost</hostname> | ||
<port>6000</port> | ||
<user>myuser1</user> | ||
<password>mypass1</password> | ||
</dev> | ||
<uat> | ||
<name>UAT</name> | ||
<hostname>uathost</hostname> | ||
<port>60020</port> | ||
<user>myuser2</user> | ||
<password>mypass2</password> | ||
</uat> | ||
</servers> |