Skip to content

Commit aa0d643

Browse files
committed
create annotations to replace DBSelectionExtension features
1 parent f9ea159 commit aa0d643

File tree

7 files changed

+365
-0
lines changed

7 files changed

+365
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
* This annotation was based off of ORM's similar solution for Dialects:
48+
* @see org.hibernate.testing.orm.junit.SkipForDialect.java
49+
*/
50+
51+
@SuppressWarnings("JavadocReference")
52+
@Inherited
53+
@Retention( RetentionPolicy.RUNTIME )
54+
@Target({ ElementType.TYPE, ElementType.METHOD})
55+
@Repeatable( DisableForGroup.class )
56+
57+
@ExtendWith( DisableForDBTypeCondition.class )
58+
public @interface DisableFor {
59+
DatabaseConfiguration.DBType value();
60+
String reason() default "<undefined>";
61+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
9+
import java.util.Optional;
10+
11+
import org.hibernate.reactive.containers.DatabaseConfiguration;
12+
13+
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
14+
import org.junit.jupiter.api.extension.ExecutionCondition;
15+
import org.junit.jupiter.api.extension.ExtensionContext;
16+
17+
import static org.junit.platform.commons.util.AnnotationUtils.findAnnotation;
18+
19+
class DisableForDBTypeCondition<A extends DisableFor> implements ExecutionCondition {
20+
21+
@Override
22+
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
23+
Optional<DisableForGroup> disabledForGroup = findAnnotation( context.getElement(), DisableForGroup.class );
24+
if ( disabledForGroup != null && disabledForGroup.isPresent() ) {
25+
for ( DisableFor annotation : disabledForGroup.get().value() ) {
26+
if ( annotation.value() == DatabaseConfiguration.dbType() ) {
27+
return ConditionEvaluationResult.disabled( annotation.reason() );
28+
}
29+
}
30+
return ConditionEvaluationResult.enabled( "" );
31+
}
32+
33+
Optional<DisableFor> disabledFor = findAnnotation( context.getElement(), DisableFor.class );
34+
if ( disabledFor.isPresent() ) {
35+
DatabaseConfiguration.DBType type = disabledFor.get().value();
36+
return type == DatabaseConfiguration.dbType() ?
37+
ConditionEvaluationResult.disabled( disabledFor.get().reason() ) :
38+
ConditionEvaluationResult.enabled( "" );
39+
}
40+
return ConditionEvaluationResult.enabled( "" );
41+
}
42+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
* This annotation was based off of ORM's similar solution for Dialects:
53+
* @see org.hibernate.testing.orm.junit.SkipForDialects.java
54+
*/
55+
@Inherited
56+
@Retention(RetentionPolicy.RUNTIME)
57+
@Target({ ElementType.TYPE, ElementType.METHOD})
58+
59+
@ExtendWith( DisableForDBTypeCondition.class )
60+
public @interface DisableForGroup {
61+
DisableFor[] value();
62+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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( POSTGRES )
40+
* public void test(VertxTestContext context) {
41+
* ....
42+
* }
43+
* }
44+
* }</pre>
45+
*
46+
*
47+
* This annotation was based off of ORM's similar solution for Dialects:
48+
* @see org.hibernate.testing.orm.junit.SkipForDialect.java
49+
*/
50+
51+
@SuppressWarnings("JavadocReference")
52+
@Inherited
53+
@Retention( RetentionPolicy.RUNTIME )
54+
@Target({ ElementType.TYPE, ElementType.METHOD})
55+
@Repeatable( EnableForGroup.class )
56+
57+
@ExtendWith( EnableForDBTypeCondition.class )
58+
public @interface EnableFor {
59+
DatabaseConfiguration.DBType value();
60+
}
Lines changed: 41 additions & 0 deletions
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( "" );
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+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
* This annotation was based off of ORM's similar solution for Dialects:
53+
* @see org.hibernate.testing.orm.junit.RequiresDialects.java
54+
*/
55+
@Inherited
56+
@Retention(RetentionPolicy.RUNTIME)
57+
@Target({ ElementType.TYPE, ElementType.METHOD})
58+
59+
@ExtendWith( EnableForDBTypeCondition.class )
60+
public @interface EnableForGroup {
61+
EnableFor[] value();
62+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
The annotations `EnableFor()` and `DisableFor()` allow enabling/disabling a test class for specific DB types or enabling/disabling individual test methods within a test class.
2+
3+
The allowable database types are defined the `DatabaseConfiguration.DBTYPE` enum list.
4+
5+
Annotations can be added to a class:
6+
```
7+
@EnableFor(value = MYSQL)
8+
@EnableFor(value = DB2)
9+
public class EnableForDBClassTest extends BaseReactiveTest {
10+
.
11+
.
12+
```
13+
or a test method:
14+
```
15+
@Test
16+
@EnableFor( MYSQL )
17+
@EnableFor( DB2 )
18+
public void testMethod(VertxTestContext context) {
19+
.
20+
.
21+
```
22+
23+
You can also use the plural types to wrap multiple annotations with **DisableForGroup** and **EnableForGroup**:
24+
```
25+
@EnableForGroup(value = {
26+
@EnableFor( MYSQL ),
27+
@EnableFor( DB2 )
28+
})
29+
public class EnableForGroupClassTest extends BaseReactiveTest {
30+
.
31+
.
32+
.
33+
```
34+
35+
36+
37+

0 commit comments

Comments
 (0)