From 60c1b23c7b72cca921cb10be98375e67ac12e99f Mon Sep 17 00:00:00 2001 From: Hardy Ferentschik Date: Fri, 16 Mar 2012 11:00:44 +0100 Subject: [PATCH] HHH-6271 Introducing Byteman in order to really test that a certain log message gets never called. --- build.gradle | 19 +++-- .../xml/ejb3/OrmVersion1SupportedTest.java | 25 ++++++- hibernate-testing/hibernate-testing.gradle | 3 + .../testing/byteman/BytemanHelper.java | 69 +++++++++++++++++++ .../junit4/BaseCoreFunctionalTestCase.java | 2 +- 5 files changed, 110 insertions(+), 8 deletions(-) create mode 100644 hibernate-testing/src/main/java/org/hibernate/testing/byteman/BytemanHelper.java diff --git a/build.gradle b/build.gradle index 66befc0315..e8f28eff23 100644 --- a/build.gradle +++ b/build.gradle @@ -49,6 +49,7 @@ idea { slf4jVersion = '1.6.1' junitVersion = '4.10' h2Version = '1.2.145' +bytemanVersion = '1.5.2' libraries = [ // Ant @@ -93,12 +94,15 @@ libraries = [ jcl: 'commons-logging:commons-logging:99.0-does-not-exist', - junit: "junit:junit:${junitVersion}", - jpa_modelgen: 'org.hibernate:hibernate-jpamodelgen:1.1.1.Final', - shrinkwrap_api: 'org.jboss.shrinkwrap:shrinkwrap-api:1.0.0-beta-6', - shrinkwrap: 'org.jboss.shrinkwrap:shrinkwrap-impl-base:1.0.0-beta-6', - validator: 'org.hibernate:hibernate-validator:4.2.0.Final', - h2: "com.h2database:h2:${h2Version}" + junit: "junit:junit:${junitVersion}", + byteman: "org.jboss.byteman:byteman:${bytemanVersion}", + byteman_install: "org.jboss.byteman:byteman-install:${bytemanVersion}", + byteman_bmunit: "org.jboss.byteman:byteman-bmunit:${bytemanVersion}", + jpa_modelgen: 'org.hibernate:hibernate-jpamodelgen:1.1.1.Final', + shrinkwrap_api: 'org.jboss.shrinkwrap:shrinkwrap-api:1.0.0-beta-6', + shrinkwrap: 'org.jboss.shrinkwrap:shrinkwrap-impl-base:1.0.0-beta-6', + validator: 'org.hibernate:hibernate-validator:4.2.0.Final', + h2: "com.h2database:h2:${h2Version}" ] @@ -145,6 +149,9 @@ subprojects { subProject -> dependencies { compile( libraries.logging ) testCompile( libraries.junit ) + testCompile( libraries.byteman ) + testCompile( libraries.byteman_install ) + testCompile( libraries.byteman_bmunit ) testRuntime( libraries.slf4j_api ) testRuntime( libraries.slf4j_log4j12 ) testRuntime( libraries.jcl_slf4j ) diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/annotations/xml/ejb3/OrmVersion1SupportedTest.java b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/xml/ejb3/OrmVersion1SupportedTest.java index b6c21a0b58..425e08ea35 100644 --- a/hibernate-core/src/matrix/java/org/hibernate/test/annotations/xml/ejb3/OrmVersion1SupportedTest.java +++ b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/xml/ejb3/OrmVersion1SupportedTest.java @@ -23,19 +23,40 @@ */ package org.hibernate.test.annotations.xml.ejb3; +import org.jboss.byteman.contrib.bmunit.BMRule; +import org.jboss.byteman.contrib.bmunit.BMRules; +import org.jboss.byteman.contrib.bmunit.BMUnitRunner; import org.junit.Test; +import org.junit.runner.RunWith; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.byteman.BytemanHelper; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import static org.junit.Assert.assertEquals; @TestForIssue(jiraKey = "HHH-6271") +@RunWith(BMUnitRunner.class) public class OrmVersion1SupportedTest extends BaseCoreFunctionalTestCase { @Test - public void testOrm1Support() throws Exception { + @BMRules(rules = { + @BMRule(targetClass = "org.hibernate.internal.CoreMessageLogger_$logger", + targetMethod = "parsingXmlError", + helper = "org.hibernate.testing.byteman.BytemanHelper", + action = "countInvocation()", + name = "testOrm1Support"), + @BMRule(targetClass = "org.hibernate.internal.CoreMessageLogger_$logger", + targetMethod = "parsingXmlErrorForFile", + helper = "org.hibernate.testing.byteman.BytemanHelper", + action = "countInvocation()", + name = "testOrm1Support") + }) + public void testOrm1Support() { + // need to call buildSessionFactory, because this test is not using org.hibernate.testing.junit4.CustomRunner + buildSessionFactory(); + Session s = openSession(); Transaction tx = s.beginTransaction(); Light light = new Light(); @@ -47,6 +68,8 @@ public class OrmVersion1SupportedTest extends BaseCoreFunctionalTestCase { assertEquals( 1, s.getNamedQuery( "find.the.light" ).list().size() ); tx.rollback(); s.close(); + + assertEquals( "HHH00196 should not be called", 0, BytemanHelper.getAndResetInvocationCount() ); } @Override diff --git a/hibernate-testing/hibernate-testing.gradle b/hibernate-testing/hibernate-testing.gradle index a5d5f2336b..7c96881cdb 100644 --- a/hibernate-testing/hibernate-testing.gradle +++ b/hibernate-testing/hibernate-testing.gradle @@ -3,6 +3,9 @@ apply plugin: 'java' dependencies { compile project( ':hibernate-core' ) compile "junit:junit:4.8.2" + compile( libraries.byteman ) + compile( libraries.byteman_install ) + compile( libraries.byteman_bmunit ) compile "com.experlog:xapool:1.5.0" compile ( "org.jboss.jbossts:jbossjta:4.15.1.Final" ) { transitive=false; diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/byteman/BytemanHelper.java b/hibernate-testing/src/main/java/org/hibernate/testing/byteman/BytemanHelper.java new file mode 100644 index 0000000000..4c77cfd16e --- /dev/null +++ b/hibernate-testing/src/main/java/org/hibernate/testing/byteman/BytemanHelper.java @@ -0,0 +1,69 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2012, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.testing.byteman; + +import java.util.concurrent.atomic.AtomicInteger; + +import org.jboss.byteman.rule.Rule; +import org.jboss.byteman.rule.helper.Helper; +import org.jboss.logging.Logger; + +/** + * @author Sanne Grinovero (C) 2011 Red Hat Inc. + * @author Hardy Ferentschik + */ +public class BytemanHelper extends Helper { + private static final Logger log = Logger.getLogger( BytemanHelper.class ); + + public static final AtomicInteger counter = new AtomicInteger(); + + protected BytemanHelper(Rule rule) { + super( rule ); + } + + public void sleepASecond() { + try { + log.info( "Byteman rule triggered: sleeping a second" ); + Thread.sleep( 1000 ); + } + catch ( InterruptedException e ) { + Thread.currentThread().interrupt(); + log.error( "unexpected interruption", e ); + } + } + + public void throwNPE(String message) { + //Needed because of Bug BYTEMAN-173: can't simply inject a NPE from the rule + throw new NullPointerException( message ); + } + + public void countInvocation() { + log.debug( "Increment call count" ); + counter.incrementAndGet(); + } + + public static int getAndResetInvocationCount() { + return counter.getAndSet( 0 ); + } +} diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseCoreFunctionalTestCase.java b/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseCoreFunctionalTestCase.java index bf21ab933d..ac2de4b2d6 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseCoreFunctionalTestCase.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseCoreFunctionalTestCase.java @@ -127,7 +127,7 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase { @BeforeClassOnce @SuppressWarnings( {"UnusedDeclaration"}) - private void buildSessionFactory() { + protected void buildSessionFactory() { // for now, build the configuration to get all the property settings configuration = constructAndConfigureConfiguration(); serviceRegistry = buildServiceRegistry( configuration );