-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
199 additions
and
154 deletions.
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
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
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
23 changes: 9 additions & 14 deletions
23
...rydsl/src/test/java/com/coreoz/plume/db/querydsl/crud/CrudDaoQuerydslTransactionTest.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
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: 8 additions & 13 deletions
21
...rc/test/java/com/coreoz/plume/db/querydsl/transaction/TransactionManagerQuerydslTest.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
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
26 changes: 26 additions & 0 deletions
26
plume-db-test/src/main/java/com/coreoz/plume/db/guice/GuiceTest.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,26 @@ | ||
package com.coreoz.plume.db.guice; | ||
|
||
import com.google.inject.Module; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Make a unit test be injected by Guice | ||
*/ | ||
@ExtendWith(GuiceTestRunner.class) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.TYPE}) | ||
public @interface GuiceTest { | ||
/** | ||
* The Guice modules used to execute the tests | ||
*/ | ||
Class<? extends Module>[] value(); | ||
/** | ||
* If the injector created for the modules should be reused in other tests (or use the already created injector) | ||
*/ | ||
boolean cacheInjector() default true; | ||
} |
49 changes: 49 additions & 0 deletions
49
plume-db-test/src/main/java/com/coreoz/plume/db/guice/GuiceTestRunner.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,49 @@ | ||
package com.coreoz.plume.db.guice; | ||
|
||
import com.google.inject.Guice; | ||
import com.google.inject.Injector; | ||
import com.google.inject.Module; | ||
import lombok.SneakyThrows; | ||
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
|
||
import java.util.Arrays; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class GuiceTestRunner implements BeforeTestExecutionCallback { | ||
private static final Map<String, Injector> injectorCache = new ConcurrentHashMap<>(); | ||
|
||
@SneakyThrows | ||
@Override | ||
public void beforeTestExecution(ExtensionContext context) throws Exception { | ||
Optional<Object> test = context.getTestInstance(); | ||
|
||
if (test.isPresent()) { | ||
GuiceTest guiceTest = test.get().getClass().getAnnotation(GuiceTest.class); | ||
|
||
if (guiceTest != null) { | ||
Injector injector = computeInjector(guiceTest); | ||
injector.injectMembers(test.get()); | ||
} | ||
} | ||
} | ||
|
||
private static Injector computeInjector(GuiceTest guiceTest) { | ||
if (guiceTest.cacheInjector()) { | ||
return injectorCache.computeIfAbsent(Arrays.toString(guiceTest.value()), cacheKey -> createInjector(guiceTest.value())); | ||
} | ||
return createInjector(guiceTest.value()); | ||
} | ||
|
||
private static Injector createInjector(Class<? extends Module>[] modules) { | ||
return Guice.createInjector(Arrays.stream(modules).map(clazz -> { | ||
try { | ||
return clazz.getDeclaredConstructor().newInstance(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
}).toArray(Module[]::new)); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
plume-db/src/test/java/com/coreoz/plume/db/pagination/PageTest.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
2 changes: 1 addition & 1 deletion
2
plume-db/src/test/java/com/coreoz/plume/db/pagination/PagesTest.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
2 changes: 1 addition & 1 deletion
2
plume-db/src/test/java/com/coreoz/plume/db/pagination/SliceTest.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
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
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
Oops, something went wrong.