cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r1033573 - in /cayenne/sandbox/cayenne-mixin/branches/3.0: ./ src/main/java/org/apache/cayenne/mixin/ src/test/java/org/apache/cayenne/mixin/
Date Wed, 10 Nov 2010 16:34:23 GMT
Author: aadamchik
Date: Wed Nov 10 16:34:23 2010
New Revision: 1033573

URL: http://svn.apache.org/viewvc?rev=1033573&view=rev
Log:
a way to get the handlers back from MixinHandlerManager

Added:
    cayenne/sandbox/cayenne-mixin/branches/3.0/src/test/java/org/apache/cayenne/mixin/MockMixinHandler.java
Modified:
    cayenne/sandbox/cayenne-mixin/branches/3.0/pom.xml
    cayenne/sandbox/cayenne-mixin/branches/3.0/src/main/java/org/apache/cayenne/mixin/MixinHandlerManager.java
    cayenne/sandbox/cayenne-mixin/branches/3.0/src/test/java/org/apache/cayenne/mixin/MixinHandlerManagerTest.java

Modified: cayenne/sandbox/cayenne-mixin/branches/3.0/pom.xml
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-mixin/branches/3.0/pom.xml?rev=1033573&r1=1033572&r2=1033573&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-mixin/branches/3.0/pom.xml (original)
+++ cayenne/sandbox/cayenne-mixin/branches/3.0/pom.xml Wed Nov 10 16:34:23 2010
@@ -8,7 +8,7 @@
 		<version>3.0.1</version>
 	</parent>
 	<artifactId>cayenne-mixin</artifactId>
-	<version>3.0.0.1</version>
+	<version>3.0.0.2</version>
 	<name>Library: cayenne-mixin</name>
 	<packaging>jar</packaging>
 	<properties>

Modified: cayenne/sandbox/cayenne-mixin/branches/3.0/src/main/java/org/apache/cayenne/mixin/MixinHandlerManager.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-mixin/branches/3.0/src/main/java/org/apache/cayenne/mixin/MixinHandlerManager.java?rev=1033573&r1=1033572&r2=1033573&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-mixin/branches/3.0/src/main/java/org/apache/cayenne/mixin/MixinHandlerManager.java
(original)
+++ cayenne/sandbox/cayenne-mixin/branches/3.0/src/main/java/org/apache/cayenne/mixin/MixinHandlerManager.java
Wed Nov 10 16:34:23 2010
@@ -20,6 +20,8 @@ package org.apache.cayenne.mixin;
 
 import java.lang.annotation.Annotation;
 import java.lang.annotation.Inherited;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
 
 import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.DataObject;
@@ -33,15 +35,24 @@ import org.apache.cayenne.util.Util;
  */
 public class MixinHandlerManager {
 
+	protected ConcurrentMap<Class<?>, MixinHandler<?>> handlers;
 	protected EntityResolver entityResolver;
 
 	public MixinHandlerManager(EntityResolver entityResolver) {
+		this.handlers = new ConcurrentHashMap<Class<?>, MixinHandler<?>>();
 		this.entityResolver = entityResolver;
 	}
 
 	@SuppressWarnings("all")
 	public void addMixinHandler(MixinHandler<?> handler) {
 
+		// we allow multiple handler types for the same mixin, but each handler
+		// class must be unique
+		if (handlers.putIfAbsent(handler.getClass(), handler) != null) {
+			throw new IllegalStateException("A handler of type "
+					+ handler.getClass().getName() + " is already registered");
+		}
+
 		Class<? extends Annotation> type = handler.getMixinType();
 		boolean inherited = type.isAnnotationPresent(Inherited.class);
 
@@ -71,4 +82,13 @@ public class MixinHandlerManager {
 			} while (inherited && entityTypeOrSupertype != null);
 		}
 	}
+
+	/**
+	 * Returns a previously registered {@link MixinHandler} or null if no
+	 * handler of a given type is registered.
+	 */
+	@SuppressWarnings("all")
+	public <T extends MixinHandler<?>> T getMixinHandler(Class<T> handlerType)
{
+		return (T) handlers.get(handlerType);
+	}
 }

Modified: cayenne/sandbox/cayenne-mixin/branches/3.0/src/test/java/org/apache/cayenne/mixin/MixinHandlerManagerTest.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-mixin/branches/3.0/src/test/java/org/apache/cayenne/mixin/MixinHandlerManagerTest.java?rev=1033573&r1=1033572&r2=1033573&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-mixin/branches/3.0/src/test/java/org/apache/cayenne/mixin/MixinHandlerManagerTest.java
(original)
+++ cayenne/sandbox/cayenne-mixin/branches/3.0/src/test/java/org/apache/cayenne/mixin/MixinHandlerManagerTest.java
Wed Nov 10 16:34:23 2010
@@ -21,6 +21,7 @@ package org.apache.cayenne.mixin;
 import junit.framework.TestCase;
 
 import org.apache.cayenne.CayenneDataObject;
+import org.apache.cayenne.DataObject;
 import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.map.EntityResolver;
 import org.apache.cayenne.map.ObjEntity;
@@ -35,18 +36,18 @@ public class MixinHandlerManagerTest ext
 
 	public void testAddMixinHandler() {
 		EntityResolver resolver = new EntityResolver();
-		
+
 		DataMap map = new DataMap("m");
 		ObjEntity e1 = new ObjEntity("x");
 		e1.setClassName(MockMixinDataObject.class.getName());
 		map.addObjEntity(e1);
-		
+
 		ObjEntity e2 = new ObjEntity("y");
 		e2.setClassName(CayenneDataObject.class.getName());
 		map.addObjEntity(e2);
-		
+
 		resolver.addDataMap(map);
-		
+
 		MixinHandlerManager mixinHandlerManager = new MixinHandlerManager(
 				resolver);
 
@@ -72,4 +73,60 @@ public class MixinHandlerManagerTest ext
 		mixinHandlerManager.addMixinHandler(handler);
 		assertEquals(1, counter[0]);
 	}
+
+	public void testAddMixinHandlerDupes() {
+		EntityResolver resolver = new EntityResolver();
+		MixinHandlerManager mixinHandlerManager = new MixinHandlerManager(
+				resolver);
+
+		mixinHandlerManager.addMixinHandler(new MockMixinHandler());
+
+		// disallow dupes for same TYPE of handler
+		try {
+			mixinHandlerManager.addMixinHandler(new MockMixinHandler());
+			fail("dupe handler not detected");
+		} catch (IllegalStateException e) {
+			// expected
+		}
+
+		// allow dupes for different handlers for the same mixin
+		mixinHandlerManager.addMixinHandler(new MixinHandler<MockMixin>() {
+			@Override
+			public void addMixin(Class<? extends DataObject> entityType) {
+				// noop
+			}
+
+			@Override
+			public Class<MockMixin> getMixinType() {
+				return MockMixin.class;
+			}
+		});
+	}
+
+	public void testGetMixinHandler() {
+		EntityResolver resolver = new EntityResolver();
+		MixinHandlerManager mixinHandlerManager = new MixinHandlerManager(
+				resolver);
+
+		MixinHandler<MockMixin> h1 = new MockMixinHandler();
+
+		MixinHandler<MockMixin> h2 = new MixinHandler<MockMixin>() {
+			@Override
+			public void addMixin(Class<? extends DataObject> entityType) {
+				// noop
+			}
+
+			@Override
+			public Class<MockMixin> getMixinType() {
+				return MockMixin.class;
+			}
+		};
+
+		mixinHandlerManager.addMixinHandler(h2);
+		assertNull(mixinHandlerManager.getMixinHandler(MockMixinHandler.class));
+
+		mixinHandlerManager.addMixinHandler(h1);
+		assertSame(h1, mixinHandlerManager
+				.getMixinHandler(MockMixinHandler.class));
+	}
 }

Added: cayenne/sandbox/cayenne-mixin/branches/3.0/src/test/java/org/apache/cayenne/mixin/MockMixinHandler.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-mixin/branches/3.0/src/test/java/org/apache/cayenne/mixin/MockMixinHandler.java?rev=1033573&view=auto
==============================================================================
--- cayenne/sandbox/cayenne-mixin/branches/3.0/src/test/java/org/apache/cayenne/mixin/MockMixinHandler.java
(added)
+++ cayenne/sandbox/cayenne-mixin/branches/3.0/src/test/java/org/apache/cayenne/mixin/MockMixinHandler.java
Wed Nov 10 16:34:23 2010
@@ -0,0 +1,16 @@
+package org.apache.cayenne.mixin;
+
+import org.apache.cayenne.DataObject;
+
+public class MockMixinHandler implements MixinHandler<MockMixin> {
+
+	@Override
+	public void addMixin(Class<? extends DataObject> entityType) {
+		// noop
+	}
+
+	@Override
+	public Class<MockMixin> getMixinType() {
+		return MockMixin.class;
+	}
+}



Mime
View raw message