Skip to content

Commit 9a40344

Browse files
blafondDavideD
authored andcommitted
[hibernate#1827] create EnableFor and DisableFor annotations
1 parent 2e88335 commit 9a40344

File tree

6 files changed

+314
-0
lines changed

6 files changed

+314
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive.annotations;
7+
8+
import java.lang.annotation.ElementType;
9+
import java.lang.annotation.Inherited;
10+
import java.lang.annotation.Repeatable;
11+
import java.lang.annotation.Retention;
12+
import java.lang.annotation.RetentionPolicy;
13+
import java.lang.annotation.Target;
14+
15+
import org.hibernate.reactive.containers.DatabaseConfiguration;
16+
17+
import org.junit.jupiter.api.extension.ExtendWith;
18+
19+
/**
20+
* Annotation that allows disabling tests or test methods for specific database types.
21+
* <p>
22+
* Types are defined in {@link DatabaseConfiguration.DBType} and can be
23+
* applied to a test class or a test method
24+
*
25+
* <pre>{@code
26+
*
27+
* @DisableFor( value = MYSQL, reason = "Reason #1")
28+
* public class DisableDBForClassTest {
29+
*
30+
* @Test
31+
* public void test(VertxTestContext context) {
32+
* ....
33+
* }
34+
* }
35+
*
36+
* public class DisableDBForMethodTest {
37+
*
38+
* @Test
39+
* @DisableFor( value = POSTGRES, reason = "Reason #2")
40+
* public void test(VertxTestContext context) {
41+
* ....
42+
* }
43+
* }
44+
* }</pre>
45+
*
46+
*/
47+
48+
@SuppressWarnings("JavadocReference")
49+
@Inherited
50+
@Retention( RetentionPolicy.RUNTIME )
51+
@Target({ ElementType.TYPE, ElementType.METHOD})
52+
@Repeatable( DisableForGroup.class )
53+
@ExtendWith( DisableForDBTypeCondition.class )
54+
public @interface DisableFor {
55+
DatabaseConfiguration.DBType value();
56+
String reason() default "<undefined>";
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive.annotations;
7+
8+
import java.util.Optional;
9+
10+
import org.hibernate.reactive.containers.DatabaseConfiguration;
11+
12+
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
13+
import org.junit.jupiter.api.extension.ExecutionCondition;
14+
import org.junit.jupiter.api.extension.ExtensionContext;
15+
16+
import static org.junit.platform.commons.util.AnnotationUtils.findAnnotation;
17+
18+
class DisableForDBTypeCondition<A extends DisableFor> implements ExecutionCondition {
19+
20+
@Override
21+
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
22+
Optional<DisableForGroup> disabledForGroup = findAnnotation( context.getElement(), DisableForGroup.class );
23+
if ( disabledForGroup != null && disabledForGroup.isPresent() ) {
24+
for ( DisableFor annotation : disabledForGroup.get().value() ) {
25+
if ( annotation.value() == DatabaseConfiguration.dbType() ) {
26+
return ConditionEvaluationResult.disabled( annotation.reason() );
27+
}
28+
}
29+
return ConditionEvaluationResult.enabled( "" );
30+
}
31+
32+
Optional<DisableFor> disabledFor = findAnnotation( context.getElement(), DisableFor.class );
33+
if ( disabledFor.isPresent() ) {
34+
DatabaseConfiguration.DBType type = disabledFor.get().value();
35+
return type == DatabaseConfiguration.dbType() ?
36+
ConditionEvaluationResult.disabled( disabledFor.get().reason() ) :
37+
ConditionEvaluationResult.enabled( "" );
38+
}
39+
return ConditionEvaluationResult.enabled( "" );
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive.annotations;
7+
8+
import java.lang.annotation.ElementType;
9+
import java.lang.annotation.Inherited;
10+
import java.lang.annotation.Retention;
11+
import java.lang.annotation.RetentionPolicy;
12+
import java.lang.annotation.Target;
13+
14+
import org.hibernate.reactive.containers.DatabaseConfiguration;
15+
16+
import org.junit.jupiter.api.extension.ExtendWith;
17+
18+
/**
19+
* Annotation that allows disabling tests or test methods for specific database types.
20+
*
21+
* Types are defined in {@link DatabaseConfiguration.DBType} and can be
22+
* applied to a test class or a test method
23+
*
24+
* <pre>{@code
25+
*
26+
* @DisableForGroup( {
27+
* @DisableFor(value = MYSQL, reason = "Reason #1"),
28+
* @DisableFor(value = DB2, reason = "Reason #2")
29+
* } )
30+
* public class DisableDBsForClassTest {
31+
*
32+
* @Test
33+
* public void test(VertxTestContext context) {
34+
* ....
35+
* }
36+
* }
37+
*
38+
* public class DisableDBsForMethodTest {
39+
*
40+
* @Test
41+
* @DisableForGroup( {
42+
* @DisableFor(value = POSTGRES, reason = "Reason #3"),
43+
* @DisableFor(value = MYSQL, reason = "Reason #4")
44+
* } )
45+
* public void test(VertxTestContext context) {
46+
* ....
47+
* }
48+
* }
49+
* }</pre>
50+
*
51+
*/
52+
53+
@Inherited
54+
@Retention(RetentionPolicy.RUNTIME)
55+
@Target({ ElementType.TYPE, ElementType.METHOD})
56+
@ExtendWith( DisableForDBTypeCondition.class )
57+
public @interface DisableForGroup {
58+
DisableFor[] value();
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive.annotations;
7+
8+
import java.lang.annotation.ElementType;
9+
import java.lang.annotation.Inherited;
10+
import java.lang.annotation.Repeatable;
11+
import java.lang.annotation.Retention;
12+
import java.lang.annotation.RetentionPolicy;
13+
import java.lang.annotation.Target;
14+
15+
import org.hibernate.reactive.containers.DatabaseConfiguration;
16+
17+
import org.junit.jupiter.api.extension.ExtendWith;
18+
19+
/**
20+
* Annotation that allows disabling tests or test methods only for specific database types.
21+
* <p>
22+
* Types are defined in {@link DatabaseConfiguration.DBType} and can be
23+
* applied to a test class or a test method
24+
*
25+
* <pre>{@code
26+
*
27+
* @EnableFor( MYSQL )
28+
* public class EnableDBForClassTest {
29+
*
30+
* @Test
31+
* public void test(VertxTestContext context) {
32+
* ....
33+
* }
34+
* }
35+
*
36+
* public class EnableDBForMethodTest {
37+
*
38+
* @Test
39+
* @EnableFor( value = POSTGRES, "optional reason" )
40+
* public void test(VertxTestContext context) {
41+
* ....
42+
* }
43+
* }
44+
* }</pre>
45+
*
46+
*/
47+
48+
@SuppressWarnings("JavadocReference")
49+
@Inherited
50+
@Retention( RetentionPolicy.RUNTIME )
51+
@Target({ ElementType.TYPE, ElementType.METHOD})
52+
@Repeatable( EnableForGroup.class )
53+
@ExtendWith( EnableForDBTypeCondition.class )
54+
public @interface EnableFor {
55+
DatabaseConfiguration.DBType value();
56+
String reason() default "<undefined>";
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive.annotations;
7+
8+
import java.util.Optional;
9+
10+
import org.hibernate.reactive.containers.DatabaseConfiguration;
11+
12+
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
13+
import org.junit.jupiter.api.extension.ExecutionCondition;
14+
import org.junit.jupiter.api.extension.ExtensionContext;
15+
16+
import static org.junit.platform.commons.util.AnnotationUtils.findAnnotation;
17+
18+
public class EnableForDBTypeCondition<A extends EnableFor> implements ExecutionCondition {
19+
20+
@Override
21+
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
22+
Optional<EnableForGroup> enableForGroup = findAnnotation( context.getElement(), EnableForGroup.class );
23+
if ( enableForGroup != null && enableForGroup.isPresent() ) {
24+
for ( EnableFor annotation : enableForGroup.get().value() ) {
25+
if ( annotation.value() == DatabaseConfiguration.dbType() ) {
26+
return ConditionEvaluationResult.enabled( annotation.reason() );
27+
}
28+
}
29+
return ConditionEvaluationResult.disabled( "" );
30+
}
31+
32+
Optional<EnableFor> enableFor = findAnnotation( context.getElement(), EnableFor.class );
33+
if( enableFor != null && enableFor.isPresent() ) {
34+
DatabaseConfiguration.DBType type = enableFor.get().value();
35+
return type == DatabaseConfiguration.dbType() ?
36+
ConditionEvaluationResult.enabled( "" ) :
37+
ConditionEvaluationResult.disabled( "" );
38+
}
39+
return ConditionEvaluationResult.enabled( "" );
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive.annotations;
7+
8+
import java.lang.annotation.ElementType;
9+
import java.lang.annotation.Inherited;
10+
import java.lang.annotation.Retention;
11+
import java.lang.annotation.RetentionPolicy;
12+
import java.lang.annotation.Target;
13+
14+
import org.hibernate.reactive.containers.DatabaseConfiguration;
15+
16+
import org.junit.jupiter.api.extension.ExtendWith;
17+
18+
/**
19+
* Annotation that allows enabling tests or test methods only for specific database types.
20+
*
21+
* Types are defined in {@link DatabaseConfiguration.DBType} and can be
22+
* applied to a test class or a test method
23+
*
24+
* <pre>{@code
25+
*
26+
* @EnableForGroup( {
27+
* @EnableFor( MYSQL ),
28+
* @EnableFor( DB2 )
29+
* } )
30+
* public class EnableDBsForClassTest {
31+
*
32+
* @Test
33+
* public void test(VertxTestContext context) {
34+
* ....
35+
* }
36+
* }
37+
*
38+
* public class EnableDBsForMethodTest {
39+
*
40+
* @Test
41+
* @EnableForGroup( {
42+
* @EnableFor( POSTGRES),
43+
* @EnableFor( MYSQL )
44+
* } )
45+
* public void test(VertxTestContext context) {
46+
* ....
47+
* }
48+
* }
49+
* }</pre>
50+
*
51+
*/
52+
53+
@Inherited
54+
@Retention(RetentionPolicy.RUNTIME)
55+
@Target({ ElementType.TYPE, ElementType.METHOD})
56+
@ExtendWith( EnableForDBTypeCondition.class )
57+
public @interface EnableForGroup {
58+
EnableFor[] value();
59+
}

0 commit comments

Comments
 (0)