Browse Source

Make JSON assertions more strict

pull/28332/head
Stephane Nicoll 4 years ago
parent
commit
10dc10dbf9
  1. 14
      spring-core/src/main/java/org/springframework/aot/nativex/ResourceHintsWriter.java
  2. 4
      spring-core/src/test/java/org/springframework/aot/nativex/FileNativeConfigurationWriterTests.java
  3. 2
      spring-core/src/test/java/org/springframework/aot/nativex/JavaSerializationHintsWriterTests.java
  4. 2
      spring-core/src/test/java/org/springframework/aot/nativex/ProxyHintsWriterTests.java
  5. 4
      spring-core/src/test/java/org/springframework/aot/nativex/ReflectionHintsWriterTests.java
  6. 2
      spring-core/src/test/java/org/springframework/aot/nativex/ResourceHintsWriterTests.java

14
spring-core/src/main/java/org/springframework/aot/nativex/ResourceHintsWriter.java

@ -46,7 +46,7 @@ class ResourceHintsWriter { @@ -46,7 +46,7 @@ class ResourceHintsWriter {
public void write(BasicJsonWriter writer, ResourceHints hints) {
Map<String, Object> attributes = new LinkedHashMap<>();
attributes.put("resources", toAttributes(hints));
addIfNotEmpty(attributes, "resources", toAttributes(hints));
handleResourceBundles(attributes, hints.resourceBundles());
writer.writeObject(attributes);
}
@ -81,7 +81,17 @@ class ResourceHintsWriter { @@ -81,7 +81,17 @@ class ResourceHintsWriter {
}
private void addIfNotEmpty(Map<String, Object> attributes, String name, @Nullable Object value) {
if (value != null && (value instanceof Collection<?> collection && !collection.isEmpty())) {
if (value instanceof Collection<?> collection) {
if (!collection.isEmpty()) {
attributes.put(name, value);
}
}
else if (value instanceof Map<?, ?> map) {
if (!map.isEmpty()) {
attributes.put(name, value);
}
}
else if (value != null) {
attributes.put(name, value);
}
}

4
spring-core/src/test/java/org/springframework/aot/nativex/FileNativeConfigurationWriterTests.java

@ -144,7 +144,7 @@ public class FileNativeConfigurationWriterTests { @@ -144,7 +144,7 @@ public class FileNativeConfigurationWriterTests {
],
"queriedMethods": [
{ "name": "<init>", "parameterTypes": [ "java.util.List", "boolean", "org.springframework.util.MimeType" ] },
{ "name": "getDefaultCharset" }
{ "name": "getDefaultCharset", "parameterTypes": [ ] }
]
}
]""", "reflect-config.json");
@ -186,7 +186,7 @@ public class FileNativeConfigurationWriterTests { @@ -186,7 +186,7 @@ public class FileNativeConfigurationWriterTests {
private void assertEquals(String expectedString, String filename) throws IOException, JSONException {
Path jsonFile = tempDir.resolve("META-INF").resolve("native-image").resolve(filename);
String content = new String(Files.readAllBytes(jsonFile));
JSONAssert.assertEquals(expectedString, content, JSONCompareMode.LENIENT);
JSONAssert.assertEquals(expectedString, content, JSONCompareMode.NON_EXTENSIBLE);
}
}

2
spring-core/src/test/java/org/springframework/aot/nativex/JavaSerializationHintsWriterTests.java

@ -65,7 +65,7 @@ public class JavaSerializationHintsWriterTests { @@ -65,7 +65,7 @@ public class JavaSerializationHintsWriterTests {
StringWriter out = new StringWriter();
BasicJsonWriter writer = new BasicJsonWriter(out, "\t");
JavaSerializationHintsWriter.INSTANCE.write(writer, hints);
JSONAssert.assertEquals(expectedString, out.toString(), JSONCompareMode.LENIENT);
JSONAssert.assertEquals(expectedString, out.toString(), JSONCompareMode.NON_EXTENSIBLE);
}
}

2
spring-core/src/test/java/org/springframework/aot/nativex/ProxyHintsWriterTests.java

@ -66,7 +66,7 @@ public class ProxyHintsWriterTests { @@ -66,7 +66,7 @@ public class ProxyHintsWriterTests {
StringWriter out = new StringWriter();
BasicJsonWriter writer = new BasicJsonWriter(out, "\t");
ProxyHintsWriter.INSTANCE.write(writer, hints);
JSONAssert.assertEquals(expectedString, out.toString(), JSONCompareMode.LENIENT);
JSONAssert.assertEquals(expectedString, out.toString(), JSONCompareMode.NON_EXTENSIBLE);
}
}

4
spring-core/src/test/java/org/springframework/aot/nativex/ReflectionHintsWriterTests.java

@ -95,7 +95,7 @@ public class ReflectionHintsWriterTests { @@ -95,7 +95,7 @@ public class ReflectionHintsWriterTests {
],
"queriedMethods": [
{ "name": "<init>", "parameterTypes": [ "java.util.List", "boolean", "org.springframework.util.MimeType" ] },
{ "name": "getDefaultCharset" }
{ "name": "getDefaultCharset", "parameterTypes": [ ] }
]
}
]""", hints);
@ -191,7 +191,7 @@ public class ReflectionHintsWriterTests { @@ -191,7 +191,7 @@ public class ReflectionHintsWriterTests {
StringWriter out = new StringWriter();
BasicJsonWriter writer = new BasicJsonWriter(out, "\t");
ReflectionHintsWriter.INSTANCE.write(writer, hints);
JSONAssert.assertEquals(expectedString, out.toString(), JSONCompareMode.LENIENT);
JSONAssert.assertEquals(expectedString, out.toString(), JSONCompareMode.NON_EXTENSIBLE);
}
}

2
spring-core/src/test/java/org/springframework/aot/nativex/ResourceHintsWriterTests.java

@ -120,7 +120,7 @@ public class ResourceHintsWriterTests { @@ -120,7 +120,7 @@ public class ResourceHintsWriterTests {
StringWriter out = new StringWriter();
BasicJsonWriter writer = new BasicJsonWriter(out, "\t");
ResourceHintsWriter.INSTANCE.write(writer, hints);
JSONAssert.assertEquals(expectedString, out.toString(), JSONCompareMode.LENIENT);
JSONAssert.assertEquals(expectedString, out.toString(), JSONCompareMode.NON_EXTENSIBLE);
}
}

Loading…
Cancel
Save