Skip to content

Commit a52f7d0

Browse files
scordiojoel-costigliola
authored andcommitted
Try map input as last attempt in PropertyOrFieldSupport (#1763)
1 parent deab4c1 commit a52f7d0

File tree

2 files changed

+90
-15
lines changed

2 files changed

+90
-15
lines changed

src/main/java/org/assertj/core/util/introspection/PropertyOrFieldSupport.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
import static java.lang.String.format;
1616
import static org.assertj.core.util.Preconditions.checkArgument;
1717

18-
import org.assertj.core.util.VisibleForTesting;
19-
2018
import java.util.Map;
2119

20+
import org.assertj.core.util.VisibleForTesting;
21+
2222
public class PropertyOrFieldSupport {
2323
private static final String SEPARATOR = ".";
2424
private PropertySupport propertySupport;
@@ -60,28 +60,30 @@ public Object getValueOf(String propertyOrFieldName, Object input) {
6060
return getSimpleValue(propertyOrFieldName, input);
6161
}
6262

63-
public Object getSimpleValue(String propertyOrFieldName, Object input) {
64-
// first check if input object is a map
65-
if (input instanceof Map) {
66-
Map<?, ?> map = (Map<?, ?>) input;
67-
return map.get(propertyOrFieldName);
68-
}
69-
70-
// then try to get given property values from objects, then try fields
63+
public Object getSimpleValue(String name, Object input) {
64+
// try to get name as a property, then try as a field, then try as a map key
7165
try {
72-
return propertySupport.propertyValueOf(propertyOrFieldName, Object.class, input);
66+
return propertySupport.propertyValueOf(name, Object.class, input);
7367
} catch (IntrospectionError propertyIntrospectionError) {
74-
// no luck with properties, let's try fields
68+
// no luck as a property, let's try as a field
7569
try {
76-
return fieldSupport.fieldValue(propertyOrFieldName, Object.class, input);
70+
return fieldSupport.fieldValue(name, Object.class, input);
7771
} catch (IntrospectionError fieldIntrospectionError) {
78-
// no field nor property found with given name, it is considered as an error
72+
// neither field nor property found with given name
73+
74+
// if the input object is a map, try name as a map key
75+
if (input instanceof Map) {
76+
Map<?, ?> map = (Map<?, ?>) input;
77+
return map.get(name);
78+
}
79+
80+
// no value found with given name, it is considered as an error
7981
String message = format("%nCan't find any field or property with name '%s'.%n" +
8082
"Error when introspecting properties was :%n" +
8183
"- %s %n" +
8284
"Error when introspecting fields was :%n" +
8385
"- %s",
84-
propertyOrFieldName, propertyIntrospectionError.getMessage(),
86+
name, propertyIntrospectionError.getMessage(),
8587
fieldIntrospectionError.getMessage());
8688
throw new IntrospectionError(message, fieldIntrospectionError);
8789
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2012-2020 the original author or authors.
12+
*/
13+
package org.assertj.core.util.introspection;
14+
15+
import static org.assertj.core.api.BDDAssertions.then;
16+
17+
import java.util.AbstractMap;
18+
import java.util.Collection;
19+
import java.util.HashMap;
20+
21+
import org.junit.jupiter.api.DisplayName;
22+
import org.junit.jupiter.api.Test;
23+
24+
/**
25+
* Tests for <code>{@link PropertyOrFieldSupport#getSimpleValue(String, Object)}</code> with {@code Map} input.
26+
*
27+
* @author Stefano Cordio
28+
*/
29+
@DisplayName("PropertyOrFieldSupport getSimpleValue(String, Map)")
30+
class PropertyOrFieldSupport_getSimpleValue_with_Map_input_Test {
31+
32+
private final PropertyOrFieldSupport underTest = PropertyOrFieldSupport.EXTRACTION;
33+
private final AbstractMap<String, String> map = new HashMap<>();
34+
35+
@Test
36+
void should_extract_property_value_even_if_map_key_matches_given_name() {
37+
// GIVEN
38+
map.put("empty", "value"); // key clashes with AbstractMap#isEmpty()
39+
// WHEN
40+
Object value = underTest.getSimpleValue("empty", map);
41+
// THEN
42+
then(value).isInstanceOf(Boolean.class);
43+
}
44+
45+
@Test
46+
void should_extract_field_value_even_if_map_key_matches_given_name() {
47+
// GIVEN
48+
map.put("keySet", "value"); // key clashes with AbstractMap#keySet
49+
// WHEN
50+
Object value = underTest.getSimpleValue("keySet", map);
51+
// THEN
52+
then(value).isInstanceOf(Collection.class);
53+
}
54+
55+
@Test
56+
void should_extract_map_value_when_no_property_or_field_matches_given_name() {
57+
// GIVEN
58+
map.put("key", "value");
59+
// WHEN
60+
Object value = underTest.getSimpleValue("key", map);
61+
// THEN
62+
then(value).isEqualTo("value");
63+
}
64+
65+
@Test
66+
void should_extract_null_when_given_name_is_not_found() {
67+
// WHEN
68+
Object value = underTest.getSimpleValue("unknown", map);
69+
// THEN
70+
then(value).isNull();
71+
}
72+
73+
}

0 commit comments

Comments
 (0)