Enable creating an API using Spring Web #5

Merged
niat8586 merged 39 commits from spring into develop 2024-11-06 11:23:29 +01:00
2 changed files with 18 additions and 12 deletions
Showing only changes of commit 4d96fc4671 - Show all commits

View File

@ -55,6 +55,11 @@
<artifactId>wicket-tester</artifactId> <artifactId>wicket-tester</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-spring</artifactId>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.wicketstuff</groupId> <groupId>org.wicketstuff</groupId>
<artifactId>wicketstuff-jasperreports</artifactId> <artifactId>wicketstuff-jasperreports</artifactId>

View File

@ -12,6 +12,8 @@ import org.apache.wicket.Session;
import org.apache.wicket.guice.GuiceComponentInjector; import org.apache.wicket.guice.GuiceComponentInjector;
import org.apache.wicket.request.Request; import org.apache.wicket.request.Request;
import org.apache.wicket.request.Response; import org.apache.wicket.request.Response;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import org.apache.wicket.spring.test.ApplicationContextMock;
import org.apache.wicket.util.tester.WicketTester; import org.apache.wicket.util.tester.WicketTester;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
@ -361,9 +363,12 @@ public abstract class SciProTest {
@BeforeEach @BeforeEach
public void setUpApplication() { public void setUpApplication() {
final Class<? extends SciProTest> clazz = getClass(); final Class<? extends SciProTest> clazz = getClass();
final Injector injector = Guice.createInjector(new MockModule(clazz)); ApplicationContextMock applicationContext = new MockModule(clazz);
SciProApplication application = new SciProApplication(injector.getInstance(CurrentProfile.class)) { CurrentProfile currentProfile = new CurrentProfile();
currentProfile.setCurrentProfileString("TEST");
SciProApplication application = new SciProApplication(currentProfile) {
@Override @Override
public Session newSession(Request request, Response response) { public Session newSession(Request request, Response response) {
@ -371,7 +376,7 @@ public abstract class SciProTest {
} }
}; };
application.getComponentInstantiationListeners().add( application.getComponentInstantiationListeners().add(
new GuiceComponentInjector(application, injector)); new SpringComponentInjector(application, applicationContext));
tester = new WicketTester(application, System.getProperty("user.dir") + "/src/main/webapp"); tester = new WicketTester(application, System.getProperty("user.dir") + "/src/main/webapp");
setLoggedIn(true); setLoggedIn(true);
@ -464,27 +469,23 @@ public abstract class SciProTest {
return sb.toString(); return sb.toString();
} }
private class MockModule extends AbstractModule { private class MockModule extends ApplicationContextMock {
private final Class<? extends SciProTest> clazz; private final Class<? extends SciProTest> clazz;
public MockModule(Class<? extends SciProTest> clazz) { public MockModule(Class<? extends SciProTest> clazz) {
this.clazz = clazz; this.clazz = clazz;
configure();
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override private void configure() {
protected void configure() { putBean(Clock.systemDefaultZone());
Multibinder.newSetBinder(binder(), Lifecycle.class);
Multibinder.newSetBinder(binder(), UserImportService.class)
.addBinding().toInstance(userImportService);
bindConstant().annotatedWith(Names.named("profile")).to("TEST");
bind(Clock.class).toInstance(Clock.systemDefaultZone());
for (Class<?> c = clazz; c != null; c = c.getSuperclass()) { for (Class<?> c = clazz; c != null; c = c.getSuperclass()) {
for (Field f : c.getDeclaredFields()) { for (Field f : c.getDeclaredFields()) {
if (f.getAnnotation(Mock.class) != null && f.trySetAccessible()) { if (f.getAnnotation(Mock.class) != null && f.trySetAccessible()) {
try { try {
Class<Object> type = (Class<Object>) f.getType(); Class<Object> type = (Class<Object>) f.getType();
bind(type).toInstance(f.get(SciProTest.this)); putBean(f.getName(), f.get(SciProTest.this));
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }