-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1110 from HubSpot/fix-npe-type-map
More gracefully handle null keys/values in map resolver
- Loading branch information
Showing
2 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/test/java/com/hubspot/jinjava/el/TypeConvertingMapELResolverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.hubspot.jinjava.el; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class TypeConvertingMapELResolverTest { | ||
private TypeConvertingMapELResolver typeConvertingMapELResolver; | ||
|
||
@Before | ||
public void setup() { | ||
typeConvertingMapELResolver = new TypeConvertingMapELResolver(false); | ||
} | ||
|
||
@Test | ||
public void itResolvesProperties() { | ||
Map<String, String> values = ImmutableMap.of("1", "value1", "2", "value2"); | ||
assertThat(typeConvertingMapELResolver.getValue(new JinjavaELContext(), values, "2")) | ||
.isEqualTo("value2"); | ||
} | ||
|
||
@Test | ||
public void itConvertsPropertyClassWhenResolvingProperty() { | ||
Map<String, String> values = ImmutableMap.of("1", "value1", "2", "value2"); | ||
assertThat(typeConvertingMapELResolver.getValue(new JinjavaELContext(), values, 1)) | ||
.isEqualTo("value1"); | ||
} | ||
|
||
@Test | ||
public void itHandlesNullKeyValuesWhenResolvingProperty() { | ||
Map<String, String> values = new HashMap<>(); | ||
values.put(null, "nullValue"); | ||
values.put("1", "value1"); | ||
values.put("2", "value2"); | ||
assertThat(typeConvertingMapELResolver.getValue(new JinjavaELContext(), values, 1)) | ||
.isEqualTo("value1"); | ||
} | ||
|
||
@Test | ||
public void itHandlesMapWithOnlyNullKey() { | ||
Map<String, String> values = new HashMap<>(); | ||
values.put(null, "nullValue"); | ||
assertThat(typeConvertingMapELResolver.getValue(new JinjavaELContext(), values, 1)) | ||
.isEqualTo(null); | ||
} | ||
|
||
@Test | ||
public void itResolvesNullPropertyValue() { | ||
Map<String, String> values = new HashMap<>(); | ||
values.put(null, "nullValue"); | ||
values.put("1", "value1"); | ||
values.put("2", "value2"); | ||
assertThat(typeConvertingMapELResolver.getValue(new JinjavaELContext(), values, null)) | ||
.isEqualTo("nullValue"); | ||
} | ||
} |