Java Assert assertPasswordMeetsRequirements(String password, boolean expected)

Here you can find the source of assertPasswordMeetsRequirements(String password, boolean expected)

Description

assert Password Meets Requirements

License

Open Source License

Declaration

private static void assertPasswordMeetsRequirements(String password, boolean expected) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    private static void assertPasswordMeetsRequirements(String password, boolean expected) {
        boolean actual = passwordMeetsRequirements(password);
        String quotedPassword = (password == null) ? "null" : "\"" + password + "\"";
        String output = "passwordMeetsRequirements(" + quotedPassword + ") = " + actual;
        if (actual == expected) {
            output += " (expected)";
        } else {/*from  ww  w. j  a v  a 2s.  c om*/
            output += ", expected " + expected + ". NOOOOOOOOOOOOOOOOOOOOOOOOOO!";
        }
        System.out.println(output);
    }

    /**
     * Checks to see if the supplied password meets the following requirements:
     *   Must be between 8 and 25 characters long
     *   Contains no whitespace
     *   Contains no slashes "/" "\"
     *   Contains at least three of the following:
     *     * lower case letter
     *     * upper case letter
     *     * digit
     *     * other character
     *
     * @param password
     * @return true if the password meets the requirements, false otherwise
     */
    public static boolean passwordMeetsRequirements(String password) {
        if (password == null) {
            return false;
        }
        if (password.length() < 8 || password.length() > 25) {
            return false;
        }
        boolean containsLowerCase = false;
        boolean containsUpperCase = false;
        boolean containsDigit = false;
        boolean containsWhitespace = false;
        boolean containsSlash = false;
        boolean containsOther = false;
        for (int i = 0; i < password.length(); i++) {
            char single = password.charAt(i);
            if (Character.isLowerCase(single)) {
                containsLowerCase = true;
            } else if (Character.isUpperCase(single)) {
                containsUpperCase = true;
            } else if (Character.isDigit(single)) {
                containsDigit = true;
            } else if (Character.isWhitespace(single)) {
                containsWhitespace = true;
            } else if (single == '/' || single == '\\') {
                containsSlash = true;
            } else {
                containsOther = true;
            }
        }
        if (containsWhitespace || containsSlash) {
            return false;
        }
        int varietyCount = 0;
        varietyCount += containsLowerCase ? 1 : 0;
        varietyCount += containsUpperCase ? 1 : 0;
        varietyCount += containsDigit ? 1 : 0;
        varietyCount += containsOther ? 1 : 0;
        return (varietyCount >= 3);
    }
}

Related

  1. assertOneBitMask(long mask)
  2. assertParameter(Class expectedClass, Object[] params, int index)
  3. assertParameterInRange(long param, long lower, long upper)
  4. assertParameterWithinBounds(String name, long lower, long upper, long parameter)
  5. assertParameterWithinBounds(String name, long lower, long upper, long parameter)
  6. assertPositive(int num, String err)
  7. assertPositive(int val, String msg)
  8. assertPositive(int value)
  9. assertPrecondition(boolean b)