Skip to content

Commit 17bc27b

Browse files
committed
Ensure QuarkusJsonbComponentInstanceCreator can be used in unit tests
Fixes: #4090
1 parent c894ad6 commit 17bc27b

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

extensions/jsonb/runtime/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
<groupId>io.quarkus</groupId>
3535
<artifactId>quarkus-jsonp</artifactId>
3636
</dependency>
37+
<dependency>
38+
<groupId>org.junit.jupiter</groupId>
39+
<artifactId>junit-jupiter</artifactId>
40+
<scope>test</scope>
41+
</dependency>
3742
</dependencies>
3843

3944
<build>

extensions/jsonb/runtime/src/main/java/io/quarkus/jsonb/QuarkusJsonbComponentInstanceCreator.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.eclipse.yasson.spi.JsonbComponentInstanceCreator;
1010

1111
import io.quarkus.arc.Arc;
12+
import io.quarkus.arc.ArcContainer;
1213
import io.quarkus.arc.InstanceHandle;
1314

1415
public class QuarkusJsonbComponentInstanceCreator implements JsonbComponentInstanceCreator {
@@ -34,17 +35,26 @@ public void close() throws IOException {
3435
@Override
3536
public <T> T getOrCreateComponent(Class<T> componentClass) {
3637
return (T) components.computeIfAbsent(componentClass, c -> {
37-
InstanceHandle<T> beanHandle = Arc.container().instance(componentClass);
38+
ArcContainer container = Arc.container();
39+
if (container == null) { // this class can be used in unit tests where Arc obviously doesn't run
40+
return fallbackCreate(componentClass);
41+
}
42+
43+
InstanceHandle<T> beanHandle = container.instance(componentClass);
3844
if (beanHandle.isAvailable()) {
3945
beanHandles.add(beanHandle);
4046
return beanHandle.get();
4147
}
42-
try {
43-
return componentClass.newInstance();
44-
} catch (InstantiationException | IllegalAccessException e) {
45-
throw new IllegalStateException("Cannot instantiate Jsonb component: " + componentClass, e);
46-
}
48+
return fallbackCreate(componentClass);
4749
});
4850
}
4951

52+
private <T> Object fallbackCreate(Class<T> componentClass) {
53+
try {
54+
return componentClass.newInstance();
55+
} catch (InstantiationException | IllegalAccessException e) {
56+
throw new IllegalStateException("Cannot instantiate Jsonb component: " + componentClass, e);
57+
}
58+
}
59+
5060
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package io.quarkus.jsonb;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.time.Instant;
6+
7+
import javax.json.bind.JsonbBuilder;
8+
import javax.json.bind.adapter.JsonbAdapter;
9+
import javax.json.bind.annotation.JsonbProperty;
10+
import javax.json.bind.annotation.JsonbTypeAdapter;
11+
12+
import org.junit.jupiter.api.Test;
13+
14+
class JsonbUnitTest {
15+
16+
@Test
17+
void testJsonbWorksProperly() {
18+
SomeClass instance = JsonbBuilder.create().fromJson("{\"time\":1570039000}", SomeClass.class);
19+
assertEquals(Instant.ofEpochSecond(1570039000), instance.getTime());
20+
}
21+
22+
public static class SomeClass {
23+
24+
@JsonbProperty("time")
25+
@JsonbTypeAdapter(EpochSecondsAdapter.class)
26+
private Instant time;
27+
28+
public SomeClass() {
29+
}
30+
31+
public Instant getTime() {
32+
return time;
33+
}
34+
35+
public void setTime(Instant time) {
36+
this.time = time;
37+
}
38+
39+
public static class EpochSecondsAdapter implements JsonbAdapter<Instant, Long> {
40+
@Override
41+
public Long adaptToJson(Instant obj) {
42+
return obj.getEpochSecond();
43+
}
44+
45+
@Override
46+
public Instant adaptFromJson(Long obj) {
47+
return Instant.ofEpochSecond(obj);
48+
}
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)