And the ministry of communications development in the name of muhammad al khorezmi tashkent university of information technologies
Download 175,21 Kb.
|
Mustaqil Ish-1
We should know that we need PasswordData and PasswordValidator objects to start validating passwords. We've already discussed PasswordData. Let's create PasswordValidator now.
Firstly, we should define a set of rules for password validation. We have to pass them to constructor while creating a PasswordValidator object: PasswordValidator passwordValidator = new PasswordValidator(new LengthRule(5)); There are two ways of passing our password to a PasswordData object. We pass it to either the constructor or the setter method: PasswordData passwordData = new PasswordData("1234"); PasswordData passwordData2 = new PasswordData(); passwordData.setPassword("1234"); We can validate our password by calling validate() method on PasswordValidator: RuleResult validate = passwordValidator.validate(passwordData); As a result, we'll get a RuleResult object. 3.3. Rule ResultRuleResult holds interesting information about a validation process. It comes as a result of the validate() method. First of all, it can tell us whether the password is valid: Assert.assertEquals(false, validate.isValid()); Moreover, we can learn what errors are returned when the password is invalid. Error codes and validation descriptions are kept in RuleResultDetail: RuleResultDetail ruleResultDetail = validate.getDetails().get(0); Assert.assertEquals("TOO_SHORT", ruleResultDetail.getErrorCode()); Assert.assertEquals(5, ruleResultDetail.getParameters().get("minimumLength")); Assert.assertEquals(5, ruleResultDetail.getParameters().get("maximumLength")); Finally, we can explore the password validation's metadata with RuleResultMetadata: Integer lengthCount = validate .getMetadata() .getCounts() .get(RuleResultMetadata.CountCategory.Length); Assert.assertEquals(Integer.valueOf(4), lengthCount); Download 175,21 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2025
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling