#342 - Polishing.
Extracted exception message into a constant. Added author where necessary and extended copyright headers. Fixed import order. Moved unit test into ControllerLinkBuilderUnitTest. Tweaked it to use JUnit rule. Original pull request: #344.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,10 +17,11 @@ package org.springframework.hateoas.mvc;
|
||||
|
||||
import static org.springframework.util.StringUtils.*;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URI;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.core.AnnotationMappingDiscoverer;
|
||||
import org.springframework.hateoas.core.DummyInvocationUtils;
|
||||
@@ -41,9 +42,11 @@ import org.springframework.web.util.UriTemplate;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Kamill Sokol
|
||||
* @author Greg Turnquist
|
||||
*/
|
||||
public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuilder> {
|
||||
|
||||
private static final String REQUEST_ATTRIBUTES_MISSING = "Could not find current request via RequestContextHolder. Is this being called from a Spring MVC handler?";
|
||||
private static final MappingDiscoverer DISCOVERER = new AnnotationMappingDiscoverer(RequestMapping.class);
|
||||
private static final ControllerLinkBuilderFactory FACTORY = new ControllerLinkBuilderFactory();
|
||||
|
||||
@@ -236,7 +239,7 @@ public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuil
|
||||
private static HttpServletRequest getCurrentRequest() {
|
||||
|
||||
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
|
||||
Assert.state(requestAttributes != null, "Could not find current request via RequestContextHolder. Is this being called from a Spring MVC handler?");
|
||||
Assert.state(requestAttributes != null, REQUEST_ATTRIBUTES_MISSING);
|
||||
Assert.isInstanceOf(ServletRequestAttributes.class, requestAttributes);
|
||||
HttpServletRequest servletRequest = ((ServletRequestAttributes) requestAttributes).getRequest();
|
||||
Assert.state(servletRequest != null, "Could not find current HttpServletRequest");
|
||||
|
||||
@@ -15,8 +15,8 @@
|
||||
*/
|
||||
package org.springframework.hateoas.mvc;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
@@ -24,7 +24,9 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.hateoas.Identifiable;
|
||||
import org.springframework.hateoas.Link;
|
||||
@@ -35,6 +37,7 @@ import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.util.UriComponents;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
@@ -45,9 +48,12 @@ import org.springframework.web.util.UriComponentsBuilder;
|
||||
* @author Dietrich Schulten
|
||||
* @author Kamill Sokol
|
||||
* @author Oemer Yildiz
|
||||
* @author Greg Turnquist
|
||||
*/
|
||||
public class ControllerLinkBuilderUnitTest extends TestUtils {
|
||||
|
||||
public @Rule ExpectedException exception = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void createsLinkToControllerRoot() {
|
||||
|
||||
@@ -435,6 +441,22 @@ public class ControllerLinkBuilderUnitTest extends TestUtils {
|
||||
assertThat(link.getHref(), endsWith("/bar"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #342
|
||||
*/
|
||||
@Test
|
||||
public void mentionsRequiredUsageWithinWebRequestInException() {
|
||||
|
||||
exception.expect(IllegalStateException.class);
|
||||
exception.expectMessage("request");
|
||||
exception.expectMessage("Spring MVC");
|
||||
|
||||
RequestContextHolder.setRequestAttributes(null);
|
||||
|
||||
linkTo(methodOn(ControllerLinkBuilderUnitTest.PersonsAddressesController.class, 15).getAddressesForCountry("DE"))
|
||||
.withSelfRel();
|
||||
}
|
||||
|
||||
private static UriComponents toComponents(Link link) {
|
||||
return UriComponentsBuilder.fromUriString(link.getHref()).build();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user