From 86ba2beb290e022937d65c2603c00de1c60129a0 Mon Sep 17 00:00:00 2001 From: Lukasz Antoniak Date: Mon, 17 Dec 2012 14:48:42 +0100 Subject: [PATCH] HHH-6841 - @SkipForDialects and @RequiresDialects --- .../hibernate/testing/RequiresDialect.java | 2 + .../hibernate/testing/RequiresDialects.java | 18 +++++++++ .../org/hibernate/testing/SkipForDialect.java | 2 + .../hibernate/testing/SkipForDialects.java | 18 +++++++++ .../testing/junit4/CustomRunner.java | 22 +++++++---- .../org/hibernate/testing/junit4/Helper.java | 37 +++++++++++++++++++ 6 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 hibernate-testing/src/main/java/org/hibernate/testing/RequiresDialects.java create mode 100644 hibernate-testing/src/main/java/org/hibernate/testing/SkipForDialects.java diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/RequiresDialect.java b/hibernate-testing/src/main/java/org/hibernate/testing/RequiresDialect.java index 3371491ee9..2217068eff 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/RequiresDialect.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/RequiresDialect.java @@ -34,6 +34,8 @@ import org.hibernate.dialect.Dialect; * Annotation used to indicate that a test should be run only when run against the * indicated dialects. * + * @see RequiresDialects + * * @author Hardy Ferentschik */ @Target({ ElementType.METHOD, ElementType.TYPE }) diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/RequiresDialects.java b/hibernate-testing/src/main/java/org/hibernate/testing/RequiresDialects.java new file mode 100644 index 0000000000..3affd4d0c4 --- /dev/null +++ b/hibernate-testing/src/main/java/org/hibernate/testing/RequiresDialects.java @@ -0,0 +1,18 @@ +package org.hibernate.testing; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Plural annotation for {@link RequiresDialect}. + * Useful when test needs to be run against more than one dialect because of a different reason. + * + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.METHOD, ElementType.TYPE }) +public @interface RequiresDialects { + RequiresDialect[] value(); +} diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/SkipForDialect.java b/hibernate-testing/src/main/java/org/hibernate/testing/SkipForDialect.java index 2010706e6e..428185761e 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/SkipForDialect.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/SkipForDialect.java @@ -34,6 +34,8 @@ import org.hibernate.dialect.Dialect; * Annotation used to indicate that a test should be skipped when run against the * indicated dialects. * + * @see SkipForDialects + * * @author Hardy Ferentschik * @author Steve Ebersole */ diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/SkipForDialects.java b/hibernate-testing/src/main/java/org/hibernate/testing/SkipForDialects.java new file mode 100644 index 0000000000..5cc0d63162 --- /dev/null +++ b/hibernate-testing/src/main/java/org/hibernate/testing/SkipForDialects.java @@ -0,0 +1,18 @@ +package org.hibernate.testing; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Plural annotation for {@link SkipForDialect}. + * Useful when more than one dialect needs to be skipped because of a different reason. + * + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.METHOD, ElementType.TYPE }) +public @interface SkipForDialects { + SkipForDialect[] value(); +} diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/junit4/CustomRunner.java b/hibernate-testing/src/main/java/org/hibernate/testing/junit4/CustomRunner.java index 201247b49c..5dbe56eb79 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/junit4/CustomRunner.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/junit4/CustomRunner.java @@ -25,8 +25,10 @@ package org.hibernate.testing.junit4; import java.lang.annotation.Annotation; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.Comparator; +import java.util.LinkedList; import java.util.List; import org.jboss.logging.Logger; @@ -44,8 +46,10 @@ import org.hibernate.testing.DialectCheck; import org.hibernate.testing.FailureExpected; import org.hibernate.testing.RequiresDialect; import org.hibernate.testing.RequiresDialectFeature; +import org.hibernate.testing.RequiresDialects; import org.hibernate.testing.Skip; import org.hibernate.testing.SkipForDialect; +import org.hibernate.testing.SkipForDialects; /** * The Hibernate-specific {@link org.junit.runner.Runner} implementation which layers {@link ExtendedFrameworkMethod} @@ -151,7 +155,9 @@ public class CustomRunner extends BlockJUnit4ClassRunner { } protected void sortMethods(List computedTestMethods) { - if( CollectionHelper.isEmpty( computedTestMethods ))return; + if ( CollectionHelper.isEmpty( computedTestMethods ) ) { + return; + } Collections.sort( computedTestMethods, new Comparator() { @Override public int compare(FrameworkMethod o1, FrameworkMethod o2) { @@ -228,9 +234,10 @@ public class CustomRunner extends BlockJUnit4ClassRunner { } } - // @SkipForDialect - SkipForDialect skipForDialectAnn = Helper.locateAnnotation( SkipForDialect.class, frameworkMethod, getTestClass() ); - if ( skipForDialectAnn != null ) { + // @SkipForDialects & @SkipForDialect + for ( SkipForDialect skipForDialectAnn : Helper.collectAnnotations( + SkipForDialect.class, SkipForDialects.class, frameworkMethod, getTestClass() + ) ) { for ( Class dialectClass : skipForDialectAnn.value() ) { if ( skipForDialectAnn.strictMatching() ) { if ( dialectClass.equals( dialect.getClass() ) ) { @@ -245,9 +252,10 @@ public class CustomRunner extends BlockJUnit4ClassRunner { } } - // @RequiresDialect - RequiresDialect requiresDialectAnn = Helper.locateAnnotation( RequiresDialect.class, frameworkMethod, getTestClass() ); - if ( requiresDialectAnn != null ) { + // @RequiresDialects & @RequiresDialect + for ( RequiresDialect requiresDialectAnn : Helper.collectAnnotations( + RequiresDialect.class, RequiresDialects.class, frameworkMethod, getTestClass() + ) ) { boolean foundMatch = false; for ( Class dialectClass : requiresDialectAnn.value() ) { foundMatch = requiresDialectAnn.strictMatching() diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/junit4/Helper.java b/hibernate-testing/src/main/java/org/hibernate/testing/junit4/Helper.java index a67206a018..3a9dbdd1b8 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/junit4/Helper.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/junit4/Helper.java @@ -25,6 +25,9 @@ package org.hibernate.testing.junit4; import java.lang.annotation.Annotation; import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; import java.util.Map; import org.junit.runners.model.FrameworkMethod; @@ -33,6 +36,8 @@ import org.junit.runners.model.TestClass; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.testing.FailureExpected; +import org.hibernate.testing.SkipForDialect; +import org.hibernate.testing.SkipForDialects; /** * Centralized utility functionality @@ -84,6 +89,38 @@ public class Helper { return annotation; } + /** + * @param singularAnnotationClass Singular annotation class (e.g. {@link SkipForDialect}). + * @param pluralAnnotationClass Plural annotation class (e.g. {@link SkipForDialects}). Assuming that the only + * declared method is an array of singular annotations. + * @param frameworkMethod Test method. + * @param testClass Test class. + * @param Singular annotation type. + * @param

Plural annotation type. + * @return Collection of all singular annotations or an empty list. + */ + @SuppressWarnings("unchecked") + public static List collectAnnotations(Class singularAnnotationClass, + Class

pluralAnnotationClass, + FrameworkMethod frameworkMethod, + TestClass testClass) { + final List collection = new LinkedList(); + final S singularAnn = Helper.locateAnnotation( singularAnnotationClass, frameworkMethod, testClass ); + if ( singularAnn != null ) { + collection.add( singularAnn ); + } + final P pluralAnn = Helper.locateAnnotation( pluralAnnotationClass, frameworkMethod, testClass ); + if ( pluralAnn != null ) { + try { + collection.addAll( Arrays.asList( (S[]) pluralAnnotationClass.getDeclaredMethods()[0].invoke(pluralAnn) ) ); + } + catch ( Exception e ) { + throw new RuntimeException( e ); + } + } + return collection; + } + public static String extractMessage(FailureExpected failureExpected) { StringBuilder buffer = new StringBuilder(); buffer.append( '(' ).append( failureExpected.jiraKey() ).append( ')' );