Skip to content

Commit

Permalink
Update #2158 test to verify it is fixed (via #3143)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 25, 2021
1 parent cc1a04b commit 8d91226
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.fasterxml.jackson.databind.deser.jdk;

import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
Expand All @@ -10,6 +12,7 @@
import com.fasterxml.jackson.core.type.TypeReference;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;

import org.junit.Assert;

Expand Down Expand Up @@ -64,11 +67,45 @@ public static TestEnum2725 getByIntegerId(final Integer id) {
public static TestEnum2725 getByStringId(final String id) {
return Integer.parseInt(id) == FOO.i ? FOO : null;
}
}
}

// for [databind#2158]
private static final class DummyDto2158 {
@JsonValue
private final String value;

private DummyDto2158(String value) {
this.value = value;
}

@JsonCreator
static DummyDto2158 fromValue(String value) {
if (value.isEmpty()) {
throw new IllegalArgumentException("Value must be nonempty");
}

return new DummyDto2158(value.toLowerCase(Locale.ROOT));
}

@Override
public boolean equals(Object o) {
return o instanceof DummyDto2158 && ((DummyDto2158) o).value.equals(value);
}

@Override
public int hashCode() { return Objects.hash(value); }

@Override
public String toString() { return String.format("DummyDto{value=%s}", value); }
}

private static final TypeReference<Map<DummyDto2158, Integer>> MAP_TYPE_2158 =
new TypeReference<Map<DummyDto2158, Integer>>() {};

/*
/**********************************************************
/**********************************************************************
/* Test methods, wrapper keys
/**********************************************************
/**********************************************************************
*/

final private ObjectMapper MAPPER = objectMapper();
Expand Down Expand Up @@ -135,9 +172,9 @@ public void testDoubleMapKeyDeserialization() throws Exception
}

/*
/**********************************************************
/**********************************************************************
/* Test methods, other
/**********************************************************
/**********************************************************************
*/

public void testDeserializeKeyViaFactory() throws Exception
Expand Down Expand Up @@ -178,4 +215,22 @@ public void testEnumWithCreatorMapKeyDeserialization() throws Exception
assertNotNull(output);
assertEquals(1, output.size());
}

// [databind#2158]
public void testDeserializeInvalidKey() throws Exception
{
try {
MAPPER.readValue("{ \"\": 0 }", MAP_TYPE_2158);
fail("Should no pass");
} catch (InvalidFormatException e) {
verifyException(e, "Value must be nonempty");
}
}

// [databind#2158]
public void testNormalizeKey() throws Exception
{
assertEquals(Collections.singletonMap(DummyDto2158.fromValue("foo"), 0),
MAPPER.readValue("{ \"FOO\": 0 }", MAP_TYPE_2158));
}
}

This file was deleted.

0 comments on commit 8d91226

Please sign in to comment.