#132 - Added guard to Resource to shield it from being used with Collection content.

Fixed ticket links in Jackson2HalIntegrationTests.
This commit is contained in:
Oliver Gierke
2013-12-29 16:01:43 +01:00
parent 88da0672ab
commit 3b41fc9149
3 changed files with 13 additions and 1 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.hateoas;
import java.util.Arrays;
import java.util.Collection;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlRootElement;
@@ -58,6 +59,7 @@ public class Resource<T> extends ResourceSupport {
public Resource(T content, Iterable<Link> links) {
Assert.notNull(content, "Content must not be null!");
Assert.isTrue(!(content instanceof Collection), "Content must not be a collection! Use Resources instead!");
this.content = content;
this.add(links);
}

View File

@@ -18,6 +18,8 @@ package org.springframework.hateoas;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.util.Collections;
import org.junit.Test;
/**
@@ -64,4 +66,9 @@ public class ResourceUnitTest {
assertThat(left, is(not(right)));
assertThat(right, is(not(left)));
}
@Test(expected = IllegalArgumentException.class)
public void rejectsCollectionContent() {
new Resource<Object>(Collections.emptyList());
}
}

View File

@@ -274,7 +274,7 @@ public class Jackson2HalIntegrationTest extends AbstractJackson2MarshallingInteg
}
/**
* @see #126
* @see #125
*/
@Test
public void rendersCuriesCorrectly() throws Exception {
@@ -285,6 +285,9 @@ public class Jackson2HalIntegrationTest extends AbstractJackson2MarshallingInteg
assertThat(getCuriedObjectMapper().writeValueAsString(resources), is(CURIED_DOCUMENT));
}
/**
* @see #125
*/
@Test
public void doesNotRenderCuriesIfNoLinkIsPresent() throws Exception {