Make MapBasedRow immutable (#7130)

* Make MapBasedRow immutable

* add null check
This commit is contained in:
Jihoon Son 2019-02-28 16:07:14 -08:00 committed by GitHub
parent a0afd7931d
commit 9a62157a06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -21,10 +21,12 @@ package org.apache.druid.data.input;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
import org.apache.druid.guice.annotations.PublicApi;
import org.apache.druid.java.util.common.DateTimes;
import org.joda.time.DateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@ -43,7 +45,7 @@ public class MapBasedRow implements Row
)
{
this.timestamp = timestamp;
this.event = event;
this.event = Collections.unmodifiableMap(Preconditions.checkNotNull(event, "event"));
}
public MapBasedRow(

View File

@ -22,10 +22,18 @@ package org.apache.druid.data.input;
import com.google.common.collect.ImmutableMap;
import org.apache.druid.java.util.common.DateTimes;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.HashMap;
import java.util.Map;
public class MapBasedRowTest
{
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void testGetLongMetricFromString()
{
@ -50,4 +58,15 @@ public class MapBasedRowTest
Assert.assertEquals(-9223372036854775807L, row.getMetric("k5"));
Assert.assertEquals(9223372036854775802L, row.getMetric("k6"));
}
@Test
public void testImmutability()
{
final Map<String, Object> event = new HashMap<>();
event.put("k0", 1);
event.put("k1", 2);
final MapBasedRow row = new MapBasedRow(DateTimes.nowUtc(), event);
expectedException.expect(UnsupportedOperationException.class);
row.getEvent().put("k2", 3);
}
}