Skip to content

Commit

Permalink
restore #withResourceBundle
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuyuki-baba committed May 25, 2016
1 parent 999359d commit 437225a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
36 changes: 31 additions & 5 deletions src/main/java/com/nulabinc/zxcvbn/Feedback.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

public class Feedback {

private static final String BUNDLE_NAME = "com/nulabinc/zxcvbn/messages";
private static final String DEFAULT_BUNDLE_NAME = "com/nulabinc/zxcvbn/messages";

public static final String DEFAULT_SUGGESTIONS_USE_FEW_WORDS = "feedback.default.suggestions.useFewWords";
public static final String DEFAULT_SUGGESTIONS_NO_NEED_SYMBOLS = "feedback.default.suggestions.noNeedSymbols";
Expand Down Expand Up @@ -56,8 +56,8 @@ public String getWarning(Locale locale) {
if (this.warning == null) {
return "";
}
ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE_NAME, locale);
return bundle.getString(this.warning);
ResourceBundle messages = resolveResourceBundle(locale);
return l10n(messages, this.warning);
}

public List<String> getSuggestions() {
Expand All @@ -66,13 +66,25 @@ public List<String> getSuggestions() {

public List<String> getSuggestions(Locale locale) {
List<String> suggestionTexts = new ArrayList<>(this.suggestions.length);
ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE_NAME, locale);
ResourceBundle messages = resolveResourceBundle(locale);
for (String suggestion : this.suggestions) {
suggestionTexts.add(bundle.getString(suggestion));
suggestionTexts.add(l10n(messages, suggestion));
}
return suggestionTexts;
}

protected ResourceBundle resolveResourceBundle(Locale locale) {
return ResourceBundle.getBundle(DEFAULT_BUNDLE_NAME, locale);
}

public Feedback withResourceBundle(ResourceBundle messages) {
return new ResourceBundleFeedback(messages, warning, suggestions);
}

private String l10n(ResourceBundle messages, String messageId) {
return messages != null ? messages.getString(messageId) : messageId;
}

static Feedback getFeedback(int score, List<Match> sequence) {
if (sequence.size() == 0) {
return getFeedbackWithoutWarnings(
Expand Down Expand Up @@ -184,4 +196,18 @@ private static Feedback getDictionaryMatchFeedback(Match match, boolean isSoleMa
}
return new Feedback(warning, suggestions.toArray(new String[suggestions.size()]));
}

private static class ResourceBundleFeedback extends Feedback{
private ResourceBundle messages;

private ResourceBundleFeedback(ResourceBundle messages, String warning, String... suggestions) {
super(warning, suggestions);
this.messages = messages;
}

@Override
protected ResourceBundle resolveResourceBundle(Locale locale) {
return messages;
}
}
}
18 changes: 18 additions & 0 deletions src/test/java/com/nulabinc/zxcvbn/FeedbackTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@ public void testJapaneseSuggestions() {
}
}

@Test
public void testUnknownWarning() {
Zxcvbn zxcvbn = new Zxcvbn();
Strength strength = zxcvbn.measure(password);
Feedback feedback = strength.getFeedback().withResourceBundle(null);

Assert.assertEquals("Unexpected warning", expectedWarning, feedback.getWarning());
}

@Test
public void testUnknownSuggestions() {
Zxcvbn zxcvbn = new Zxcvbn();
Strength strength = zxcvbn.measure(password);
Feedback feedback = strength.getFeedback().withResourceBundle(null);

Assert.assertArrayEquals("Unexpected suggestions", expectedSuggestions, feedback.getSuggestions().toArray());
}

@Parameterized.Parameters(name = "{0}")
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][]{
Expand Down

0 comments on commit 437225a

Please sign in to comment.