mirror of
https://github.com/hibernate/hibernate-orm
synced 2025-02-16 08:05:05 +00:00
re-enable tests
re-organize some tests added `@LoggingInspections` added `@MessageKeyWatcher`
This commit is contained in:
parent
3c72f6fe12
commit
e4111a5453
@ -1,37 +0,0 @@
|
||||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.engine.jdbc.env.internal;
|
||||
|
||||
import org.hibernate.internal.CoreMessageLogger;
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.hibernate.testing.logger.LoggerInspectionRule;
|
||||
import org.hibernate.testing.logger.Triggerable;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class LobCreationCheckSkipTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Rule
|
||||
public LoggerInspectionRule logInspection = new LoggerInspectionRule(
|
||||
Logger.getMessageLogger( CoreMessageLogger.class, LobCreatorBuilderImpl.class.getName()
|
||||
) );
|
||||
|
||||
private Triggerable triggerable = logInspection.watchForLogMessages( "HHH000424:" );
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
assertFalse(triggerable.wasTriggered());
|
||||
}
|
||||
}
|
@ -36,17 +36,14 @@
|
||||
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
|
||||
import org.hibernate.boot.spi.BootstrapContext;
|
||||
import org.hibernate.boot.spi.XmlMappingBinderAccess;
|
||||
import org.hibernate.internal.CoreMessageLogger;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseUnitTestCase;
|
||||
import org.hibernate.testing.logger.LoggerInspectionRule;
|
||||
import org.hibernate.testing.logger.Triggerable;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.hibernate.testing.orm.junit.Logger;
|
||||
import org.hibernate.testing.orm.junit.MessageKeyInspection;
|
||||
import org.hibernate.testing.orm.junit.MessageKeyWatcher;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.mockito.Mockito;
|
||||
|
||||
@ -62,7 +59,11 @@
|
||||
* @author Vlad Mihalcea
|
||||
* @author Petteri Pitkanen
|
||||
*/
|
||||
public class ScanningCoordinatorTest extends BaseUnitTestCase {
|
||||
@MessageKeyInspection(
|
||||
messageKey = "Unable to resolve class [a.b.C] named in persistence unit",
|
||||
logger = @Logger( loggerNameClass = ScanningCoordinator.class )
|
||||
)
|
||||
public class ScanningCoordinatorTest {
|
||||
|
||||
private ManagedResourcesImpl managedResources = Mockito.mock( ManagedResourcesImpl.class );
|
||||
private ScanResult scanResult = Mockito.mock( ScanResult.class );
|
||||
@ -75,13 +76,7 @@ public class ScanningCoordinatorTest extends BaseUnitTestCase {
|
||||
|
||||
private ClassLoaderService classLoaderService = Mockito.mock( ClassLoaderService.class );
|
||||
|
||||
private Triggerable triggerable;
|
||||
|
||||
@Rule
|
||||
public LoggerInspectionRule logInspection = new LoggerInspectionRule(
|
||||
Logger.getMessageLogger( CoreMessageLogger.class, ScanningCoordinator.class.getName() ) );
|
||||
|
||||
@Before
|
||||
@BeforeEach
|
||||
public void init() {
|
||||
Mockito.reset( managedResources );
|
||||
Mockito.reset( scanResult );
|
||||
@ -100,13 +95,10 @@ public void init() {
|
||||
when( classLoaderService.classForName( eq( "a.b.C" ) ) ).thenThrow( ClassLoadingException.class );
|
||||
when( classLoaderService.locateResource( eq( "a/b/c.class" ) ) ).thenReturn( null );
|
||||
when( classLoaderService.locateResource( eq( "a/b/c/package-info.class" ) ) ).thenReturn( null );
|
||||
|
||||
triggerable = logInspection.watchForLogMessages( "Unable" );
|
||||
triggerable.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testApplyScanResultsToManagedResourcesWithNullRootUrl() {
|
||||
public void testApplyScanResultsToManagedResourcesWithNullRootUrl(MessageKeyWatcher watcher) {
|
||||
|
||||
ScanningCoordinator.INSTANCE.applyScanResultsToManagedResources(
|
||||
managedResources,
|
||||
@ -114,11 +106,11 @@ public void testApplyScanResultsToManagedResourcesWithNullRootUrl() {
|
||||
bootstrapContext,
|
||||
xmlMappingBinderAccess
|
||||
);
|
||||
assertEquals( "Unable to resolve class [a.b.C] named in persistence unit [null]", triggerable.triggerMessage() );
|
||||
assertEquals( "Unable to resolve class [a.b.C] named in persistence unit [null]", watcher.getFirstTriggeredMessage() );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testApplyScanResultsToManagedResourcesWithNotNullRootUrl()
|
||||
public void testApplyScanResultsToManagedResourcesWithNotNullRootUrl(MessageKeyWatcher watcher)
|
||||
throws MalformedURLException {
|
||||
when( scanEnvironment.getRootUrl() ).thenReturn( new URL( "http://http://hibernate.org/" ) );
|
||||
|
||||
@ -128,7 +120,7 @@ public void testApplyScanResultsToManagedResourcesWithNotNullRootUrl()
|
||||
bootstrapContext,
|
||||
xmlMappingBinderAccess
|
||||
);
|
||||
assertEquals( "Unable to resolve class [a.b.C] named in persistence unit [http://http://hibernate.org/]", triggerable.triggerMessage() );
|
||||
assertEquals( "Unable to resolve class [a.b.C] named in persistence unit [http://http://hibernate.org/]", watcher.getFirstTriggeredMessage() );
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -24,22 +24,20 @@
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.collection.internal.AbstractPersistentCollection;
|
||||
import org.hibernate.internal.CoreMessageLogger;
|
||||
import org.hibernate.type.CollectionType;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.logger.LoggerInspectionRule;
|
||||
import org.hibernate.testing.logger.Triggerable;
|
||||
import org.hibernate.testing.orm.junit.DomainModel;
|
||||
import org.hibernate.testing.orm.junit.LoggingInspections;
|
||||
import org.hibernate.testing.orm.junit.LoggingInspectionsScope;
|
||||
import org.hibernate.testing.orm.junit.MessageKeyWatcher;
|
||||
import org.hibernate.testing.orm.junit.SessionFactory;
|
||||
import org.hibernate.testing.orm.junit.SessionFactoryScope;
|
||||
import org.junit.Rule;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
@ -57,23 +55,28 @@
|
||||
}
|
||||
)
|
||||
@SessionFactory
|
||||
@LoggingInspections(
|
||||
messages = {
|
||||
@LoggingInspections.Message(
|
||||
messageKey = "HHH000494",
|
||||
loggers = @org.hibernate.testing.orm.junit.Logger( loggerNameClass = CollectionType.class )
|
||||
),
|
||||
@LoggingInspections.Message(
|
||||
messageKey = "HHH000495",
|
||||
loggers = @org.hibernate.testing.orm.junit.Logger( loggerNameClass = AbstractPersistentCollection.class )
|
||||
),
|
||||
@LoggingInspections.Message(
|
||||
messageKey = "HHH000496",
|
||||
loggers = @org.hibernate.testing.orm.junit.Logger( loggerNameClass = AbstractPersistentCollection.class )
|
||||
),
|
||||
@LoggingInspections.Message(
|
||||
messageKey = "HHH000498",
|
||||
loggers = @org.hibernate.testing.orm.junit.Logger( loggerNameClass = AbstractPersistentCollection.class )
|
||||
)
|
||||
}
|
||||
)
|
||||
public class DetachedBagDelayedOperationTest {
|
||||
|
||||
@Rule
|
||||
public LoggerInspectionRule logInspectionCollectionType = new LoggerInspectionRule(
|
||||
Logger.getMessageLogger( CoreMessageLogger.class, CollectionType.class.getName() )
|
||||
);
|
||||
|
||||
@Rule
|
||||
public LoggerInspectionRule logInspectionAbstractPersistentCollection = new LoggerInspectionRule(
|
||||
Logger.getMessageLogger( CoreMessageLogger.class, AbstractPersistentCollection.class.getName() )
|
||||
);
|
||||
|
||||
private Triggerable triggerableIgnoreQueuedOperationsOnMerge;
|
||||
private Triggerable triggerableQueuedOperationWhenAttachToSession;
|
||||
private Triggerable triggerableQueuedOperationWhenDetachFromSession;
|
||||
private Triggerable triggerableQueuedOperationOnRollback;
|
||||
|
||||
@BeforeEach
|
||||
public void setup(SessionFactoryScope scope) {
|
||||
Parent parent = new Parent();
|
||||
@ -91,16 +94,6 @@ public void setup(SessionFactoryScope scope) {
|
||||
session.persist( parent );
|
||||
}
|
||||
);
|
||||
|
||||
triggerableIgnoreQueuedOperationsOnMerge = logInspectionCollectionType.watchForLogMessages( "HHH000494" );
|
||||
triggerableQueuedOperationWhenAttachToSession = logInspectionAbstractPersistentCollection.watchForLogMessages(
|
||||
"HHH000495" );
|
||||
triggerableQueuedOperationWhenDetachFromSession = logInspectionAbstractPersistentCollection.watchForLogMessages(
|
||||
"HHH000496" );
|
||||
triggerableQueuedOperationOnRollback = logInspectionAbstractPersistentCollection.watchForLogMessages(
|
||||
"HHH000498" );
|
||||
|
||||
resetTriggerables();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
@ -115,7 +108,9 @@ public void cleanup(SessionFactoryScope scope) {
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-11209")
|
||||
public void testMergeDetachedCollectionWithQueuedOperations(SessionFactoryScope scope) {
|
||||
public void testMergeDetachedCollectionWithQueuedOperations(
|
||||
SessionFactoryScope scope,
|
||||
LoggingInspectionsScope loggingScope) {
|
||||
final Parent pOriginal = scope.fromTransaction(
|
||||
session -> {
|
||||
Parent p = session.get( Parent.class, 1L );
|
||||
@ -126,6 +121,12 @@ public void testMergeDetachedCollectionWithQueuedOperations(SessionFactoryScope
|
||||
return p;
|
||||
}
|
||||
);
|
||||
|
||||
final MessageKeyWatcher opMergedWatcher = loggingScope.getWatcher( "HHH000494", CollectionType.class );
|
||||
final MessageKeyWatcher opAttachedWatcher = loggingScope.getWatcher( "HHH000495", AbstractPersistentCollection.class );
|
||||
final MessageKeyWatcher opDetachedWatcher = loggingScope.getWatcher( "HHH000496", AbstractPersistentCollection.class );
|
||||
final MessageKeyWatcher opRollbackWatcher = loggingScope.getWatcher( "HHH000498", AbstractPersistentCollection.class );
|
||||
|
||||
final Parent pWithQueuedOperations = scope.fromTransaction(
|
||||
session -> {
|
||||
Parent p = (Parent) session.merge( pOriginal );
|
||||
@ -137,23 +138,34 @@ public void testMergeDetachedCollectionWithQueuedOperations(SessionFactoryScope
|
||||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
assertTrue( ( (AbstractPersistentCollection) p.getChildren() ).hasQueuedOperations() );
|
||||
|
||||
checkTriggerablesNotTriggered();
|
||||
assertFalse( opMergedWatcher.wasTriggered() );
|
||||
assertFalse( opAttachedWatcher.wasTriggered() );
|
||||
assertFalse( opDetachedWatcher.wasTriggered() );
|
||||
assertFalse( opRollbackWatcher.wasTriggered() );
|
||||
|
||||
session.detach( p );
|
||||
assertTrue( triggerableQueuedOperationWhenDetachFromSession.wasTriggered() );
|
||||
|
||||
assertTrue( opDetachedWatcher.wasTriggered() );
|
||||
assertEquals(
|
||||
"HHH000496: Detaching an uninitialized collection with queued operations from a session: [org.hibernate.orm.test.collection.delayedOperation.DetachedBagDelayedOperationTest$Parent.children#1]",
|
||||
triggerableQueuedOperationWhenDetachFromSession.triggerMessage()
|
||||
opDetachedWatcher.getFirstTriggeredMessage()
|
||||
);
|
||||
triggerableQueuedOperationWhenDetachFromSession.reset();
|
||||
opDetachedWatcher.reset();
|
||||
|
||||
// Make sure nothing else got triggered
|
||||
checkTriggerablesNotTriggered();
|
||||
assertFalse( opMergedWatcher.wasTriggered() );
|
||||
assertFalse( opAttachedWatcher.wasTriggered() );
|
||||
assertFalse( opDetachedWatcher.wasTriggered() );
|
||||
assertFalse( opRollbackWatcher.wasTriggered() );
|
||||
|
||||
return p;
|
||||
}
|
||||
);
|
||||
|
||||
checkTriggerablesNotTriggered();
|
||||
assertFalse( opMergedWatcher.wasTriggered() );
|
||||
assertFalse( opAttachedWatcher.wasTriggered() );
|
||||
assertFalse( opDetachedWatcher.wasTriggered() );
|
||||
assertFalse( opRollbackWatcher.wasTriggered() );
|
||||
|
||||
assertTrue( ( (AbstractPersistentCollection) pWithQueuedOperations.getChildren() ).hasQueuedOperations() );
|
||||
|
||||
@ -161,16 +173,19 @@ public void testMergeDetachedCollectionWithQueuedOperations(SessionFactoryScope
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
|
||||
checkTriggerablesNotTriggered();
|
||||
assertFalse( opMergedWatcher.wasTriggered() );
|
||||
assertFalse( opAttachedWatcher.wasTriggered() );
|
||||
assertFalse( opDetachedWatcher.wasTriggered() );
|
||||
assertFalse( opRollbackWatcher.wasTriggered() );
|
||||
|
||||
assertFalse( triggerableIgnoreQueuedOperationsOnMerge.wasTriggered() );
|
||||
assertFalse( opMergedWatcher.wasTriggered() );
|
||||
Parent p = (Parent) session.merge( pWithQueuedOperations );
|
||||
assertTrue( triggerableIgnoreQueuedOperationsOnMerge.wasTriggered() );
|
||||
assertTrue( opMergedWatcher.wasTriggered() );
|
||||
assertEquals(
|
||||
"HHH000494: Attempt to merge an uninitialized collection with queued operations; queued operations will be ignored: [org.hibernate.orm.test.collection.delayedOperation.DetachedBagDelayedOperationTest$Parent.children#1]",
|
||||
triggerableIgnoreQueuedOperationsOnMerge.triggerMessage()
|
||||
opMergedWatcher.getFirstTriggeredMessage()
|
||||
);
|
||||
triggerableIgnoreQueuedOperationsOnMerge.reset();
|
||||
opMergedWatcher.reset();
|
||||
|
||||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
assertFalse( ( (AbstractPersistentCollection) p.getChildren() ).hasQueuedOperations() );
|
||||
@ -190,12 +205,21 @@ public void testMergeDetachedCollectionWithQueuedOperations(SessionFactoryScope
|
||||
}
|
||||
);
|
||||
|
||||
checkTriggerablesNotTriggered();
|
||||
assertFalse( opMergedWatcher.wasTriggered() );
|
||||
assertFalse( opAttachedWatcher.wasTriggered() );
|
||||
assertFalse( opDetachedWatcher.wasTriggered() );
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-11209")
|
||||
public void testSaveOrUpdateDetachedCollectionWithQueuedOperations(SessionFactoryScope scope) {
|
||||
public void testSaveOrUpdateDetachedCollectionWithQueuedOperations(
|
||||
SessionFactoryScope scope,
|
||||
LoggingInspectionsScope loggingScope) {
|
||||
final MessageKeyWatcher opMergedWatcher = loggingScope.getWatcher( "HHH000494", CollectionType.class );
|
||||
final MessageKeyWatcher opAttachedWatcher = loggingScope.getWatcher( "HHH000495", AbstractPersistentCollection.class );
|
||||
final MessageKeyWatcher opDetachedWatcher = loggingScope.getWatcher( "HHH000496", AbstractPersistentCollection.class );
|
||||
final MessageKeyWatcher opRollbackWatcher = loggingScope.getWatcher( "HHH000498", AbstractPersistentCollection.class );
|
||||
|
||||
final Parent pOriginal = scope.fromTransaction(
|
||||
session -> {
|
||||
Parent p = session.get( Parent.class, 1L );
|
||||
@ -217,23 +241,32 @@ public void testSaveOrUpdateDetachedCollectionWithQueuedOperations(SessionFactor
|
||||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
assertTrue( ( (AbstractPersistentCollection) p.getChildren() ).hasQueuedOperations() );
|
||||
|
||||
checkTriggerablesNotTriggered();
|
||||
assertFalse( opMergedWatcher.wasTriggered() );
|
||||
assertFalse( opAttachedWatcher.wasTriggered() );
|
||||
assertFalse( opDetachedWatcher.wasTriggered() );
|
||||
assertFalse( opRollbackWatcher.wasTriggered() );
|
||||
|
||||
session.detach( p );
|
||||
assertTrue( triggerableQueuedOperationWhenDetachFromSession.wasTriggered() );
|
||||
assertTrue( opDetachedWatcher.wasTriggered() );
|
||||
assertEquals(
|
||||
"HHH000496: Detaching an uninitialized collection with queued operations from a session: [org.hibernate.orm.test.collection.delayedOperation.DetachedBagDelayedOperationTest$Parent.children#1]",
|
||||
triggerableQueuedOperationWhenDetachFromSession.triggerMessage()
|
||||
opDetachedWatcher.getFirstTriggeredMessage()
|
||||
);
|
||||
triggerableQueuedOperationWhenDetachFromSession.reset();
|
||||
opDetachedWatcher.reset();
|
||||
|
||||
// Make sure nothing else got triggered
|
||||
checkTriggerablesNotTriggered();
|
||||
assertFalse( opMergedWatcher.wasTriggered() );
|
||||
assertFalse( opAttachedWatcher.wasTriggered() );
|
||||
assertFalse( opDetachedWatcher.wasTriggered() );
|
||||
assertFalse( opRollbackWatcher.wasTriggered() );
|
||||
|
||||
return p;
|
||||
}
|
||||
);
|
||||
|
||||
checkTriggerablesNotTriggered();
|
||||
assertFalse( opMergedWatcher.wasTriggered() );
|
||||
assertFalse( opAttachedWatcher.wasTriggered() );
|
||||
assertFalse( opDetachedWatcher.wasTriggered() );
|
||||
|
||||
assertTrue( ( (AbstractPersistentCollection) pAfterDetachWithQueuedOperations.getChildren() ).hasQueuedOperations() );
|
||||
|
||||
@ -241,19 +274,25 @@ public void testSaveOrUpdateDetachedCollectionWithQueuedOperations(SessionFactor
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
|
||||
checkTriggerablesNotTriggered();
|
||||
assertFalse( opMergedWatcher.wasTriggered() );
|
||||
assertFalse( opAttachedWatcher.wasTriggered() );
|
||||
assertFalse( opDetachedWatcher.wasTriggered() );
|
||||
assertFalse( opRollbackWatcher.wasTriggered() );
|
||||
|
||||
assertFalse( triggerableQueuedOperationWhenAttachToSession.wasTriggered() );
|
||||
assertFalse( opAttachedWatcher.wasTriggered() );
|
||||
session.saveOrUpdate( pAfterDetachWithQueuedOperations );
|
||||
assertTrue( triggerableQueuedOperationWhenAttachToSession.wasTriggered() );
|
||||
assertTrue( opAttachedWatcher.wasTriggered() );
|
||||
assertEquals(
|
||||
"HHH000495: Attaching an uninitialized collection with queued operations to a session: [org.hibernate.orm.test.collection.delayedOperation.DetachedBagDelayedOperationTest$Parent.children#1]",
|
||||
triggerableQueuedOperationWhenAttachToSession.triggerMessage()
|
||||
opAttachedWatcher.getFirstTriggeredMessage()
|
||||
);
|
||||
triggerableQueuedOperationWhenAttachToSession.reset();
|
||||
opAttachedWatcher.reset();
|
||||
|
||||
// Make sure nothing else got triggered
|
||||
checkTriggerablesNotTriggered();
|
||||
assertFalse( opMergedWatcher.wasTriggered() );
|
||||
assertFalse( opAttachedWatcher.wasTriggered() );
|
||||
assertFalse( opDetachedWatcher.wasTriggered() );
|
||||
assertFalse( opRollbackWatcher.wasTriggered() );
|
||||
|
||||
assertFalse( Hibernate.isInitialized( pAfterDetachWithQueuedOperations.getChildren() ) );
|
||||
assertTrue( ( (AbstractPersistentCollection) pAfterDetachWithQueuedOperations.getChildren() ).hasQueuedOperations() );
|
||||
@ -273,12 +312,22 @@ public void testSaveOrUpdateDetachedCollectionWithQueuedOperations(SessionFactor
|
||||
}
|
||||
);
|
||||
|
||||
checkTriggerablesNotTriggered();
|
||||
assertFalse( opMergedWatcher.wasTriggered() );
|
||||
assertFalse( opAttachedWatcher.wasTriggered() );
|
||||
assertFalse( opDetachedWatcher.wasTriggered() );
|
||||
assertFalse( opRollbackWatcher.wasTriggered() );
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-11209")
|
||||
public void testCollectionWithQueuedOperationsOnRollback(SessionFactoryScope scope) {
|
||||
public void testCollectionWithQueuedOperationsOnRollback(
|
||||
SessionFactoryScope scope,
|
||||
LoggingInspectionsScope loggingScope) {
|
||||
final MessageKeyWatcher opMergedWatcher = loggingScope.getWatcher( "HHH000494", CollectionType.class );
|
||||
final MessageKeyWatcher opAttachedWatcher = loggingScope.getWatcher( "HHH000495", AbstractPersistentCollection.class );
|
||||
final MessageKeyWatcher opDetachedWatcher = loggingScope.getWatcher( "HHH000496", AbstractPersistentCollection.class );
|
||||
final MessageKeyWatcher opRollbackWatcher = loggingScope.getWatcher( "HHH000498", AbstractPersistentCollection.class );
|
||||
|
||||
final Parent pOriginal = scope.fromTransaction(
|
||||
session -> {
|
||||
Parent p = session.get( Parent.class, 1L );
|
||||
@ -301,7 +350,10 @@ public void testCollectionWithQueuedOperationsOnRollback(SessionFactoryScope sco
|
||||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
assertTrue( ( (AbstractPersistentCollection) p.getChildren() ).hasQueuedOperations() );
|
||||
|
||||
checkTriggerablesNotTriggered();
|
||||
assertFalse( opMergedWatcher.wasTriggered() );
|
||||
assertFalse( opAttachedWatcher.wasTriggered() );
|
||||
assertFalse( opDetachedWatcher.wasTriggered() );
|
||||
assertFalse( opRollbackWatcher.wasTriggered() );
|
||||
|
||||
// save a new Parent with the same ID to throw an exception.
|
||||
|
||||
@ -315,22 +367,13 @@ public void testCollectionWithQueuedOperationsOnRollback(SessionFactoryScope sco
|
||||
catch (EntityExistsException expected) {
|
||||
}
|
||||
|
||||
assertTrue( triggerableQueuedOperationOnRollback.wasTriggered() );
|
||||
triggerableQueuedOperationOnRollback.reset();
|
||||
assertTrue( opRollbackWatcher.wasTriggered() );
|
||||
opRollbackWatcher.reset();
|
||||
|
||||
checkTriggerablesNotTriggered();
|
||||
}
|
||||
|
||||
private void resetTriggerables() {
|
||||
triggerableIgnoreQueuedOperationsOnMerge.reset();
|
||||
triggerableQueuedOperationWhenAttachToSession.reset();
|
||||
triggerableQueuedOperationWhenDetachFromSession.reset();
|
||||
}
|
||||
|
||||
private void checkTriggerablesNotTriggered() {
|
||||
assertFalse( triggerableIgnoreQueuedOperationsOnMerge.wasTriggered() );
|
||||
assertFalse( triggerableQueuedOperationWhenAttachToSession.wasTriggered() );
|
||||
assertFalse( triggerableQueuedOperationWhenDetachFromSession.wasTriggered() );
|
||||
assertFalse( opMergedWatcher.wasTriggered() );
|
||||
assertFalse( opAttachedWatcher.wasTriggered() );
|
||||
assertFalse( opDetachedWatcher.wasTriggered() );
|
||||
assertFalse( opRollbackWatcher.wasTriggered() );
|
||||
}
|
||||
|
||||
@Entity(name = "Parent")
|
||||
|
@ -41,12 +41,13 @@
|
||||
import org.hibernate.internal.CoreMessageLogger;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.logger.LoggerInspectionRule;
|
||||
import org.hibernate.testing.logger.Triggerable;
|
||||
import org.hibernate.testing.orm.junit.DomainModel;
|
||||
import org.hibernate.testing.orm.junit.Logger;
|
||||
import org.hibernate.testing.orm.junit.LoggingInspections;
|
||||
import org.hibernate.testing.orm.junit.LoggingInspectionsScope;
|
||||
import org.hibernate.testing.orm.junit.MessageKeyWatcher;
|
||||
import org.hibernate.testing.orm.junit.SessionFactory;
|
||||
import org.hibernate.testing.orm.junit.SessionFactoryScope;
|
||||
import org.junit.Rule;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
@ -65,15 +66,26 @@
|
||||
}
|
||||
)
|
||||
@SessionFactory
|
||||
@LoggingInspections(
|
||||
messages = {
|
||||
@LoggingInspections.Message(
|
||||
messageKey = "HHH000470",
|
||||
loggers = @Logger( loggerNameClass = AbstractPersistentCollection.class )
|
||||
),
|
||||
@LoggingInspections.Message(
|
||||
messageKey = "HHH000471",
|
||||
loggers = @Logger( loggerNameClass = AbstractPersistentCollection.class )
|
||||
)
|
||||
}
|
||||
)
|
||||
public class MultipleSessionCollectionWarningTest {
|
||||
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( AbstractPersistentCollection.class );
|
||||
|
||||
@Rule
|
||||
public LoggerInspectionRule logInspection = new LoggerInspectionRule( LOG );
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-9518")
|
||||
public void testSetCurrentSessionOverwritesNonConnectedSesssion(SessionFactoryScope scope) {
|
||||
public void testSetCurrentSessionOverwritesNonConnectedSession(
|
||||
SessionFactoryScope scope,
|
||||
LoggingInspectionsScope loggingScope) {
|
||||
Parent p = new Parent();
|
||||
Child c = new Child();
|
||||
p.children.add( c );
|
||||
@ -101,15 +113,15 @@ public void testSetCurrentSessionOverwritesNonConnectedSesssion(SessionFactorySc
|
||||
s2 -> {
|
||||
s2.getTransaction().begin();
|
||||
try {
|
||||
Triggerable triggerable = logInspection.watchForLogMessages( "HHH000470:" );
|
||||
assertFalse( triggerable.wasTriggered() );
|
||||
final MessageKeyWatcher watcher = loggingScope.getWatcher( "HHH000470", AbstractPersistentCollection.class );
|
||||
assertFalse( watcher.wasTriggered() );
|
||||
|
||||
// The following should trigger warning because we're setting a new session when the collection already
|
||||
// has a non-null session (and the collection is not "connected" to that session);
|
||||
// Since s1 was not flushed, the collection role will not be known (no way to test that other than inspection).
|
||||
s2.saveOrUpdate( p );
|
||||
|
||||
assertTrue( triggerable.wasTriggered() );
|
||||
assertTrue( watcher.wasTriggered() );
|
||||
|
||||
// collection's session should be overwritten with s2
|
||||
assertSame( s2, ( (AbstractPersistentCollection) p.children ).getSession() );
|
||||
@ -129,7 +141,9 @@ public void testSetCurrentSessionOverwritesNonConnectedSesssion(SessionFactorySc
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-9518")
|
||||
public void testSetCurrentSessionOverwritesNonConnectedSesssionFlushed(SessionFactoryScope scope) {
|
||||
public void testSetCurrentSessionOverwritesNonConnectedSessionFlushed(
|
||||
SessionFactoryScope scope,
|
||||
LoggingInspectionsScope loggingScope) {
|
||||
Parent p = new Parent();
|
||||
Child c = new Child();
|
||||
p.children.add( c );
|
||||
@ -160,15 +174,15 @@ public void testSetCurrentSessionOverwritesNonConnectedSesssionFlushed(SessionFa
|
||||
s2 -> {
|
||||
s2.getTransaction().begin();
|
||||
try {
|
||||
Triggerable triggerable = logInspection.watchForLogMessages( "HHH000470:" );
|
||||
assertFalse( triggerable.wasTriggered() );
|
||||
final MessageKeyWatcher watcher = loggingScope.getWatcher( "HHH000470", AbstractPersistentCollection.class );
|
||||
assertFalse( watcher.wasTriggered() );
|
||||
|
||||
// The following should trigger warning because we're setting a new session when the collection already
|
||||
// has a non-null session (and the collection is not "connected" to that session);
|
||||
// The collection role and key should be included in the message (no way to test that other than inspection).
|
||||
s2.saveOrUpdate( p );
|
||||
|
||||
assertTrue( triggerable.wasTriggered() );
|
||||
assertTrue( watcher.wasTriggered() );
|
||||
|
||||
// collection's session should be overwritten with s2
|
||||
assertSame( s2, ( (AbstractPersistentCollection) p.children ).getSession() );
|
||||
@ -189,7 +203,9 @@ public void testSetCurrentSessionOverwritesNonConnectedSesssionFlushed(SessionFa
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-9518")
|
||||
public void testUnsetSessionCannotOverwriteNonConnectedSesssion(SessionFactoryScope scope) {
|
||||
public void testUnsetSessionCannotOverwriteNonConnectedSession(
|
||||
SessionFactoryScope scope,
|
||||
LoggingInspectionsScope loggingScope) {
|
||||
Parent p = new Parent();
|
||||
Child c = new Child();
|
||||
p.children.add( c );
|
||||
@ -217,15 +233,15 @@ public void testUnsetSessionCannotOverwriteNonConnectedSesssion(SessionFactorySc
|
||||
s2 -> {
|
||||
s2.getTransaction().begin();
|
||||
try {
|
||||
Triggerable triggerable = logInspection.watchForLogMessages( "HHH000471:" );
|
||||
assertFalse( triggerable.wasTriggered() );
|
||||
final MessageKeyWatcher watcher = loggingScope.getWatcher( "HHH000471", AbstractPersistentCollection.class );
|
||||
assertFalse( watcher.wasTriggered() );
|
||||
|
||||
// The following should trigger warning because we're unsetting a different session.
|
||||
// We should not do this in practice; it is done here only to force the warning.
|
||||
// Since s1 was not flushed, the collection role will not be known (no way to test that).
|
||||
assertFalse( ( (PersistentCollection) p.children ).unsetSession( s2 ) );
|
||||
|
||||
assertTrue( triggerable.wasTriggered() );
|
||||
assertTrue( watcher.wasTriggered() );
|
||||
|
||||
// collection's session should still be s1
|
||||
assertSame( s1, ( (AbstractPersistentCollection) p.children ).getSession() );
|
||||
@ -247,7 +263,9 @@ public void testUnsetSessionCannotOverwriteNonConnectedSesssion(SessionFactorySc
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-9518")
|
||||
public void testUnsetSessionCannotOverwriteConnectedSesssion(SessionFactoryScope scope) {
|
||||
public void testUnsetSessionCannotOverwriteConnectedSession(
|
||||
SessionFactoryScope scope,
|
||||
LoggingInspectionsScope loggingScope) {
|
||||
Parent p = new Parent();
|
||||
Child c = new Child();
|
||||
p.children.add( c );
|
||||
@ -270,15 +288,15 @@ public void testUnsetSessionCannotOverwriteConnectedSesssion(SessionFactoryScope
|
||||
s2 -> {
|
||||
s2.getTransaction().begin();
|
||||
try {
|
||||
Triggerable triggerable = logInspection.watchForLogMessages( "HHH000471:" );
|
||||
assertFalse( triggerable.wasTriggered() );
|
||||
final MessageKeyWatcher watcher = loggingScope.getWatcher( "HHH000471", AbstractPersistentCollection.class );
|
||||
assertFalse( watcher.wasTriggered() );
|
||||
|
||||
// The following should trigger warning because we're unsetting a different session
|
||||
// We should not do this in practice; it is done here only to force the warning.
|
||||
// Since s1 was not flushed, the collection role will not be known (no way to test that).
|
||||
assertFalse( ( (PersistentCollection) p.children ).unsetSession( s2 ) );
|
||||
|
||||
assertTrue( triggerable.wasTriggered() );
|
||||
assertTrue( watcher.wasTriggered() );
|
||||
|
||||
// collection's session should still be s1
|
||||
assertSame( s1, ( (AbstractPersistentCollection) p.children ).getSession() );
|
||||
@ -298,7 +316,9 @@ public void testUnsetSessionCannotOverwriteConnectedSesssion(SessionFactoryScope
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-9518")
|
||||
public void testUnsetSessionCannotOverwriteConnectedSesssionFlushed(SessionFactoryScope scope) {
|
||||
public void testUnsetSessionCannotOverwriteConnectedSessionFlushed(
|
||||
SessionFactoryScope scope,
|
||||
LoggingInspectionsScope loggingScope) {
|
||||
Parent p = new Parent();
|
||||
Child c = new Child();
|
||||
p.children.add( c );
|
||||
@ -324,15 +344,15 @@ public void testUnsetSessionCannotOverwriteConnectedSesssionFlushed(SessionFacto
|
||||
s2 -> {
|
||||
s2.getTransaction().begin();
|
||||
try {
|
||||
Triggerable triggerable = logInspection.watchForLogMessages( "HHH000471:" );
|
||||
assertFalse( triggerable.wasTriggered() );
|
||||
final MessageKeyWatcher watcher = loggingScope.getWatcher( "HHH000471", AbstractPersistentCollection.class );
|
||||
assertFalse( watcher.wasTriggered() );
|
||||
|
||||
// The following should trigger warning because we're unsetting a different session
|
||||
// We should not do this in practice; it is done here only to force the warning.
|
||||
// The collection role and key should be included in the message (no way to test that other than inspection).
|
||||
assertFalse( ( (PersistentCollection) p.children ).unsetSession( s2 ) );
|
||||
|
||||
assertTrue( triggerable.wasTriggered() );
|
||||
assertTrue( watcher.wasTriggered() );
|
||||
|
||||
// collection's session should still be s1
|
||||
assertSame( s1, ( (AbstractPersistentCollection) p.children ).getSession() );
|
||||
|
30
hibernate-core/src/test/java/org/hibernate/orm/test/jdbc/env/LobCreationCheckSkipTest.java
vendored
Normal file
30
hibernate-core/src/test/java/org/hibernate/orm/test/jdbc/env/LobCreationCheckSkipTest.java
vendored
Normal file
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.orm.test.jdbc.env;
|
||||
|
||||
import org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl;
|
||||
|
||||
import org.hibernate.testing.orm.junit.Jpa;
|
||||
import org.hibernate.testing.orm.junit.Logger;
|
||||
import org.hibernate.testing.orm.junit.MessageKeyInspection;
|
||||
import org.hibernate.testing.orm.junit.MessageKeyWatcher;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
@Jpa
|
||||
@MessageKeyInspection( messageKey = "HHH000424", logger = @Logger( loggerNameClass = LobCreatorBuilderImpl.class) )
|
||||
public class LobCreationCheckSkipTest {
|
||||
@Test
|
||||
public void test(MessageKeyWatcher watcher) {
|
||||
assertFalse( watcher.wasTriggered() );
|
||||
}
|
||||
}
|
@ -9,6 +9,7 @@
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import org.hibernate.AssertionFailure;
|
||||
|
||||
import org.jboss.logging.BasicLogger;
|
||||
import org.jboss.logging.DelegatingBasicLogger;
|
||||
|
||||
@ -24,6 +25,25 @@ public final class LogInspectionHelper {
|
||||
private LogInspectionHelper() {
|
||||
}
|
||||
|
||||
private static Log4DelegatingLogger convertType(Object loggerReference) {
|
||||
if ( loggerReference instanceof DelegatingBasicLogger) {
|
||||
//Most loggers generated via the annotation processor are of this type
|
||||
DelegatingBasicLogger wrapper = (DelegatingBasicLogger) loggerReference;
|
||||
try {
|
||||
return extractFromWrapper( wrapper );
|
||||
}
|
||||
catch (Exception cause) {
|
||||
throw new RuntimeException( cause );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! ( loggerReference instanceof Log4DelegatingLogger ) ) {
|
||||
throw new AssertionFailure( "Unexpected log type: JBoss Logger didn't register the custom TestableLoggerProvider as logger provider" );
|
||||
}
|
||||
|
||||
return (Log4DelegatingLogger) loggerReference;
|
||||
}
|
||||
|
||||
public static void registerListener(LogListener listener, BasicLogger log) {
|
||||
convertType( log ).registerListener( listener );
|
||||
}
|
||||
|
@ -23,8 +23,14 @@ public class JUnitHelper {
|
||||
public static ExtensionContext.Store locateExtensionStore(
|
||||
Class<? extends Extension> extensionClass,
|
||||
ExtensionContext context,
|
||||
Object testInstance) {
|
||||
return context.getStore( create( extensionClass.getName(), testInstance ) );
|
||||
Object scopeObject) {
|
||||
return context.getStore( create( extensionClass.getName(), scopeObject ) );
|
||||
}
|
||||
|
||||
public static ExtensionContext.Store locateExtensionStore(
|
||||
ExtensionContext context,
|
||||
Object... scopeRefs) {
|
||||
return context.getStore( create( scopeRefs ) );
|
||||
}
|
||||
|
||||
private JUnitHelper() {
|
||||
|
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.testing.orm.junit;
|
||||
|
||||
/**
|
||||
* Must specify one of {@link #loggerNameClass} or {@link #loggerName()}
|
||||
*/
|
||||
public @interface Logger {
|
||||
// I think we can actually look up the "bare" Logger and still get the same
|
||||
// capability in terms of register listeners
|
||||
//Class<? extends BasicLogger> messageLoggerClass() default CoreMessageLogger.class;
|
||||
|
||||
/**
|
||||
* The `Class` used as the base for the logger name.
|
||||
*
|
||||
* @see org.jboss.logging.Logger#getLogger(Class)
|
||||
*/
|
||||
Class<?> loggerNameClass() default void.class;
|
||||
|
||||
/**
|
||||
* The `Class` used as the base for the logger name.
|
||||
*
|
||||
* @see org.jboss.logging.Logger#getLogger(Class)
|
||||
*/
|
||||
String loggerName() default "";
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.testing.orm.junit;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
/**
|
||||
* Injects the ability to watch multiple for log messages being triggered.
|
||||
*
|
||||
* Only available at the class-level
|
||||
*
|
||||
* For watching a single message-key, {@link MessageKeyInspection} is a
|
||||
* better option.
|
||||
*/
|
||||
@Inherited
|
||||
@Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
|
||||
@ExtendWith( LoggingInspectionsExtension.class )
|
||||
@ExtendWith( LoggingInspectionsScopeResolver.class )
|
||||
public @interface LoggingInspections {
|
||||
Message[] messages() default {};
|
||||
|
||||
@interface Message {
|
||||
/**
|
||||
* The message-key to watch for. The message-key is the combination of
|
||||
* {@link org.jboss.logging.annotations.MessageLogger#projectCode()}
|
||||
* and {@link org.jboss.logging.annotations.Message#id()} used by
|
||||
* JBoss Logging to prefix each messaged log event
|
||||
*/
|
||||
String messageKey();
|
||||
|
||||
/**
|
||||
* Descriptor of the log messages to watch for
|
||||
*/
|
||||
Logger[] loggers() default {};
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.testing.orm.junit;
|
||||
|
||||
import org.junit.jupiter.api.extension.BeforeEachCallback;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext.Store;
|
||||
import org.junit.jupiter.api.extension.TestInstancePostProcessor;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class LoggingInspectionsExtension implements TestInstancePostProcessor, BeforeEachCallback {
|
||||
private static final String KEY = LoggingInspectionsExtension.class.getName();
|
||||
|
||||
// todo (6.0) : have this implement `AfterEachCallback` support to reset after each test?
|
||||
|
||||
@Override
|
||||
public void postProcessTestInstance(
|
||||
Object testInstance,
|
||||
ExtensionContext context) {
|
||||
resolveLoggingInspectionScope( testInstance, context );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeEach(ExtensionContext context) {
|
||||
final Store extensionStore = locateExtensionStore( context.getRequiredTestInstance(), context );
|
||||
final LoggingInspectionsScope existing = (LoggingInspectionsScope) extensionStore.get( KEY );
|
||||
if ( existing != null ) {
|
||||
existing.resetWatchers();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public static LoggingInspectionsScope resolveLoggingInspectionScope(Object testInstance, ExtensionContext context) {
|
||||
final Store extensionStore = locateExtensionStore( testInstance, context );
|
||||
final Object existing = extensionStore.get( KEY );
|
||||
if ( existing != null ) {
|
||||
return (LoggingInspectionsScope) existing;
|
||||
}
|
||||
|
||||
// we'll need to create it...
|
||||
|
||||
// find the annotation
|
||||
final LoggingInspections loggingInspections = testInstance.getClass().getAnnotation( LoggingInspections.class );
|
||||
|
||||
// Create the scope and add to context store
|
||||
final LoggingInspectionsScope scope = new LoggingInspectionsScope( loggingInspections, context );
|
||||
extensionStore.put( KEY, scope );
|
||||
|
||||
return scope;
|
||||
}
|
||||
|
||||
private static Store locateExtensionStore(Object testInstance, ExtensionContext context) {
|
||||
return JUnitHelper.locateExtensionStore( EntityManagerFactoryExtension.class, context, testInstance );
|
||||
}
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.testing.orm.junit;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
|
||||
/**
|
||||
* Manages all of the MessageKeyWatcher defined by LoggingInspectionsScope
|
||||
*/
|
||||
public class LoggingInspectionsScope {
|
||||
private final Map<String, Map<String,MessageKeyWatcherImpl>> watcherMap = new HashMap<>();
|
||||
|
||||
|
||||
public LoggingInspectionsScope(LoggingInspections loggingInspections, ExtensionContext context) {
|
||||
for ( int i = 0; i < loggingInspections.messages().length; i++ ) {
|
||||
final LoggingInspections.Message message = loggingInspections.messages()[ i ];
|
||||
|
||||
final String messageKey = message.messageKey().trim();
|
||||
assert ! messageKey.isEmpty();
|
||||
|
||||
if ( message.loggers().length == 0 ) {
|
||||
return;
|
||||
}
|
||||
|
||||
final Map<String, MessageKeyWatcherImpl> messageKeyWatcherMap;
|
||||
final Map<String, MessageKeyWatcherImpl> existingMessageKeyWatcherMap = watcherMap.get( messageKey );
|
||||
if ( existingMessageKeyWatcherMap != null ) {
|
||||
messageKeyWatcherMap = existingMessageKeyWatcherMap;
|
||||
}
|
||||
else {
|
||||
messageKeyWatcherMap = new HashMap<>();
|
||||
watcherMap.put( messageKey, messageKeyWatcherMap );
|
||||
}
|
||||
|
||||
for ( Logger logger : message.loggers() ) {
|
||||
final String loggerKey = MessageKeyWatcherImpl.loggerKey( logger );
|
||||
final MessageKeyWatcherImpl watcher;
|
||||
final MessageKeyWatcherImpl existingWatcher = messageKeyWatcherMap.get( loggerKey );
|
||||
if ( existingWatcher != null ) {
|
||||
watcher = existingWatcher;
|
||||
}
|
||||
else {
|
||||
watcher = new MessageKeyWatcherImpl( messageKey );
|
||||
messageKeyWatcherMap.put( loggerKey, watcher );
|
||||
}
|
||||
watcher.addLogger( logger );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void resetWatchers() {
|
||||
watcherMap.forEach(
|
||||
(messageKey,loggerMap) -> loggerMap.forEach( (logger,watcher) -> watcher.reset() )
|
||||
);
|
||||
}
|
||||
|
||||
public MessageKeyWatcher getWatcher(String messageKey, String loggerName) {
|
||||
final Map<String, MessageKeyWatcherImpl> messageKeyWatcherMap = watcherMap.get( messageKey );
|
||||
return messageKeyWatcherMap.get( loggerName );
|
||||
}
|
||||
|
||||
public MessageKeyWatcher getWatcher(String messageKey, Class<?> loggerNameClass) {
|
||||
final Map<String, MessageKeyWatcherImpl> messageKeyWatcherMap = watcherMap.get( messageKey );
|
||||
return messageKeyWatcherMap.get( loggerNameClass.getName() );
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.testing.orm.junit;
|
||||
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
import org.junit.jupiter.api.extension.ParameterContext;
|
||||
import org.junit.jupiter.api.extension.ParameterResolutionException;
|
||||
import org.junit.jupiter.api.extension.ParameterResolver;
|
||||
|
||||
/**
|
||||
* ParameterResolver implementation for resolving
|
||||
* {@link LoggingInspectionsScope} ParameterResolver
|
||||
*/
|
||||
public class LoggingInspectionsScopeResolver implements ParameterResolver {
|
||||
@Override
|
||||
public boolean supportsParameter(
|
||||
ParameterContext parameterContext,
|
||||
ExtensionContext extensionContext) {
|
||||
return LoggingInspectionsScope.class.isAssignableFrom(
|
||||
parameterContext.getParameter().getType()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LoggingInspectionsScope resolveParameter(
|
||||
ParameterContext parameterContext,
|
||||
ExtensionContext extensionContext) throws ParameterResolutionException {
|
||||
return LoggingInspectionsExtension.resolveLoggingInspectionScope(
|
||||
extensionContext.getRequiredTestInstance(),
|
||||
extensionContext
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.testing.orm.junit;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
/**
|
||||
* Injects the ability to watch for a log messages being triggered.
|
||||
*
|
||||
* For watching a multiple message-keys, see {@link LoggingInspections}
|
||||
*/
|
||||
@Inherited
|
||||
@Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE, ElementType.METHOD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
|
||||
@ExtendWith( MessageKeyInspectionExtension.class )
|
||||
@ExtendWith( MessageKeyWatcherResolver.class )
|
||||
public @interface MessageKeyInspection {
|
||||
/**
|
||||
* The message key to look for.
|
||||
*
|
||||
* @apiNote This is effectively a starts-with check. We simply check
|
||||
* that the logged message starts with the value from here
|
||||
*/
|
||||
String messageKey();
|
||||
|
||||
/**
|
||||
* The logger to watch on
|
||||
*/
|
||||
Logger logger();
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.testing.orm.junit;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.junit.jupiter.api.extension.BeforeEachCallback;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
import org.junit.jupiter.api.extension.TestInstancePostProcessor;
|
||||
|
||||
import static org.junit.jupiter.api.extension.ExtensionContext.Namespace.create;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class MessageKeyInspectionExtension implements TestInstancePostProcessor, BeforeEachCallback {
|
||||
public static final String KEY = LoggingInspectionsExtension.class.getName();
|
||||
|
||||
@Override
|
||||
public void postProcessTestInstance(Object testInstance, ExtensionContext context) {
|
||||
// Process the MessageKeyInspection annotation that happens at the class-level
|
||||
|
||||
final ExtensionContext.Store instanceStore = resolveInstanceStore( testInstance, context );
|
||||
|
||||
final Object existing = instanceStore.get( KEY );
|
||||
if ( existing != null ) {
|
||||
// odd, but there would be nothing to do
|
||||
return;
|
||||
}
|
||||
|
||||
// find the annotation, create the watcher and add it to the context
|
||||
final MessageKeyInspection inspection = testInstance.getClass().getAnnotation( MessageKeyInspection.class );
|
||||
final MessageKeyWatcherImpl watcher = new MessageKeyWatcherImpl( inspection.messageKey() );
|
||||
watcher.addLogger( inspection.logger() );
|
||||
|
||||
instanceStore.put( KEY, watcher );
|
||||
}
|
||||
|
||||
public static ExtensionContext.Store resolveInstanceStore(Object testInstance, ExtensionContext context) {
|
||||
final ExtensionContext.Namespace instanceStoreNamespace = create( testInstance );
|
||||
return context.getStore( instanceStoreNamespace );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeEach(ExtensionContext context) {
|
||||
final Method method = context.getRequiredTestMethod();
|
||||
|
||||
final ExtensionContext.Store methodStore = resolveMethodStore( context );
|
||||
final MessageKeyWatcher existing = (MessageKeyWatcher) methodStore.get( KEY );
|
||||
if ( existing != null ) {
|
||||
prepareForUse( existing );
|
||||
// already there - nothing to do
|
||||
return;
|
||||
}
|
||||
|
||||
// if the test-method is annotated, use a one-off watcher for that message
|
||||
final MessageKeyInspection inspectionAnn = method.getAnnotation( MessageKeyInspection.class );
|
||||
if ( inspectionAnn != null ) {
|
||||
final MessageKeyWatcherImpl watcher = new MessageKeyWatcherImpl( inspectionAnn.messageKey() );
|
||||
watcher.addLogger( inspectionAnn.logger() );
|
||||
methodStore.put( KEY, watcher );
|
||||
prepareForUse( watcher );
|
||||
return;
|
||||
}
|
||||
|
||||
// look for a class/instance-level watcher
|
||||
final ExtensionContext.Store instanceStore = resolveInstanceStore( context.getRequiredTestInstance(), context );
|
||||
final MessageKeyWatcher instanceLevelWatcher = (MessageKeyWatcher) instanceStore.get( KEY );
|
||||
if ( instanceLevelWatcher != null ) {
|
||||
methodStore.put( KEY, instanceLevelWatcher );
|
||||
prepareForUse( instanceLevelWatcher );
|
||||
}
|
||||
}
|
||||
|
||||
private void prepareForUse(MessageKeyWatcher watcher) {
|
||||
watcher.reset();
|
||||
}
|
||||
|
||||
public static ExtensionContext.Store resolveMethodStore(ExtensionContext context) {
|
||||
final ExtensionContext.Namespace instanceStoreNamespace = create( context.getRequiredTestMethod() );
|
||||
return context.getStore( instanceStoreNamespace );
|
||||
}
|
||||
|
||||
public static MessageKeyWatcher getWatcher(ExtensionContext context) {
|
||||
final ExtensionContext.Store methodStore = resolveMethodStore( context );
|
||||
final Object ref = methodStore.get( KEY );
|
||||
if ( ref == null ) {
|
||||
throw new IllegalStateException( "No watcher available" );
|
||||
}
|
||||
return (MessageKeyWatcher) ref;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.testing.orm.junit;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public interface MessageKeyWatcher {
|
||||
String getMessageKey();
|
||||
|
||||
boolean wasTriggered();
|
||||
|
||||
List<String> getTriggeredMessages();
|
||||
|
||||
String getFirstTriggeredMessage();
|
||||
|
||||
void reset();
|
||||
}
|
@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.testing.orm.junit;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.testing.logger.LogInspectionHelper;
|
||||
import org.hibernate.testing.logger.LogListener;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
/**
|
||||
* MessageIdWatcher implementation
|
||||
*/
|
||||
public class MessageKeyWatcherImpl implements MessageKeyWatcher, LogListener {
|
||||
private final String messageKey;
|
||||
private final List<String> loggerNames = new ArrayList<>();
|
||||
private final List<String> triggeredMessages = new ArrayList<>();
|
||||
|
||||
public MessageKeyWatcherImpl(String messageKey) {
|
||||
this.messageKey = messageKey;
|
||||
}
|
||||
|
||||
public void addLoggerName(String name) {
|
||||
loggerNames.add( name );
|
||||
}
|
||||
|
||||
public void addLogger(org.hibernate.testing.orm.junit.Logger loggerAnn) {
|
||||
final Logger logger;
|
||||
if ( loggerAnn.loggerNameClass() != void.class ) {
|
||||
logger = Logger.getLogger( loggerAnn.loggerNameClass() );
|
||||
}
|
||||
else if ( "".equals( loggerAnn.loggerName().trim() ) ) {
|
||||
logger = Logger.getLogger( loggerAnn.loggerName().trim() );
|
||||
}
|
||||
else {
|
||||
throw new IllegalStateException(
|
||||
"@LoggingInspections for prefix '" + messageKey +
|
||||
"' did not specify proper Logger name. Use `@LoggingInspections#loggerName" +
|
||||
" or `@LoggingInspections#loggerNameClass`"
|
||||
);
|
||||
}
|
||||
|
||||
LogInspectionHelper.registerListener( this, logger );
|
||||
}
|
||||
|
||||
public static String loggerKey(org.hibernate.testing.orm.junit.Logger loggerAnn) {
|
||||
final Logger logger;
|
||||
if ( loggerAnn.loggerNameClass() != void.class ) {
|
||||
logger = Logger.getLogger( loggerAnn.loggerNameClass() );
|
||||
}
|
||||
else if ( ! "".equals( loggerAnn.loggerName().trim() ) ) {
|
||||
logger = Logger.getLogger( loggerAnn.loggerName().trim() );
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException(
|
||||
"`@Logger` must specify either `#loggerNameClass` or `#loggerName`"
|
||||
);
|
||||
}
|
||||
|
||||
return logger.getName();
|
||||
}
|
||||
|
||||
public List<String> getLoggerNames() {
|
||||
return loggerNames;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessageKey() {
|
||||
return messageKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean wasTriggered() {
|
||||
return ! triggeredMessages.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTriggeredMessages() {
|
||||
return triggeredMessages;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFirstTriggeredMessage() {
|
||||
return triggeredMessages.isEmpty() ? null : triggeredMessages.get( 0 );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
triggeredMessages.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loggedEvent(Logger.Level level, String renderedMessage, Throwable thrown) {
|
||||
if ( renderedMessage != null ) {
|
||||
if ( renderedMessage.startsWith( messageKey ) ) {
|
||||
triggeredMessages.add( renderedMessage );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MessageIdWatcherImpl{" +
|
||||
"messageKey='" + messageKey + '\'' +
|
||||
", loggerNames=" + loggerNames +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.testing.orm.junit;
|
||||
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
import org.junit.jupiter.api.extension.ParameterContext;
|
||||
import org.junit.jupiter.api.extension.ParameterResolutionException;
|
||||
import org.junit.jupiter.api.extension.ParameterResolver;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class MessageKeyWatcherResolver implements ParameterResolver {
|
||||
@Override
|
||||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
|
||||
return MessageKeyWatcher.class.isAssignableFrom( parameterContext.getParameter().getType() );
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageKeyWatcher resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
|
||||
return MessageKeyInspectionExtension.getWatcher( extensionContext );
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user