kwin commented on code in PR #22:
URL: 
https://github.com/apache/sling-org-apache-sling-i18n/pull/22#discussion_r3167691892


##########
src/test/java/org/apache/sling/i18n/impl/JcrResourceBundleTest.java:
##########
@@ -447,4 +452,40 @@ public void test_json_dictionary() throws Exception {
         }
         assertEquals(MESSAGES_DE.size(), counter);
     }
+
+    @Test
+    public void test_json_dictionary_with_invalid_encoding_is_ignored() throws 
Exception {
+        Map<String, Object> targetDictionary = new LinkedHashMap<>();
+
+        invokeLoadJsonDictionary(mockJsonResource("{\"key\":\"value\"}", 
"does-not-exist"), targetDictionary);
+
+        assertTrue("dictionary should stay empty for invalid encoding 
metadata", targetDictionary.isEmpty());
+    }
+
+    @Test
+    public void test_json_dictionary_with_invalid_json_is_ignored() throws 
Exception {
+        Map<String, Object> targetDictionary = new LinkedHashMap<>();
+
+        invokeLoadJsonDictionary(mockJsonResource("{\"key\":", 
StandardCharsets.UTF_8.name()), targetDictionary);
+
+        assertTrue("dictionary should stay empty for malformed JSON", 
targetDictionary.isEmpty());
+    }
+
+    private void invokeLoadJsonDictionary(Resource resource, Map<String, 
Object> targetDictionary) throws Exception {
+        JcrResourceBundle bundle = new JcrResourceBundle(new Locale("de"), 
null, resolver, null, new PathFilter());
+        Method method = 
JcrResourceBundle.class.getDeclaredMethod("loadJsonDictionary", Resource.class, 
Map.class);
+        method.setAccessible(true);
+        method.invoke(bundle, resource, targetDictionary);
+    }
+
+    private Resource mockJsonResource(String json, String encoding) {
+        Resource resource = Mockito.mock(Resource.class);
+        ResourceMetadata metadata = Mockito.mock(ResourceMetadata.class);
+        Mockito.when(resource.getPath()).thenReturn("/apps/i18n/de.json");
+        Mockito.when(resource.getResourceMetadata()).thenReturn(metadata);
+        Mockito.when(metadata.getCharacterEncoding()).thenReturn(encoding);
+        Mockito.when(resource.adaptTo(InputStream.class))
+                .thenReturn(new 
ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)));
+        return resource;
+    }

Review Comment:
   Added via https://github.com/apache/sling-org-apache-sling-i18n/pull/23.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to