XmlValidatorManager implements ValidatorManager { @Override public String getCacheKey(final Project project, final File projectDir, final Props props) { final Props nonNullProps = props == null ? new Props() : props; StringBuilder compoundedKey = new StringBuilder(); for (final Entry<String, ProjectValidator> validator : this.validators.entrySet()) { try { ProjectValidatorCacheable cacheableValidator = (ProjectValidatorCacheable) validator.getValue(); compoundedKey.append(cacheableValidator.getCacheKey(project, projectDir, nonNullProps)); } catch (ClassCastException e) { } } return HashUtils.SHA1.getHashStr(compoundedKey.toString()); } XmlValidatorManager(final Props props); @Override void loadValidators(final Props props, final Logger log); @Override String getCacheKey(final Project project, final File projectDir, final Props props); @Override Map<String, ValidationReport> validate(final Project project, final File projectDir, final Props additionalProps); @Override List<String> getValidatorsInfo(); static final String VALIDATOR_TAG; static final String CLASSNAME_ATTR; static final String ITEM_TAG; }
@Test public void testGetCacheKeyProjectValidatorCacheable() throws Exception { final Project project = mock(Project.class); final File projectDir = mock(File.class); final Props props = new Props(); final ProjectValidatorCacheable mockValidatorCacheable = mock(ProjectValidatorCacheable.class); final ProjectValidator mockValidator = mock(ProjectValidator.class); final XmlValidatorManager manager = new XmlValidatorManager(props); final String cacheKey = "abc123"; final String expectedResultingCacheKey = HashUtils.SHA1.getHashStr(cacheKey); when(mockValidatorCacheable.getCacheKey(project, projectDir, props)).thenReturn(cacheKey); Map<String, ProjectValidator> mockedValidators = new HashMap(); mockedValidators.put("NORMAL", mockValidator); mockedValidators.put("CACHEABLE", mockValidatorCacheable); Field validatorsField = manager.getClass().getDeclaredField("validators"); validatorsField.setAccessible(true); FieldSetter.setField(manager, validatorsField, mockedValidators); assertEquals(expectedResultingCacheKey, manager.getCacheKey(project, projectDir, props)); }