You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by da...@apache.org on 2002/05/09 07:09:04 UTC
cvs commit: jakarta-ant-myrmidon/container/src/test/org/apache/myrmidon/components/type/test DefaultTypeManagerTestCase.java
darrell 02/05/08 22:09:04
Modified: container/src/java/org/apache/myrmidon/components/type
NamespaceAwareTypeFactory.java Resources.properties
TypeName.java
container/src/test/org/apache/myrmidon/components/type/test
DefaultTypeManagerTestCase.java
Log:
* Namespaces now allow internal '.' characters.
* Check for ambiguity in unqualified type names (_was_ returning first found)
Revision Changes Path
1.2 +33 -18 jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/type/NamespaceAwareTypeFactory.java
Index: NamespaceAwareTypeFactory.java
===================================================================
RCS file: /home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/type/NamespaceAwareTypeFactory.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- NamespaceAwareTypeFactory.java 8 May 2002 04:10:26 -0000 1.1
+++ NamespaceAwareTypeFactory.java 9 May 2002 05:09:04 -0000 1.2
@@ -24,7 +24,7 @@
* searched for a type matching the type name.
*
* @author <a href="mailto:darrell@apache.org">Darrell DeBoer</a>
- * @version $Revision: 1.1 $ $Date: 2002/05/08 04:10:26 $
+ * @version $Revision: 1.2 $ $Date: 2002/05/09 05:09:04 $
*/
class NamespaceAwareTypeFactory
implements TypeFactory
@@ -142,15 +142,17 @@
public boolean canCreate( String name )
{
TypeName qname = parseName( name );
- TypeFactory factory = findFactory( qname );
- if( factory != null )
- {
- return true;
- }
- else
- {
- return false;
+ try {
+ TypeFactory factory = findFactory( qname );
+
+ if( factory != null )
+ {
+ return true;
+ }
}
+ catch( TypeException exc ) {}
+
+ return false;
}
/**
@@ -182,8 +184,10 @@
* @param qname The qualified TypeName to search for.
* @return The TypeFactory which is able to create the named type,
* or <code>null</code> if none can be found.
+ * @throws TypeException If an error occurs finding the factory.
*/
private TypeFactory findFactory( TypeName qname )
+ throws TypeException
{
TypeFactory factory = findLocalFactoryUseCache( qname );
@@ -201,8 +205,10 @@
* @param qname The qualified TypeName to search for.
* @return The TypeFactory which is able to create the named type,
* or <code>null</code> if none can be found.
+ * @throws TypeException If the name cannot be resolved unambiguously.
*/
private TypeFactory findLocalFactoryUseCache( TypeName qname )
+ throws TypeException
{
if ( m_typeFactoryCache.containsKey( qname ) )
{
@@ -225,36 +231,45 @@
* @param qname The TypeName to search for.
* @return The TypeFactory which is able to create the named type,
* or <code>null</code> if none can be found.
+ * @throws TypeException If the name cannot be resolved unambiguously.
*/
private TypeFactory findLocalFactory( TypeName qname )
+ throws TypeException
{
- TypeFactory factory;
+ TypeFactory factory = null;
if( qname.hasNamespace() )
{
String namespace = qname.getNamespace();
- factory = (TypeFactory)m_namespaceFactories.get( namespace );
- if( factory != null && factory.canCreate( qname.getShortName() ) )
+ TypeFactory tryFactory = (TypeFactory)m_namespaceFactories.get( namespace );
+ if( tryFactory != null && tryFactory.canCreate( qname.getShortName() ) )
{
- return factory;
+ factory = tryFactory;
}
}
else
{
// Search all namespaces
- // TODO: make sure that this lookup is not ambiguous.
+ String shortName = qname.getShortName();
Iterator factories = m_namespaceFactories.values().iterator();
while( factories.hasNext() )
{
- factory = (TypeFactory)factories.next();
- if( factory.canCreate( qname.getShortName() ) )
+ TypeFactory tryFactory = (TypeFactory)factories.next();
+ if( tryFactory.canCreate( shortName ) )
{
- return factory;
+ // If we've already found a factory, the name is ambiguous.
+ if ( factory != null )
+ {
+ String message = REZ.getString( "ambiguous-type-name.error",
+ shortName );
+ throw new TypeException( message );
+ }
+ factory = tryFactory;
}
}
}
- return null;
+ return factory;
}
protected TypeName parseName( String name )
1.6 +2 -1 jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/type/Resources.properties
Index: Resources.properties
===================================================================
RCS file: /home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/type/Resources.properties,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- Resources.properties 8 May 2002 04:10:26 -0000 1.5
+++ Resources.properties 9 May 2002 05:09:04 -0000 1.6
@@ -9,4 +9,5 @@
no-work-interface.error=Role {0} does not specify accessible work interface.
# NamespaceAwareTypeFactory
-invalid-type-name.error=Invalid type name '{0}'. Type names may not contain the namespace separator character '{1}'.
+invalid-type-name.error=Invalid type name "{0}". Type names may not contain the namespace separator character "{1}".
+ambiguous-type-name.error=Could not locate a factory for type name "{0}", as it is ambiguous.
\ No newline at end of file
1.2 +7 -2 jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/type/TypeName.java
Index: TypeName.java
===================================================================
RCS file: /home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/type/TypeName.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TypeName.java 8 May 2002 04:10:26 -0000 1.1
+++ TypeName.java 9 May 2002 05:09:04 -0000 1.2
@@ -14,7 +14,7 @@
* it's namespace and type-name components.
*
* @author <a href="mailto:darrell@apache.org">Darrell DeBoer</a>
- * @version $Revision: 1.1 $ $Date: 2002/05/08 04:10:26 $
+ * @version $Revision: 1.2 $ $Date: 2002/05/09 05:09:04 $
*/
final class TypeName
{
@@ -35,7 +35,7 @@
}
m_fullName = typeName;
- int pos = typeName.indexOf( TypeManager.NAMESPACE_SEPARATOR );
+ int pos = typeName.lastIndexOf( TypeManager.NAMESPACE_SEPARATOR );
if( pos == -1 )
{
m_shortName = typeName;
@@ -70,6 +70,11 @@
boolean hasNamespace()
{
return ( m_namespace != null );
+ }
+
+ public String toString()
+ {
+ return m_fullName;
}
public int hashCode()
1.2 +37 -5 jakarta-ant-myrmidon/container/src/test/org/apache/myrmidon/components/type/test/DefaultTypeManagerTestCase.java
Index: DefaultTypeManagerTestCase.java
===================================================================
RCS file: /home/cvs/jakarta-ant-myrmidon/container/src/test/org/apache/myrmidon/components/type/test/DefaultTypeManagerTestCase.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DefaultTypeManagerTestCase.java 8 May 2002 04:10:27 -0000 1.1
+++ DefaultTypeManagerTestCase.java 9 May 2002 05:09:04 -0000 1.2
@@ -20,7 +20,7 @@
* Test cases for the DefaultTypeManager
*
* @author <a href="mailto:darrell@apache.org">Darrell DeBoer</a>
- * @version $Revision: 1.1 $ $Date: 2002/05/08 04:10:27 $
+ * @version $Revision: 1.2 $ $Date: 2002/05/09 05:09:04 $
*/
public class DefaultTypeManagerTestCase
extends AbstractComponentTest
@@ -31,7 +31,7 @@
private static final String UNKNOWN_ROLE = "unknown-role";
private static final String UNKNOWN_TYPE = "unknown-type";
- private static final String TEST_NAMESPACE = "test-namespace";
+ private static final String TEST_NAMESPACE = "test.namespace";
private static final String TEST_ROLE = TestRole.ROLE;
private static final String TYPE_NAME1 = "test-type1";
@@ -281,6 +281,25 @@
assertCreate( child, TEST_ROLE, TYPE_NAME1, TYPE_CLASS2 );
}
+ public void testAmbiguousLookup() throws Exception
+ {
+ final String ns1 = "ns1";
+ final String ns2 = "ns2";
+
+ // Register 2 types with the same name, but different namespaces.
+ registerType( m_typeManager, TEST_ROLE, ns1, TYPE_NAME1, TYPE_CLASS1 );
+ registerType( m_typeManager, TEST_ROLE, ns2, TYPE_NAME1, TYPE_CLASS2 );
+
+ // Check that we can create using the qnames.
+ assertCreate( m_typeManager, TEST_ROLE, makeQName( ns1, TYPE_NAME1 ), TYPE_CLASS1 );
+ assertCreate( m_typeManager, TEST_ROLE, makeQName( ns2, TYPE_NAME1 ), TYPE_CLASS2 );
+
+ // Try to create with short name (ambiguous)
+ String message = REZ.getString( "ambiguous-type-name.error",
+ TYPE_NAME1 );
+ assertCantCreate( m_typeManager, TEST_ROLE, TYPE_NAME1, message );
+ }
+
/**
* Combines the namespace and the typename, separated by the namespace separator.
*/
@@ -324,7 +343,7 @@
{
TypeFactory factory = typeManager.getFactory( roleName );
boolean canCreate = factory.canCreate( typeName );
- assertTrue( "Could not create", canCreate );
+ assertTrue( "Could not create: " + typeName, canCreate );
Object created = factory.create( typeName );
assertTrue( "Wrong class created", expectedClass.isInstance( created ) );
@@ -339,6 +358,20 @@
final String typeName )
throws Exception
{
+ final String message = REZ.getString( "no-factory.error", typeName );
+ assertCantCreate( typeManager, roleName, typeName, message );
+ }
+
+ /**
+ * Checks that the named type cannot be created by the TypeManager provided,
+ * failing with the specified error message.
+ */
+ private void assertCantCreate( final TypeManager typeManager,
+ final String roleName,
+ final String typeName,
+ final String errorMessage )
+ throws TypeException
+ {
TypeFactory factory = typeManager.getFactory( roleName );
assertTrue( !factory.canCreate( typeName ) );
@@ -349,8 +382,7 @@
}
catch( TypeException te )
{
- String message = REZ.getString( "no-factory.error", typeName );
- assertSameMessage( message, te );
+ assertSameMessage( errorMessage, te );
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>