You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@etch.apache.org by sc...@apache.org on 2009/03/10 22:21:45 UTC
svn commit: r752270 - in /incubator/etch/branches/config/services/config: ./
src/main/java/org/apache/etch/services/config/
src/test/java/org/apache/etch/services/config/
Author: sccomer
Date: Tue Mar 10 21:21:44 2009
New Revision: 752270
URL: http://svn.apache.org/viewvc?rev=752270&view=rev
Log:
updated, bug fixes, unit tests.
Modified:
incubator/etch/branches/config/services/config/remote.yml
incubator/etch/branches/config/services/config/src/main/java/org/apache/etch/services/config/MainConfigurationClient.java
incubator/etch/branches/config/services/config/src/main/java/org/apache/etch/services/config/YamlConfig.java
incubator/etch/branches/config/services/config/src/test/java/org/apache/etch/services/config/TestYamlConfig.java
Modified: incubator/etch/branches/config/services/config/remote.yml
URL: http://svn.apache.org/viewvc/incubator/etch/branches/config/services/config/remote.yml?rev=752270&r1=752269&r2=752270&view=diff
==============================================================================
--- incubator/etch/branches/config/services/config/remote.yml (original)
+++ incubator/etch/branches/config/services/config/remote.yml Tue Mar 10 21:21:44 2009
@@ -1,8 +1,16 @@
bool: true
+bool2: "false"
int: 23
+int2: "34"
+int3: abc
dbl: 4.5
+dbl2: "5.6"
+dbl3: 67
+dbl4: abc
str: boo
+str2: false
date: 12/19/2008
+date2: !java.util.Date 12/23/2008
users:
fred: 1234
mary: 2345
Modified: incubator/etch/branches/config/services/config/src/main/java/org/apache/etch/services/config/MainConfigurationClient.java
URL: http://svn.apache.org/viewvc/incubator/etch/branches/config/services/config/src/main/java/org/apache/etch/services/config/MainConfigurationClient.java?rev=752270&r1=752269&r2=752270&view=diff
==============================================================================
--- incubator/etch/branches/config/services/config/src/main/java/org/apache/etch/services/config/MainConfigurationClient.java (original)
+++ incubator/etch/branches/config/services/config/src/main/java/org/apache/etch/services/config/MainConfigurationClient.java Tue Mar 10 21:21:44 2009
@@ -39,7 +39,7 @@
{
// get our configUri from a local config file.
ConfigurationServer local = new YamlConfig( null, "services/config/local" );
- String configUri = local.getStringPath( null, "configUri" );
+ String configUri = local.getStringPath( local.getRoot(), "configUri" );
System.out.println( "configUri = "+configUri );
local.unloadConfig();
local = null;
@@ -52,22 +52,30 @@
System.out.println( "connected to config service" );
String name = new URL( configUri ).getUri();
- server.loadConfig( name );
+ Object root = server.loadConfig( name );
System.out.println( "loaded configuration named '"+name+"'" );
// Load a config and play with it.
- System.out.println( "bool = "+server.getBooleanPath( null, "bool" ) );
- System.out.println( "int = "+server.getIntegerPath( null, "int" ) );
- System.out.println( "dbl = "+server.getDoublePath( null, "dbl" ) );
- System.out.println( "str = "+server.getStringPath( null, "str" ) );
- System.out.println( "date = "+server.getDatePath( null, "date" ) );
+ System.out.println( "bool = "+server.getBooleanPath( root, "bool" ) );
+ System.out.println( "int = "+server.getIntegerPath( root, "int" ) );
+ System.out.println( "dbl = "+server.getDoublePath( root, "dbl" ) );
+ System.out.println( "str = "+server.getStringPath( root, "str" ) );
+ System.out.println( "date = "+server.getDatePath( root, "date" ) );
- System.out.println( "users/mary = "+server.getStringPath( null, "users/mary" ) );
+ System.out.println( "users/mary = "+server.getStringPath( root, "users/mary" ) );
- Object users = server.getConfigPath( null, "users" );
+ Object users = server.getConfigPath( root, "users" );
System.out.println( "users/mary = "+server.getStringPath( users, "mary" ) );
System.out.println( "users/alice = "+server.getStringPath( users, "alice" ) );
+ System.out.println( "primes/4 = "+server.getIntegerPath( root, "primes/4" ) );
+ System.out.println( "primes/5 = "+server.getIntegerPath( root, "primes/5" ) );
+
+ System.out.println( "paths:" );
+ for (Object o: server.listConfigIds( root, null, null ))
+ System.out.println( server.getPath( o ) );
+ System.out.println( "done." );
+
// Disconnect from the service.
server._stopAndWaitDown( 4000 );
}
Modified: incubator/etch/branches/config/services/config/src/main/java/org/apache/etch/services/config/YamlConfig.java
URL: http://svn.apache.org/viewvc/incubator/etch/branches/config/services/config/src/main/java/org/apache/etch/services/config/YamlConfig.java?rev=752270&r1=752269&r2=752270&view=diff
==============================================================================
--- incubator/etch/branches/config/services/config/src/main/java/org/apache/etch/services/config/YamlConfig.java (original)
+++ incubator/etch/branches/config/services/config/src/main/java/org/apache/etch/services/config/YamlConfig.java Tue Mar 10 21:21:44 2009
@@ -102,7 +102,7 @@
try
{
- Thread.sleep( 50 );
+ Thread.sleep( 1000 );
}
catch ( InterruptedException e )
{
@@ -479,6 +479,8 @@
// PRIVATE //
/////////////
+ ///CLOVER:OFF
+
/**
* Dumps the config space.
*/
@@ -490,12 +492,13 @@
System.out.println( "done" );
}
+ ///CLOVER:ON
+
private static final int ID_RADIX = Character.MAX_RADIX;
private String abs2rel( String path )
{
- if (!isAbsolute( path ))
- return path;
+ Assertion.check( isAbsolute( path ), "path is absolute" );
return path.substring( 1 );
}
@@ -524,17 +527,26 @@
{
if (id == null)
throw new IllegalArgumentException( "id == null" );
+
if (!(id instanceof String))
throw new IllegalArgumentException( "id is not valid: "+id );
+
String s = (String) id;
+
if (!s.startsWith( "#" ))
throw new IllegalArgumentException( "id is not valid: "+id );
+
if (!s.endsWith( "#" ))
throw new IllegalArgumentException( "id is not valid: "+id );
+
try
{
return Integer.parseInt( s.substring( 1 ).substring( 0, s.length()-2 ), ID_RADIX );
}
+ catch ( StringIndexOutOfBoundsException e )
+ {
+ throw new IllegalArgumentException( "id is not valid: "+id );
+ }
catch ( NumberFormatException e )
{
throw new IllegalArgumentException( "id is not valid: "+id );
@@ -746,9 +758,15 @@
for (int v: value)
{
if (offset > 0)
+ {
offset--;
- else
- a[i++] = toId( v );
+ continue;
+ }
+
+ if (i >= a.length)
+ break;
+
+ a[i++] = toId( v );
}
return a;
@@ -802,11 +820,15 @@
public Object value;
+ ///CLOVER:OFF
+
@Override
public String toString()
{
return String.format( "Conf name %s, value %s", name, value );
}
+
+ ///CLOVER:ON
public boolean isRoot()
{
@@ -847,61 +869,74 @@
public Boolean getBoolean()
{
- if (value == null)
- return null;
if (value instanceof Boolean)
return (Boolean) value;
+
if (value instanceof String)
return Boolean.valueOf( (String) value );
+
throw new IllegalArgumentException( "cannot convert value to Boolean" );
}
public Integer getInteger()
{
- if (value == null)
- return null;
if (value instanceof Integer)
return (Integer) value;
- if (value instanceof Number)
- return ((Number) value).intValue();
+
if (value instanceof String)
- return Integer.valueOf( (String) value );
+ {
+ try
+ {
+ return Integer.valueOf( (String) value );
+ }
+ catch ( NumberFormatException e )
+ {
+ throw new IllegalArgumentException( "cannot convert value to Integer: "+value );
+ }
+ }
+
throw new IllegalArgumentException( "cannot convert value to Integer" );
}
public Double getDouble()
{
- if (value == null)
- return null;
if (value instanceof Double)
return (Double) value;
+
if (value instanceof Number)
return ((Number) value).doubleValue();
+
if (value instanceof String)
- return Double.valueOf( (String) value );
+ {
+ try
+ {
+ return Double.valueOf( (String) value );
+ }
+ catch ( NumberFormatException e )
+ {
+ throw new IllegalArgumentException( "cannot convert value to Double: "+value );
+ }
+ }
+
throw new IllegalArgumentException( "cannot convert value to Double" );
}
public String getString()
{
- if (value == null)
- return null;
- if (value instanceof String)
- return (String) value;
- if (value instanceof Number)
- return value.toString();
if (value instanceof Boolean)
return value.toString();
+ if (value instanceof Number)
+ return value.toString();
if (value instanceof Date)
return value.toString();
+ if (value instanceof String)
+ return (String) value;
throw new IllegalArgumentException( "cannot convert value to String" );
}
@SuppressWarnings("deprecation")
public Date getDate()
{
- if (value == null)
- return null;
if (value instanceof Date)
return (Date) value;
if (value instanceof String)
@@ -911,9 +946,6 @@
public List<?> getList( Integer depth )
{
- if (value == null)
- return null;
-
if (isList())
return getList0( depth );
@@ -944,9 +976,6 @@
public Map<?, ?> getMap( Integer depth )
{
- if (value == null)
- return null;
-
if (isMap())
return getMap0( depth );
Modified: incubator/etch/branches/config/services/config/src/test/java/org/apache/etch/services/config/TestYamlConfig.java
URL: http://svn.apache.org/viewvc/incubator/etch/branches/config/services/config/src/test/java/org/apache/etch/services/config/TestYamlConfig.java?rev=752270&r1=752269&r2=752270&view=diff
==============================================================================
--- incubator/etch/branches/config/services/config/src/test/java/org/apache/etch/services/config/TestYamlConfig.java (original)
+++ incubator/etch/branches/config/services/config/src/test/java/org/apache/etch/services/config/TestYamlConfig.java Tue Mar 10 21:21:44 2009
@@ -5,6 +5,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.NoSuchElementException;
import org.apache.etch.services.config.Configuration.ConfigurationException;
import org.junit.Assert;
@@ -236,7 +237,7 @@
/** @throws Exception */
@Test
- public void root1() throws Exception
+ public void props1() throws Exception
{
ConfigurationServer c = new YamlConfig( null, REMOTE );
Object r = c.getRoot();
@@ -244,14 +245,95 @@
// test properties on the root node
- Assert.assertNull( c.getParent( r ) );
- Assert.assertEquals( "", c.getName( r ) );
- Assert.assertNull( c.getIndex( r ) );
- Assert.assertEquals( "/", c.getPath( r ) );
- Assert.assertTrue( c.isRoot( r ) );
- Assert.assertFalse( c.isList( r ) );
- Assert.assertTrue( c.isMap( r ) );
- Assert.assertEquals( 7, c.size( r ) );
+ testProps( c, r, null, "", null, "/", true, false, true, 15 );
+
+ // test properties on the users node
+
+ testProps( c, c.getConfigPath( r, "users" ), r, "users", null, "/users",
+ false, false, true, 4 );
+
+ // test properties on the primes node
+
+ testProps( c, c.getConfigPath( r, "primes" ), r, "primes", null,
+ "/primes", false, true, false, 7 );
+
+ // test properties on the primes/4 node
+
+ testProps( c, c.getConfigPath( r, "primes/4" ),
+ c.getConfigPath( r, "primes" ), "4", 4, "/primes/4", false, false,
+ false, null );
+ }
+
+ /** @throws Exception */
+ @Test( expected = NoSuchElementException.class )
+ public void props2() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ // value is not a List or a Map
+ c.size( c.getConfigPath( r, "primes/4" ) );
+ }
+
+ /** @throws Exception */
+ @Test( expected = IllegalArgumentException.class )
+ public void props3() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ // id == null
+ c.size( null );
+ }
+
+ /** @throws Exception */
+ @Test( expected = IllegalArgumentException.class )
+ public void props4() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ // id == null
+ c.size( "#" );
+ }
+
+ /** @throws Exception */
+ @Test( expected = IllegalArgumentException.class )
+ public void props5() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ // id == null
+ c.size( "#0" );
+ }
+
+ /** @throws Exception */
+ @Test( expected = IllegalStateException.class )
+ public void props6() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null );
+ // id == null
+ c.size( "#0#" );
+ }
+
+ private void testProps( ConfigurationServer c, Object node, Object parent,
+ String name, Integer index, String path, boolean isRoot, boolean isList,
+ boolean isMap, Integer size )
+ {
+ Assert.assertEquals( parent, c.getParent( node ) );
+ Assert.assertEquals( name, c.getName( node ) );
+ Assert.assertEquals( index, c.getIndex( node ) );
+ Assert.assertEquals( path, c.getPath( node ) );
+ Assert.assertEquals( isRoot, c.isRoot( node ) );
+ Assert.assertEquals( isList, c.isList( node ) );
+ Assert.assertEquals( isMap, c.isMap( node ) );
+ if (size != null)
+ Assert.assertEquals( size, c.size( node ) );
+ }
+
+ /** @throws Exception */
+ @Test
+ public void root1() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
// try absolute paths for root
@@ -402,6 +484,26 @@
}
/** @throws Exception */
+ @Test( expected = IllegalStateException.class )
+ public void root12() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null );
+ c.getRoot();
+ }
+
+ /** @throws Exception */
+ @Test( expected = NoSuchElementException.class )
+ public void root13() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ // abc
+ c.getConfigPath( r, "primes/4/abc" );
+ }
+
+ /** @throws Exception */
@Test
public void hasValuePath() throws Exception
{
@@ -436,6 +538,7 @@
Assert.assertNotNull( r );
Assert.assertEquals( true, c.getBooleanPath( r, "bool" ) );
+ Assert.assertEquals( false, c.getBooleanPath( r, "bool2" ) );
Assert.assertNull( c.getBooleanPath( r, "blah" ) );
}
@@ -472,6 +575,7 @@
Assert.assertNotNull( r );
Assert.assertEquals( 23, c.getIntegerPath( r, "int" ) );
+ Assert.assertEquals( 34, c.getIntegerPath( r, "int2" ) );
Assert.assertNull( c.getIntegerPath( r, "blah" ) );
}
@@ -500,6 +604,18 @@
}
/** @throws Exception */
+ @Test( expected = IllegalArgumentException.class )
+ public void getIntegerPath4() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ // cannot convert value to Integer
+ c.getIntegerPath( r, "int3" );
+ }
+
+ /** @throws Exception */
@Test
public void getDoublePath1() throws Exception
{
@@ -508,6 +624,8 @@
Assert.assertNotNull( r );
Assert.assertEquals( 4.5, c.getDoublePath( r, "dbl" ) );
+ Assert.assertEquals( 5.6, c.getDoublePath( r, "dbl2" ) );
+ Assert.assertEquals( 67, c.getDoublePath( r, "dbl3" ) );
Assert.assertNull( c.getDoublePath( r, "blah" ) );
}
@@ -536,6 +654,18 @@
}
/** @throws Exception */
+ @Test( expected = IllegalArgumentException.class )
+ public void getDoublePath4() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ // cannot convert value to Double
+ c.getDoublePath( r, "dbl4" );
+ }
+
+ /** @throws Exception */
@SuppressWarnings("deprecation")
@Test
public void getDatePath1() throws Exception
@@ -545,6 +675,7 @@
Assert.assertNotNull( r );
Assert.assertEquals( new Date( "12/19/2008" ), c.getDatePath( r, "date" ) );
+ Assert.assertEquals( new Date( "12/23/2008" ), c.getDatePath( r, "date2" ) );
Assert.assertNull( c.getDatePath( r, "blah" ) );
}
@@ -585,6 +716,7 @@
Assert.assertEquals( "4.5", c.getStringPath( r, "dbl" ) );
Assert.assertEquals( "boo", c.getStringPath( r, "str" ) );
Assert.assertEquals( "12/19/2008", c.getStringPath( r, "date" ) );
+ Assert.assertEquals( "Tue Dec 23 00:00:00 CST 2008", c.getStringPath( r, "date2" ) );
Assert.assertNull( c.getStringPath( r, "blah" ) );
}
@@ -715,6 +847,184 @@
c.getMapPath( r, "int", null );
}
+ /** @throws Exception */
+ @Test
+ public void getConfigIndex1() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ Object p = c.getConfigPath( r, "primes" );
+ Assert.assertEquals( 7, c.getInteger( c.getConfigIndex( p, 4 ) ) );
+ }
+
+ /** @throws Exception */
+ @Test( expected = IllegalArgumentException.class )
+ public void getConfigIndex2() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ Object p = c.getConfigPath( r, "primes" );
+ c.getConfigIndex( p, -1 );
+ }
+
+ /** @throws Exception */
+ @Test( expected = IllegalArgumentException.class )
+ public void getConfigIndex3() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ Object p = c.getConfigPath( r, "primes" );
+ c.getConfigIndex( p, 99 );
+ }
+
+ /** @throws Exception */
+ @Test( expected = IllegalArgumentException.class )
+ public void getConfigIndex4() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ Object p = c.getConfigPath( r, "primes" );
+ c.getConfigIndex( p, null );
+ }
+
+ /** @throws Exception */
+ @Test( expected = IllegalArgumentException.class )
+ public void getConfigIndex5() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ c.getConfigIndex( r, 0 );
+ }
+
+ /** @throws Exception */
+ @Test( expected = IllegalArgumentException.class )
+ public void getConfigIndex6() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ c.getConfigIndex( null, 0 );
+ }
+
+ /** @throws Exception */
+ @Test
+ public void listConfigPathIds1() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ Object[] ids = c.listConfigPathIds( r, "users", null, null );
+ Assert.assertEquals( 4, ids.length );
+ }
+
+ /** @throws Exception */
+ @Test
+ public void listConfigPathIds2() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ Object[] ids = c.listConfigPathIds( r, "primes", null, null );
+ Assert.assertEquals( 7, ids.length );
+ }
+
+ /** @throws Exception */
+ @Test( expected = NoSuchElementException.class )
+ public void listConfigPathIds3() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ // value is not a List or a Map
+ c.listConfigPathIds( r, "primes/4", null, null );
+ }
+
+ /** @throws Exception */
+ @Test
+ public void listConfigPathIds4() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ Object[] ids = c.listConfigPathIds( r, "primes", 1, 3 );
+ Assert.assertEquals( 3, ids.length );
+ }
+
+ /** @throws Exception */
+ @Test
+ public void listConfigPathIds5() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ Object[] ids = c.listConfigPathIds( r, "primes", 5, 4 );
+ Assert.assertEquals( 2, ids.length );
+ }
+
+ /** @throws Exception */
+ @Test( expected = IllegalArgumentException.class )
+ public void listConfigPathIds6() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ // offset < 0
+ c.listConfigPathIds( r, "primes", -1, 4 );
+ }
+
+ /** @throws Exception */
+ @Test
+ public void listConfigPathIds7() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ Object[] ids = c.listConfigPathIds( r, "primes", 7, 4 );
+ Assert.assertEquals( 0, ids.length );
+ }
+
+ /** @throws Exception */
+ @Test( expected = IllegalArgumentException.class )
+ public void listConfigPathIds8() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ // offset > size
+ c.listConfigPathIds( r, "primes", 8, 4 );
+ }
+
+ /** @throws Exception */
+ @Test( expected = IllegalArgumentException.class )
+ public void listConfigPathIds9() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ // count < 0
+ c.listConfigPathIds( r, "primes", 0, -1 );
+ }
+
private static class MyConfigurationClient implements ConfigurationClient
{
public void configValuesChanged( Object[] updated )