EntityLifecycleCallbackResolver { EntityLifecycleListenerManager resolve() { resolveLifecycleCallbacks(); resolveEntityListeners(); if (managedType.getSupertype() != null) { manager.setParent(managedType.getSupertype().getLifecycleListenerManager()); } return manager; } EntityLifecycleCallbackResolver(AbstractIdentifiableType<?> et);   }
@Test public void resolveDiscoversEntityLifecycleListeners() throws Exception { final AbstractIdentifiableType<OWLClassR> type = typeFor(OWLClassR.class); final EntityLifecycleListenerManager callbackManager = resolve(type); assertTrue(callbackManager.getLifecycleCallbacks().containsKey(LifecycleEvent.POST_LOAD)); assertEquals(OWLClassR.class.getDeclaredMethod("postLoad"), callbackManager.getLifecycleCallbacks().get(LifecycleEvent.POST_LOAD)); }
@Test public void resolveThrowsMetamodelInitializationExceptionWhenMultipleListenersForOnePhaseAreDeclaredOnType() { thrown.expect(MetamodelInitializationException.class); thrown.expectMessage("The type [" + ClassWithMultipleListeners.class.getName() + "] has multiple lifecycle callbacks for the lifecycle event [" + LifecycleEvent.POST_LOAD + "]."); resolve(typeFor(ClassWithMultipleListeners.class)); }
@Test public void resolveThrowsMetamodelInitializationExceptionWhenLifecycleListenerTakesArguments() { thrown.expect(MetamodelInitializationException.class); thrown.expectMessage("The callback method [takesArguments] in type [" + ClassWithLifecycleListenerTakingArguments.class.getName() + "] has incorrect signature. It should not have any arguments."); resolve(typeFor(ClassWithLifecycleListenerTakingArguments.class)); }
@Test public void resolveThrowsMetamodelInitializationExceptionWhenLifecycleIsNotVoid() { thrown.expect(MetamodelInitializationException.class); thrown.expectMessage("The callback method [returnsData] in type [" + ClassWithNonVoidLifecycleListener.class.getName() + "] has incorrect signature. Its return type should be void."); resolve(typeFor(ClassWithNonVoidLifecycleListener.class)); }
@Test public void resolveThrowsMetamodelInitializationExceptionWhenLifecycleListenerIsFinal() { thrown.expect(MetamodelInitializationException.class); thrown.expectMessage("The callback method [finalListener] in type [" + ClassWithFinalLifecycleListener.class.getName() + "] has incorrect signature. It should not be static or final."); resolve(typeFor(ClassWithFinalLifecycleListener.class)); }
@Test public void resolveThrowsMetamodelInitializationExceptionWhenLifecycleListenerIsStatic() { thrown.expect(MetamodelInitializationException.class); thrown.expectMessage("The callback method [staticListener] in type [" + ClassWithStaticLifecycleListener.class.getName() + "] has incorrect signature. It should not be static or final."); resolve(typeFor(ClassWithStaticLifecycleListener.class)); }
@Test public void resolveSetsUpReferenceToSupertypeLifecycleManager() throws Exception { final AbstractIdentifiableType<OWLClassS> parent = typeFor(OWLClassS.class); parent.setLifecycleListenerManager(resolve(parent)); final AbstractIdentifiableType<OWLClassR> child = typeFor(OWLClassR.class); child.setSupertype(parent); final EntityLifecycleListenerManager result = resolve(child); final Field parentField = EntityLifecycleListenerManager.class.getDeclaredField("parent"); parentField.setAccessible(true); assertEquals(parent.getLifecycleListenerManager(), parentField.get(result)); }
@Test public void resolveCreatesInstanceOfEntityListenerDeclaredBySpecifiedEntityType() { final AbstractIdentifiableType<OWLClassS> et = typeFor(OWLClassS.class); final EntityLifecycleListenerManager result = resolve(et); assertEquals(1, result.getEntityListeners().size()); assertTrue(result.getEntityListeners().get(0) instanceof ParentListener); }
@Test public void resolveThrowsMetamodelInitializationExceptionWhenUnableToInstantiateEntityListener() { thrown.expect(MetamodelInitializationException.class); thrown.expectMessage("Unable to instantiate entity listener of type " + InvalidListener.class + ". The listener has to have a public no-arg constructor."); final AbstractIdentifiableType<EntityWithInvalidListener> et = typeFor(EntityWithInvalidListener.class); resolve(et); }
@Test public void resolveRegistersCallbacksDeclaredInEntityListener() throws Exception { final AbstractIdentifiableType<OWLClassS> et = typeFor(OWLClassS.class); final EntityLifecycleListenerManager result = resolve(et); assertEquals(1, result.getEntityListenerCallbacks().size()); final Map<LifecycleEvent, Method> callbacks = result.getEntityListenerCallbacks().values().iterator().next(); assertTrue(callbacks.containsKey(LifecycleEvent.PRE_PERSIST)); assertEquals(ParentListener.getPrePersistMethod(), callbacks.get(LifecycleEvent.PRE_PERSIST)); assertTrue(callbacks.containsKey(LifecycleEvent.POST_PERSIST)); assertEquals(ParentListener.getPostPersistMethod(), callbacks.get(LifecycleEvent.POST_PERSIST)); }
@Test public void resolveThrowsMetamodelInitializationExceptionWhenEntityListenerCallbackHasInvalidNumberOfParameters() { thrown.expect(MetamodelInitializationException.class); thrown.expectMessage("The callback method [invalidArgumentCount] in entity listener [" + ListenerWithInvalidArgumentCount.class.getName() + "] has incorrect signature. It should take exactly one argument."); final AbstractIdentifiableType<EntityWithListenerWithInvalidArgumentCount> et = typeFor( EntityWithListenerWithInvalidArgumentCount.class); resolve(et); }
@Test public void resolveThrowsMetamodelInitializationExceptionWhenEntityListenerCallbackIsNotVoid() { thrown.expect(MetamodelInitializationException.class); thrown.expectMessage("The callback method [nonVoid] in entity listener [" + ListenerWithNonVoidCallback.class.getName() + "] has incorrect signature. Its return type should be void."); final AbstractIdentifiableType<EntityWithListenerWithNonVoidCallback> et = typeFor( EntityWithListenerWithNonVoidCallback.class); resolve(et); }
@Test public void resolveThrowsMetamodelInitializationExceptionWhenEntityListenerCallbackIsStatic() { thrown.expect(MetamodelInitializationException.class); thrown.expectMessage("The callback method [staticCallback] in entity listener [" + ListenerWithStaticCallback.class.getName() + "] has incorrect signature. It should not be static or final."); final AbstractIdentifiableType<EntityWithListenerWithStaticCallback> et = typeFor( EntityWithListenerWithStaticCallback.class); resolve(et); }
@Test public void resolveThrowsMetamodelInitializationExceptionWhenEntityListenerHasMultipleCallbacksForSameEvent() { thrown.expect(MetamodelInitializationException.class); thrown.expectMessage("The entity listener [" + ListenerWithMultipleCallbacksForSameEvent.class.getName() + "] has multiple callbacks for the lifecycle event [" + LifecycleEvent.POST_LOAD + "]."); final AbstractIdentifiableType<EntityWithListenerWithMultipleConflictingCallbacks> et = typeFor( EntityWithListenerWithMultipleConflictingCallbacks.class); resolve(et); }
@Test public void resolveThrowsMetamodelInitializationExceptionWhenEntityListenerCallbackHasInvalidParameterType() { thrown.expect(MetamodelInitializationException.class); thrown.expectMessage("The callback method [callback] in entity listener [" + ListenerWithInvalidParameterTypeCallback.class.getName() + "] has incorrect signature. Its parameter should be of type [" + Object.class.getName() + "] or [" + EntityWithListenerWithInvalidParameterTypeCallback.class.getName() + "]."); final AbstractIdentifiableType<EntityWithListenerWithInvalidParameterTypeCallback> et = typeFor( EntityWithListenerWithInvalidParameterTypeCallback.class); resolve(et); }