PasswordGenerator { public static String generateRandomPassword(int num) { Random r = new SecureRandom(); StringBuilder password = new StringBuilder(); if (num < minLength) { for (int i = 0; i < minLength; i++) { password.append(generateAlphaNumeric(r)); } } else { List<Character> passwordChars = new ArrayList<Character>(); passwordChars.add(generateLowercaseChar(r)); passwordChars.add(generateUppercaseChar(r)); passwordChars.add(generateDigit(r)); for (int i = passwordChars.size(); i < num; i++) { passwordChars.add(generateAlphaNumeric(r)); } Collections.shuffle(passwordChars, new SecureRandom()); for (char c : passwordChars) { password.append(c); } } return password.toString(); }  static String generateRandomPassword(int num); static String generatePresharedKey(int numChars);  }
@Test public void generateRandomPassword() { Assert.assertTrue(PasswordGenerator.generateRandomPassword(0).length() == 3); Assert.assertTrue(PasswordGenerator.generateRandomPassword(1).length() == 3); Assert.assertTrue(PasswordGenerator.generateRandomPassword(5).length() == 5); String password = PasswordGenerator.generateRandomPassword(8); Assert.assertTrue(containsDigit(password)); Assert.assertTrue(containsLowercase(password)); Assert.assertTrue(containsUppercase(password)); }