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/05 22:56:30 UTC
svn commit: r750619 - in /incubator/etch/branches/config/services/config:
remote.yml src/main/java/org/apache/etch/services/config/YamlConfig.java
src/test/java/org/apache/etch/services/config/TestYamlConfig.java
Author: sccomer
Date: Thu Mar 5 21:56:29 2009
New Revision: 750619
URL: http://svn.apache.org/viewvc?rev=750619&view=rev
Log:
updated with more and better tests.
Modified:
incubator/etch/branches/config/services/config/remote.yml
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=750619&r1=750618&r2=750619&view=diff
==============================================================================
--- incubator/etch/branches/config/services/config/remote.yml (original)
+++ incubator/etch/branches/config/services/config/remote.yml Thu Mar 5 21:56:29 2009
@@ -8,3 +8,11 @@
mary: 2345
alice: 9876
jack: 8765
+primes:
+ - 1
+ - 2
+ - 3
+ - 5
+ - 7
+ - 11
+ - 13
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=750619&r1=750618&r2=750619&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 Thu Mar 5 21:56:29 2009
@@ -148,10 +148,7 @@
public Object getParent( Object id )
{
- Integer iid = getConf( id ).parent;
- if (iid == null)
- return null;
- return toId( iid );
+ return toId( getConf( id ).parent );
}
public String getName( Object id )
@@ -286,49 +283,23 @@
}
}
- // id might be null.
+ // id is not null
Integer iid = fromId( id );
+ // if we have a simple path just let getConfig0 handle it
+
if (path.indexOf( '/' ) < 0)
return toId( getConfig0( iid, path ) );
- if (iid != null && path.startsWith( "/" ))
- iid = null;
-
- getConf( iid );
+ // complex path
StringTokenizer st = new StringTokenizer( path, "/" );
- while (st.hasMoreTokens())
+ while (iid != null && st.hasMoreTokens())
iid = getConfig0( iid, st.nextToken() );
return toId( iid );
}
-
- private String abs2rel( String path )
- {
- if (!isAbsolute( path ))
- return path;
- return path.substring( 1 );
- }
-
- private static boolean isAbsolute( String path )
- {
- checkPath( path );
- return path.startsWith( "/" );
- }
-
- private static boolean isRelative( String path )
- {
- checkPath( path );
- return !path.startsWith( "/" );
- }
-
- private static void checkPath( String path )
- {
- if (path == null)
- throw new IllegalArgumentException( "path == null" );
- }
////////////////////////
// NODE / PATH ACCESS //
@@ -490,22 +461,61 @@
// PRIVATE //
/////////////
+ private static final int ID_RADIX = Character.MAX_RADIX;
+
+ private String abs2rel( String path )
+ {
+ if (!isAbsolute( path ))
+ return path;
+ return path.substring( 1 );
+ }
+
+ private static boolean isAbsolute( String path )
+ {
+ checkPath( path );
+ return path.startsWith( "/" );
+ }
+
+ private static boolean isRelative( String path )
+ {
+ checkPath( path );
+ return !path.startsWith( "/" );
+ }
+
+ private static void checkPath( String path )
+ {
+ if (path == null)
+ throw new IllegalArgumentException( "path == null" );
+ }
+
private final Set<Integer> subs = Collections.synchronizedSet( new HashSet<Integer>() );
private Object toId( Integer iid )
{
if (iid == null)
- throw new NullPointerException( "iid == null" );
- return iid;
+ return null;
+ return "#"+Integer.toString( 0, ID_RADIX )+"#";
}
private int fromId( Object id )
{
if (id == null)
throw new IllegalArgumentException( "id == null" );
- if (id instanceof Integer)
- return (Integer) id;
- return ((Number) id).intValue();
+ 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 ( NumberFormatException e )
+ {
+ throw new IllegalArgumentException( "id is not valid: "+id );
+ }
}
private final static int INTERVAL = 5*1000;
@@ -610,7 +620,7 @@
{
if (true) return;
- Conf c = getConf( id );
+ Conf c = getConf0( id );
Assertion.check( (parent == null && c.parent == null) ||
(parent != null && c.parent != null && parent.equals( c.parent )),
@@ -670,20 +680,20 @@
return k;
}
- private Conf getConf( int iid )
+ private Conf getConf0( int iid )
{
if (!isLoaded())
throw new IllegalStateException( "no config loaded" );
if (iid < 0 || iid >= configs.size())
- throw new IllegalArgumentException( "id < 0 || id >= configs.size()" );
+ throw new IllegalArgumentException( "id < 0 || id >= configs.size(): id = "+iid+", configs.size() = "+configs.size() );
return configs.get( iid );
}
private Conf getConf( Object id )
{
- return getConf( fromId( id ) );
+ return getConf0( fromId( id ) );
}
private Object[] toIdArray( Collection<Integer> value, Integer offset,
@@ -721,18 +731,28 @@
return a;
}
- private Integer getConfig0( Integer iid, String name )
+ private Integer getConfig0( int iid, String name )
{
- Conf c = getConf( iid );
+ if (name.length() == 0)
+ return iid;
if (name.equals( "." ))
return iid;
+ Conf c = getConf0( iid );
+
if (name.equals( ".." ))
return c.parent;
if (c.isList())
- return c.list().get( Integer.valueOf( name ) );
+ {
+ int i = Integer.valueOf( name );
+ // i might be out of range.
+ List<Integer> list = c.list();
+ if (i < 0 || i >= list.size())
+ throw new IllegalArgumentException( "i < 0 || i >= list.size()" );
+ return list.get( i );
+ }
if (c.isMap())
{
@@ -752,12 +772,6 @@
this.name = name;
this.value = value;
}
-
- public Object getValue()
- {
- // TODO Auto-generated method stub
- return null;
- }
public Integer parent;
@@ -797,6 +811,11 @@
return value != null;
}
+ public Object getValue()
+ {
+ return value;
+ }
+
public Boolean getBoolean()
{
if (value == null)
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=750619&r1=750618&r2=750619&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 Thu Mar 5 21:56:29 2009
@@ -230,27 +230,169 @@
/** @throws Exception */
@Test
- public void getParent1() throws Exception
+ public void root1() throws Exception
{
ConfigurationServer c = new YamlConfig( null, REMOTE );
-
Object r = c.getRoot();
Assert.assertNotNull( r );
- // these are euphemisms for the root node
+ // test properties on the root node
+
Assert.assertNull( c.getParent( r ) );
- Assert.assertNull( c.getParent( c.getConfigPath( r, "" ) ) );
- Assert.assertNull( c.getParent( c.getConfigPath( r, "." ) ) );
- Assert.assertNull( c.getParent( c.getConfigPath( r, "/" ) ) );
- Assert.assertNull( c.getParent( c.getConfigPath( null, "/" ) ) );
+ 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 ) );
+
+ // try absolute paths for root
+
+ Assert.assertEquals( r, c.getConfigPath( null, "/" ) ); // root
+ Assert.assertEquals( r, c.getConfigPath( -1, "/" ) ); // root
+ Assert.assertEquals( r, c.getConfigPath( "abc", "/" ) ); // root
+ Assert.assertEquals( r, c.getConfigPath( false, "/" ) ); // root
+ Assert.assertEquals( r, c.getConfigPath( r, "/" ) ); // root
+
+ // try relative paths for root
- Assert.assertEquals( r,
- c.getParent( c.getConfigPath( null, "/bool" ) ) );
- Assert.assertEquals( r,
- c.getParent( c.getConfigPath( r, "bool" ) ) );
+ Assert.assertEquals( r, c.getConfigPath( r, "" ) ); // root
+ Assert.assertEquals( r, c.getConfigPath( r, "." ) ); // root
+ Assert.assertEquals( r, c.getConfigPath( r, "./." ) ); // root
+ Assert.assertEquals( r, c.getConfigPath( r, "././." ) ); // root
+ Assert.assertEquals( r, c.getConfigPath( r, "bool/.." ) ); // root
+
+ // other relative paths
+
+ Assert.assertNull( c.getConfigPath( r, ".." ) ); // parent
+ Assert.assertNotNull( c.getConfigPath( r, "bool" ) ); // child
+
+ // bad paths
+
+ Assert.assertNull( c.getConfigPath( r, "blah" ) );
+ Assert.assertNull( c.getConfigPath( r, "blah/blah" ) );
+ Assert.assertNotNull( c.getConfigPath( r, "primes/4" ) );
+ }
+
+ /** @throws Exception */
+ @Test( expected = IllegalArgumentException.class )
+ public void root2() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ // path == null
+ c.getConfigPath( r, null );
+ }
+
+ /** @throws Exception */
+ @Test( expected = IllegalArgumentException.class )
+ public void root3() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ // path must be absolute when id == null
+ c.getConfigPath( null, "foo" );
+ }
+
+ /** @throws Exception */
+ @Test( expected = IllegalArgumentException.class )
+ public void root4() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ // id is not valid
+ c.getConfigPath( -1, "foo" );
+ }
+
+ /** @throws Exception */
+ @Test( expected = IllegalArgumentException.class )
+ public void root5() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ // id is not valid
+ c.getConfigPath( 0, "foo" );
+ }
+
+ /** @throws Exception */
+ @Test( expected = IllegalArgumentException.class )
+ public void root6() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ // id is not valid
+ c.getConfigPath( 99, "foo" );
+ }
+
+ /** @throws Exception */
+ @Test( expected = IllegalArgumentException.class )
+ public void root7() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ // id is not valid
+ c.getConfigPath( "abc", "foo" );
+ }
+
+ /** @throws Exception */
+ @Test( expected = IllegalArgumentException.class )
+ public void root8() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ // id is not valid
+ c.getConfigPath( "#abc#", "foo" );
+ }
+
+ /** @throws Exception */
+ @Test( expected = IllegalArgumentException.class )
+ public void root9() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ // id is not valid
+ c.getConfigPath( "#?#", "foo" );
+ }
+
+ /** @throws Exception */
+ @Test( expected = IllegalArgumentException.class )
+ public void root10() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
+
+ // id is not valid
+ c.getConfigPath( false, "foo" );
+ }
+
+ /** @throws Exception */
+ @Test( expected = IllegalArgumentException.class )
+ public void root11() throws Exception
+ {
+ ConfigurationServer c = new YamlConfig( null, REMOTE );
+ Object r = c.getRoot();
+ Assert.assertNotNull( r );
- Assert.assertEquals( c.getConfigPath( null, "users" ),
- c.getParent( c.getConfigPath( null, "users/fred" ) ) );
+ // i < 0 || i >= list.size()
+ c.getConfigPath( r, "primes/12" );
}
private static class MyConfigurationClient implements ConfigurationClient