AdvancedPropertiesValidator implements GenericMetaInfDataValidator { @Override public Collection<ValidationMessage> validateMetaInfData(@NotNull InputStream input, @NotNull Path filePath) { Collection<ValidationMessage> messages = new LinkedList<>(); try { PackageProperties properties = DefaultPackageProperties.fromInputStream(input); for (Map.Entry<String, PropertiesValidator> entry : propertiesValidators.entrySet()) { messages.add(new ValidationMessage(ValidationMessageSeverity.DEBUG, "Validating with validator " + entry.getKey() + "...")); Collection<ValidationMessage> propertiesValidatorMessages = entry.getValue().validate(properties); if (propertiesValidatorMessages != null) { messages.addAll(ValidationViolation.wrapMessages(entry.getKey(), propertiesValidatorMessages, null, null, null, 0, 0)); } } } catch (InvalidPropertiesFormatException e) { messages.add(new ValidationMessage(severity, MESSAGE_INVALID_PROPERTIES_XML, e)); } catch (IOException e) { throw new IllegalStateException("Could not read from input stream " + filePath, e); } return messages; } AdvancedPropertiesValidator(ValidationMessageSeverity severity); void setPropertiesValidators(Map<String, PropertiesValidator> propertiesValidators); @Override Collection<ValidationMessage> done(); @Override Collection<ValidationMessage> validateMetaInfData(@NotNull InputStream input, @NotNull Path filePath); @Override boolean shouldValidateMetaInfData(@NotNull Path filePath);  }
@Test public void testProperties() throws URISyntaxException, IOException, SAXException, ParserConfigurationException, ConfigurationException { Mockito.when(propertiesValidator1.validate(Mockito.any())).thenReturn(Collections.singleton(new ValidationMessage(ValidationMessageSeverity.ERROR, "error1"))); Mockito.when(propertiesValidator2.validate(Mockito.any())).thenReturn(null); PackageProperties properties; try (InputStream input = this.getClass().getResourceAsStream("/simple-package/META-INF/vault/properties.xml"); InputStream input2 = this.getClass().getResourceAsStream("/simple-package/META-INF/vault/properties.xml")) { Collection<ValidationMessage> messages = validator.validateMetaInfData(input, Paths.get("vault/properties.xml")); ValidationExecutorTest.assertViolation(messages, new ValidationViolation("id1", ValidationMessageSeverity.ERROR, "error1")); properties = DefaultPackageProperties.fromInputStream(input2); Mockito.verify(propertiesValidator1).validate(properties); } }
@Test public void testPropertiesWithInvalidElement() throws URISyntaxException, IOException, SAXException, ParserConfigurationException, ConfigurationException { try (InputStream input = this.getClass().getResourceAsStream("/invalid-package/META-INF/vault/properties.xml")) { Collection<ValidationMessage> messages = validator.validateMetaInfData(input, Paths.get("vault/properties.xml")); ValidationExecutorTest.assertViolation(messages, new ValidationMessage(ValidationMessageSeverity.WARN, AdvancedPropertiesValidator.MESSAGE_INVALID_PROPERTIES_XML, new InvalidPropertiesFormatException("org.xml.sax.SAXParseException; lineNumber: 35; columnNumber: 19; Element type \"someinvalidentry\" must be declared."))); } }