Created
November 22, 2010 13:30
-
-
Save fumokmm/709971 to your computer and use it in GitHub Desktop.
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
test.value.string = 'ddd' // String | |
test.value.boolean = true // boolean | |
test.value.char = 'a' as char // char | |
test.value.byte = 0xFF as byte // byte | |
test.value.short = 10 as short // short | |
test.value.int = 100 // int | |
test.value.long = 200L // long | |
test.value.float = 1.0F // float | |
test.value.double = 9.9D as double // double | |
tree { | |
value { | |
value1 = 10 | |
value2 = 20 | |
for (i in 3..10) { | |
this."value${i}" = i * 10 | |
} | |
} | |
} |
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
class ConfigUtil { | |
def origin = null // File or Properties or String | |
ConfigObject map = null // holding ConfigObject | |
String env = null | |
ConfigUtil(File file) { | |
this.origin = file.toURL() | |
init() | |
} | |
ConfigUtil(Properties prop) { | |
this.origin = prop | |
init() | |
} | |
ConfigUtil(String str) { | |
this.origin = str | |
init() | |
} | |
void init() { | |
def conf = env ? new ConfigSlurper(env) : new ConfigSlurper() | |
this.map = conf.parse(this.origin) | |
} | |
def environments(env) { | |
this.env = env | |
init() | |
this | |
} | |
@Override | |
String toString() { | |
def sw = new StringWriter() | |
map.writeTo(sw) | |
sw.toString() | |
} | |
Properties toProperties() { | |
map.toProperties() | |
} | |
def getProperty(String name) { | |
if (map) map[name] | |
else '' | |
} | |
} |
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
class ConfigUtilTest extends GroovyTestCase { | |
void testValueString() { | |
def conf = getConf('config.groovy') | |
assertEquals('ddd', conf.test.value.string) | |
assertEquals(java.lang.String, conf.test.value.string.class) | |
} | |
void testValueBoolean() { | |
def conf = getConf('config.groovy') | |
assertEquals(true, conf.test.value.boolean) | |
assertEquals(java.lang.Boolean, conf.test.value.boolean.class) | |
} | |
void testValueCharacter() { | |
def conf = getConf('config.groovy') | |
assertEquals('a' as char, conf.test.value.char) | |
assertEquals(java.lang.Character, conf.test.value.char.class) | |
} | |
void testValueByte() { | |
def conf = getConf('config.groovy') | |
assertEquals(0xFF as byte, conf.test.value.byte) | |
assertEquals(java.lang.Byte, conf.test.value.byte.class) | |
} | |
void testValueShort() { | |
def conf = getConf('config.groovy') | |
assertEquals(10 as short, conf.test.value.short) | |
assertEquals(java.lang.Short, conf.test.value.short.class) | |
} | |
void testValueInteger() { | |
def conf = getConf('config.groovy') | |
assertEquals(100, conf.test.value.int) | |
assertEquals(java.lang.Integer, conf.test.value.int.class) | |
} | |
void testValueLong() { | |
def conf = getConf('config.groovy') | |
assertEquals(200L, conf.test.value.long) | |
assertEquals(java.lang.Long, conf.test.value.long.class) | |
} | |
void testValueFloat() { | |
def conf = getConf('config.groovy') | |
assertEquals(1.0F, conf.test.value.float) | |
assertEquals(java.lang.Float, conf.test.value.float.class) | |
} | |
void testValueDouble() { | |
def conf = getConf('config.groovy') | |
assertEquals(9.9D as double, conf.test.value.double) | |
assertEquals(java.lang.Double, conf.test.value.double.class) | |
} | |
void testTreeValue() { | |
def conf = getConf('config.groovy') | |
assertEquals(10, conf.tree.value.value1) | |
assertEquals(20, conf.tree.value.value2) | |
assertEquals(30, conf.tree.value.value3) | |
assertEquals(40, conf.tree.value.value4) | |
assertEquals(50, conf.tree.value.value5) | |
assertEquals(60, conf.tree.value.value6) | |
assertEquals(70, conf.tree.value.value7) | |
assertEquals(80, conf.tree.value.value8) | |
assertEquals(90, conf.tree.value.value9) | |
assertEquals(100, conf.tree.value.value10) | |
} | |
void testToString() { | |
assertEquals('''\ | |
test { | |
value { | |
string="ddd" | |
this."boolean"=true | |
this."char"=a | |
this."byte"=-1 | |
this."short"=10 | |
this."int"=100 | |
this."long"=200 | |
this."float"=1.0 | |
this."double"=9.9 | |
} | |
} | |
tree { | |
value { | |
value1=10 | |
value2=20 | |
value3=30 | |
value4=40 | |
value5=50 | |
value6=60 | |
value7=70 | |
value8=80 | |
value9=90 | |
value10=100 | |
} | |
} | |
''', getConf('config.groovy').toString()) | |
} | |
void testToProperties() { | |
def prop = getConf('config.groovy').toProperties() | |
// caution: The value convert to String. | |
assertEquals('ddd', prop['test.value.string']) | |
assertEquals('true', prop['test.value.boolean']) | |
assertEquals('a' as char, prop['test.value.char']) | |
assertEquals('0xFF will be -1', '-1', prop['test.value.byte']) | |
assertEquals('10', prop['test.value.short']) | |
assertEquals('100', prop['test.value.int']) | |
assertEquals('200', prop['test.value.long']) | |
assertEquals('1.0', prop['test.value.float']) | |
assertEquals('9.9', prop['test.value.double']) | |
(1..10).each { | |
assertEquals((it * 10).toString(), prop["tree.value.value$it"]) | |
} | |
} | |
def getConf(file) { | |
new ConfigUtil(new File(file)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment