leosutic 2002/10/04 02:19:11
Modified: util/src/test/org/apache/excalibur/util/test
MultiDelegateTestCase.java DelegateTestCase.java
Log:
Added tests for equality and hashCode for delegates and multidelegates.
Revision Changes Path
1.3 +77 -1
jakarta-avalon-excalibur/util/src/test/org/apache/excalibur/util/test/MultiDelegateTestCase.java
Index: MultiDelegateTestCase.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/util/src/test/org/apache/excalibur/util/test/MultiDelegateTestCase.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- MultiDelegateTestCase.java 4 Oct 2002 02:57:47 -0000 1.2
+++ MultiDelegateTestCase.java 4 Oct 2002 09:19:11 -0000 1.3
@@ -162,6 +162,82 @@
publisher.event.remove( Delegate.newDelegate (getClass(),
"myStaticEventHandler", Publisher.Listener.class) );
publisher.fireEvent ();
assertEquals (0, myStaticEventHandlerFired); // No invocations
- }
+ }
+
+ private static int myOtherStaticEventHandlerFired = 0;
+
+ public static void myOtherStaticEventHandler ()
+ {
+ myOtherStaticEventHandlerFired++;
+ }
+
+ public void testEquality () throws Exception
+ {
+ MultiDelegate event1 = Delegate.newMultiDelegate( Publisher.Listener.class
);
+ MultiDelegate event2 = Delegate.newMultiDelegate( Publisher.Listener.class
);
+
+ assertTrue (event1 == event1);
+ assertTrue (event1 != event2);
+ assertTrue (event1.equals (event1));
+ assertTrue (!event1.equals (event2));
+
+ assertTrue (event2 == event2);
+ assertTrue (event2 != event1);
+ assertTrue (event2.equals (event2));
+ assertTrue (!event2.equals (event1));
+ assertTrue (event2.hashCode () != event1.hashCode ());
+
+ MultiDelegate event = Delegate.newMultiDelegate( Publisher.Listener.class );
+ event1.add( Delegate.newDelegate (getClass(), "myStaticEventHandler",
Publisher.Listener.class) );
+ event2.add( Delegate.newDelegate (getClass(), "myOtherStaticEventHandler",
Publisher.Listener.class) );
+
+ myStaticEventHandlerFired = 0;
+ myOtherStaticEventHandlerFired = 0;
+
+ ((Publisher.Listener) event1).onEvent ();
+ assertEquals (1, myStaticEventHandlerFired);
+ ((Publisher.Listener) event2).onEvent ();
+ assertEquals (1, myOtherStaticEventHandlerFired);
+
+ myStaticEventHandlerFired = 0;
+ myOtherStaticEventHandlerFired = 0;
+
+ event.add (event1);
+ ((Publisher.Listener) event).onEvent ();
+ assertEquals (1, myStaticEventHandlerFired); // event1
+ assertEquals (0, myOtherStaticEventHandlerFired);
+
+ myStaticEventHandlerFired = 0;
+ myOtherStaticEventHandlerFired = 0;
+
+ event.add (event2);
+ ((Publisher.Listener) event).onEvent ();
+ assertEquals (1, myStaticEventHandlerFired); // event1 and event2
+ assertEquals (1, myOtherStaticEventHandlerFired);
+
+ myStaticEventHandlerFired = 0;
+ myOtherStaticEventHandlerFired = 0;
+
+ event.remove (event1);
+ ((Publisher.Listener) event).onEvent ();
+ assertEquals (0, myStaticEventHandlerFired); // event2
+ assertEquals (1, myOtherStaticEventHandlerFired);
+
+ myStaticEventHandlerFired = 0;
+ myOtherStaticEventHandlerFired = 0;
+
+ event.remove (event1); // Should have no effect
+ ((Publisher.Listener) event).onEvent ();
+ assertEquals (0, myStaticEventHandlerFired); // event2
+ assertEquals (1, myOtherStaticEventHandlerFired);
+
+ myStaticEventHandlerFired = 0;
+ myOtherStaticEventHandlerFired = 0;
+
+ event.remove (event2); // Should have effect
+ ((Publisher.Listener) event).onEvent ();
+ assertEquals (0, myStaticEventHandlerFired); // none
+ assertEquals (0, myOtherStaticEventHandlerFired);
+ }
}
1.7 +187 -121
jakarta-avalon-excalibur/util/src/test/org/apache/excalibur/util/test/DelegateTestCase.java
Index: DelegateTestCase.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/util/src/test/org/apache/excalibur/util/test/DelegateTestCase.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- DelegateTestCase.java 3 Oct 2002 15:13:56 -0000 1.6
+++ DelegateTestCase.java 4 Oct 2002 09:19:11 -0000 1.7
@@ -1,121 +1,187 @@
-/*
-
- ============================================================================
- The Apache Software License, Version 1.1
- ============================================================================
-
- Copyright (C) @year@ 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
- [EMAIL PROTECTED]
-
- 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.excalibur.util.test;
-
-import junit.framework.TestCase;
-import org.apache.excalibur.util.Delegate;
-
-
-/**
- * @author <a href="[EMAIL PROTECTED]">Berin Loritsch</a>
- */
-public class DelegateTestCase extends TestCase
-{
- private final static String MESSAGE = "message";
-
- public DelegateTestCase( String name )
- {
- super( name );
- }
-
- public String echo( String message )
- {
- return "Echo: " + message;
- }
-
- public String otherEcho( String message )
- {
- return "OtherEcho: " + message;
- }
-
- public static String staticEcho( String message )
- {
- return "static: " + message;
- }
-
- public void testDelegate()
- {
- EchoDelegate delegate = (EchoDelegate) Delegate.newDelegate( this, "echo",
EchoDelegate.class );
-
- assertTrue( delegate.echo( MESSAGE ).startsWith( "Echo: " ) );
- }
-
- public void testDifferentDelegate()
- {
- EchoDelegate delegate =
- (EchoDelegate) Delegate.newDelegate( this, "echo",
EchoDelegate.class );
- EchoDelegate otherDelegate =
- (EchoDelegate) Delegate.newDelegate( this, "otherEcho",
EchoDelegate.class );
-
- assertTrue( delegate.echo( MESSAGE ).startsWith( "Echo: " ) );
- assertTrue( otherDelegate.echo( MESSAGE ).startsWith( "OtherEcho: " ) );
- }
-
- public void testStaticDelegate()
- {
- EchoDelegate delegate = (EchoDelegate) Delegate.newDelegate( getClass(),
"staticEcho", EchoDelegate.class );
-
- assertTrue( delegate.echo( MESSAGE ).startsWith( "static: " ) );
- }
-
- protected void checkDelegate( EchoDelegate delegate )
- {
- String answer = delegate.echo( MESSAGE );
-
- assertEquals( MESSAGE, answer.substring(answer.indexOf(' ') + 1) );
- }
-
- public void testExpectedUse()
- {
- checkDelegate( (EchoDelegate) Delegate.newDelegate( this, "echo",
EchoDelegate.class ) );
- checkDelegate( (EchoDelegate) Delegate.newDelegate( this, "otherEcho",
EchoDelegate.class ) );
- checkDelegate( (EchoDelegate) Delegate.newDelegate( getClass(),
"staticEcho", EchoDelegate.class ) );
- }
-}
+/*
+
+ ============================================================================
+ The Apache Software License, Version 1.1
+ ============================================================================
+
+ Copyright (C) @year@ 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
+ [EMAIL PROTECTED]
+
+ 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.excalibur.util.test;
+
+import junit.framework.TestCase;
+import org.apache.excalibur.util.Delegate;
+
+
+/**
+ * @author <a href="[EMAIL PROTECTED]">Berin Loritsch</a>
+ */
+public class DelegateTestCase extends TestCase
+{
+ private final static String MESSAGE = "message";
+
+ public DelegateTestCase( String name )
+ {
+ super( name );
+ }
+
+ public String echo( String message )
+ {
+ return "Echo: " + message;
+ }
+
+ public String otherEcho( String message )
+ {
+ return "OtherEcho: " + message;
+ }
+
+ public static String staticEcho( String message )
+ {
+ return "static: " + message;
+ }
+
+ public void testDelegate()
+ {
+ EchoDelegate delegate = (EchoDelegate) Delegate.newDelegate( this, "echo",
EchoDelegate.class );
+
+ assertTrue( delegate.echo( MESSAGE ).startsWith( "Echo: " ) );
+ }
+
+ public void testDifferentDelegate()
+ {
+ EchoDelegate delegate =
+ (EchoDelegate) Delegate.newDelegate( this, "echo",
EchoDelegate.class );
+ EchoDelegate otherDelegate =
+ (EchoDelegate) Delegate.newDelegate( this, "otherEcho",
EchoDelegate.class );
+
+ assertTrue( delegate.echo( MESSAGE ).startsWith( "Echo: " ) );
+ assertTrue( otherDelegate.echo( MESSAGE ).startsWith( "OtherEcho: " ) );
+ }
+
+ public void testStaticDelegate()
+ {
+ EchoDelegate delegate = (EchoDelegate) Delegate.newDelegate( getClass(),
"staticEcho", EchoDelegate.class );
+
+ assertTrue( delegate.echo( MESSAGE ).startsWith( "static: " ) );
+ }
+
+ protected void checkDelegate( EchoDelegate delegate )
+ {
+ String answer = delegate.echo( MESSAGE );
+
+ assertEquals( MESSAGE, answer.substring(answer.indexOf(' ') + 1) );
+ }
+
+ public void testExpectedUse()
+ {
+ checkDelegate( (EchoDelegate) Delegate.newDelegate( this, "echo",
EchoDelegate.class ) );
+ checkDelegate( (EchoDelegate) Delegate.newDelegate( this, "otherEcho",
EchoDelegate.class ) );
+ checkDelegate( (EchoDelegate) Delegate.newDelegate( getClass(),
"staticEcho", EchoDelegate.class ) );
+ }
+
+ public void testEquality()
+ {
+ Object echo1 = Delegate.newDelegate( this, "echo", EchoDelegate.class );
+ Object echo2 = Delegate.newDelegate( this, "echo", EchoDelegate.class );
+
+ Object otherEcho1 = Delegate.newDelegate( this, "otherEcho",
EchoDelegate.class );
+ Object otherEcho2 = Delegate.newDelegate( this, "otherEcho",
EchoDelegate.class );
+
+ Object staticEcho1 = Delegate.newDelegate( getClass(), "staticEcho",
EchoDelegate.class );
+ Object staticEcho2 = Delegate.newDelegate( getClass(), "staticEcho",
EchoDelegate.class );
+
+ assertTrue( echo1.equals(echo1) );
+ assertTrue( echo1.equals(echo2) );
+ assertTrue( echo1.hashCode () == echo1.hashCode ());
+ assertTrue( echo1.hashCode () == echo2.hashCode ());
+ assertTrue( !echo1.equals(otherEcho1) );
+ assertTrue( !echo1.equals(otherEcho2) );
+ assertTrue( !echo1.equals(staticEcho1) );
+ assertTrue( !echo1.equals(staticEcho2) );
+
+ assertTrue( echo2.equals(echo1) );
+ assertTrue( echo2.equals(echo2) );
+ assertTrue( echo2.hashCode () == echo1.hashCode ());
+ assertTrue( echo2.hashCode () == echo2.hashCode ());
+ assertTrue( !echo2.equals(otherEcho1) );
+ assertTrue( !echo2.equals(otherEcho2) );
+ assertTrue( !echo2.equals(staticEcho1) );
+ assertTrue( !echo2.equals(staticEcho2) );
+
+ assertTrue( !otherEcho1.equals(echo1) );
+ assertTrue( !otherEcho1.equals(echo2) );
+ assertTrue( otherEcho1.equals(otherEcho1) );
+ assertTrue( otherEcho1.equals(otherEcho2) );
+ assertTrue( otherEcho1.hashCode () == otherEcho1.hashCode ());
+ assertTrue( otherEcho1.hashCode () == otherEcho2.hashCode ());
+ assertTrue( !otherEcho1.equals(staticEcho1) );
+ assertTrue( !otherEcho1.equals(staticEcho2) );
+
+ assertTrue( !otherEcho2.equals(echo1) );
+ assertTrue( !otherEcho2.equals(echo2) );
+ assertTrue( otherEcho2.equals(otherEcho1) );
+ assertTrue( otherEcho2.equals(otherEcho2) );
+ assertTrue( otherEcho2.hashCode () == otherEcho1.hashCode ());
+ assertTrue( otherEcho2.hashCode () == otherEcho2.hashCode ());
+ assertTrue( !otherEcho2.equals(staticEcho1) );
+ assertTrue( !otherEcho2.equals(staticEcho2) );
+
+ assertTrue( !staticEcho1.equals(echo1) );
+ assertTrue( !staticEcho1.equals(echo2) );
+ assertTrue( !staticEcho1.equals(otherEcho1) );
+ assertTrue( !staticEcho1.equals(otherEcho2) );
+ assertTrue( staticEcho1.equals(staticEcho1) );
+ assertTrue( staticEcho1.equals(staticEcho2) );
+ assertTrue( staticEcho1.hashCode () == staticEcho1.hashCode ());
+ assertTrue( staticEcho1.hashCode () == staticEcho2.hashCode ());
+
+ assertTrue( !staticEcho2.equals(echo1) );
+ assertTrue( !staticEcho2.equals(echo2) );
+ assertTrue( !staticEcho2.equals(otherEcho1) );
+ assertTrue( !staticEcho2.equals(otherEcho2) );
+ assertTrue( staticEcho2.equals(staticEcho1) );
+ assertTrue( staticEcho2.equals(staticEcho2) );
+ assertTrue( staticEcho2.hashCode () == staticEcho1.hashCode ());
+ assertTrue( staticEcho2.hashCode () == staticEcho2.hashCode ());
+ }
+}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>