Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try map input as last attempt in PropertyOrFieldSupport #1763

Merged
merged 1 commit into from
Jan 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
import static java.lang.String.format;
import static org.assertj.core.util.Preconditions.checkArgument;

import org.assertj.core.util.VisibleForTesting;

import java.util.Map;

import org.assertj.core.util.VisibleForTesting;

public class PropertyOrFieldSupport {
private static final String SEPARATOR = ".";
private PropertySupport propertySupport;
Expand Down Expand Up @@ -60,28 +60,30 @@ public Object getValueOf(String propertyOrFieldName, Object input) {
return getSimpleValue(propertyOrFieldName, input);
}

public Object getSimpleValue(String propertyOrFieldName, Object input) {
// first check if input object is a map
if (input instanceof Map) {
Map<?, ?> map = (Map<?, ?>) input;
return map.get(propertyOrFieldName);
}

// then try to get given property values from objects, then try fields
public Object getSimpleValue(String name, Object input) {
// try to get name as a property, then try as a field, then try as a map key
try {
return propertySupport.propertyValueOf(propertyOrFieldName, Object.class, input);
return propertySupport.propertyValueOf(name, Object.class, input);
} catch (IntrospectionError propertyIntrospectionError) {
// no luck with properties, let's try fields
// no luck as a property, let's try as a field
try {
return fieldSupport.fieldValue(propertyOrFieldName, Object.class, input);
return fieldSupport.fieldValue(name, Object.class, input);
} catch (IntrospectionError fieldIntrospectionError) {
// no field nor property found with given name, it is considered as an error
// neither field nor property found with given name

// if the input object is a map, try name as a map key
if (input instanceof Map) {
Map<?, ?> map = (Map<?, ?>) input;
return map.get(name);
}

// no value found with given name, it is considered as an error
String message = format("%nCan't find any field or property with name '%s'.%n" +
"Error when introspecting properties was :%n" +
"- %s %n" +
"Error when introspecting fields was :%n" +
"- %s",
propertyOrFieldName, propertyIntrospectionError.getMessage(),
name, propertyIntrospectionError.getMessage(),
fieldIntrospectionError.getMessage());
throw new IntrospectionError(message, fieldIntrospectionError);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2020 the original author or authors.
*/
package org.assertj.core.util.introspection;

import static org.assertj.core.api.BDDAssertions.then;

import java.util.AbstractMap;
import java.util.Collection;
import java.util.HashMap;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

/**
* Tests for <code>{@link PropertyOrFieldSupport#getSimpleValue(String, Object)}</code> with {@code Map} input.
*
* @author Stefano Cordio
*/
@DisplayName("PropertyOrFieldSupport getSimpleValue(String, Map)")
class PropertyOrFieldSupport_getSimpleValue_with_Map_input_Test {
scordio marked this conversation as resolved.
Show resolved Hide resolved

private final PropertyOrFieldSupport underTest = PropertyOrFieldSupport.EXTRACTION;
private final AbstractMap<String, String> map = new HashMap<>();

@Test
void should_extract_property_value_even_if_map_key_matches_given_name() {
// GIVEN
map.put("empty", "value"); // key clashes with AbstractMap#isEmpty()
// WHEN
Object value = underTest.getSimpleValue("empty", map);
// THEN
then(value).isInstanceOf(Boolean.class);
}

@Test
void should_extract_field_value_even_if_map_key_matches_given_name() {
// GIVEN
map.put("keySet", "value"); // key clashes with AbstractMap#keySet
// WHEN
Object value = underTest.getSimpleValue("keySet", map);
// THEN
then(value).isInstanceOf(Collection.class);
}

@Test
void should_extract_map_value_when_no_property_or_field_matches_given_name() {
// GIVEN
map.put("key", "value");
// WHEN
Object value = underTest.getSimpleValue("key", map);
// THEN
then(value).isEqualTo("value");
}

@Test
void should_extract_null_when_given_name_is_not_found() {
// WHEN
Object value = underTest.getSimpleValue("unknown", map);
// THEN
then(value).isNull();
}

}