Skip to content

Commit aac62ee

Browse files
committed
#29 - Support generic interfaces like Repository<T,I>
This bumps dinject-generator to 1.9 (which does all the work) and adds tests for this
1 parent 2a91f46 commit aac62ee

File tree

11 files changed

+221
-2
lines changed

11 files changed

+221
-2
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<dependency>
5151
<groupId>io.dinject</groupId>
5252
<artifactId>dinject-generator</artifactId>
53-
<version>1.8</version>
53+
<version>1.9</version>
5454
<scope>test</scope>
5555
</dependency>
5656

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.example.coffee.generic;
2+
3+
public class Haz {
4+
5+
public long id;
6+
7+
public Haz(Long id) {
8+
this.id = id;
9+
}
10+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.example.coffee.generic;
2+
3+
import javax.inject.Inject;
4+
import javax.inject.Singleton;
5+
6+
/**
7+
* Has a Dependency via generic interface.
8+
*/
9+
@Singleton
10+
public class HazManager {
11+
12+
private final Repository<Haz,Long> hazRepo;
13+
14+
@Inject
15+
public HazManager(Repository<Haz, Long> hazRepo) {
16+
this.hazRepo = hazRepo;
17+
}
18+
19+
public Haz find(Long id) {
20+
return hazRepo.findById(id);
21+
}
22+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package org.example.coffee.generic;
2+
3+
import io.dinject.BeanContext;
4+
import io.dinject.BootContext;
5+
import io.dinject.SystemContext;
6+
import org.junit.Test;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
import static org.mockito.ArgumentMatchers.anyLong;
10+
import static org.mockito.Mockito.when;
11+
12+
public class HazManagerTest {
13+
14+
@Test
15+
public void find_when_allWired() {
16+
17+
HazManager hazManager = SystemContext.getBean(HazManager.class);
18+
Haz haz = hazManager.find(42L);
19+
20+
assertThat(haz.id).isEqualTo(42L);
21+
}
22+
23+
@Test
24+
public void fin_with_mockHaz() {
25+
26+
try (BeanContext context = new BootContext()
27+
.withMock(HazRepo.class)
28+
.load()) {
29+
30+
HazManager hazManager = context.getBean(HazManager.class);
31+
Haz haz = hazManager.find(42L);
32+
33+
assertThat(haz).isNull();
34+
}
35+
}
36+
37+
@Test
38+
public void find_with_stubHazUsingMockito() {
39+
40+
try (BeanContext context = new BootContext()
41+
.withMock(HazRepo.class, hazRepo -> {
42+
when(hazRepo.findById(anyLong())).thenReturn(new Haz(-23L));
43+
})
44+
.load()) {
45+
46+
HazManager hazManager = context.getBean(HazManager.class);
47+
Haz haz = hazManager.find(42L);
48+
49+
assertThat(haz.id).isEqualTo(-23L);
50+
}
51+
}
52+
53+
@Test
54+
public void find_with_testDouble() {
55+
56+
TDHazRepo testDouble = new TDHazRepo();
57+
58+
try (BeanContext context = new BootContext()
59+
.withBeans(testDouble)
60+
.load()) {
61+
62+
HazManager hazManager = context.getBean(HazManager.class);
63+
64+
Haz haz = hazManager.find(42L);
65+
assertThat(haz.id).isEqualTo(64L);
66+
67+
testDouble.id = 48L;
68+
haz = hazManager.find(42L);
69+
assertThat(haz.id).isEqualTo(48L);
70+
71+
testDouble.id = 128L;
72+
haz = hazManager.find(42L);
73+
assertThat(haz.id).isEqualTo(128L);
74+
75+
}
76+
}
77+
78+
/**
79+
* Test double for HazRepo - nice when we want interesting test behaviour.
80+
*/
81+
class TDHazRepo extends HazRepo {
82+
83+
long id = 64L;
84+
85+
@Override
86+
public Haz findById(Long paramId) {
87+
return new Haz(id);
88+
}
89+
}
90+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.example.coffee.generic;
2+
3+
import javax.inject.Singleton;
4+
5+
/**
6+
* Implementation of a generic interface.
7+
*/
8+
@Singleton
9+
public class HazRepo implements Repository<Haz,Long> {
10+
11+
@Override
12+
public Haz findById(Long id) {
13+
return new Haz(id);
14+
}
15+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.example.coffee.generic;
2+
3+
import org.example.coffee.grind.AMusher;
4+
5+
import javax.inject.Singleton;
6+
7+
/**
8+
* Has multiple dependencies that are generic interface.
9+
*/
10+
@Singleton
11+
class MultiGenericConsumer {
12+
13+
private final Repository<Haz, Long> hazRepo;
14+
15+
private final SomeGeneric<String> stringProcessor;
16+
17+
private final AMusher aMusher;
18+
19+
MultiGenericConsumer(Repository<Haz, Long> hazRepo, AMusher aMusher, SomeGeneric<String> stringProcessor) {
20+
this.hazRepo = hazRepo;
21+
this.aMusher = aMusher;
22+
this.stringProcessor = stringProcessor;
23+
}
24+
25+
String findAndDo(long id) {
26+
final Haz byId = hazRepo.findById(id);
27+
return (byId == null) ? "not found" : "found " + stringProcessor.process("" + byId.id);
28+
}
29+
30+
String mushString() {
31+
return aMusher.toString();
32+
}
33+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.example.coffee.generic;
2+
3+
import io.dinject.SystemContext;
4+
import org.junit.Test;
5+
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
8+
public class MultiGenericConsumerTest {
9+
10+
@Test
11+
public void find() {
12+
13+
MultiGenericConsumer bean = SystemContext.getBean(MultiGenericConsumer.class);
14+
15+
assertThat(bean.findAndDo(34L)).isEqualTo("found 34 stuff");
16+
assertThat(bean.mushString()).isNotNull();
17+
}
18+
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.example.coffee.generic;
2+
3+
/**
4+
* Generics interface that we want to use as a dependency.
5+
*/
6+
public interface Repository<T,I> {
7+
8+
T findById(I id);
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.example.coffee.generic;
2+
3+
/**
4+
* Generics interface that we want to use as a dependency.
5+
*/
6+
public interface SomeGeneric<D> {
7+
8+
String process(D data);
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.example.coffee.generic;
2+
3+
import javax.inject.Singleton;
4+
5+
@Singleton
6+
public class SomeGenericString implements SomeGeneric<String> {
7+
8+
@Override
9+
public String process(String data) {
10+
return data + " stuff";
11+
}
12+
}

src/test/java/org/example/coffee/grind/AMusher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void preDestroy() {
2424
countClose++;
2525
}
2626

27-
int getCountInit() {
27+
public int getCountInit() {
2828
return countInit;
2929
}
3030

0 commit comments

Comments
 (0)