forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoobyRunner.java
More file actions
219 lines (192 loc) · 6.66 KB
/
JoobyRunner.java
File metadata and controls
219 lines (192 loc) · 6.66 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jooby.test;
import com.google.inject.Binder;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigValueFactory;
import org.jooby.Env;
import org.jooby.Jooby;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.MultipleFailureException;
import org.junit.runners.model.Statement;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* JUnit4 block runner for Jooby. Internal use only.
*
* @author edgar
*/
public class JoobyRunner extends BlockJUnit4ClassRunner {
private Jooby app;
private int port;
private int securePort;
private Class<?> server;
public JoobyRunner(final Class<?> klass) throws InitializationError {
super(klass);
prepare(klass, null);
}
public JoobyRunner(final Class<?> klass, final Class<?> server) throws InitializationError {
super(klass);
prepare(klass, server);
}
@Override
protected String getName() {
if (server != null) {
return "[" + server.getSimpleName().toLowerCase() + "]";
}
return super.getName();
}
@Override
protected String testName(final FrameworkMethod method) {
if (server != null) {
return method.getName() + getName();
}
return super.testName(method);
}
private void prepare(final Class<?> klass, final Class<?> server) throws InitializationError {
try {
this.port = port("coverage.port", 9999);
this.securePort = port("coverage.securePort", 9943);
this.server = server;
Class<?> appClass = klass;
if (!Jooby.class.isAssignableFrom(appClass)) {
throw new InitializationError("Invalid jooby app: " + appClass);
}
int processors = Math.max(1, Runtime.getRuntime().availableProcessors());
// required by Jetty (processors * 2, 1(http), 1(https), 1(request)
int maxThreads = processors * 2 + 3;
Config config = ConfigFactory.empty("test-config")
.withValue("server.join", ConfigValueFactory.fromAnyRef(false))
.withValue("server.http.IdleTimeout", ConfigValueFactory.fromAnyRef("5m"))
.withValue("server.threads.Min", ConfigValueFactory.fromAnyRef(1))
.withValue("server.threads.Max", ConfigValueFactory.fromAnyRef(maxThreads))
.withValue("application.port", ConfigValueFactory.fromAnyRef(port))
.withValue("undertow.ioThreads", ConfigValueFactory.fromAnyRef(2))
.withValue("undertow.workerThreads", ConfigValueFactory.fromAnyRef(4))
.withValue("netty.threads.Parent", ConfigValueFactory.fromAnyRef(2));
if (server != null) {
config = config.withFallback(ConfigFactory.empty()
.withValue("server.module", ConfigValueFactory.fromAnyRef(server.getName())));
}
app = (Jooby) appClass.newInstance();
if (app instanceof ServerFeature) {
int appport = ((ServerFeature) app).port;
if (appport > 0) {
config = config.withValue("application.port", ConfigValueFactory.fromAnyRef(appport));
this.port = appport;
}
int sappport = ((ServerFeature) app).securePort;
if (sappport > 0) {
config = config.withValue("application.securePort",
ConfigValueFactory.fromAnyRef(sappport));
this.securePort = sappport;
}
}
Config testConfig = config;
app.use(new Jooby.Module() {
@Override
public void configure(final Env mode, final Config config, final Binder binder) {
}
@Override
public Config config() {
return testConfig;
}
});
} catch (Exception ex) {
throw new InitializationError(Arrays.asList(ex));
}
}
@Override
protected Statement withBeforeClasses(final Statement statement) {
Statement next = super.withBeforeClasses(statement);
return new Statement() {
@Override
public void evaluate() throws Throwable {
app.start();
next.evaluate();
}
};
}
@Override
protected Object createTest() throws Exception {
Object test = super.createTest();
Class<? extends Object> c = test.getClass();
set(test, c, "port", port);
set(test, c, "securePort", securePort);
return test;
}
@SuppressWarnings("rawtypes")
private void set(final Object test, final Class clazz, final String field, final Object value)
throws Exception {
try {
Field f = clazz.getDeclaredField(field);
f.setAccessible(true);
f.set(test, value);
} catch (NoSuchFieldException ex) {
Class superclass = clazz.getSuperclass();
if (superclass != Object.class) {
set(test, superclass, field, value);
}
}
}
@Override
protected Statement withAfterClasses(final Statement statement) {
Statement next = super.withAfterClasses(statement);
return new Statement() {
@Override
public void evaluate() throws Throwable {
List<Throwable> errors = new ArrayList<Throwable>();
try {
next.evaluate();
} catch (Throwable e) {
errors.add(e);
}
try {
app.stop();
} catch (Exception ex) {
errors.add(ex);
}
if (errors.isEmpty()) {
return;
}
if (errors.size() == 1) {
throw errors.get(0);
}
throw new MultipleFailureException(errors);
}
};
}
private int port(String property, Integer defaultPort) throws IOException {
String port = System.getProperty(property, defaultPort.toString());
if (port.equalsIgnoreCase("random")) {
try (ServerSocket socket = new ServerSocket(0)) {
return socket.getLocalPort();
}
} else {
return Integer.parseInt(port);
}
}
}