Skip to content

Commit

Permalink
Merge branch 'ot-gh175-vmessages'
Browse files Browse the repository at this point in the history
  • Loading branch information
garcia-jj committed Oct 11, 2013
2 parents 2c501a2 + fd82762 commit 1a8bac3
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import br.com.caelum.vraptor.core.Converters;
import br.com.caelum.vraptor.http.InvalidParameterException;
import br.com.caelum.vraptor.validator.Message;
import br.com.caelum.vraptor.validator.ValidationMessage;
import br.com.caelum.vraptor.validator.SimpleMessage;
import br.com.caelum.vraptor.validator.annotation.ValidationException;

import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -96,7 +96,7 @@ public Object instantiate(Target<?> target, Parameters parameters) {
}
private void handleException(Target<?> target, Throwable e) {
if (e.getClass().isAnnotationPresent(ValidationException.class)) {
errors.add(new ValidationMessage(e.getLocalizedMessage(), target.getName()));
errors.add(new SimpleMessage(target.getName(), e.getLocalizedMessage()));
} else if (e.getCause() == null) {
throw new InvalidParameterException("Exception when trying to instantiate " + target, e);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,16 @@
*
* @author Guilherme Silveira
*/
public class ValidationMessage implements Message {
public class SimpleMessage implements Message {

private static final long serialVersionUID = 1L;

private final String message, category;
private final Object[] messageParameters;

public ValidationMessage(String message, String category) {
this(message, category, (Object[]) null);
}

public ValidationMessage(String message, String category, Object... messageParameters) {
this.message = message;
public SimpleMessage(String category, String message, Object... messageParameters) {
this.category = category;
this.message = message;
this.messageParameters = messageParameters;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import br.com.caelum.vraptor.interceptor.ExecuteMethodInterceptor;
import br.com.caelum.vraptor.interceptor.Interceptor;
import br.com.caelum.vraptor.interceptor.ParametersInstantiatorInterceptor;
import br.com.caelum.vraptor.validator.ValidationMessage;
import br.com.caelum.vraptor.validator.SimpleMessage;

import com.google.common.base.Joiner;

Expand Down Expand Up @@ -113,7 +113,7 @@ public void intercept(InterceptorStack stack, ControllerMethod method, Object co
BeanValidatorContext ctx = new BeanValidatorContext(v);
String msg = interpolator.interpolate(v.getMessageTemplate(), ctx, locale);
String category = extractCategory(names, v);
validator.add(new ValidationMessage(msg, category));
validator.add(new SimpleMessage(category, msg));

logger.debug("added message {}={} for contraint violation", msg, category);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
import br.com.caelum.vraptor.http.ParameterNameProvider;
import br.com.caelum.vraptor.http.ParametersProvider;
import br.com.caelum.vraptor.validator.Message;
import br.com.caelum.vraptor.validator.ValidationMessage;
import br.com.caelum.vraptor.validator.SimpleMessage;
import br.com.caelum.vraptor.view.FlashScope;

public class ParametersInstantiatorInterceptorTest {
Expand Down Expand Up @@ -244,7 +244,7 @@ private <T> Answer<T> addErrorsToListAndReturn(final T value, final String... me
@Override
public T answer(InvocationOnMock invocation) throws Throwable {
for (String message : messages) {
errors.add(new ValidationMessage(message, "test"));
errors.add(new SimpleMessage("test", message));
}
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import br.com.caelum.vraptor.controller.BeanClass;
import br.com.caelum.vraptor.controller.ControllerMethod;
import br.com.caelum.vraptor.validator.ValidationMessage;
import br.com.caelum.vraptor.validator.SimpleMessage;

/**
* Useful matchers to use while mocking and hamcresting tests with internal
Expand Down Expand Up @@ -76,16 +76,16 @@ public void describeTo(Description description) {
};
}

public static Matcher<ValidationMessage> error(final String category, final String message) {
return new TypeSafeMatcher<ValidationMessage>() {
public static Matcher<SimpleMessage> error(final String category, final String message) {
return new TypeSafeMatcher<SimpleMessage>() {

@Override
protected void describeMismatchSafely(ValidationMessage item, Description mismatchDescription) {
protected void describeMismatchSafely(SimpleMessage item, Description mismatchDescription) {
mismatchDescription.appendText(" validation message='" +item.getMessage() + "', category = '"+item.getCategory()+"'");
}

@Override
protected boolean matchesSafely(ValidationMessage m) {
protected boolean matchesSafely(SimpleMessage m) {
return message.equals(m.getMessage()) && category.equals(m.getCategory());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
@RunWith(MockitoJUnitRunner.class)
public class DefaultValidatorTest {

private static final Message A_MESSAGE = new ValidationMessage("", "");
private static final Message A_MESSAGE = new SimpleMessage("", "");
private @Mock Result result = new MockResult();
private @Mock LogicResult logicResult;
private @Mock PageResult pageResult;
Expand Down Expand Up @@ -120,7 +120,7 @@ public void shouldNotRedirectIfHasNotErrors() {
public void testThatValidatorGoToRedirectsToTheErrorPageImmediatellyAndNotBeforeThis() {
try {
// call all other validation methods and don't expect them to redirect
validator.addAll(Arrays.asList(new ValidationMessage("test", "test")));
validator.addAll(Arrays.asList(new SimpleMessage("test", "test")));

when(pageResult.of(MyComponent.class)).thenReturn(instance);

Expand All @@ -133,8 +133,8 @@ public void testThatValidatorGoToRedirectsToTheErrorPageImmediatellyAndNotBefore

@Test
public void shouldParametrizeMessage() {
Message message0 = new ValidationMessage("The simple message", "category");
Message message1 = new ValidationMessage("The {0} message", "category", "simple");
Message message0 = new SimpleMessage("category", "The simple message");
Message message1 = new SimpleMessage("category", "The {0} message", "simple");

assertThat(message0.getMessage(), is("The simple message"));
assertThat(message1.getMessage(), is("The simple message"));
Expand All @@ -145,15 +145,15 @@ public void doNothingIfCheckingSuccess() {
Client c = new Client();
c.name = "The name";

validator.check(c.name != null, new ValidationMessage("not null", "client.name"));
validator.check(c.name != null, new SimpleMessage("client.name", "not null"));
assertThat(validator.getErrors(), hasSize(0));
}

@Test
public void shouldAddMessageIfCheckingFails() {
Client c = new Client();

validator.check(c.name != null, new ValidationMessage("not null", "client.name"));
validator.check(c.name != null, new SimpleMessage("client.name", "not null"));
assertThat(validator.getErrors(), hasSize(1));
assertThat(validator.getErrors().get(0).getMessage(), containsString("not null"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import br.com.caelum.vraptor.validator.I18nMessage;
import br.com.caelum.vraptor.validator.Message;
import br.com.caelum.vraptor.validator.SingletonResourceBundle;
import br.com.caelum.vraptor.validator.ValidationMessage;
import br.com.caelum.vraptor.validator.SimpleMessage;

import com.google.common.collect.Lists;
import com.google.gson.JsonSerializer;
Expand Down Expand Up @@ -155,7 +155,7 @@ public void shouldSetMovedPermanentlyStatusOfLogic() throws Exception {

@Test
public void shouldSerializeErrorMessages() throws Exception {
Message normal = new ValidationMessage("The message", "category");
Message normal = new SimpleMessage("category", "The message");
I18nMessage i18ned = new I18nMessage("category", "message");
i18ned.setBundle(new SingletonResourceBundle("message", "Something else"));

Expand All @@ -175,7 +175,7 @@ public void shouldSerializeErrorMessages() throws Exception {

@Test
public void shouldSerializeErrorMessagesInJSON() throws Exception {
Message normal = new ValidationMessage("The message", "category");
Message normal = new SimpleMessage("category", "The message");
I18nMessage i18ned = new I18nMessage("category", "message");
i18ned.setBundle(new SingletonResourceBundle("message", "Something else"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import br.com.caelum.vraptor.musicjungle.interceptor.Public;
import br.com.caelum.vraptor.musicjungle.interceptor.UserInfo;
import br.com.caelum.vraptor.musicjungle.model.User;
import br.com.caelum.vraptor.validator.ValidationMessage;
import br.com.caelum.vraptor.validator.SimpleMessage;

/**
* This class will be responsible to login/logout users.
Expand Down Expand Up @@ -94,7 +94,7 @@ public void login(String login, String password) {
// if no user is found, adds an error message to the validator
// "invalid_login_or_password" is the message key from messages.properties,
// and that key is used with the fmt taglib in index.jsp, for example: <fmt:message key="error.key">
validator.check(currentUser != null, new ValidationMessage("login", "invalid_login_or_password"));
validator.check(currentUser != null, new SimpleMessage("login", "invalid_login_or_password"));

// you can use "this" to redirect to another logic from this controller
validator.onErrorUsePageOf(this).login();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import br.com.caelum.vraptor.musicjungle.model.Music;
import br.com.caelum.vraptor.musicjungle.model.MusicOwner;
import br.com.caelum.vraptor.musicjungle.model.User;
import br.com.caelum.vraptor.validator.ValidationMessage;
import br.com.caelum.vraptor.validator.SimpleMessage;

/**
* The resource <code>MusicOwnerController</code> handles all
Expand Down Expand Up @@ -88,10 +88,10 @@ public void addToMyList(final User user, final Music music) {
final User sessionUser = refreshUser();

validator.check(user.getLogin().equals(sessionUser.getLogin()),
new ValidationMessage("user", "you_cant_add_to_others_list"));
new SimpleMessage("user", "you_cant_add_to_others_list"));

validator.check(!sessionUser.getMusics().contains(music),
new ValidationMessage("music", "you_already_have_this_music"));
new SimpleMessage("music", "you_already_have_this_music"));

validator.onErrorUsePageOf(UsersController.class).home();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import br.com.caelum.vraptor.musicjungle.controller.HomeController;
import br.com.caelum.vraptor.musicjungle.dao.UserDao;
import br.com.caelum.vraptor.musicjungle.model.User;
import br.com.caelum.vraptor.validator.ValidationMessage;
import br.com.caelum.vraptor.validator.SimpleMessage;

/**
* Interceptor to check if the user is in the session.
Expand Down Expand Up @@ -70,7 +70,7 @@ public void intercept(SimpleInterceptorStack stack) {
*/
if (current == null) {
// remember added parameters will survive one more request, when there is a redirect
result.include("errors", asList(new ValidationMessage("user is not logged in", "user")));
result.include("errors", asList(new SimpleMessage("user", "user is not logged in")));
result.redirectTo(HomeController.class).login();
return;
}
Expand Down

0 comments on commit 1a8bac3

Please sign in to comment.