PluginNameValidator implements Validator { protected void validateName(final String name, final ValidatorWithReasonCallback callback) { final String nameWithoutExtension = (name.lastIndexOf(".") >= 0 ? name.substring(0, name.lastIndexOf(".")) : name); final RuleValidator nameValidator = getNameValidator(); if (!nameValidator.isValid(nameWithoutExtension)) { callback.onFailure(nameValidator.getValidationError()); return; } pluginServices.call(new RemoteCallback<Collection<Plugin>>() { @Override public void callback(final Collection<Plugin> plugins) { Set<Activity> activities = pluginsInfo.getAllPlugins(plugins); for (Activity activity : activities) { if (activity.getName().equalsIgnoreCase(nameWithoutExtension)) { callback.onFailure(ValidationErrorReason.DUPLICATED_NAME.name()); return; } } callback.onSuccess(); } }).listPlugins(); }  @Override void validate(final String name,
                         final ValidatorCallback callback);  }
@Test public void validateEmptyName() { validator.validateName("", callback); verify(callback).onFailure(ValidationErrorReason.EMPTY_NAME.name()); validator.validateName("notEmpty", callback); verify(callback).onSuccess(); }
@Test public void validateEmptyNameWithExtension() { validator.validateName(".plugin", callback); verify(callback).onFailure(ValidationErrorReason.EMPTY_NAME.name()); validator.validateName("notEmpty.plugin", callback); verify(callback).onSuccess(); }
@Test public void validateInvalidName() { validator.validateName("invalid*.plugin", callback); verify(callback).onFailure(ValidationErrorReason.INVALID_NAME.name()); validator.validateName("valid.plugin", callback); verify(callback).onSuccess(); }
@Test public void validateDuplicatedName() { validator.validateName("existingPerspectiveLayout.plugin", callback); verify(callback).onFailure(ValidationErrorReason.DUPLICATED_NAME.name()); validator.validateName("nonExistingPerspectiveLayout.plugin", callback); verify(callback).onSuccess(); }