Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding equals and hashcode on I18nMessage #999

Merged
merged 3 commits into from
Aug 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import static com.google.common.base.Objects.toStringHelper;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Objects;
import java.util.ResourceBundle;

import javax.enterprise.inject.Vetoed;
Expand Down Expand Up @@ -108,4 +110,22 @@ public String toString() {
return toStringHelper(this).add("category", category).add("message", message).add("severity",severity).add("parameters", parameters).toString();
}

@Override
public int hashCode() {
return Objects.hashCode(category) ^ Objects.hashCode(message)
^ Objects.hash(parameters) ^ Objects.hashCode(severity);
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
I18nMessage other = (I18nMessage) obj;
return Objects.equals(category, other.category)
&& Objects.equals(message, other.message)
&& ((parameters == null && other.parameters == null)
|| Arrays.equals(parameters, other.parameters))
&& Objects.equals(severity, other.severity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package br.com.caelum.vraptor.validator;

import java.io.Serializable;
import java.util.Objects;
import java.util.ResourceBundle;

import javax.enterprise.inject.Vetoed;
Expand Down Expand Up @@ -48,4 +49,18 @@ public String getKey(ResourceBundle bundle) {
public String toString() {
return String.format("i18n(%s)", key);
}
}

@Override
public int hashCode() {
return Objects.hashCode(key);
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
I18nParam other = (I18nParam) obj;
return Objects.equals(key, other.key);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package br.com.caelum.vraptor.validator;

import static br.com.caelum.vraptor.validator.Severity.INFO;
import static br.com.caelum.vraptor.validator.Severity.WARN;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;

import org.junit.Test;

public class I18nMessageTest {

@Test
public void shouldBeEqualAccordingToMessageCategoryParamsAndSeverity() {

Message m1 = new I18nMessage(new I18nParam("foo"), "equal", INFO, "param");
Message m2 = new I18nMessage(new I18nParam("foo"), "equal", INFO, "param");
assertThat("should be equals, since has the same params", m1, equalTo(m2));

Message m3 = new I18nMessage(new I18nParam("bar"), "equal", INFO, "param");
assertThat("shouldn't be equals, i18nParam is different", m2, not(equalTo(m3)));

Message m4 = new I18nMessage(new I18nParam("bar"), "not.equal", INFO, "param");
assertThat("shouldn't be equals, category is different", m3, not(equalTo(m4)));

Message m5 = new I18nMessage(new I18nParam("bar"), "not.equal", INFO, "other");
assertThat("shouldn't be equals, message is different", m4, not(equalTo(m5)));

Message m6 = new I18nMessage(new I18nParam("bar"), "not.equal", WARN, "other");
assertThat("shouldn't be equals, severity is different", m5, not(equalTo(m6)));
}
}