HHH-10513 - Add test for issue

This commit is contained in:
Andrea Boriero 2016-02-11 18:28:45 +00:00
parent 45e76918e8
commit 70c6abcbae
3 changed files with 155 additions and 6 deletions

View File

@ -0,0 +1,131 @@
/*
* 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.test.locking.warning;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import org.jboss.byteman.contrib.bmunit.BMRule;
import org.jboss.byteman.contrib.bmunit.BMRules;
import org.jboss.byteman.contrib.bmunit.BMUnitRunner;
import org.jboss.logging.Logger;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.loader.Loader;
import org.hibernate.query.Query;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.testing.logger.LoggerInspectionRule;
import org.hibernate.testing.logger.Triggerable;
import static org.junit.Assert.assertFalse;
/**
* @author Andrea Boriero
*/
@TestForIssue(jiraKey = "HHH-10513")
@RunWith(BMUnitRunner.class)
public class LockNoneWarmingTest extends BaseCoreFunctionalTestCase {
private Triggerable triggerable;
@Rule
public LoggerInspectionRule logInspection = new LoggerInspectionRule(
Logger.getMessageLogger( CoreMessageLogger.class, Loader.class.getName() )
);
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {Item.class, Bid.class};
}
@Before
public void setUp(){
buildSessionFactory();
final Set messagesPrefixes = new HashSet<>( );
messagesPrefixes.add( "HHH000444" );
messagesPrefixes.add( "HHH000445" );
triggerable = logInspection.watchForLogMessages( messagesPrefixes );
try (Session s = openSession();) {
Transaction tx = s.beginTransaction();
try {
Item item = new Item();
item.name = "ZZZZ";
s.persist( item );
tx.commit();
}
catch (Exception e) {
if ( tx.isActive() ) {
tx.rollback();
}
}
}
}
@After
public void tearDown(){
releaseSessionFactory();
triggerable.reset();
}
@Test
@BMRules(rules = {
@BMRule(targetClass = "org.hibernate.dialect.Dialect",
targetMethod = "useFollowOnLocking",
action = "return true",
name = "H2DialectUseFollowOnLocking")
})
public void testQuerySetLockModeNONEDoNotLogAWarnMessageWhenTheDialectUseFollowOnLockingIsTrue() {
try (Session s = openSession();) {
final Query query = s.createQuery( "from Item i join i.bids b where name = :name" );
query.setParameter( "name", "ZZZZ" );
query.setLockMode( "i", LockMode.NONE );
query.setLockMode( "b", LockMode.NONE );
query.list();
assertFalse( triggerable.triggerMessage(), triggerable.wasTriggered() );
}
}
@Entity(name = "Item")
@Table(name = "ITEM")
public static class Item implements Serializable {
@Id
String name;
@OneToMany(mappedBy = "item", fetch = FetchType.EAGER)
Set<Bid> bids = new HashSet<Bid>();
}
@Entity(name = "Bid")
@Table(name = "BID")
public static class Bid implements Serializable {
@Id
float amount;
@Id
@ManyToOne
Item item;
}
}

View File

@ -6,6 +6,8 @@
*/
package org.hibernate.testing.logger;
import java.util.Set;
import org.junit.rules.ExternalResource;
import org.jboss.logging.BasicLogger;
@ -38,4 +40,10 @@ public final class LoggerInspectionRule extends ExternalResource {
return listener;
}
public Triggerable watchForLogMessages(Set<String> prefixes) {
TriggerOnPrefixLogListener listener = new TriggerOnPrefixLogListener( prefixes );
registerListener( listener );
return listener;
}
}

View File

@ -6,6 +6,8 @@
*/
package org.hibernate.testing.logger;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
@ -13,19 +15,27 @@ import org.jboss.logging.Logger.Level;
final class TriggerOnPrefixLogListener implements LogListener, Triggerable {
private final String expectedPrefix;
private Set<String> expectedPrefixes = new HashSet<>();
private final AtomicBoolean triggered = new AtomicBoolean( false );
private final AtomicReference<String> triggerMessage = new AtomicReference<String>( null );
public TriggerOnPrefixLogListener(String expectedPrefix) {
this.expectedPrefix = expectedPrefix;
expectedPrefixes.add( expectedPrefix );
}
public TriggerOnPrefixLogListener(Set<String> expectedPrefixes) {
this.expectedPrefixes = expectedPrefixes;
}
@Override
public void loggedEvent(Level level, String renderedMessage, Throwable thrown) {
if ( renderedMessage != null && renderedMessage.startsWith( expectedPrefix ) ) {
triggered.set( true );
triggerMessage.set(renderedMessage);
if ( renderedMessage != null ) {
for ( String expectedPrefix : expectedPrefixes ) {
if ( renderedMessage.startsWith( expectedPrefix ) ) {
triggered.set( true );
triggerMessage.set( renderedMessage );
}
}
}
}
@ -42,6 +52,6 @@ final class TriggerOnPrefixLogListener implements LogListener, Triggerable {
@Override
public void reset() {
triggered.set( false );
triggerMessage.set(null);
triggerMessage.set( null );
}
}