diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/upload/CommonsUploadMultipartObserver.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/upload/CommonsUploadMultipartObserver.java index e48273e3b..f6dc71a48 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/upload/CommonsUploadMultipartObserver.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/observer/upload/CommonsUploadMultipartObserver.java @@ -144,7 +144,7 @@ protected void processFile(FileItem item, String name, MutableRequest request) { logger.debug("Uploaded file: {} with {}", name, upload); } catch (IOException e) { - throw new InvalidParameterException("Cant parse uploaded file " + item.getName(), e); + throw new InvalidParameterException("Can't parse uploaded file " + item.getName(), e); } } diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/VRaptorMatchers.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/VRaptorMatchers.java index dcfa88ec1..38d2a45ca 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/VRaptorMatchers.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/VRaptorMatchers.java @@ -27,6 +27,8 @@ import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; +import br.com.caelum.vraptor.converter.ConversionException; +import br.com.caelum.vraptor.converter.ConversionMessage; import br.com.caelum.vraptor.http.route.Route; import br.com.caelum.vraptor.validator.Message; @@ -92,4 +94,25 @@ protected boolean matchesSafely(Message message) { } }; } + + public static TypeSafeMatcher hasConversionException(String message) { + final Matcher delegate = equalTo(message); + return new TypeSafeMatcher() { + @Override + protected boolean matchesSafely(Exception item) { + if (item instanceof ConversionException) { + ConversionMessage message = ((ConversionException) item).getValidationMessage(); + message.setBundle(ResourceBundle.getBundle("messages")); + return delegate.matches(message.getMessage()); + } + + return false; + } + + @Override + public void describeTo(Description description) { + delegate.describeTo(description); + } + }; + } } diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/VRaptorTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/VRaptorTest.java index c46cde305..cf994c886 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/VRaptorTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/VRaptorTest.java @@ -28,17 +28,24 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; @RunWith(WeldJunitRunner.class) public class VRaptorTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + @Inject private VRaptor vRaptor; @Inject private MockStaticContentHandler handler; - @Test(expected = ServletException.class) + @Test public void shoudlComplainIfNotInAServletEnviroment() throws Exception { + exception.expect(ServletException.class); + ServletRequest request = mock(ServletRequest.class); ServletResponse response = mock(ServletResponse.class); vRaptor.doFilter(request, response, null); diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/controller/ControllerNotFoundHandlerTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/controller/ControllerNotFoundHandlerTest.java index c8bb62181..1f560fae0 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/controller/ControllerNotFoundHandlerTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/controller/ControllerNotFoundHandlerTest.java @@ -27,7 +27,9 @@ import javax.servlet.ServletException; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -38,6 +40,9 @@ public class ControllerNotFoundHandlerTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private ControllerNotFoundHandler notFoundHandler; private @Mock MutableRequest webRequest; private @Mock MutableResponse webResponse; @@ -56,16 +61,17 @@ public void couldntFindDefersRequestToContainer() throws Exception { verify(chain, only()).doFilter(webRequest, webResponse); } - @Test(expected=InterceptionException.class) + @Test public void shouldThrowInterceptionExceptionIfIOExceptionOccurs() throws Exception { + exception.expect(InterceptionException.class); doThrow(new IOException()).when(chain).doFilter(webRequest, webResponse); notFoundHandler.couldntFind(chain, webRequest, webResponse); } - @Test(expected=InterceptionException.class) + @Test public void shouldThrowInterceptionExceptionIfServletExceptionOccurs() throws Exception { + exception.expect(InterceptionException.class); doThrow(new ServletException()).when(chain).doFilter(webRequest, webResponse); notFoundHandler.couldntFind(chain, webRequest, webResponse); } - -} +} \ No newline at end of file diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/controller/DefaultMethodNotAllowedHandlerTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/controller/DefaultMethodNotAllowedHandlerTest.java index d640eb7be..49e9d7f1f 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/controller/DefaultMethodNotAllowedHandlerTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/controller/DefaultMethodNotAllowedHandlerTest.java @@ -28,7 +28,9 @@ import javax.servlet.http.HttpServletResponse; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import br.com.caelum.vraptor.InterceptionException; import br.com.caelum.vraptor.http.MutableRequest; @@ -36,6 +38,9 @@ public class DefaultMethodNotAllowedHandlerTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private DefaultMethodNotAllowedHandler handler; private MutableResponse response; private MutableRequest request; @@ -71,10 +76,11 @@ public void shouldNotSendMethodNotAllowedIfTheRequestMethodIsOptions() throws Ex verify(response, never()).sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); } - @Test(expected=InterceptionException.class) + @Test public void shouldThrowInterceptionExceptionIfAnIOExceptionOccurs() throws Exception { - doThrow(new IOException()).when(response).sendError(anyInt()); + exception.expect(InterceptionException.class); + doThrow(new IOException()).when(response).sendError(anyInt()); this.handler.deny(request, response, EnumSet.of(HttpMethod.GET, HttpMethod.POST)); } } diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/controller/HttpMethodTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/controller/HttpMethodTest.java index 0f9dc4fb2..4179e510b 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/controller/HttpMethodTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/controller/HttpMethodTest.java @@ -15,19 +15,25 @@ */ package br.com.caelum.vraptor.controller; +import static org.hamcrest.CoreMatchers.containsString; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.when; import javax.servlet.http.HttpServletRequest; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; public class HttpMethodTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private @Mock HttpServletRequest request; @Before @@ -39,20 +45,25 @@ public void setup() { public void shouldConvertGETStringToGetMethodForRequestParameter() throws Exception { when(request.getParameter("_method")).thenReturn("gEt"); when(request.getMethod()).thenReturn("POST"); - + assertEquals(HttpMethod.GET, HttpMethod.of(request)); } - @Test(expected = IllegalArgumentException.class) + @Test public void shouldThrowExceptionForNotKnowHttpMethodsForRequestParameter() throws Exception { + exception.expect(IllegalArgumentException.class); + exception.expectMessage(containsString("HTTP Method not known")); + when(request.getParameter("_method")).thenReturn("JUMP!"); when(request.getMethod()).thenReturn("POST"); HttpMethod.of(request); } - - @Test(expected = InvalidInputException.class) + + @Test public void shouldThrowInvalidInputExceptionIf_methodIsUsedInGETRequests() throws Exception { + exception.expect(InvalidInputException.class); + when(request.getParameter("_method")).thenReturn("DELETE"); when(request.getMethod()).thenReturn("GET"); HttpMethod.of(request); @@ -66,8 +77,11 @@ public void shouldConvertGETStringToGetMethod() throws Exception { assertEquals(HttpMethod.GET, HttpMethod.of(request)); } - @Test(expected = IllegalArgumentException.class) + @Test public void shouldThrowExceptionForNotKnowHttpMethods() throws Exception { + exception.expect(IllegalArgumentException.class); + exception.expectMessage(containsString("HTTP Method not known")); + when(request.getParameter("_method")).thenReturn(null); when(request.getMethod()).thenReturn("JUMP!"); diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/BigDecimalConverterTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/BigDecimalConverterTest.java index bd361cf02..34bd67cb0 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/BigDecimalConverterTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/BigDecimalConverterTest.java @@ -17,12 +17,11 @@ package br.com.caelum.vraptor.converter; -import static br.com.caelum.vraptor.VRaptorMatchers.hasMessage; +import static br.com.caelum.vraptor.VRaptorMatchers.hasConversionException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.fail; import static org.mockito.Mockito.when; import java.math.BigDecimal; @@ -32,17 +31,18 @@ import javax.servlet.http.HttpSession; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import br.com.caelum.vraptor.converter.ConversionException; -import br.com.caelum.vraptor.converter.BigDecimalConverter; import br.com.caelum.vraptor.http.MutableRequest; public class BigDecimalConverterTest { - static final String LOCALE_KEY = "javax.servlet.jsp.jstl.fmt.locale"; + @Rule + public ExpectedException exception = ExpectedException.none(); private BigDecimalConverter converter; private @Mock MutableRequest request; @@ -52,7 +52,6 @@ public class BigDecimalConverterTest { @Before public void setup() { MockitoAnnotations.initMocks(this); - when(request.getServletContext()).thenReturn(context); converter = new BigDecimalConverter(new Locale("pt", "BR")); @@ -83,11 +82,8 @@ public void shouldBeAbleToConvertNull() { @Test public void shouldThrowExceptionWhenUnableToParse() { - try { - converter.convert("vr3.9", BigDecimal.class); - fail("Should throw exception"); - } catch (ConversionException e) { - assertThat(e.getValidationMessage(), hasMessage("vr3.9 is not a valid number.")); - } + exception.expect(hasConversionException("vr3.9 is not a valid number.")); + + converter.convert("vr3.9", BigDecimal.class); } -} +} \ No newline at end of file diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/BigIntegerConverterTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/BigIntegerConverterTest.java index df9ee4898..277b61beb 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/BigIntegerConverterTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/BigIntegerConverterTest.java @@ -17,18 +17,18 @@ package br.com.caelum.vraptor.converter; -import static br.com.caelum.vraptor.VRaptorMatchers.hasMessage; +import static br.com.caelum.vraptor.VRaptorMatchers.hasConversionException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.fail; import java.math.BigInteger; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; - +import org.junit.rules.ExpectedException; /** * VRaptor's BigInteger converter test. @@ -37,6 +37,9 @@ */ public class BigIntegerConverterTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private BigIntegerConverter converter; @Before @@ -51,21 +54,14 @@ public void shouldBeAbleToConvertIntegerNumbers() { @Test public void shouldComplainAboutNonIntegerNumbers() { - try { - converter.convert("2.3", BigInteger.class); - } catch (ConversionException e) { - assertThat(e.getValidationMessage(), hasMessage("2.3 is not a valid number.")); - } + exception.expect(hasConversionException("2.3 is not a valid number.")); + converter.convert("2.3", BigInteger.class); } @Test public void shouldComplainAboutInvalidNumber() { - try { - converter.convert("---", BigInteger.class); - fail("should throw an exception"); - } catch (ConversionException e) { - assertThat(e.getValidationMessage(), hasMessage("--- is not a valid number.")); - } + exception.expect(hasConversionException("--- is not a valid number.")); + converter.convert("---", BigInteger.class); } @Test @@ -77,5 +73,4 @@ public void shouldNotComplainAboutNull() { public void shouldNotComplainAboutEmpty() { assertThat(converter.convert("", BigInteger.class), is(nullValue())); } - -} +} \ No newline at end of file diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/BooleanConverterTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/BooleanConverterTest.java index 25b4fa015..3e838e0c8 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/BooleanConverterTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/BooleanConverterTest.java @@ -17,19 +17,22 @@ package br.com.caelum.vraptor.converter; -import static br.com.caelum.vraptor.VRaptorMatchers.hasMessage; +import static br.com.caelum.vraptor.VRaptorMatchers.hasConversionException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.fail; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; - +import org.junit.rules.ExpectedException; public class BooleanConverterTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private BooleanConverter converter; @Before @@ -58,11 +61,13 @@ public void shouldConvertYesNo() { assertThat(converter.convert("yes", Boolean.class), is(equalTo(true))); assertThat(converter.convert("no", Boolean.class), is(equalTo(false))); } + @Test public void shouldConvertYN() { assertThat(converter.convert("y", Boolean.class), is(equalTo(true))); assertThat(converter.convert("n", Boolean.class), is(equalTo(false))); } + @Test public void shouldConvertOnOff() { assertThat(converter.convert("on", Boolean.class), is(equalTo(true))); @@ -79,11 +84,7 @@ public void shouldConvertIgnoringCase() { @Test public void shouldThrowExceptionForInvalidString() { - try { - converter.convert("not a boolean!", Boolean.class); - fail("should throw an exception"); - } catch(ConversionException e) { - assertThat(e.getValidationMessage(), hasMessage("NOT A BOOLEAN! is not a valid boolean. Please use true/false, yes/no, y/n or on/off")); - } + exception.expect(hasConversionException("NOT A BOOLEAN! is not a valid boolean. Please use true/false, yes/no, y/n or on/off")); + converter.convert("not a boolean!", Boolean.class); } -} +} \ No newline at end of file diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/ByteConverterTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/ByteConverterTest.java index 84b19a410..c9465e0e3 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/ByteConverterTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/ByteConverterTest.java @@ -17,19 +17,22 @@ package br.com.caelum.vraptor.converter; -import static br.com.caelum.vraptor.VRaptorMatchers.hasMessage; +import static br.com.caelum.vraptor.VRaptorMatchers.hasConversionException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.fail; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; - +import org.junit.rules.ExpectedException; public class ByteConverterTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private ByteConverter converter; @Before @@ -44,12 +47,8 @@ public void shouldBeAbleToConvertNumbers() { @Test public void shouldComplainAboutInvalidNumber() { - try { - converter.convert("---", Byte.class); - fail("should throw an exception"); - } catch (ConversionException e) { - assertThat(e.getValidationMessage(), hasMessage("--- is not a valid number.")); - } + exception.expect(hasConversionException("--- is not a valid number.")); + converter.convert("---", Byte.class); } @Test @@ -61,5 +60,4 @@ public void shouldNotComplainAboutNull() { public void shouldNotComplainAboutEmpty() { assertThat(converter.convert("", Byte.class), is(nullValue())); } - -} +} \ No newline at end of file diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/CalendarConverterTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/CalendarConverterTest.java index 5d5da1fee..3af02da30 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/CalendarConverterTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/CalendarConverterTest.java @@ -17,12 +17,11 @@ package br.com.caelum.vraptor.converter; -import static br.com.caelum.vraptor.VRaptorMatchers.hasMessage; +import static br.com.caelum.vraptor.VRaptorMatchers.hasConversionException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.fail; import static org.mockito.Mockito.when; import java.util.Calendar; @@ -32,7 +31,9 @@ import javax.servlet.http.HttpSession; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -40,7 +41,8 @@ public class CalendarConverterTest { - static final String LOCALE_KEY = "javax.servlet.jsp.jstl.fmt.locale"; + @Rule + public ExpectedException exception = ExpectedException.none(); private CalendarConverter converter; private @Mock MutableRequest request; @@ -50,7 +52,6 @@ public class CalendarConverterTest { @Before public void setup() { MockitoAnnotations.initMocks(this); - when(request.getServletContext()).thenReturn(context); converter = new CalendarConverter(new Locale("pt", "BR")); @@ -78,11 +79,7 @@ public void shouldBeAbleToConvertNull() { @Test public void shouldThrowExceptionWhenUnableToParse() { - try { - converter.convert("a,10/06/2008/a/b/c", Calendar.class); - fail("should throw an exception"); - } catch (ConversionException e) { - assertThat(e.getValidationMessage(), hasMessage("a,10/06/2008/a/b/c is not a valid date.")); - } + exception.expect(hasConversionException("a,10/06/2008/a/b/c is not a valid date.")); + converter.convert("a,10/06/2008/a/b/c", Calendar.class); } -} +} \ No newline at end of file diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/CharacterConverterTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/CharacterConverterTest.java index a8c35a5e7..81e70399e 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/CharacterConverterTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/CharacterConverterTest.java @@ -17,19 +17,22 @@ package br.com.caelum.vraptor.converter; -import static br.com.caelum.vraptor.VRaptorMatchers.hasMessage; +import static br.com.caelum.vraptor.VRaptorMatchers.hasConversionException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.fail; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; - +import org.junit.rules.ExpectedException; public class CharacterConverterTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private CharacterConverter converter; @Before @@ -44,12 +47,8 @@ public void shouldBeAbleToConvertCharacters() { @Test public void shouldComplainAboutStringTooBig() { - try { - converter.convert("---", Character.class); - fail("should throw an exception"); - } catch (ConversionException e) { - assertThat(e.getValidationMessage(), hasMessage("--- is not a valid character.")); - } + exception.expect(hasConversionException("--- is not a valid character.")); + converter.convert("---", Character.class); } @Test @@ -57,5 +56,4 @@ public void shouldNotComplainAboutNullAndEmpty() { assertThat(converter.convert(null, Character.class), is(nullValue())); assertThat(converter.convert("", Character.class), is(nullValue())); } - -} +} \ No newline at end of file diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/DateConverterTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/DateConverterTest.java index 9b2d6b699..9a9fe8bef 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/DateConverterTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/DateConverterTest.java @@ -17,12 +17,11 @@ package br.com.caelum.vraptor.converter; -import static br.com.caelum.vraptor.VRaptorMatchers.hasMessage; +import static br.com.caelum.vraptor.VRaptorMatchers.hasConversionException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.fail; import static org.mockito.Mockito.when; import java.text.ParseException; @@ -34,7 +33,9 @@ import javax.servlet.http.HttpSession; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -42,7 +43,8 @@ public class DateConverterTest { - static final String LOCALE_KEY = "javax.servlet.jsp.jstl.fmt.locale"; + @Rule + public ExpectedException exception = ExpectedException.none(); private DateConverter converter; private @Mock MutableRequest request; @@ -52,7 +54,6 @@ public class DateConverterTest { @Before public void setup() { MockitoAnnotations.initMocks(this); - when(request.getServletContext()).thenReturn(context); converter = new DateConverter(new Locale("pt", "BR")); @@ -76,11 +77,7 @@ public void shouldBeAbleToConvertNull() { @Test public void shouldThrowExceptionWhenUnableToParse() { - try { - converter.convert("a,10/06/2008/a/b/c", Date.class); - fail("should throw an exception"); - } catch (ConversionException e) { - assertThat(e.getValidationMessage(), hasMessage("a,10/06/2008/a/b/c is not a valid date.")); - } + exception.expect(hasConversionException("a,10/06/2008/a/b/c is not a valid date.")); + converter.convert("a,10/06/2008/a/b/c", Date.class); } -} +} \ No newline at end of file diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/DoubleConverterTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/DoubleConverterTest.java index 1f50ec13d..61ba64174 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/DoubleConverterTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/DoubleConverterTest.java @@ -17,12 +17,11 @@ package br.com.caelum.vraptor.converter; -import static br.com.caelum.vraptor.VRaptorMatchers.hasMessage; +import static br.com.caelum.vraptor.VRaptorMatchers.hasConversionException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.fail; import static org.mockito.Mockito.when; import java.util.Locale; @@ -31,17 +30,18 @@ import javax.servlet.http.HttpSession; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import br.com.caelum.vraptor.converter.ConversionException; -import br.com.caelum.vraptor.converter.DoubleConverter; import br.com.caelum.vraptor.http.MutableRequest; public class DoubleConverterTest { - static final String LOCALE_KEY = "javax.servlet.jsp.jstl.fmt.locale"; + @Rule + public ExpectedException exception = ExpectedException.none(); private DoubleConverter converter; private @Mock MutableRequest request; @@ -51,7 +51,6 @@ public class DoubleConverterTest { @Before public void setup() { MockitoAnnotations.initMocks(this); - when(request.getServletContext()).thenReturn(context); converter = new DoubleConverter(new Locale("pt", "BR")); @@ -70,23 +69,19 @@ public void shouldBeAbleToConvertWithENUS() { assertThat(converter.convert("10.01", Double.class), is(equalTo(new Double("10.01")))); } - @Test - public void shouldBeAbleToConvertEmpty() { - assertThat(converter.convert("", Double.class), is(nullValue())); - } + @Test + public void shouldBeAbleToConvertEmpty() { + assertThat(converter.convert("", Double.class), is(nullValue())); + } - @Test - public void shouldBeAbleToConvertNull() { - assertThat(converter.convert(null, Double.class), is(nullValue())); - } + @Test + public void shouldBeAbleToConvertNull() { + assertThat(converter.convert(null, Double.class), is(nullValue())); + } @Test public void shouldThrowExceptionWhenUnableToParse() { - try { - converter.convert("vr3.9", Double.class); - fail("Should throw exception"); - } catch (ConversionException e) { - assertThat(e.getValidationMessage(), hasMessage("vr3.9 is not a valid number.")); - } + exception.expect(hasConversionException("vr3.9 is not a valid number.")); + converter.convert("vr3.9", Double.class); } -} +} \ No newline at end of file diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/EnumConverterTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/EnumConverterTest.java index 65118d7d0..72f64910c 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/EnumConverterTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/EnumConverterTest.java @@ -17,18 +17,22 @@ package br.com.caelum.vraptor.converter; -import static br.com.caelum.vraptor.VRaptorMatchers.hasMessage; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static br.com.caelum.vraptor.VRaptorMatchers.hasConversionException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertEquals; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; public class EnumConverterTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private Converter converter; @Before @@ -45,7 +49,6 @@ public void shouldBeAbleToConvertByOrdinal() { @Test public void shouldBeAbleToConvertByName() { - Enum value = converter.convert("FIRST", MyCustomEnum.class); MyCustomEnum first = MyCustomEnum.FIRST; assertEquals(value, first); @@ -58,32 +61,20 @@ public void shouldConvertEmptyToNull() { @Test public void shouldComplainAboutInvalidIndex() { - try { - converter.convert("3200", MyCustomEnum.class); - fail("should throw an exception"); - } catch (ConversionException e) { - assertThat(e.getValidationMessage(), hasMessage("3200 is not a valid option.")); - } + exception.expect(hasConversionException("3200 is not a valid option.")); + converter.convert("3200", MyCustomEnum.class); } @Test public void shouldComplainAboutInvalidNumber() { - try { - converter.convert("32a00", MyCustomEnum.class); - fail("should throw an exception"); - } catch (ConversionException e) { - assertThat(e.getValidationMessage(), hasMessage("32a00 is not a valid option.")); - } + exception.expect(hasConversionException("32a00 is not a valid option.")); + converter.convert("32a00", MyCustomEnum.class); } @Test public void shouldComplainAboutInvalidOrdinal() { - try { - converter.convert("THIRD", MyCustomEnum.class); - fail("should throw an exception"); - } catch (ConversionException e) { - assertThat(e.getValidationMessage(), hasMessage("THIRD is not a valid option.")); - } + exception.expect(hasConversionException("THIRD is not a valid option.")); + converter.convert("THIRD", MyCustomEnum.class); } @Test @@ -94,4 +85,4 @@ public void shouldAcceptNull() { enum MyCustomEnum { FIRST, SECOND } -} +} \ No newline at end of file diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/FloatConverterTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/FloatConverterTest.java index 205eeb280..b5b28e17a 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/FloatConverterTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/FloatConverterTest.java @@ -17,12 +17,11 @@ package br.com.caelum.vraptor.converter; -import static br.com.caelum.vraptor.VRaptorMatchers.hasMessage; +import static br.com.caelum.vraptor.VRaptorMatchers.hasConversionException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.fail; import static org.mockito.Mockito.when; import java.util.Locale; @@ -31,17 +30,18 @@ import javax.servlet.http.HttpSession; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import br.com.caelum.vraptor.converter.ConversionException; -import br.com.caelum.vraptor.converter.FloatConverter; import br.com.caelum.vraptor.http.MutableRequest; public class FloatConverterTest { - static final String LOCALE_KEY = "javax.servlet.jsp.jstl.fmt.locale"; + @Rule + public ExpectedException exception = ExpectedException.none(); private FloatConverter converter; private @Mock MutableRequest request; @@ -51,7 +51,6 @@ public class FloatConverterTest { @Before public void setup() { MockitoAnnotations.initMocks(this); - when(request.getServletContext()).thenReturn(context); converter = new FloatConverter(new Locale("pt", "BR")); @@ -82,11 +81,7 @@ public void shouldBeAbleToConvertNull() { @Test public void shouldThrowExceptionWhenUnableToParse() { - try { - converter.convert("vr3.9", Float.class); - fail("Should throw exception"); - } catch (ConversionException e) { - assertThat(e.getValidationMessage(), hasMessage("vr3.9 is not a valid number.")); - } + exception.expect(hasConversionException("vr3.9 is not a valid number.")); + converter.convert("vr3.9", Float.class); } -} +} \ No newline at end of file diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/IntegerConverterTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/IntegerConverterTest.java index 6bc9657ab..982d130d9 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/IntegerConverterTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/IntegerConverterTest.java @@ -17,19 +17,22 @@ package br.com.caelum.vraptor.converter; -import static br.com.caelum.vraptor.VRaptorMatchers.hasMessage; +import static br.com.caelum.vraptor.VRaptorMatchers.hasConversionException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.fail; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; - +import org.junit.rules.ExpectedException; public class IntegerConverterTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private IntegerConverter converter; @Before @@ -44,12 +47,8 @@ public void shouldBeAbleToConvertNumbers() { @Test public void shouldComplainAboutInvalidNumber() { - try { - converter.convert("---", Integer.class); - fail("should throw an exception"); - } catch (ConversionException e) { - assertThat(e.getValidationMessage(), hasMessage("--- is not a valid number.")); - } + exception.expect(hasConversionException("--- is not a valid number.")); + converter.convert("---", Integer.class); } @Test @@ -61,4 +60,4 @@ public void shouldNotComplainAboutNull() { public void shouldNotComplainAboutEmpty() { assertThat(converter.convert("", Integer.class), is(nullValue())); } -} +} \ No newline at end of file diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/LongConverterTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/LongConverterTest.java index e7cb7ef8f..1e51d0e09 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/LongConverterTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/LongConverterTest.java @@ -17,19 +17,22 @@ package br.com.caelum.vraptor.converter; -import static br.com.caelum.vraptor.VRaptorMatchers.hasMessage; +import static br.com.caelum.vraptor.VRaptorMatchers.hasConversionException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.fail; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; - +import org.junit.rules.ExpectedException; public class LongConverterTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private LongConverter converter; @Before @@ -44,12 +47,8 @@ public void shouldBeAbleToConvertNumbers(){ @Test public void shouldComplainAboutInvalidNumber() { - try { - converter.convert("---", long.class); - fail("should throw an exception"); - } catch (ConversionException e) { - assertThat(e.getValidationMessage(), hasMessage("--- is not a valid number.")); - } + exception.expect(hasConversionException("--- is not a valid number.")); + converter.convert("---", long.class); } @Test @@ -61,5 +60,4 @@ public void shouldNotComplainAboutNull() { public void shouldNotComplainAboutEmpty() { assertThat(converter.convert("", long.class), is(nullValue())); } - -} +} \ No newline at end of file diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveBooleanConverterTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveBooleanConverterTest.java index 14e2c2f83..a270e52f2 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveBooleanConverterTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveBooleanConverterTest.java @@ -17,19 +17,21 @@ package br.com.caelum.vraptor.converter; +import static br.com.caelum.vraptor.VRaptorMatchers.hasConversionException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.fail; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; - -import br.com.caelum.vraptor.VRaptorMatchers; - +import org.junit.rules.ExpectedException; public class PrimitiveBooleanConverterTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private PrimitiveBooleanConverter converter; @Before @@ -83,12 +85,7 @@ public void shouldConvertIgnoringCase() { @Test public void shouldThrowExceptionForInvalidString() { - try { - converter.convert("not a boolean!", boolean.class); - fail("should throw an exception"); - } catch (ConversionException e) { - assertThat(e.getValidationMessage(), - VRaptorMatchers.hasMessage("NOT A BOOLEAN! is not a valid boolean. Please use true/false, yes/no, y/n or on/off")); - } + exception.expect(hasConversionException("NOT A BOOLEAN! is not a valid boolean. Please use true/false, yes/no, y/n or on/off")); + converter.convert("not a boolean!", boolean.class); } -} +} \ No newline at end of file diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveByteConverterTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveByteConverterTest.java index 3c684fb0a..d2627c30f 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveByteConverterTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveByteConverterTest.java @@ -17,18 +17,21 @@ package br.com.caelum.vraptor.converter; -import static br.com.caelum.vraptor.VRaptorMatchers.hasMessage; +import static br.com.caelum.vraptor.VRaptorMatchers.hasConversionException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.fail; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; - +import org.junit.rules.ExpectedException; public class PrimitiveByteConverterTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private PrimitiveByteConverter converter; @Before @@ -43,12 +46,8 @@ public void shouldBeAbleToConvertNumbers() { @Test public void shouldComplainAboutInvalidNumber() { - try { - converter.convert("---", byte.class); - fail("should throw an exception"); - } catch (ConversionException e) { - assertThat(e.getValidationMessage(), hasMessage("--- is not a valid number.")); - } + exception.expect(hasConversionException("--- is not a valid number.")); + converter.convert("---", byte.class); } @Test @@ -60,5 +59,4 @@ public void shouldConvertToZeroWhenNull() { public void shouldConvertToZeroWhenEmpty() { assertThat(converter.convert("", byte.class), is(equalTo((byte) 0))); } - -} +} \ No newline at end of file diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveCharConverterTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveCharConverterTest.java index 9b8a1336a..91b6b49a5 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveCharConverterTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveCharConverterTest.java @@ -17,18 +17,21 @@ package br.com.caelum.vraptor.converter; -import static br.com.caelum.vraptor.VRaptorMatchers.hasMessage; +import static br.com.caelum.vraptor.VRaptorMatchers.hasConversionException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.fail; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; - +import org.junit.rules.ExpectedException; public class PrimitiveCharConverterTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private PrimitiveCharConverter converter; @Before @@ -43,12 +46,8 @@ public void shouldBeAbleToConvertNumbers() { @Test public void shouldComplainAboutInvalidNumber() { - try { - converter.convert("---", char.class); - fail("should throw an exception"); - } catch (ConversionException e) { - assertThat(e.getValidationMessage(), hasMessage("--- is not a valid character.")); - } + exception.expect(hasConversionException("--- is not a valid character.")); + converter.convert("---", char.class); } @Test @@ -60,5 +59,4 @@ public void shouldConvertToZeroWhenNull() { public void shouldConvertToZeroWhenEmpty() { assertThat(converter.convert("", char.class), is(equalTo('\u0000'))); } - -} +} \ No newline at end of file diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveDoubleConverterTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveDoubleConverterTest.java index 53a8d1ee9..45d202126 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveDoubleConverterTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveDoubleConverterTest.java @@ -17,11 +17,10 @@ package br.com.caelum.vraptor.converter; -import static br.com.caelum.vraptor.VRaptorMatchers.hasMessage; +import static br.com.caelum.vraptor.VRaptorMatchers.hasConversionException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.fail; import static org.mockito.Mockito.when; import java.util.Locale; @@ -30,17 +29,18 @@ import javax.servlet.http.HttpSession; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import br.com.caelum.vraptor.converter.ConversionException; -import br.com.caelum.vraptor.converter.PrimitiveDoubleConverter; import br.com.caelum.vraptor.http.MutableRequest; public class PrimitiveDoubleConverterTest { - static final String LOCALE_KEY = "javax.servlet.jsp.jstl.fmt.locale"; + @Rule + public ExpectedException exception = ExpectedException.none(); private PrimitiveDoubleConverter converter; private @Mock MutableRequest request; @@ -50,7 +50,6 @@ public class PrimitiveDoubleConverterTest { @Before public void setup() { MockitoAnnotations.initMocks(this); - when(request.getServletContext()).thenReturn(context); converter = new PrimitiveDoubleConverter(new Locale("pt", "BR")); @@ -69,23 +68,19 @@ public void shouldBeAbleToConvertWithENUS() { assertThat(converter.convert("10.01", double.class), is(equalTo(Double.parseDouble("10.01")))); } - @Test - public void shouldBeAbleToConvertEmpty() { - assertThat(converter.convert("", double.class), is(equalTo(0d))); - } + @Test + public void shouldBeAbleToConvertEmpty() { + assertThat(converter.convert("", double.class), is(equalTo(0d))); + } - @Test - public void shouldBeAbleToConvertNull() { - assertThat(converter.convert(null, double.class), is(equalTo(0d))); - } + @Test + public void shouldBeAbleToConvertNull() { + assertThat(converter.convert(null, double.class), is(equalTo(0d))); + } @Test public void shouldThrowExceptionWhenUnableToParse() { - try { - converter.convert("vr3.9", double.class); - fail("Should throw exception"); - } catch (ConversionException e) { - assertThat(e.getValidationMessage(), hasMessage("vr3.9 is not a valid number.")); - } + exception.expect(hasConversionException("vr3.9 is not a valid number.")); + converter.convert("vr3.9", double.class); } -} +} \ No newline at end of file diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveFloatConverterTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveFloatConverterTest.java index ab00095cd..b35814b4c 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveFloatConverterTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveFloatConverterTest.java @@ -16,11 +16,10 @@ */ package br.com.caelum.vraptor.converter; -import static br.com.caelum.vraptor.VRaptorMatchers.hasMessage; +import static br.com.caelum.vraptor.VRaptorMatchers.hasConversionException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.fail; import static org.mockito.Mockito.when; import java.util.Locale; @@ -29,16 +28,19 @@ import javax.servlet.http.HttpSession; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import br.com.caelum.vraptor.converter.ConversionException; -import br.com.caelum.vraptor.converter.PrimitiveFloatConverter; import br.com.caelum.vraptor.http.MutableRequest; public class PrimitiveFloatConverterTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private PrimitiveFloatConverter converter; private @Mock MutableRequest request; private @Mock HttpSession session; @@ -47,7 +49,6 @@ public class PrimitiveFloatConverterTest { @Before public void setup() { MockitoAnnotations.initMocks(this); - when(request.getServletContext()).thenReturn(context); converter = new PrimitiveFloatConverter(new Locale("pt", "BR")); @@ -66,23 +67,19 @@ public void shouldBeAbleToConvertWithENUS() { assertThat(converter.convert("10.01", float.class), is(equalTo(Float.parseFloat("10.01")))); } - @Test - public void shouldBeAbleToConvertEmpty() { - assertThat(converter.convert("", float.class), is(equalTo(0f))); - } + @Test + public void shouldBeAbleToConvertEmpty() { + assertThat(converter.convert("", float.class), is(equalTo(0f))); + } - @Test - public void shouldBeAbleToConvertNull() { - assertThat(converter.convert(null, float.class), is(equalTo(0f))); - } + @Test + public void shouldBeAbleToConvertNull() { + assertThat(converter.convert(null, float.class), is(equalTo(0f))); + } @Test public void shouldThrowExceptionWhenUnableToParse() { - try { - converter.convert("vr3.9", float.class); - fail("Should throw exception"); - } catch (ConversionException e) { - assertThat(e.getValidationMessage(), hasMessage("vr3.9 is not a valid number.")); - } + exception.expect(hasConversionException("vr3.9 is not a valid number.")); + converter.convert("vr3.9", float.class); } -} +} \ No newline at end of file diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveIntConverterTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveIntConverterTest.java index f447616e8..0bfd5a0bd 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveIntConverterTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveIntConverterTest.java @@ -17,18 +17,21 @@ package br.com.caelum.vraptor.converter; -import static br.com.caelum.vraptor.VRaptorMatchers.hasMessage; +import static br.com.caelum.vraptor.VRaptorMatchers.hasConversionException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.fail; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; - +import org.junit.rules.ExpectedException; public class PrimitiveIntConverterTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private PrimitiveIntConverter converter; @Before @@ -43,12 +46,8 @@ public void shouldBeAbleToConvertNumbers() { @Test public void shouldComplainAboutInvalidNumber() { - try { - converter.convert("---", int.class); - fail("should throw an exception"); - } catch (ConversionException e) { - assertThat(e.getValidationMessage(), hasMessage("--- is not a valid number.")); - } + exception.expect(hasConversionException("--- is not a valid number.")); + converter.convert("---", int.class); } @Test @@ -60,5 +59,4 @@ public void shouldConvertToZeroWhenNull() { public void shouldConvertToZeroWhenEmpty() { assertThat(converter.convert("", int.class), is(equalTo(0))); } - -} +} \ No newline at end of file diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveLongConverterTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveLongConverterTest.java index f258eb45e..2ec9d9391 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveLongConverterTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveLongConverterTest.java @@ -17,18 +17,21 @@ package br.com.caelum.vraptor.converter; -import static br.com.caelum.vraptor.VRaptorMatchers.hasMessage; +import static br.com.caelum.vraptor.VRaptorMatchers.hasConversionException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.fail; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; - +import org.junit.rules.ExpectedException; public class PrimitiveLongConverterTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private PrimitiveLongConverter converter; @Before @@ -43,12 +46,8 @@ public void shouldBeAbleToConvertNumbers() { @Test public void shouldComplainAboutInvalidNumber() { - try { - converter.convert("---", long.class); - fail("should throw an exception"); - } catch (ConversionException e) { - assertThat(e.getValidationMessage(), hasMessage("--- is not a valid number.")); - } + exception.expect(hasConversionException("--- is not a valid number.")); + converter.convert("---", long.class); } @Test @@ -60,5 +59,4 @@ public void shouldConvertToZeroWhenNull() { public void shouldConvertToZeroWhenEmpty() { assertThat(converter.convert("", long.class), is(equalTo(0L))); } - -} +} \ No newline at end of file diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveShortConverterTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveShortConverterTest.java index c0660c5df..79825fbfb 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveShortConverterTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/PrimitiveShortConverterTest.java @@ -17,19 +17,21 @@ package br.com.caelum.vraptor.converter; +import static br.com.caelum.vraptor.VRaptorMatchers.hasConversionException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.fail; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; - -import br.com.caelum.vraptor.VRaptorMatchers; - +import org.junit.rules.ExpectedException; public class PrimitiveShortConverterTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private PrimitiveShortConverter converter; @Before @@ -44,12 +46,8 @@ public void shouldBeAbleToConvertNumbers(){ @Test public void shouldComplainAboutInvalidNumber() { - try { - converter.convert("---", short.class); - fail("should throw an exception"); - } catch (ConversionException e) { - assertThat(e.getValidationMessage(), VRaptorMatchers.hasMessage("--- is not a valid number.")); - } + exception.expect(hasConversionException("--- is not a valid number.")); + converter.convert("---", short.class); } @Test @@ -61,5 +59,4 @@ public void shouldConvertToZeroWhenNull() { public void shouldConvertToZeroWhenEmpty() { assertThat(converter.convert("", short.class), is(equalTo((short) 0))); } - -} +} \ No newline at end of file diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/ShortConverterTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/ShortConverterTest.java index 1ffcc826f..b391c84fe 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/ShortConverterTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/converter/ShortConverterTest.java @@ -17,19 +17,22 @@ package br.com.caelum.vraptor.converter; -import static br.com.caelum.vraptor.VRaptorMatchers.hasMessage; +import static br.com.caelum.vraptor.VRaptorMatchers.hasConversionException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.fail; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; - +import org.junit.rules.ExpectedException; public class ShortConverterTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private ShortConverter converter; @Before @@ -44,12 +47,8 @@ public void shouldBeAbleToConvertNumbers() { @Test public void shouldComplainAboutInvalidNumber() { - try { - converter.convert("---", Short.class); - fail("should throw an exception"); - } catch (ConversionException e) { - assertThat(e.getValidationMessage(), hasMessage("--- is not a valid number.")); - } + exception.expect(hasConversionException("--- is not a valid number.")); + converter.convert("---", Short.class); } @Test @@ -61,5 +60,4 @@ public void shouldComplainAboutNull() { public void shouldComplainAboutEmpty() { assertThat(converter.convert("", Short.class), is(nullValue())); } - -} +} \ No newline at end of file diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/core/DefaultConvertersTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/core/DefaultConvertersTest.java index 977822273..675bff7cf 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/core/DefaultConvertersTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/core/DefaultConvertersTest.java @@ -23,7 +23,9 @@ import static org.mockito.Mockito.when; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -35,6 +37,9 @@ public class DefaultConvertersTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + @Mock private Container container; private DefaultConverters converters; @@ -45,13 +50,19 @@ public void setup() { this.converters = new DefaultConverters(container, cache); } - @Test(expected = IllegalStateException.class) + @Test public void complainsIfNoConverterFound() { + exception.expect(IllegalStateException.class); + exception.expectMessage("Unable to find converter for " + getClass().getName()); + converters.to(DefaultConvertersTest.class); } - @Test(expected = IllegalStateException.class) + @Test public void convertingANonAnnotatedConverterEndsUpComplaining() { + exception.expect(IllegalStateException.class); + exception.expectMessage("The converter type " + WrongConverter.class.getName() + " should have the Convert annotation"); + converters.register(WrongConverter.class); } diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandlerTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandlerTest.java index 2a5d24f97..54d7b4e9c 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandlerTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/core/ToInstantiateInterceptorHandlerTest.java @@ -16,6 +16,7 @@ */ package br.com.caelum.vraptor.core; +import static org.hamcrest.CoreMatchers.containsString; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -23,7 +24,9 @@ import java.io.IOException; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -34,6 +37,9 @@ public class ToInstantiateInterceptorHandlerTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private @Mock Container container; private @Mock Interceptor interceptor; private @Mock InterceptorStack stack; @@ -63,10 +69,13 @@ public static class Dependency { } - @Test(expected = InterceptionException.class) + @Test public void shouldComplainWhenUnableToInstantiateAnInterceptor() throws InterceptionException, IOException { + exception.expect(InterceptionException.class); + exception.expectMessage(containsString("Unable to instantiate interceptor for")); + when(container.instanceFor(MyWeirdInterceptor.class)).thenReturn(null); - + ToInstantiateInterceptorHandler handler = new ToInstantiateInterceptorHandler(container, MyWeirdInterceptor.class); handler.execute(null, null, null); diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/environment/DefaultEnvironmentTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/environment/DefaultEnvironmentTest.java index 13bd5a5a0..129e4201a 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/environment/DefaultEnvironmentTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/environment/DefaultEnvironmentTest.java @@ -28,10 +28,15 @@ import javax.servlet.ServletContext; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; public class DefaultEnvironmentTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + @Test public void shouldUseTheCurrentEnvironmentFileIfFound() throws IOException { ServletContext context = mock(ServletContext.class); @@ -67,8 +72,10 @@ public void shouldUseFalseIfFeatureIsNotPresent() throws IOException { assertThat(env.supports("feature_that_doesnt_exists"), equalTo(false)); } - @Test(expected = NoSuchElementException.class) + @Test public void shouldThrowExceptionIfKeyDoesNotExist() throws Exception { + exception.expect(NoSuchElementException.class); + ServletContext context = mock(ServletContext.class); ServletBasedEnvironment env = new ServletBasedEnvironment(context); env.get("key_that_doesnt_exist"); diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/environment/EnvironmentPropertyProducerTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/environment/EnvironmentPropertyProducerTest.java index cf8c86bcd..532ee35d4 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/environment/EnvironmentPropertyProducerTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/environment/EnvironmentPropertyProducerTest.java @@ -22,7 +22,9 @@ import javax.enterprise.inject.Instance; import javax.inject.Inject; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import br.com.caelum.vraptor.WeldJunitRunner; @@ -30,6 +32,9 @@ @RunWith(WeldJunitRunner.class) public class EnvironmentPropertyProducerTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + @Inject @Property("email.server.host") private String mailHost; @@ -54,8 +59,9 @@ public void shouldInferKeyFromFieldName() throws Exception { assertEquals(itWorks, "It Works!"); } - @Test(expected=NoSuchElementException.class) + @Test public void shouldNotResolveUnexistentKeys() throws Exception { + exception.expect(NoSuchElementException.class); nonExistent.get(); } diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/environment/ServletBasedEnvironmentTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/environment/ServletBasedEnvironmentTest.java index 5de44b587..cc0a75fe2 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/environment/ServletBasedEnvironmentTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/environment/ServletBasedEnvironmentTest.java @@ -28,10 +28,15 @@ import javax.servlet.ServletContext; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; public class ServletBasedEnvironmentTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + @Test public void shouldUseTheCurrentEnvironmentFileIfFound() throws IOException { ServletContext context = mock(ServletContext.class); @@ -60,8 +65,10 @@ public void shouldNotUseAnyPropertiesIfItDoesntExist() throws IOException { assertFalse(env.has("unexistant_key")); } - @Test(expected = NoSuchElementException.class) + @Test public void shouldThrowExceptionIfKeyDoesNotExist() throws Exception { + exception.expect(NoSuchElementException.class); + ServletContext context = mock(ServletContext.class); ServletBasedEnvironment env = new ServletBasedEnvironment(context); env.get("key_that_doesnt_exist"); diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/http/DefaultControllerTranslatorTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/http/DefaultControllerTranslatorTest.java index 8b9fe08af..855fe3810 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/http/DefaultControllerTranslatorTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/http/DefaultControllerTranslatorTest.java @@ -26,10 +26,14 @@ import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.when; +import java.util.EnumSet; + import javax.servlet.http.HttpServletRequest; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -40,6 +44,9 @@ public class DefaultControllerTranslatorTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private @Mock Router router; private @Mock HttpServletRequest request; @@ -104,11 +111,15 @@ public void shouldAcceptCaseInsensitiveGetRequestUsingThe_methodParameter() { } - @Test(expected=MethodNotAllowedException.class) + @Test public void shouldThrowExceptionWhenRequestANotKnownMethod() { + exception.expect(MethodNotAllowedException.class); + exception.expectMessage("Method COOK is not allowed for requested URI. Allowed Methods are [GET, POST]"); + when(request.getRequestURI()).thenReturn("/url"); when(request.getMethod()).thenReturn("COOK"); when(router.parse(anyString(), any(HttpMethod.class), any(MutableRequest.class))).thenReturn(method); + when(router.allowedMethodsFor("/url")).thenReturn(EnumSet.of(HttpMethod.GET, HttpMethod.POST)); translator.translate(webRequest); } diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/http/ParametersProviderTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/http/ParametersProviderTest.java index 2e821515a..5377b3166 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/http/ParametersProviderTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/http/ParametersProviderTest.java @@ -15,6 +15,7 @@ */ package br.com.caelum.vraptor.http; +import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.arrayContaining; import static org.hamcrest.Matchers.containsInAnyOrder; @@ -40,7 +41,9 @@ import javax.servlet.http.HttpServletRequest; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -60,6 +63,8 @@ public abstract class ParametersProviderTest { + @Rule + public ExpectedException exception = ExpectedException.none(); protected @Mock Converters converters; protected ParameterNameProvider nameProvider; @@ -264,8 +269,11 @@ public void addsValidationMessageWhenSetterFailsWithAValidationException() throw assertThat(errors.size(), is(greaterThan(0))); } - @Test(expected=InvalidParameterException.class) + @Test public void throwsExceptionWhenSetterFailsWithOtherException() throws Exception { + exception.expect(InvalidParameterException.class); + exception.expectMessage(containsString("Exception when trying to instantiate")); + requestParameterIs(error, "wrongCat.id", "guilherme"); getParameters(error); diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/http/route/PathAnnotationRoutesParserTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/http/route/PathAnnotationRoutesParserTest.java index 8f07e00bb..189a536c0 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/http/route/PathAnnotationRoutesParserTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/http/route/PathAnnotationRoutesParserTest.java @@ -17,6 +17,7 @@ package br.com.caelum.vraptor.http.route; +import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; @@ -33,7 +34,9 @@ import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.invocation.InvocationOnMock; @@ -58,6 +61,9 @@ public class PathAnnotationRoutesParserTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private Proxifier proxifier; private @Mock Converters converters; private NoTypeFinder typeFinder; @@ -159,8 +165,11 @@ public void noSlashPath() { } } - @Test(expected = IllegalArgumentException.class) + @Test public void addsAPrefixToMethodsWhenTheControllerHasMoreThanOneAnnotatedPath() throws Exception { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("You must specify exactly one path on @Path at class " + MoreThanOnePathAnnotatedController.class.getName()); + parser.rulesFor(new DefaultBeanClass(MoreThanOnePathAnnotatedController.class)); } @@ -345,9 +354,12 @@ public void toInherit() { } } - @Test(expected=IllegalArgumentException.class) + @Test public void shouldThrowExceptionIfPathAnnotationHasEmptyArray() throws Exception { + exception.expect(IllegalArgumentException.class); + exception.expectMessage(containsString("You must specify at least one path on @Path at")); + parser.rulesFor(new DefaultBeanClass(NoPath.class)); } @@ -539,8 +551,11 @@ public void addsAPrefixToMethodsWhenTheGetControllerIsAnnotatedWithPath() throws assertThat(route, canHandle(GetAnnotatedController.class, "withoutPath")); } - @Test(expected=IllegalArgumentException.class) + @Test public void throwsExceptionWhenTheGetControllerHasAmbiguousDeclaration() throws Exception { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("You should specify paths either in @Path(\"/path\") or @Get(\"/path\") (or @Post, @Put, @Delete), not both at"); + parser.rulesFor(new DefaultBeanClass(WrongGetAnnotatedController.class)); } diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/http/route/RulesTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/http/route/RulesTest.java index f5e0cdd91..809d069bd 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/http/route/RulesTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/http/route/RulesTest.java @@ -20,7 +20,9 @@ import static org.mockito.Mockito.when; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -29,6 +31,9 @@ public class RulesTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private @Mock Router router; private @Mock Proxifier proxifier; private DefaultRouteBuilder routeBuilder; @@ -42,8 +47,10 @@ public void setup() { when(router.builderFor("")).thenReturn(routeBuilder); } - @Test(expected=IllegalRouteException.class) + @Test public void allowsAdditionOfRouteBuildersByDefaultWithNoStrategy() { + exception.expect(IllegalRouteException.class); + new Rules(router) { @Override public void routes() { diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/AcceptsNeedReturnBooleanValidationRuleTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/AcceptsNeedReturnBooleanValidationRuleTest.java index 523729105..474dbeeeb 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/AcceptsNeedReturnBooleanValidationRuleTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/AcceptsNeedReturnBooleanValidationRuleTest.java @@ -21,7 +21,9 @@ import javax.enterprise.inject.Vetoed; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import br.com.caelum.vraptor.Accepts; import br.com.caelum.vraptor.InterceptionException; @@ -29,6 +31,9 @@ public class AcceptsNeedReturnBooleanValidationRuleTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private AcceptsNeedReturnBooleanValidationRule validationRule; private StepInvoker stepInvoker; @@ -38,15 +43,21 @@ public void setUp() { validationRule = new AcceptsNeedReturnBooleanValidationRule(stepInvoker); } - @Test(expected = InterceptionException.class) + @Test public void shouldVerifyIfAcceptsMethodReturnsVoid() { + exception.expect(InterceptionException.class); + exception.expectMessage("@Accepts method must return boolean"); + Class type = VoidAcceptsInterceptor.class; List allMethods = stepInvoker.findAllMethods(type); validationRule.validate(type, allMethods); } - @Test(expected = InterceptionException.class) + @Test public void shouldVerifyIfAcceptsMethodReturnsNonBooleanType() { + exception.expect(InterceptionException.class); + exception.expectMessage("@Accepts method must return boolean"); + Class type = NonBooleanAcceptsInterceptor.class; List allMethods = stepInvoker.findAllMethods(type); validationRule.validate(type, allMethods); diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/CustomAndInternalAcceptsValidationRuleTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/CustomAndInternalAcceptsValidationRuleTest.java index a5497c9c0..0da066983 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/CustomAndInternalAcceptsValidationRuleTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/CustomAndInternalAcceptsValidationRuleTest.java @@ -19,13 +19,18 @@ import java.util.List; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import br.com.caelum.vraptor.Accepts; import br.com.caelum.vraptor.interceptor.example.NotLogged; public class CustomAndInternalAcceptsValidationRuleTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private CustomAndInternalAcceptsValidationRule validationRule; private StepInvoker stepInvoker; @@ -35,8 +40,11 @@ public void setUp() { validationRule = new CustomAndInternalAcceptsValidationRule(stepInvoker); } - @Test(expected = IllegalStateException.class) + @Test public void mustNotUseInternalAcceptsAndCustomAccepts(){ + exception.expect(IllegalStateException.class); + exception.expectMessage("Interceptor class " + InternalAndCustomAcceptsInterceptor.class.getName() + " must declare internal accepts or custom, not both"); + Class type = InternalAndCustomAcceptsInterceptor.class; List methods = stepInvoker.findAllMethods(type); validationRule.validate(type, methods); diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/NoInterceptMethodsValidationRuleTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/NoInterceptMethodsValidationRuleTest.java index a82c72fe8..5ab441dd8 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/NoInterceptMethodsValidationRuleTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/NoInterceptMethodsValidationRuleTest.java @@ -19,7 +19,9 @@ import java.util.List; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import br.com.caelum.vraptor.AfterCall; import br.com.caelum.vraptor.InterceptionException; @@ -27,6 +29,9 @@ public class NoInterceptMethodsValidationRuleTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private StepInvoker stepInvoker; @Intercepts @@ -44,8 +49,11 @@ public void setUp() { this.stepInvoker = new StepInvoker(); } - @Test(expected=InterceptionException.class) + @Test public void shoulThrowExceptionIfInterceptorDontHaveAnyCallableMethod() { + exception.expect(InterceptionException.class); + exception.expectMessage("Interceptor " + SimpleInterceptor.class.getCanonicalName() +" must declare at least one method whith @AfterCall, @AroundCall or @BeforeCall annotation"); + Class type = SimpleInterceptor.class; List allMethods = stepInvoker.findAllMethods(type); new NoInterceptMethodsValidationRule(stepInvoker).validate(type, allMethods); diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/NoStackParamValidationRuleTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/NoStackParamValidationRuleTest.java index 2548922ae..d0280b061 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/NoStackParamValidationRuleTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/NoStackParamValidationRuleTest.java @@ -16,7 +16,9 @@ package br.com.caelum.vraptor.interceptor; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import br.com.caelum.vraptor.Accepts; import br.com.caelum.vraptor.AroundCall; @@ -27,6 +29,9 @@ public class NoStackParamValidationRuleTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private NoStackParamValidationRule validationRule; private StepInvoker invoker; @@ -36,21 +41,30 @@ public void setUp() { validationRule = new NoStackParamValidationRule(invoker); } - @Test(expected = IllegalStateException.class) + @Test public void mustReceiveStackAsParameterForAroundCall() { + exception.expect(IllegalStateException.class); + exception.expectMessage("@AroundCall method must receive br.com.caelum.vraptor.core.InterceptorStack or br.com.caelum.vraptor.interceptor.SimpleInterceptorStack"); + Class type = AroundInterceptorWithoutSimpleStackParameter.class; validationRule.validate(type, invoker.findAllMethods(type)); } - @Test(expected = IllegalStateException.class) + @Test public void mustNotReceiveStackAsParameterForAcceptsCall() { + exception.expect(IllegalStateException.class); + exception.expectMessage("Non @AroundCall method must not receive br.com.caelum.vraptor.core.InterceptorStack or br.com.caelum.vraptor.interceptor.SimpleInterceptorStack"); + Class type = AcceptsInterceptorWithStackAsParameter.class; validationRule.validate(type, invoker.findAllMethods(type)); } - @Test(expected = IllegalStateException.class) + @Test public void mustNotReceiveStackAsParameterForBeforeAfterCall() { + exception.expect(IllegalStateException.class); + exception.expectMessage("Non @AroundCall method must not receive br.com.caelum.vraptor.core.InterceptorStack or br.com.caelum.vraptor.interceptor.SimpleInterceptorStack"); + Class type = BeforeAfterInterceptorWithStackAsParameter.class; validationRule.validate(type, invoker.findAllMethods(type)); } diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/StepInvokerTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/StepInvokerTest.java index fe3cd6960..2a7f5c260 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/StepInvokerTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/StepInvokerTest.java @@ -23,7 +23,9 @@ import java.lang.reflect.Method; import java.util.List; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import br.com.caelum.vraptor.AroundCall; import br.com.caelum.vraptor.BeforeCall; @@ -33,6 +35,9 @@ public class StepInvokerTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private StepInvoker stepInvoker = new StepInvoker(); @Test @@ -42,8 +47,11 @@ public void shouldNotReadInheritedMethods() throws Exception { assertEquals(method, interceptorClass.getDeclaredMethod("begin")); } - @Test(expected=IllegalStateException.class) + @Test public void shouldThrowsExceptionWhenInterceptorHasMoreThanOneAnnotatedMethod() { + exception.expect(IllegalStateException.class); + exception.expectMessage(InterceptorWithMoreThanOneBeforeCallMethod.class.getName() + " - You should not have more than one @BeforeCall annotated method"); + Class interceptorClass = InterceptorWithMoreThanOneBeforeCallMethod.class; findMethod(interceptorClass, BeforeCall.class); } diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/TopologicalSortedInterceptorRegistryTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/TopologicalSortedInterceptorRegistryTest.java index d6ad99311..800b46b12 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/TopologicalSortedInterceptorRegistryTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/interceptor/TopologicalSortedInterceptorRegistryTest.java @@ -15,6 +15,7 @@ */ package br.com.caelum.vraptor.interceptor; +import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.Matchers.hasItems; import static org.junit.Assert.assertThat; @@ -23,12 +24,17 @@ import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import br.com.caelum.vraptor.Intercepts; public class TopologicalSortedInterceptorRegistryTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + @Intercepts static interface A {} @@ -86,8 +92,11 @@ public void respectsBeforeAndAfterAttribute() throws Exception { } - @Test(expected=IllegalStateException.class) + @Test public void failsOnCycles() throws Exception { + exception.expect(IllegalStateException.class); + exception.expectMessage(containsString("There is a cycle on the interceptor sequence")); + TopologicalSortedInterceptorRegistry set = new TopologicalSortedInterceptorRegistry(); set.register(A.class); set.register(C.class); diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/DeserializingObserverTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/DeserializingObserverTest.java index 550c14b5b..d9c9a1c1c 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/DeserializingObserverTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/DeserializingObserverTest.java @@ -21,8 +21,6 @@ import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.when; -import java.io.IOException; - import javax.servlet.http.HttpServletRequest; import org.junit.Before; @@ -43,7 +41,6 @@ public class DeserializingObserverTest { - private DeserializingObserver observer; private ControllerMethod consumeXml; private ControllerMethod doesntConsume; @@ -169,14 +166,4 @@ public void willSetOnlyNonNullParameters() throws Exception { assertEquals(methodInfo.getValuedParameters()[0].getValue(), "original1"); assertEquals(methodInfo.getValuedParameters()[1].getValue(), "deserialized"); } - - @Test(expected = IOException.class) - public void shouldThrowInterceptionExceptionIfAnIOExceptionOccurs() throws Exception { - when(request.getInputStream()).thenThrow(new IOException()); - when(request.getContentType()).thenReturn("application/xml"); - - final Deserializer deserializer = mock(Deserializer.class); - when(deserializers.deserializerFor("application/xml", container)).thenReturn(deserializer); - observer.deserializes(new InterceptorsReady(consumeXml), request, methodInfo, status); - } } diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/ExecuteMethodTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/ExecuteMethodTest.java index f08d35177..272fc5616 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/ExecuteMethodTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/ExecuteMethodTest.java @@ -50,6 +50,9 @@ public class ExecuteMethodTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + @Mock private MethodInfo methodInfo; @Mock private Messages messages; @Mock private Validator validator; @@ -121,13 +124,16 @@ public void shouldBeOkIfThereIsValidationErrorsAndYouSpecifiedWhereToGo() throws observer.execute(new InterceptorsExecuted(method, controller)); } - @Test(expected=InterceptionException.class) + @Test public void shouldThrowExceptionIfYouHaventSpecifiedWhereToGoOnValidationError() throws Exception { + exception.expect(InterceptionException.class); + Method didntSpecifyWhereToGo = AnyController.class.getMethod("didntSpecifyWhereToGo"); final ControllerMethod method = DefaultControllerMethod.instanceFor(AnyController.class, didntSpecifyWhereToGo); final AnyController controller = new AnyController(validator); doThrow(new IllegalStateException()).when(messages).assertAbsenceOfErrors(); when(methodInfo.getParametersValues()).thenReturn(new Object[0]); + observer.execute(new InterceptorsExecuted(method, controller)); } diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/ParametersInstantiatorTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/ParametersInstantiatorTest.java index 1e2725912..ca66bc4df 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/ParametersInstantiatorTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/ParametersInstantiatorTest.java @@ -34,7 +34,9 @@ import net.vidageek.mirror.dsl.Mirror; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.invocation.InvocationOnMock; @@ -55,6 +57,9 @@ public class ParametersInstantiatorTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private MethodInfo methodInfo = new MethodInfo(new ParanamerNameProvider()); private @Mock ParametersProvider parametersProvider; private @Mock Validator validator; @@ -132,8 +137,10 @@ public void shouldConvertArrayParametersToIndexParameters() throws Exception { /** * Bug related */ - @Test(expected=IllegalArgumentException.class) + @Test public void shouldThrowExceptionWhenThereIsAParameterContainingDotClass() throws Exception { + exception.expect(IllegalArgumentException.class); + methodInfo.setControllerMethod(otherMethod); when(request.getParameterNames()).thenReturn(enumeration(asList("someParam.class.id", "unrelatedParam"))); diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/upload/CommonsUploadMultipartObserverTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/upload/CommonsUploadMultipartObserverTest.java index 41559a7c3..fae897bff 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/upload/CommonsUploadMultipartObserverTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/observer/upload/CommonsUploadMultipartObserverTest.java @@ -39,7 +39,9 @@ import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -60,6 +62,9 @@ */ public class CommonsUploadMultipartObserverTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + @Mock private InterceptorStack stack; @Mock private ControllerFound event; @Mock private MutableRequest request; @@ -170,11 +175,14 @@ public void emptyFiles() throws Exception { observer.upload(event, request, config, validator); } - @Test(expected = InvalidParameterException.class) + @Test public void throwsInvalidParameterExceptionIfIOExceptionOccurs() throws Exception { + exception.expect(InvalidParameterException.class); + exception.expectMessage("Can't parse uploaded file myfile.txt"); + when(event.getMethod()).thenReturn(uploadMethodController); - FileItem item = new MockFileItem("thefile0", "file.txt", new byte[0]); + FileItem item = new MockFileItem("thefile0", "myfile.txt", new byte[0]); item = spy(item); doThrow(new IOException()).when(item).getInputStream(); diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/DefaultDeserializersTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/DefaultDeserializersTest.java index 26f7ea9d2..67d914872 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/DefaultDeserializersTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/DefaultDeserializersTest.java @@ -20,18 +20,18 @@ import static org.mockito.Mockito.verify; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import br.com.caelum.vraptor.ioc.Container; -import br.com.caelum.vraptor.serialization.DefaultDeserializers; -import br.com.caelum.vraptor.serialization.Deserializer; -import br.com.caelum.vraptor.serialization.Deserializers; -import br.com.caelum.vraptor.serialization.Deserializes; public class DefaultDeserializersTest { + @Rule + public ExpectedException exception = ExpectedException.none(); private Deserializers deserializers; @Mock private Container container; @@ -49,8 +49,11 @@ public void shouldThrowExceptionWhenThereIsNoDeserializerRegisteredForGivenConte static interface NotAnnotatedDeserializer extends Deserializer {} - @Test(expected=IllegalArgumentException.class) + @Test public void allDeserializersMustBeAnnotatedWithDeserializes() throws Exception { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("You must annotate your deserializers with @Deserializes"); + deserializers.register(NotAnnotatedDeserializer.class); } diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/DeserializesHandlerTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/DeserializesHandlerTest.java index c269fef00..ace926c59 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/DeserializesHandlerTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/DeserializesHandlerTest.java @@ -15,19 +15,22 @@ */ package br.com.caelum.vraptor.serialization; +import static org.hamcrest.CoreMatchers.containsString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import br.com.caelum.vraptor.controller.DefaultBeanClass; -import br.com.caelum.vraptor.serialization.Deserializer; -import br.com.caelum.vraptor.serialization.Deserializers; -import br.com.caelum.vraptor.serialization.DeserializesHandler; public class DeserializesHandlerTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private DeserializesHandler handler; private Deserializers deserializers; @@ -40,8 +43,10 @@ public void setUp() throws Exception { static interface MyDeserializer extends Deserializer{} static interface NotADeserializer{} - @Test(expected=IllegalArgumentException.class) + @Test public void shouldThrowExceptionWhenTypeIsNotADeserializer() throws Exception { + exception.expect(IllegalArgumentException.class); + exception.expectMessage(containsString("must implement Deserializer")); handler.handle(new DefaultBeanClass(NotADeserializer.class)); } diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonDeserializerTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonDeserializerTest.java index c3e793043..fa8052060 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonDeserializerTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonDeserializerTest.java @@ -43,7 +43,9 @@ import net.vidageek.mirror.dsl.Mirror; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -68,6 +70,9 @@ public class GsonDeserializerTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private GsonDeserializerBuilder builder; private GsonDeserialization deserializer; private ParameterNameProvider provider; @@ -203,8 +208,11 @@ public void shouldDeserializerParseArraysWithRoot(){ assertThat(dog.age, is(1)); } - @Test(expected = IllegalArgumentException.class) + @Test public void shouldNotAcceptMethodsWithoutArguments() throws Exception { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("Methods that consumes representations must receive just one argument"); + deserializer.deserialize(emptyStream(), noParameter); } diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/xstream/XStreamXMLSerializationTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/xstream/XStreamXMLSerializationTest.java index 2c6f8d09a..677f59523 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/xstream/XStreamXMLSerializationTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/xstream/XStreamXMLSerializationTest.java @@ -41,7 +41,9 @@ import javax.servlet.http.HttpServletResponse; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import br.com.caelum.vraptor.environment.Environment; @@ -50,6 +52,9 @@ public class XStreamXMLSerializationTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + protected XStreamXMLSerialization serialization; protected ByteArrayOutputStream stream; protected Environment environment; @@ -307,22 +312,31 @@ public void shouldExcludeFieldsFromACollection() { assertThat(result(), not(containsString(""))); } - @Test(expected=IllegalArgumentException.class) + @Test public void shouldThrowAnExceptionWhenYouIncludeANonExistantField() { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("Field path 'wrongFieldName' doesn't exists in class"); + Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item("name", 12.99)); serialization.from(order).include("wrongFieldName").serialize(); } - @Test(expected=IllegalArgumentException.class) + @Test public void shouldThrowAnExceptionWhenYouIncludeANonExistantFieldInsideOther() { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("Field path 'wrongFieldName.client' doesn't exists in class"); + Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item("name", 12.99)); serialization.from(order).include("wrongFieldName.client").serialize(); } - @Test(expected=IllegalArgumentException.class) + @Test public void shouldThrowAnExceptionWhenYouIncludeANonExistantFieldInsideOtherNonExistantField() { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("Field path 'wrongFieldName.another' doesn't exists in class"); + Order order = new Order(new Client("guilherme silveira"), 15.0, "pack it nicely, please", new Item("name", 12.99)); serialization.from(order).include("wrongFieldName.another").serialize(); diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/xstream/XStreamXmlDeserializerTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/xstream/XStreamXmlDeserializerTest.java index 74c2e9cf8..4ac79b5ff 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/xstream/XStreamXmlDeserializerTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/xstream/XStreamXmlDeserializerTest.java @@ -27,7 +27,9 @@ import java.util.TimeZone; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import br.com.caelum.vraptor.controller.BeanClass; import br.com.caelum.vraptor.controller.ControllerMethod; @@ -40,6 +42,9 @@ public class XStreamXmlDeserializerTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private XStreamXMLDeserializer deserializer; private ControllerMethod bark; private ParameterNameProvider provider; @@ -107,10 +112,14 @@ public void annotated(DogWithAnnotations dog){ } - @Test(expected=IllegalArgumentException.class) + @Test public void shouldNotAcceptMethodsWithoutArguments() throws Exception { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("Methods that consumes xml must receive just one argument"); + deserializer.deserialize(new ByteArrayInputStream(new byte[0]), woof); } + @Test public void shouldBeAbleToDeserializeADog() throws Exception { InputStream stream = new ByteArrayInputStream("Brutus7".getBytes()); diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/validator/MessagesTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/validator/MessagesTest.java index a7ee7aced..c0d47014a 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/validator/MessagesTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/validator/MessagesTest.java @@ -1,5 +1,6 @@ package br.com.caelum.vraptor.validator; +import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.equalTo; @@ -15,10 +16,15 @@ import javax.el.ELProcessor; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; public class MessagesTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private Messages messages; @Before @@ -26,8 +32,11 @@ public void setUp() { messages = new Messages(); } - @Test(expected = IllegalStateException.class) + @Test public void shouldThrowExceptionIfMessagesHasUnhandledErrors() { + exception.expect(IllegalStateException.class); + exception.expectMessage(containsString("There are validation errors and you forgot to specify where to go.")); + messages.add(new SimpleMessage("Test", "Test message")); messages.assertAbsenceOfErrors(); } diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultLogicResultTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultLogicResultTest.java index 231703d40..beda1272f 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultLogicResultTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultLogicResultTest.java @@ -39,7 +39,9 @@ import org.hamcrest.Description; import org.hamcrest.TypeSafeMatcher; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -62,6 +64,9 @@ public class DefaultLogicResultTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private LogicResult logicResult; private @Mock Router router; @@ -220,8 +225,10 @@ public void cannotRedirectWhenLogicMethodIsAnnotatedWithAnyHttpMethodButGet() th } } - @Test(expected=ValidationException.class) + @Test public void shouldNotWrapValidationExceptionWhenForwarding() throws Exception { + exception.expect(ValidationException.class); + givenDispatcherWillBeReturnedWhenRequested(); when(response.isCommitted()).thenReturn(true); diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultPageResultTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultPageResultTest.java index 3ce3dc754..009085d19 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultPageResultTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultPageResultTest.java @@ -31,7 +31,9 @@ import javax.servlet.ServletException; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -48,6 +50,9 @@ public class DefaultPageResultTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private @Mock MutableRequest request; private @Mock MutableResponse response; private @Mock RequestDispatcher dispatcher; @@ -95,10 +100,11 @@ public void shouldNotIncludeContextPathIfURIIsAbsolute() throws Exception { verify(response, only()).sendRedirect("http://vraptor.caelum.com.br"); } - @Test(expected=ResultException.class) + @Test public void shouldThrowResultExceptionIfIOExceptionOccursWhileRedirect() throws Exception { - doThrow(new IOException()).when(response).sendRedirect(anyString()); + exception.expect(ResultException.class); + doThrow(new IOException()).when(response).sendRedirect(anyString()); view.redirectTo("/any/url"); } @@ -111,16 +117,20 @@ public void shouldForwardToGivenURI() throws Exception { } - @Test(expected=ResultException.class) + @Test public void shouldThrowResultExceptionIfServletExceptionOccursWhileForwarding() throws Exception { + exception.expect(ResultException.class); + when(request.getRequestDispatcher(anyString())).thenReturn(dispatcher); doThrow(new ServletException()).when(dispatcher).forward(request, response); view.forwardTo("/any/url"); } - @Test(expected=ResultException.class) + @Test public void shouldThrowResultExceptionIfIOExceptionOccursWhileForwarding() throws Exception { + exception.expect(ResultException.class); + when(request.getRequestDispatcher(anyString())).thenReturn(dispatcher); doThrow(new IOException()).when(dispatcher).forward(request, response); @@ -136,16 +146,20 @@ public void shouldAllowCustomPathResolverWhileForwardingView() throws ServletExc verify(dispatcher, only()).forward(request, response); } - @Test(expected=ApplicationLogicException.class) - public void shouldThrowResultExceptionIfServletExceptionOccursWhileForwardingView() throws Exception { + @Test + public void shouldThrowApplicationLogicExceptionIfServletExceptionOccursWhileForwardingView() throws Exception { + exception.expect(ApplicationLogicException.class); + when(request.getRequestDispatcher(anyString())).thenReturn(dispatcher); doThrow(new ServletException()).when(dispatcher).forward(request, response); view.defaultView(); } - @Test(expected=ResultException.class) + @Test public void shouldThrowResultExceptionIfIOExceptionOccursWhileForwardingView() throws Exception { + exception.expect(ResultException.class); + when(request.getRequestDispatcher(anyString())).thenReturn(dispatcher); doThrow(new IOException()).when(dispatcher).forward(request, response); @@ -161,16 +175,20 @@ public void shouldAllowCustomPathResolverWhileIncluding() throws ServletExceptio verify(dispatcher, only()).include(request, response); } - @Test(expected=ResultException.class) + @Test public void shouldThrowResultExceptionIfServletExceptionOccursWhileIncluding() throws Exception { + exception.expect(ResultException.class); + when(request.getRequestDispatcher(anyString())).thenReturn(dispatcher); doThrow(new ServletException()).when(dispatcher).include(request, response); view.include(); } - @Test(expected=ResultException.class) + @Test public void shouldThrowResultExceptionIfIOExceptionOccursWhileIncluding() throws Exception { + exception.expect(ResultException.class); + when(request.getRequestDispatcher(anyString())).thenReturn(dispatcher); doThrow(new IOException()).when(dispatcher).include(request, response); @@ -187,8 +205,10 @@ public void shoudNotExecuteLogicWhenUsingResultOf() { } } - @Test(expected = ProxyInvocationException.class) + @Test public void shoudThrowProxyInvocationExceptionIfAndExceptionOccursWhenUsingResultOf() { + exception.expect(ProxyInvocationException.class); + doThrow(new NullPointerException()).when(request).getRequestDispatcher(anyString()); view.of(SimpleController.class).notAllowedMethod(); } diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultValidationViewsFactoryTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultValidationViewsFactoryTest.java index 2629c3f9b..62d8509f6 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultValidationViewsFactoryTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/view/DefaultValidationViewsFactoryTest.java @@ -26,7 +26,9 @@ import org.junit.Assert; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -42,6 +44,8 @@ public class DefaultValidationViewsFactoryTest { + @Rule + public ExpectedException exception = ExpectedException.none(); private Result result; private Proxifier proxifier; @@ -65,13 +69,15 @@ public void random() { } } - @Test(expected=ValidationException.class) + + @Test public void shouldUseValidationVersionOfLogicResult() throws Exception { + exception.expect(ValidationException.class); when(result.use(LogicResult.class)).thenReturn(new MockedLogic()); - factory.instanceFor(LogicResult.class, errors).forwardTo(RandomComponent.class).random(); } + @Test public void shouldThrowExceptionOnlyAtTheEndOfValidationCall() throws Exception { @@ -83,16 +89,20 @@ public void shouldThrowExceptionOnlyAtTheEndOfValidationCall() throws Exception factory.instanceFor(PageResult.class, errors); factory.instanceFor(PageResult.class, errors).of(RandomComponent.class); } - @Test(expected=ValidationException.class) + + @Test public void shouldUseValidationVersionOfPageResult() throws Exception { - when(result.use(PageResult.class)).thenReturn(new MockedPage()); + exception.expect(ValidationException.class); + when(result.use(PageResult.class)).thenReturn(new MockedPage()); factory.instanceFor(PageResult.class, errors).forwardTo("any uri"); } - @Test(expected=ValidationException.class) + + @Test public void shouldUseValidationVersionOfEmptyResult() throws Exception { - when(result.use(EmptyResult.class)).thenReturn(new EmptyResult()); + exception.expect(ValidationException.class); + when(result.use(EmptyResult.class)).thenReturn(new EmptyResult()); factory.instanceFor(EmptyResult.class, errors); } @@ -109,30 +119,36 @@ public void onHttpResultShouldNotThrowExceptionsOnHeaders() throws Exception { factory.instanceFor(HttpResult.class, errors).addIntHeader("jkl", 456); } - @Test(expected=ValidationException.class) + @Test public void onHttpResultShouldThrowExceptionsOnSendError() throws Exception { - HttpResult httpResult = mock(HttpResult.class); + exception.expect(ValidationException.class); + HttpResult httpResult = mock(HttpResult.class); when(result.use(HttpResult.class)).thenReturn(httpResult); factory.instanceFor(HttpResult.class, errors).sendError(404); } - @Test(expected=ValidationException.class) + + @Test public void onHttpResultShouldThrowExceptionsOnSendErrorWithMessage() throws Exception { - HttpResult httpResult = mock(HttpResult.class); + exception.expect(ValidationException.class); + HttpResult httpResult = mock(HttpResult.class); when(result.use(HttpResult.class)).thenReturn(httpResult); factory.instanceFor(HttpResult.class, errors).sendError(404, "Not Found"); } - @Test(expected=ValidationException.class) + + @Test public void onHttpResultShouldThrowExceptionsOnSetStatus() throws Exception { - HttpResult httpResult = mock(HttpResult.class); + exception.expect(ValidationException.class); + HttpResult httpResult = mock(HttpResult.class); when(result.use(HttpResult.class)).thenReturn(httpResult); factory.instanceFor(HttpResult.class, errors).setStatusCode(200); } + @Test public void shouldBeAbleToChainMethodsOnHttpResult() throws Exception { HttpResult httpResult = mock(HttpResult.class); @@ -142,10 +158,11 @@ public void shouldBeAbleToChainMethodsOnHttpResult() throws Exception { factory.instanceFor(HttpResult.class, errors).addDateHeader("abc", 123l).addHeader("def", "ghi").addIntHeader("jkl", 234); } - @Test(expected=ValidationException.class) + @Test public void onStatusResultShouldThrowExceptionsOnMoved() throws Exception { - Status status = mock(Status.class); + exception.expect(ValidationException.class); + Status status = mock(Status.class); when(result.use(Status.class)).thenReturn(status); when(status.movedPermanentlyTo(RandomComponent.class)).thenReturn(new RandomComponent()); @@ -157,103 +174,121 @@ public void onStatusResultShouldThrowExceptionsOnMoved() throws Exception { factory.instanceFor(Status.class, errors).movedPermanentlyTo(RandomComponent.class).random(); } - @Test(expected=ValidationException.class) + @Test public void onStatusResultShouldThrowExceptionsOnMovedToLogic() throws Exception { - Status status = mock(Status.class); + exception.expect(ValidationException.class); + Status status = mock(Status.class); when(result.use(Status.class)).thenReturn(status); factory.instanceFor(Status.class, errors).movedPermanentlyTo("anywhere"); } - @Test(expected=ValidationException.class) + @Test public void onRefererResultShouldThrowExceptionsOnForward() throws Exception { - RefererResult referer = mock(RefererResult.class); + exception.expect(ValidationException.class); + RefererResult referer = mock(RefererResult.class); when(result.use(RefererResult.class)).thenReturn(referer); factory.instanceFor(RefererResult.class, errors).forward(); } - @Test(expected=ValidationException.class) + @Test public void onRefererResultShouldThrowExceptionsOnRedirect() throws Exception { - RefererResult referer = mock(RefererResult.class); + exception.expect(ValidationException.class); + RefererResult referer = mock(RefererResult.class); when(result.use(RefererResult.class)).thenReturn(referer); factory.instanceFor(RefererResult.class, errors).redirect(); } - @Test(expected=ValidationException.class) + @Test public void onStatusResultShouldThrowExceptionsOnNotFound() throws Exception { - Status status = mock(Status.class); + exception.expect(ValidationException.class); + Status status = mock(Status.class); when(result.use(Status.class)).thenReturn(status); factory.instanceFor(Status.class, errors).notFound(); } - @Test(expected=ValidationException.class) + + @Test public void onStatusResultShouldThrowExceptionsOnHeader() throws Exception { - Status status = mock(Status.class); + exception.expect(ValidationException.class); + Status status = mock(Status.class); when(result.use(Status.class)).thenReturn(status); factory.instanceFor(Status.class, errors).header("abc", "def"); } - @Test(expected=ValidationException.class) + @Test public void onStatusResultShouldThrowExceptionsOnCreated() throws Exception { - Status status = mock(Status.class); + exception.expect(ValidationException.class); + Status status = mock(Status.class); when(result.use(Status.class)).thenReturn(status); factory.instanceFor(Status.class, errors).created(); } - @Test(expected=ValidationException.class) + + @Test public void onStatusResultShouldThrowExceptionsOnCreatedWithLocation() throws Exception { - Status status = mock(Status.class); + exception.expect(ValidationException.class); + Status status = mock(Status.class); when(result.use(Status.class)).thenReturn(status); factory.instanceFor(Status.class, errors).created("/newLocation"); } - @Test(expected=ValidationException.class) + @Test public void onStatusResultShouldThrowExceptionsOnOk() throws Exception { - Status status = mock(Status.class); + exception.expect(ValidationException.class); + Status status = mock(Status.class); when(result.use(Status.class)).thenReturn(status); factory.instanceFor(Status.class, errors).ok(); } - @Test(expected=ValidationException.class) + @Test public void onStatusResultShouldThrowExceptionsOnConflict() throws Exception { - Status status = mock(Status.class); + exception.expect(ValidationException.class); + Status status = mock(Status.class); when(result.use(Status.class)).thenReturn(status); factory.instanceFor(Status.class, errors).conflict(); } - @Test(expected=ValidationException.class) + + @Test public void onStatusResultShouldThrowExceptionsOnMethodNotAllowed() throws Exception { - Status status = mock(Status.class); + exception.expect(ValidationException.class); + Status status = mock(Status.class); when(result.use(Status.class)).thenReturn(status); factory.instanceFor(Status.class, errors).methodNotAllowed(EnumSet.allOf(HttpMethod.class)); } - @Test(expected=ValidationException.class) + + @Test public void onStatusResultShouldThrowExceptionsOnMovedPermanentlyTo() throws Exception { - Status status = mock(Status.class); + exception.expect(ValidationException.class); + Status status = mock(Status.class); when(result.use(Status.class)).thenReturn(status); factory.instanceFor(Status.class, errors).movedPermanentlyTo("/newUri"); } - @Test(expected=ValidationException.class) + + @Test public void onStatusResultShouldThrowExceptionsOnMovedPermanentlyToLogic() throws Exception { + exception.expect(ValidationException.class); + Status status = mock(Status.class); when(result.use(Status.class)).thenReturn(status); @@ -266,8 +301,11 @@ public void onStatusResultShouldThrowExceptionsOnMovedPermanentlyToLogic() throw } factory.instanceFor(Status.class, errors).movedPermanentlyTo(RandomComponent.class).random(); } - @Test(expected=ValidationException.class) + + @Test public void onXMLSerializationResultShouldThrowExceptionOnlyOnSerializeMethod() throws Exception { + exception.expect(ValidationException.class); + JSONSerialization serialization = mock(JSONSerialization.class); serializerBuilder = mock(SerializerBuilder.class, new Answer() { @@ -325,11 +363,12 @@ public RandomSerializer recursive() { @Override public void serialize() { } - } - @Test(expected=ValidationException.class) + @Test public void onSerializerResultsShouldBeAbleToCreateValidationInstancesEvenIfChildClassesUsesCovariantType() throws Exception { + exception.expect(ValidationException.class); + JSONSerialization serialization = mock(JSONSerialization.class); serializerBuilder = new RandomSerializer();