Skip to content

Commit

Permalink
added test for injection with annotation "@nAmed"
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan-nikitko committed Jan 9, 2023
1 parent af0f61a commit a265a58
Showing 1 changed file with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@

import io.bootique.BQRuntime;
import io.bootique.Bootique;
import io.bootique.di.BQInject;
import io.bootique.jetty.junit5.JettyTester;
import io.bootique.junit5.BQApp;
import io.bootique.junit5.BQTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
Expand All @@ -45,20 +47,25 @@ public class ResourceInjectionIT {

private static final String TEST_PROPERTY = "bq.test.label";
private static final InjectedService service = new InjectedService();
private static final InjectedServiceInterface serviceA = new InjectedServiceImplA();
private static final InjectedServiceInterface serviceB = new InjectedServiceImplB();

static final JettyTester jetty = JettyTester.create();

@BQApp
static final BQRuntime app = Bootique.app("-s")
.autoLoadModules()
.module(b -> b.bind(InjectedService.class).toInstance(service))
.module(b -> b.bind(InjectedServiceInterface.class, "A").toInstance(serviceA))
.module(b -> b.bind(InjectedServiceInterface.class, "B").toInstance(serviceB))
.module(b -> b.bind(UnInjectedResource.class).toProviderInstance(() -> new UnInjectedResource(service)))
.module(b -> JerseyModule.extend(b)
.addFeature(ctx -> {
ctx.property(TEST_PROPERTY, "x");
return false;
})
.addResource(FieldInjectedResource.class)
.addResource(NamedFieldInjectedResource.class)
.addResource(ConstructorInjectedResource.class)
.addResource(UnInjectedResource.class))
.module(jetty.moduleReplacingConnectors())
Expand All @@ -83,6 +90,20 @@ public void testFieldInjected() {
r2.close();
}

@Test
public void testNamedFieldInjected() {

Response r1 = jetty.getTarget().path("nf").request().get();
assertEquals(Status.OK.getStatusCode(), r1.getStatus());
assertEquals("nf_1_2_x", r1.readEntity(String.class));
r1.close();

Response r2 = jetty.getTarget().path("nf").request().get();
assertEquals(Status.OK.getStatusCode(), r2.getStatus());
assertEquals("nf_3_4_x", r2.readEntity(String.class));
r2.close();
}

@Test
public void testConstructorInjected() {

Expand Down Expand Up @@ -127,6 +148,27 @@ public String get() {
}
}

@Path("/nf")
@Produces(MediaType.TEXT_PLAIN)
public static class NamedFieldInjectedResource {

@Inject
@Named("A")
private InjectedServiceInterface serviceA;

@Inject
@Named("B")
private InjectedServiceInterface serviceB;

@Context
private Configuration config;

@GET
public String get() {
return "nf_" + serviceA.getNext() + "_" + serviceA.getNext()+ "_" + config.getProperty(TEST_PROPERTY);
}
}

@Path("/c")
@Produces(MediaType.TEXT_PLAIN)
public static class ConstructorInjectedResource {
Expand Down Expand Up @@ -178,4 +220,37 @@ public int getNext() {
return atomicInt.incrementAndGet();
}
}

public static interface InjectedServiceInterface {

AtomicInteger atomicInt = new AtomicInteger();
public void reset();
public int getNext();
}

public static class InjectedServiceImplA implements InjectedServiceInterface {

private AtomicInteger atomicInt = new AtomicInteger();

public void reset() {
atomicInt.set(0);
}

public int getNext() {
return atomicInt.incrementAndGet();
}
}

public static class InjectedServiceImplB implements InjectedServiceInterface {

private AtomicInteger atomicInt = new AtomicInteger();

public void reset() {
atomicInt.set(0);
}

public int getNext() {
return atomicInt.incrementAndGet();
}
}
}

0 comments on commit a265a58

Please sign in to comment.