|
23 | 23 | import co.elastic.clients.util.AllowForbiddenApis;
|
24 | 24 | import jakarta.json.JsonException;
|
25 | 25 | import jakarta.json.spi.JsonProvider;
|
| 26 | +import jakarta.json.stream.JsonGenerator; |
26 | 27 | import org.junit.Assert;
|
27 | 28 | import org.junit.Test;
|
28 | 29 |
|
| 30 | +import java.io.StringWriter; |
29 | 31 | import java.net.URL;
|
30 | 32 | import java.util.Collections;
|
31 | 33 | import java.util.Enumeration;
|
| 34 | +import java.util.function.Consumer; |
32 | 35 |
|
33 | 36 | public class JsonpUtilsTest extends Assert {
|
34 | 37 |
|
@@ -60,4 +63,52 @@ public Enumeration<URL> getResources(String name) {
|
60 | 63 | Thread.currentThread().setContextClassLoader(savedLoader);
|
61 | 64 | }
|
62 | 65 | }
|
| 66 | + |
| 67 | + @Test |
| 68 | + public void testSerializeDoubleOrNull() { |
| 69 | + // ---- Double values |
| 70 | + assertEquals("{\"a\":null}", orNullHelper(g -> JsonpUtils.serializeDoubleOrNull(g, Double.NaN, Double.NaN))); |
| 71 | + assertEquals("{\"a\":1.0}", orNullHelper(g -> JsonpUtils.serializeDoubleOrNull(g, 1.0, Double.NaN))); |
| 72 | + |
| 73 | + assertEquals("{\"a\":null}", |
| 74 | + orNullHelper(g -> JsonpUtils.serializeDoubleOrNull(g, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY))); |
| 75 | + assertEquals("{\"a\":1.0}", orNullHelper(g -> JsonpUtils.serializeDoubleOrNull(g, 1.0, Double.POSITIVE_INFINITY))); |
| 76 | + |
| 77 | + assertEquals("{\"a\":null}", |
| 78 | + orNullHelper(g -> JsonpUtils.serializeDoubleOrNull(g, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY))); |
| 79 | + assertEquals("{\"a\":1.0}", orNullHelper(g -> JsonpUtils.serializeDoubleOrNull(g, 1.0, Double.NEGATIVE_INFINITY))); |
| 80 | + |
| 81 | + assertEquals("{\"a\":null}", orNullHelper(g -> JsonpUtils.serializeDoubleOrNull(g, Double.NaN, 0.0))); |
| 82 | + |
| 83 | + // Serialize defined default values |
| 84 | + assertEquals("{\"a\":0.0}", orNullHelper(g -> JsonpUtils.serializeDoubleOrNull(g, 0.0, 0.0))); |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testSerializeIntOrNull() { |
| 90 | + assertEquals("{\"a\":null}", orNullHelper(g -> JsonpUtils.serializeIntOrNull(g, Integer.MAX_VALUE, Integer.MAX_VALUE))); |
| 91 | + assertEquals("{\"a\":1}", orNullHelper(g -> JsonpUtils.serializeIntOrNull(g, 1, Integer.MAX_VALUE))); |
| 92 | + assertEquals("{\"a\":1}", orNullHelper(g -> JsonpUtils.serializeIntOrNull(g, 1, 0))); |
| 93 | + |
| 94 | + // Integer.MAX_VALUE is valid if not the default value |
| 95 | + assertEquals("{\"a\":2147483647}", orNullHelper(g -> JsonpUtils.serializeIntOrNull(g, Integer.MAX_VALUE, 0))); |
| 96 | + assertEquals("{\"a\":2147483647}", orNullHelper(g -> JsonpUtils.serializeIntOrNull(g, Integer.MAX_VALUE, Integer.MIN_VALUE))); |
| 97 | + |
| 98 | + // Serialize non infinite default values |
| 99 | + assertEquals("{\"a\":0}", orNullHelper(g -> JsonpUtils.serializeIntOrNull(g, 0, 0))); |
| 100 | + } |
| 101 | + |
| 102 | + private static String orNullHelper(Consumer<JsonGenerator> c) { |
| 103 | + StringWriter sw = new StringWriter(); |
| 104 | + JsonGenerator generator = JsonpUtils.provider().createGenerator(sw); |
| 105 | + |
| 106 | + generator.writeStartObject(); |
| 107 | + generator.writeKey("a"); |
| 108 | + c.accept(generator); |
| 109 | + generator.writeEnd(); |
| 110 | + generator.close(); |
| 111 | + |
| 112 | + return sw.toString(); |
| 113 | + } |
63 | 114 | }
|
0 commit comments