blob: bef78a22de7b51d30a554d7bf0decaccd0f232cc (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.client;
import com.google.inject.AbstractModule;
import com.google.inject.Inject;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
*/
public class ClientDriverTestCase {
@Test
public void requireThatApplicationInstanceInjectionWorks() throws Exception {
MyModule module = new MyModule();
ClientDriver.runApplication(new MyApplication(module));
assertEquals(5, module.state);
}
@Test
public void requireThatApplicationClassInjectionWorks() throws Exception {
MyModule module = new MyModule();
ClientDriver.runApplication(MyApplication.class, module);
assertEquals(5, module.state);
}
private static class MyApplication implements ClientApplication {
final MyModule module;
@Inject
MyApplication(MyModule module) {
this.module = module;
module.state = 1;
}
@Override
public void start() {
if (++module.state != 2) {
throw new IllegalStateException();
}
}
@Override
public void run() {
if (++module.state != 3) {
throw new IllegalStateException();
}
}
@Override
public void stop() {
if (++module.state != 4) {
throw new IllegalStateException();
}
}
@Override
public void destroy() {
if (++module.state != 5) {
throw new IllegalStateException();
}
}
}
private static class MyModule extends AbstractModule {
int state = 0;
@Override
protected void configure() {
bind(MyModule.class).toInstance(this);
}
}
}
|