-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the @TestTransaction annotation #11798
Merged
stuartwdouglas
merged 2 commits into
quarkusio:master
from
stuartwdouglas:testtransaction
Sep 7, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
.../main/java/io/quarkus/hibernate/orm/deployment/test/HibernateTestTransactionCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.quarkus.hibernate.orm.deployment.test; | ||
|
||
import javax.enterprise.inject.spi.CDI; | ||
import javax.persistence.EntityManager; | ||
|
||
import io.quarkus.narayana.jta.runtime.test.TestTransactionCallback; | ||
|
||
public class HibernateTestTransactionCallback implements TestTransactionCallback { | ||
@Override | ||
public void postBegin() { | ||
|
||
} | ||
|
||
@Override | ||
public void preRollback() { | ||
for (EntityManager i : CDI.current().select(EntityManager.class)) { | ||
i.flush(); | ||
} | ||
|
||
} | ||
} |
1 change: 1 addition & 0 deletions
1
.../resources/META-INF/services/io.quarkus.narayana.jta.runtime.test.TestTransactionCallback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
io.quarkus.hibernate.orm.deployment.test.HibernateTestTransactionCallback |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...src/main/java/io/quarkus/narayana/jta/runtime/interceptor/TestTransactionInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.quarkus.narayana.jta.runtime.interceptor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.ServiceLoader; | ||
|
||
import javax.inject.Inject; | ||
import javax.interceptor.AroundInvoke; | ||
import javax.interceptor.InvocationContext; | ||
import javax.transaction.UserTransaction; | ||
|
||
import io.quarkus.narayana.jta.runtime.test.TestTransactionCallback; | ||
|
||
public class TestTransactionInterceptor { | ||
|
||
static final List<TestTransactionCallback> CALLBACKS; | ||
|
||
static { | ||
List<TestTransactionCallback> callbacks = new ArrayList<>(); | ||
for (TestTransactionCallback i : ServiceLoader.load(TestTransactionCallback.class)) { | ||
callbacks.add(i); | ||
} | ||
CALLBACKS = callbacks; | ||
} | ||
|
||
@Inject | ||
UserTransaction userTransaction; | ||
|
||
@AroundInvoke | ||
public Object intercept(InvocationContext context) throws Exception { | ||
try { | ||
userTransaction.begin(); | ||
for (TestTransactionCallback i : CALLBACKS) { | ||
i.postBegin(); | ||
} | ||
Object result = context.proceed(); | ||
for (TestTransactionCallback i : CALLBACKS) { | ||
i.preRollback(); | ||
} | ||
return result; | ||
} finally { | ||
userTransaction.rollback(); | ||
} | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
...a/runtime/src/main/java/io/quarkus/narayana/jta/runtime/test/TestTransactionCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.quarkus.narayana.jta.runtime.test; | ||
|
||
public interface TestTransactionCallback { | ||
|
||
void postBegin(); | ||
|
||
void preRollback(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...ts/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/inheritance/Interceptor1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.quarkus.arc.test.interceptors.inheritance; | ||
|
||
import javax.interceptor.AroundInvoke; | ||
import javax.interceptor.Interceptor; | ||
import javax.interceptor.InvocationContext; | ||
|
||
@One | ||
@Interceptor | ||
public class Interceptor1 extends OverridenInterceptor { | ||
|
||
@AroundInvoke | ||
@Override | ||
public Object intercept(InvocationContext ctx) throws Exception { | ||
return ctx.proceed() + "1"; | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
...ts/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/inheritance/Interceptor2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.quarkus.arc.test.interceptors.inheritance; | ||
|
||
import javax.interceptor.Interceptor; | ||
|
||
@Two | ||
@Interceptor | ||
public class Interceptor2 extends OverridenInterceptor { | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...src/test/java/io/quarkus/arc/test/interceptors/inheritance/InterceptorSuperclassTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.quarkus.arc.test.interceptors.inheritance; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.arc.test.ArcTestContainer; | ||
import javax.enterprise.context.ApplicationScoped; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
/** | ||
* "Around-invoke methods may be defined on interceptor classes and/or the target class and/or super- | ||
* classes of the target class or the interceptor classes. However, only one around-invoke method may be | ||
* defined on a given class." | ||
*/ | ||
public class InterceptorSuperclassTest { | ||
|
||
@RegisterExtension | ||
public ArcTestContainer container = new ArcTestContainer(Interceptor1.class, Interceptor2.class, One.class, Two.class, | ||
OverridenInterceptor.class, Fool.class); | ||
|
||
@Test | ||
public void testInterception() { | ||
Fool fool = Arc.container().instance(Fool.class).get(); | ||
assertEquals("ping1", fool.pingOne()); | ||
assertEquals("pingoverriden", fool.pingTwo()); | ||
} | ||
|
||
@ApplicationScoped | ||
public static class Fool { | ||
|
||
@One | ||
String pingOne() { | ||
return "ping"; | ||
} | ||
|
||
@Two | ||
String pingTwo() { | ||
return "ping"; | ||
} | ||
|
||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
...nt-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/inheritance/One.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.quarkus.arc.test.interceptors.inheritance; | ||
|
||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
import javax.interceptor.InterceptorBinding; | ||
|
||
@Target({ TYPE, METHOD }) | ||
@Retention(RUNTIME) | ||
@Documented | ||
@InterceptorBinding | ||
public @interface One { | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
...ests/src/test/java/io/quarkus/arc/test/interceptors/inheritance/OverridenInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.quarkus.arc.test.interceptors.inheritance; | ||
|
||
import javax.interceptor.AroundInvoke; | ||
import javax.interceptor.InvocationContext; | ||
|
||
public class OverridenInterceptor { | ||
|
||
@AroundInvoke | ||
public Object intercept(InvocationContext ctx) throws Exception { | ||
return ctx.proceed() + "overriden"; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...nt-projects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/inheritance/Two.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.quarkus.arc.test.interceptors.inheritance; | ||
|
||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
import javax.interceptor.InterceptorBinding; | ||
|
||
@Target({ TYPE, METHOD }) | ||
@Retention(RUNTIME) | ||
@Documented | ||
@InterceptorBinding | ||
public @interface Two { | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for having considered my
flush()
-suggestion. Very cool approach with this newTestTransactionInterceptor
!I am wondering whether this block could kick in cases where it is not really needed? Usually
ServiceLoader
calls are not super-cheap.I used to apply https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom in my projects but I don't know whether this would make any difference here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, the callbacks are only loaded once per test config...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That said, we could easily turn the list into a
io.quarkus.arc.impl.LazyValue<List<TestTransactionCallback>>
...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current code is fine. ServiceLoader is expensive in terms of 'don't do this every request so you are doing it tens of thousands of times per second', its fine for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, thanks for your feedback.