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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2008-2012 SonarSource
* mailto:contact AT sonarsource DOT com
*
* Sonar is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* Sonar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.batch.bootstrap;
import org.hamcrest.Matchers;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
public class ModuleTest {
@Test
public void shouldInitModule() {
Module module = new FakeModule(FakeService.class).init();
FakeService service = module.container.getComponentByType(FakeService.class);
assertThat(service, not(nullValue()));
assertThat(service.started, is(false));
assertThat(module.container, notNullValue());
}
@Test
public void shouldStartAndStopModule() {
Module module = new FakeModule(FakeService.class).init();
module.start();
FakeService service = module.container.getComponentByType(FakeService.class);
assertThat(service.started, is(true));
module.stop();
assertThat(service.started, is(false));
}
@Test(expected = RuntimeException.class)
public void shouldNotIgnoreStartFailures() {
Module module = new FakeModule(NonStartableService.class).init();
module.start();
}
@Test
public void shouldIgnoreStopFailures() {
Module module = new FakeModule(NonStoppableService.class).init();
module.start();
module.stop(); // no exception is raised
}
@Test
public void componentsShouldBeSingletons() {
Module module = new FakeModule(FakeService.class).init();
assertThat(module.container.getComponentByType(FakeService.class) == module.container.getComponentByType(FakeService.class), is(true));
}
@Test
public void shouldInstallChildModule() {
Module parent = new FakeModule(FakeService.class).init();
parent.start();
Module child = parent.installChild(new FakeModule(ChildService.class));
assertThat(parent.container.getComponentByType(ChildService.class), Matchers.nullValue());// child not accessible from parent
assertThat(child.container.getComponentByType(FakeService.class), not(nullValue()));
assertThat(child.container.getComponentByType(ChildService.class).started, is(false));
assertThat(child.container.getComponentByType(ChildService.class).dependency, not(nullValue()));
child.start();
assertThat(child.container.getComponentByType(ChildService.class).started, is(true));
child.stop();
assertThat(child.container.getComponentByType(ChildService.class).started, is(false));
}
public static class FakeModule extends Module {
private Class[] components;
public FakeModule(Class... components) {
this.components = components;
}
@Override
protected void configure() {
for (Class component : components) {
container.addSingleton(component);
}
}
@Override
public boolean equals(Object obj) {
return false;
}
@Override
public int hashCode() {
return 42;
}
}
public static class FakeService {
boolean started = false;
public void start() {
this.started = true;
}
public void stop() {
this.started = false;
}
}
public static class ChildService {
private FakeService dependency;
private boolean started = false;
public ChildService(FakeService dependency) {
this.dependency = dependency;
}
public void start() {
this.started = true;
}
public void stop() {
this.started = false;
}
}
public static class NonStoppableService {
public void stop() {
throw new IllegalStateException("Can not stop !");
}
}
public static class NonStartableService {
public void start() {
throw new IllegalStateException("Can not start !");
}
}
}
|