Skip to content

Commit

Permalink
Improving test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
garcia-jj committed Nov 16, 2014
1 parent 8baafc8 commit fbe3635
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@
@Vetoed
public class MessageList extends ForwardingList<Message> {

private static Function<Message, String> byCategory = new Function<Message, String>() {
@Override
public String apply(Message input) {
return input.getCategory();
}
};

private final List<Message> delegate;
private Map<String, Collection<Message>> grouped;

Expand All @@ -56,7 +49,7 @@ public MessageList(List<Message> delegate) {
*/
public Map<String, Collection<Message>> getGrouped() {
if (grouped == null) {
grouped = FluentIterable.from(delegate).index(byCategory).asMap();
grouped = FluentIterable.from(delegate).index(byCategoryMapping()).asMap();
}
return grouped;
}
Expand All @@ -83,6 +76,15 @@ public boolean apply(Message input) {
};
}

private Function<Message, String> byCategoryMapping() {
return new Function<Message, String>() {
@Override
public String apply(Message input) {
return input.getCategory();
}
};
}

private Function<Message, String> toMessageString() {
return new Function<Message, String>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.View;
import br.com.caelum.vraptor.controller.ControllerMethod;
import br.com.caelum.vraptor.controller.DefaultControllerMethod;
import br.com.caelum.vraptor.controller.HttpMethod;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package br.com.caelum.vraptor.observer.download;

import static java.util.Arrays.asList;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -75,4 +76,14 @@ public void shouldUseHeadersToHttpResponse() throws IOException {
verify(response, times(1)).setHeader("Content-type", "application/zip");
verify(response, times(1)).setHeader("Content-disposition", "attachment; filename=download.zip");
}

@Test
public void testConstructWithDownloadBuilder() throws Exception {
Download fd = DownloadBuilder.of(asList(inpuFile0, inpuFile1))
.withFileName("download.zip")
.downloadable().build();
fd.write(response);

verify(response).setHeader("Content-disposition", "attachment; filename=download.zip");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public void withFieldsOnly() throws Exception {

when(observer.createServletFileUpload(config)).thenReturn(servletFileUpload);
when(servletFileUpload.parseRequest(request)).thenReturn(elements);
when(request.getCharacterEncoding()).thenReturn("UTF-8");

observer.upload(event, request, config, validator);

Expand All @@ -130,6 +131,7 @@ public void withFieldsOnlyWithInvalidCharset() throws Exception {

when(observer.createServletFileUpload(config)).thenReturn(servletFileUpload);
when(servletFileUpload.parseRequest(request)).thenReturn(elements);
when(request.getCharacterEncoding()).thenReturn("BLAH");

observer.upload(event, request, config, validator);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.el.ELProcessor;

Expand Down Expand Up @@ -61,6 +63,23 @@ public void shouldNotThrowExceptionIfMessagesHasNoErrors() {
messages.assertAbsenceOfErrors();
}

@Test
public void shouldGroupMessagesInMapFromSameCategory() {
messages.add(new SimpleMessage("client.name", "not null"));
messages.add(new SimpleMessage("client.name", "not empty"));
messages.add(new SimpleMessage("client.email", "not null"));

MessageList messageList = (MessageList) messages.getErrors();

List<Message> messagesName = new ArrayList<>(messageList.getGrouped().get("client.name"));
assertThat(messagesName, hasSize(2));
assertThat(messagesName, contains(messageList.get(0), messageList.get(1)));

List<Message> messagesEmail = new ArrayList<>(messageList.getGrouped().get("client.email"));
assertThat(messagesEmail, hasSize(1));
assertThat(messagesEmail, contains(messageList.get(2)));
}

@Test
public void shouldGroupMessagesFromSameCategory() {
messages.add(new SimpleMessage("client.name", "not null"));
Expand Down

0 comments on commit fbe3635

Please sign in to comment.