You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by bl...@apache.org on 2003/07/11 21:27:28 UTC
cvs commit: avalon-sandbox/meta/api/src/test/org/apache/avalon/meta/info/test ReferenceDescriptorTestCase.java
bloritsch 2003/07/11 12:27:28
Modified: meta/api/src/java/org/apache/avalon/meta/info
ReferenceDescriptor.java
Added: meta/api/src/test/org/apache/avalon/meta/info/test
ReferenceDescriptorTestCase.java
Log:
Add testing for the ReferenceDescriptor and clean up construction code (one place to do things)
Revision Changes Path
1.3 +20 -42 avalon-sandbox/meta/api/src/java/org/apache/avalon/meta/info/ReferenceDescriptor.java
Index: ReferenceDescriptor.java
===================================================================
RCS file: /home/cvs/avalon-sandbox/meta/api/src/java/org/apache/avalon/meta/info/ReferenceDescriptor.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ReferenceDescriptor.java 10 Jul 2003 21:01:09 -0000 1.2
+++ ReferenceDescriptor.java 11 Jul 2003 19:27:28 -0000 1.3
@@ -85,17 +85,7 @@
*/
public ReferenceDescriptor( final String type ) throws NullPointerException
{
- int index = type.indexOf( ":" );
- if ( index > -1 )
- {
- m_classname = type.substring( 0, index );
- m_version = Version.getVersion( type.substring( index + 1 ) );
- }
- else
- {
- m_classname = type;
- m_version = Version.getVersion( "1.0.0" );
- }
+ this( parseClassname(type), parseVersion(type));
}
/**
@@ -125,6 +115,22 @@
}
}
+ private static final String parseClassname( final String type )
+ {
+ return type.substring(0, getColonIndex( type ) );
+ }
+
+ private static final int getColonIndex( final String type )
+ {
+ if ( null == type ) throw new NullPointerException( "type" );
+ return Math.min( type.length(), Math.max( 0, type.indexOf( ":" ) ) );
+ }
+
+ private static final Version parseVersion( final String type )
+ {
+ return Version.getVersion( type.substring( getColonIndex( type ) + 1) );
+ }
+
/**
* Return classname of interface this reference refers to.
*
@@ -154,7 +160,7 @@
*/
public boolean matches( final ReferenceDescriptor other )
{
- return other.getClassname().equals( getClassname() )
+ return m_classname.equals( other.m_classname )
&& other.getVersion().complies( getVersion() );
}
@@ -174,7 +180,7 @@
*/
public int hashCode()
{
- return getClassname().hashCode() + getVersion().hashCode();
+ return getClassname().hashCode() ^ getVersion().hashCode();
}
/**
@@ -202,32 +208,4 @@
return match;
}
-
- /**
- * Creation of a new reference descriptor form a supplied string.
- * The implementation looks for the classname/version delimiter
- * of ":". If no version information is found, the default 1.0.0
- * version is assigned.
- *
- * @param path the structured path in for form <classname>:<version>
- * @return the new reference descriptor
- */
- public static ReferenceDescriptor newInstance( String path )
- {
- final String type;
- final Version version;
- int index = path.indexOf( ":" );
- if ( index > -1 )
- {
- type = path.substring( 0, index );
- version = Version.getVersion( path.substring( index + 1 ) );
- }
- else
- {
- type = path;
- version = Version.getVersion( "1.0.0" );
- }
- return new ReferenceDescriptor( type, version );
- }
-
}
1.1 avalon-sandbox/meta/api/src/test/org/apache/avalon/meta/info/test/ReferenceDescriptorTestCase.java
Index: ReferenceDescriptorTestCase.java
===================================================================
/*
============================================================================
The Apache Software License, Version 1.1
============================================================================
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
must not be used to endorse or promote products derived from this software
without prior written permission. For written permission, please contact
apache@apache.org.
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation. For more information on the
Apache Software Foundation, please see <http://www.apache.org/>.
*/
package org.apache.avalon.meta.info.test;
import junit.framework.TestCase;
import org.apache.avalon.framework.Version;
import org.apache.avalon.meta.info.ReferenceDescriptor;
import org.apache.avalon.meta.info.EntryDescriptor;
import java.io.*;
/**
* ReferenceDescriptorTestCase does XYZ
*
* @author <a href="bloritsch.at.apache.org">Berin Loritsch</a>
* @version CVS $ Revision: 1.1 $
*/
public class ReferenceDescriptorTestCase extends TestCase
{
private static final String m_classname = ReferenceDescriptorTestCase.class.getName();
private static final Version m_version = Version.getVersion("1.2.3");
public ReferenceDescriptorTestCase( String name )
{
super( name );
}
public void testConstructor()
{
try
{
new ReferenceDescriptor(null, m_version);
fail("Did not throw the expected NullPointerException");
}
catch (NullPointerException npe)
{
// Success!
}
try
{
new ReferenceDescriptor( null );
fail( "Did not throw the expected NullPointerException" );
}
catch ( NullPointerException npe )
{
// Success!
}
ReferenceDescriptor ref = new ReferenceDescriptor( m_classname, m_version );
checkDescriptor( ref, m_classname, m_version );
ref = new ReferenceDescriptor( m_classname + ":3.2.1" );
checkDescriptor( ref, m_classname, Version.getVersion("3.2.1"));
}
private void checkDescriptor(ReferenceDescriptor ref, String classname, Version version)
{
assertNotNull(ref);
assertNotNull(ref.getClassname());
assertEquals(classname, ref.getClassname());
assertNotNull(ref.getVersion());
assertEquals(version, ref.getVersion());
}
public void testSerialization() throws IOException, ClassNotFoundException
{
ReferenceDescriptor entry = new ReferenceDescriptor( m_classname, m_version );
checkDescriptor( entry, m_classname, m_version );
File file = new File( "test.out" );
ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream( file ) );
oos.writeObject( entry );
oos.close();
ObjectInputStream ois = new ObjectInputStream( new FileInputStream( file ) );
ReferenceDescriptor serialized = (ReferenceDescriptor) ois.readObject();
ois.close();
file.delete();
checkDescriptor( entry, m_classname, m_version );
assertEquals( entry, serialized );
assertEquals( entry.hashCode(), serialized.hashCode() );
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org