[hibernate-commits] Hibernate SVN: r14065 - in core/branches/Branch_3_2: test/org/hibernate/test and 1 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Fri Oct 5 21:52:08 EDT 2007


Author: gbadner
Date: 2007-10-05 21:52:08 -0400 (Fri, 05 Oct 2007)
New Revision: 14065

Added:
   core/branches/Branch_3_2/test/org/hibernate/test/cfg/ListenerTest.java
Modified:
   core/branches/Branch_3_2/src/org/hibernate/cfg/Configuration.java
   core/branches/Branch_3_2/test/org/hibernate/test/AllTests.java
Log:
HHH-1593 - StackOverflow when calling configuration.setListener(null)


Modified: core/branches/Branch_3_2/src/org/hibernate/cfg/Configuration.java
===================================================================
--- core/branches/Branch_3_2/src/org/hibernate/cfg/Configuration.java	2007-10-05 13:54:14 UTC (rev 14064)
+++ core/branches/Branch_3_2/src/org/hibernate/cfg/Configuration.java	2007-10-06 01:52:08 UTC (rev 14065)
@@ -1653,31 +1653,41 @@
 		setListeners( type, new String[]{impl} );
 	}
 
+	public void setListener(String type, String listener) {
+		String[] listeners = null;
+		if ( listener != null ) {
+			listeners = (String[]) Array.newInstance( String.class, 1 );
+			listeners[0] = listener;
+		}
+		setListeners( type, listeners );
+	}
+
 	public void setListeners(String type, String[] listenerClasses) {
-		Object[] listeners = (Object[]) Array.newInstance( eventListeners.getListenerClassFor(type), listenerClasses.length );
-		for ( int i = 0; i < listeners.length ; i++ ) {
-			try {
-				listeners[i] = ReflectHelper.classForName( listenerClasses[i] ).newInstance();
+		Object[] listeners = null;
+		if ( listenerClasses != null ) {
+			listeners = (Object[]) Array.newInstance( eventListeners.getListenerClassFor(type), listenerClasses.length );
+			for ( int i = 0; i < listeners.length ; i++ ) {
+				try {
+					listeners[i] = ReflectHelper.classForName( listenerClasses[i] ).newInstance();
+				}
+				catch (Exception e) {
+					throw new MappingException(
+							"Unable to instantiate specified event (" + type + ") listener class: " + listenerClasses[i],
+							e
+						);
+				}
 			}
-			catch (Exception e) {
-				throw new MappingException(
-						"Unable to instantiate specified event (" + type + ") listener class: " + listenerClasses[i],
-						e
-					);
-			}
 		}
 		setListeners( type, listeners );
 	}
 
 	public void setListener(String type, Object listener) {
-		if ( listener == null ) {
-			setListener( type, null );
-		}
-		else {
-			Object[] listeners = (Object[]) Array.newInstance( eventListeners.getListenerClassFor(type), 1 );
+		Object[] listeners = null;
+		if ( listener != null ) {
+			listeners = (Object[]) Array.newInstance( eventListeners.getListenerClassFor(type), 1 );
 			listeners[0] = listener;
-			setListeners( type, listeners );
 		}
+		setListeners( type, listeners );
 	}
 
 	public void setListeners(String type, Object[] listeners) {
@@ -1916,7 +1926,7 @@
 			}
 		}
 		else {
-			log.warn( "Unrecognized listener type [" + type + "]" );
+			throw new MappingException("Unrecognized listener type [" + type + "]");
 		}
 	}
 

Modified: core/branches/Branch_3_2/test/org/hibernate/test/AllTests.java
===================================================================
--- core/branches/Branch_3_2/test/org/hibernate/test/AllTests.java	2007-10-05 13:54:14 UTC (rev 14064)
+++ core/branches/Branch_3_2/test/org/hibernate/test/AllTests.java	2007-10-06 01:52:08 UTC (rev 14065)
@@ -21,6 +21,7 @@
 import org.hibernate.test.bytecode.BytecodeSuite;
 import org.hibernate.test.cache.CacheSuite;
 import org.hibernate.test.cascade.RefreshTest;
+import org.hibernate.test.cfg.ListenerTest;
 import org.hibernate.test.cid.CompositeIdTest;
 import org.hibernate.test.collection.CollectionSuite;
 import org.hibernate.test.component.ComponentSuite;
@@ -259,6 +260,7 @@
 			suite.addTest( WhereTest.suite() );
 			suite.addTest( IterateTest.suite() );
 			suite.addTest( RefreshTest.suite() );
+			suite.addTest( ListenerTest.suite() );
 			suite.addTest( ExtraLazyTest.suite() );
 			suite.addTest( StatsTest.suite() );
 			suite.addTest( SessionStatsTest.suite() );

Added: core/branches/Branch_3_2/test/org/hibernate/test/cfg/ListenerTest.java
===================================================================
--- core/branches/Branch_3_2/test/org/hibernate/test/cfg/ListenerTest.java	                        (rev 0)
+++ core/branches/Branch_3_2/test/org/hibernate/test/cfg/ListenerTest.java	2007-10-06 01:52:08 UTC (rev 14065)
@@ -0,0 +1,313 @@
+package org.hibernate.test.cfg;
+
+import java.util.Set;
+
+import junit.framework.Test;
+
+import org.hibernate.HibernateException;
+import org.hibernate.MappingException;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.event.DeleteEvent;
+import org.hibernate.event.DeleteEventListener;
+import org.hibernate.event.def.DefaultDeleteEventListener;
+import org.hibernate.junit.UnitTestCase;
+import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
+
+/**
+ * {@inheritDoc}
+ *
+ * @author Gail Badner
+ */
+public class ListenerTest extends UnitTestCase {
+
+	public static class InvalidListenerForTest {
+	}
+
+	public static class DeleteListenerForTest implements DeleteEventListener {
+		public void onDelete(DeleteEvent event) throws HibernateException {
+		}
+
+		public void onDelete(DeleteEvent event, Set transientEntities) throws HibernateException {
+		}
+	}
+
+	public static class AnotherDeleteListenerForTest implements DeleteEventListener {
+		public void onDelete(DeleteEvent event) throws HibernateException {
+		}
+
+		public void onDelete(DeleteEvent event, Set transientEntities) throws HibernateException {
+		}
+	}
+
+	public ListenerTest(String string) {
+		super( string );
+	}
+
+	public static Test suite() {
+		return new FunctionalTestClassTestSuite( ListenerTest.class );
+	}
+
+	public void testSetListenerNullClass() {
+		Configuration cfg = new Configuration();
+		assertNotNull( cfg.getEventListeners().getDeleteEventListeners() );
+		cfg.setListener( "delete", null );
+		assertEquals( 0, cfg.getEventListeners().getDeleteEventListeners().length );
+	}
+
+	public void testSetListenersNullClass() {
+		Configuration cfg = new Configuration();
+		assertNotNull( cfg.getEventListeners().getDeleteEventListeners() );
+		cfg.setListeners( "delete", null );
+		assertEquals( 0, cfg.getEventListeners().getDeleteEventListeners().length );
+	}
+
+	public void testSetListenerEmptyClassNameArray() {
+		Configuration cfg = new Configuration();
+		assertNotNull( cfg.getEventListeners().getDeleteEventListeners() );
+		try {
+			cfg.setListener( "delete", new String[] { } );
+			fail( "should have thrown java.lang.ArrayStoreException" );
+		}
+		catch ( ArrayStoreException ex ) {
+			// expected
+		}
+	}
+
+	public void testSetListenersEmptyClassNsmeArray() {
+		Configuration cfg = new Configuration();
+		assertNotNull( cfg.getEventListeners().getDeleteEventListeners() );
+		cfg.setListeners( "delete", new String[] { } );
+		assertEquals( 0, cfg.getEventListeners().getDeleteEventListeners().length );
+	}
+
+	public void testSetListenerEmptyClassObjectArray() {
+		Configuration cfg = new Configuration();
+		assertNotNull( cfg.getEventListeners().getDeleteEventListeners() );
+		try {
+			cfg.setListener( "delete", new Object[] { } );
+			fail( "should have thrown java.lang.ArrayStoreException" );
+		}
+		catch ( ArrayStoreException ex ) {
+			// expected
+		}
+	}
+
+	public void testSetListenersEmptyClassObjectArray() {
+		Configuration cfg = new Configuration();
+		assertNotNull( cfg.getEventListeners().getDeleteEventListeners() );
+		try {
+			cfg.setListeners( "delete", new Object[] { } );
+			fail( "should have thrown ClassCastException" );
+		}
+		catch ( ClassCastException ex ) {
+			// expected
+		}
+	}
+
+	public void testSetListenerEmptyClassArray() {
+		Configuration cfg = new Configuration();
+		assertNotNull( cfg.getEventListeners().getDeleteEventListeners() );
+		try {
+			cfg.setListener( "delete", new DefaultDeleteEventListener[] { } );
+			fail( "should have thrown java.lang.ArrayStoreException" );
+		}
+		catch ( ArrayStoreException ex ) {
+			// expected
+		}
+	}
+
+	public void testSetListenersEmptyClassArray() {
+		Configuration cfg = new Configuration();
+		assertNotNull( cfg.getEventListeners().getDeleteEventListeners() );
+		cfg.setListeners( "delete", new DefaultDeleteEventListener[] { } );
+		assertEquals( 0, cfg.getEventListeners().getDeleteEventListeners().length );
+	}
+
+	public void testSetListenerUnknownClassName() {
+		Configuration cfg = new Configuration();
+		assertNotNull( cfg.getEventListeners().getDeleteEventListeners() );
+		try {
+			cfg.setListener( "delete", "UnknownClassName" );
+			fail( "should have thrown MappingException" );
+		}
+		catch ( MappingException ex ) {
+			// expected
+		}
+	}
+
+	public void testSetListenersUnknownClassName() {
+		Configuration cfg = new Configuration();
+		assertNotNull( cfg.getEventListeners().getDeleteEventListeners() );
+		try {
+			cfg.setListeners( "delete", new String[] { "UnknownClassName" } );
+			fail( "should have thrown MappingException" );
+		}
+		catch ( MappingException ex ) {
+			// expected
+		}
+	}
+
+	public void testSetListenerInvalidClassName() {
+		Configuration cfg = new Configuration();
+		assertNotNull( cfg.getEventListeners().getDeleteEventListeners() );
+		try {
+			cfg.setListener( "delete", InvalidListenerForTest.class.getName() );
+			fail( "should have thrown MappingException" );
+		}
+		catch ( MappingException ex ) {
+			// expected
+		}
+	}
+
+	public void testSetListenersInvalidClassName() {
+		Configuration cfg = new Configuration();
+		assertNotNull( cfg.getEventListeners().getDeleteEventListeners() );
+		try {
+			cfg.setListeners( "delete", new String[] { InvalidListenerForTest.class.getName() } );
+			fail( "should have thrown MappingException" );
+		}
+		catch ( MappingException ex ) {
+			// expected
+		}
+	}
+
+	public void testSetListenerClassName() {
+		Configuration cfg = new Configuration();
+		assertNotNull( cfg.getEventListeners().getDeleteEventListeners() );
+		cfg.setListener( "delete", DeleteListenerForTest.class.getName() );
+		assertEquals( 1, cfg.getEventListeners().getDeleteEventListeners().length );
+		assertTrue( cfg.getEventListeners().getDeleteEventListeners()[0] instanceof DeleteListenerForTest );
+	}
+
+	public void testSetListenersClassName() {
+		Configuration cfg = new Configuration();
+		assertNotNull( cfg.getEventListeners().getDeleteEventListeners() );
+		cfg.setListeners( "delete", new String[] { DeleteListenerForTest.class.getName() } );
+		assertEquals( 1, cfg.getEventListeners().getDeleteEventListeners().length );
+		assertTrue( cfg.getEventListeners().getDeleteEventListeners()[0] instanceof DeleteListenerForTest );
+	}
+
+	public void testSetListenerClassNames() {
+		Configuration cfg = new Configuration();
+		assertNotNull( cfg.getEventListeners().getDeleteEventListeners() );
+		try {
+			cfg.setListener(
+					"delete", new String[] {
+					DeleteListenerForTest.class.getName(),
+					AnotherDeleteListenerForTest.class.getName()
+			}
+			);
+			fail( "should have thrown java.lang.ArrayStoreException" );
+		}
+		catch ( ArrayStoreException ex ) {
+			// expected
+		}
+	}
+
+	public void testSetListenersClassNames() {
+		Configuration cfg = new Configuration();
+		assertNotNull( cfg.getEventListeners().getDeleteEventListeners() );
+		cfg.setListeners(
+				"delete", new String[] {
+				DeleteListenerForTest.class.getName(),
+				AnotherDeleteListenerForTest.class.getName()
+		}
+		);
+		assertEquals( 2, cfg.getEventListeners().getDeleteEventListeners().length );
+		assertTrue( cfg.getEventListeners().getDeleteEventListeners()[0] instanceof DeleteListenerForTest );
+		assertTrue( cfg.getEventListeners().getDeleteEventListeners()[1] instanceof AnotherDeleteListenerForTest );
+	}
+
+	public void testSetListenerClassInstance() {
+		Configuration cfg = new Configuration();
+		assertNotNull( cfg.getEventListeners().getDeleteEventListeners() );
+		cfg.setListener( "delete", new DeleteListenerForTest() );
+		assertEquals( 1, cfg.getEventListeners().getDeleteEventListeners().length );
+	}
+
+	public void testSetListenersClassInstances() {
+		Configuration cfg = new Configuration();
+		assertNotNull( cfg.getEventListeners().getDeleteEventListeners() );
+		cfg.setListeners(
+				"delete", new DeleteEventListener[] {
+				new DeleteListenerForTest(),
+				new AnotherDeleteListenerForTest()
+		}
+		);
+		assertEquals( 2, cfg.getEventListeners().getDeleteEventListeners().length );
+		assertTrue( cfg.getEventListeners().getDeleteEventListeners()[0] instanceof DeleteListenerForTest );
+		assertTrue( cfg.getEventListeners().getDeleteEventListeners()[1] instanceof AnotherDeleteListenerForTest );
+	}
+
+	public void testSetListenerInvalidClassInstance() {
+		Configuration cfg = new Configuration();
+		assertNotNull( cfg.getEventListeners().getDeleteEventListeners() );
+		try {
+			cfg.setListener( "delete", new InvalidListenerForTest() );
+			fail( "should have thrown java.lang.ArrayStoreException" );
+		}
+		catch ( ArrayStoreException ex ) {
+			// expected
+		}
+	}
+
+	public void testSetListenersInvalidClassInstances() {
+		Configuration cfg = new Configuration();
+		assertNotNull( cfg.getEventListeners().getDeleteEventListeners() );
+		try {
+			cfg.setListeners( "delete", new InvalidListenerForTest[] { new InvalidListenerForTest() } );
+			fail( "should have thrown java.lang.ClassCastException" );
+		}
+		catch ( ClassCastException ex ) {
+			// expected
+		}
+	}
+
+	public void testSetListenerNullType() {
+		Configuration cfg = new Configuration();
+		assertNotNull( cfg.getEventListeners().getDeleteEventListeners() );
+		try {
+			cfg.setListener( null, new DeleteListenerForTest() );
+			fail( "should have thrown MappingException" );
+		}
+		catch ( MappingException ex ) {
+			// expected
+		}
+	}
+
+	public void testSetListenersNullType() {
+		Configuration cfg = new Configuration();
+		assertNotNull( cfg.getEventListeners().getDeleteEventListeners() );
+		try {
+			cfg.setListeners( null, new DeleteEventListener[] { new DeleteListenerForTest() } );
+			fail( "should have thrown MappingException" );
+		}
+		catch ( MappingException ex ) {
+			// expected
+		}
+	}
+
+	public void testSetListenerUnknownType() {
+		Configuration cfg = new Configuration();
+		assertNotNull( cfg.getEventListeners().getDeleteEventListeners() );
+		try {
+			cfg.setListener( "unknown-type", new DeleteListenerForTest() );
+			fail( "should have thrown MappingException" );
+		}
+		catch ( MappingException ex ) {
+			// expected
+		}
+	}
+
+	public void testSetListenersUnknownType() {
+		Configuration cfg = new Configuration();
+		assertNotNull( cfg.getEventListeners().getDeleteEventListeners() );
+		try {
+			cfg.setListeners( "unknown-type", new DeleteEventListener[] { new DeleteListenerForTest() } );
+			fail( "should have thrown MappingException" );
+		}
+		catch ( MappingException ex ) {
+			// expected
+		}
+	}
+}




More information about the hibernate-commits mailing list