#81, #83 - Fixed potential NullPointerExceptions in HalEmbeddedBuilder.

This commit is contained in:
Dietrich Schulten
2013-06-12 08:33:14 +02:00
committed by Oliver Gierke
parent 23e78a7418
commit 693faae0fe
2 changed files with 25 additions and 1 deletions

View File

@@ -22,6 +22,7 @@ import java.util.List;
import java.util.Map;
import org.springframework.hateoas.RelProvider;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.core.ObjectUtils;
/**
@@ -29,6 +30,7 @@ import org.springframework.hateoas.core.ObjectUtils;
* single resource relation to the collection one, once more than one object of the same type is added.
*
* @author Oliver Gierke
* @author Dietrich Schulten
*/
class HalEmbeddedBuilder {
@@ -47,13 +49,19 @@ class HalEmbeddedBuilder {
}
/**
* Adds the given value to the embeddeds.
* Adds the given value to the embeddeds. Will skip doing so if the value is {@literal null} or the content of a
* {@link Resource} is {@literal null}.
*
* @param value
*/
public void add(Object value) {
Class<?> type = ObjectUtils.getResourceType(value);
if (type == null) {
return;
}
String singleRel = getDefaultedRelFor(type, false);
List<Object> currentValue = embeddeds.get(singleRel);

View File

@@ -24,13 +24,16 @@ import java.util.Map;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.hateoas.RelProvider;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.core.EvoInflectorRelProvider;
/**
* Unit tests for {@link HalEmbeddedBuilder}.
*
* @author Oliver Gierke
* @author Dietrich Schulten
*/
public class HalEmbeddedBuilderUnitTests {
@@ -60,6 +63,19 @@ public class HalEmbeddedBuilderUnitTests {
assertThat(map.get("long"), Matchers.<List<Object>> allOf(hasSize(1), hasItem(1L)));
}
/**
* @see #81, #83
*/
@Test
public void addsNoEmbeddedsForResourceWithoutContent() {
Resource<?> resource = BeanUtils.instantiateClass(Resource.class);
HalEmbeddedBuilder halEmbeddedBuilder = new HalEmbeddedBuilder(provider);
halEmbeddedBuilder.add(resource);
assertThat(halEmbeddedBuilder.asMap().isEmpty(), is(true));
}
private Map<String, List<Object>> setUpBuilder(Object... values) {
HalEmbeddedBuilder builder = new HalEmbeddedBuilder(provider);