Fixing jdk16

This commit is contained in:
Marcin Grzejszczak
2021-07-13 15:40:18 +02:00
parent 7de31594a6
commit d7b82f10b6
11 changed files with 82 additions and 76 deletions

View File

@@ -22,8 +22,10 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.annotation.JsonInclude;
@@ -36,6 +38,11 @@ import com.fasterxml.jackson.databind.ser.PropertyWriter;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.util.ReflectionUtils;
/**
@@ -80,11 +87,29 @@ public final class MetadataUtil {
if (patch == null) {
return objectToMerge;
}
byte[] bytes = new byte[0];
try {
byte[] bytes = MAPPER.writer().writeValueAsBytes(patch);
bytes = MAPPER.writer().writeValueAsBytes(patch);
return MAPPER.readerForUpdating(objectToMerge).readValue(bytes);
}
catch (Exception e) {
if (e.getClass().toString().contains("InaccessibleObjectException") && patch instanceof Map) {
try {
YamlPropertiesFactoryBean yamlProcessor = new YamlPropertiesFactoryBean();
yamlProcessor.setResources(new ByteArrayResource(bytes));
Properties properties = yamlProcessor.getObject();
T props = (T) new Binder(
new MapConfigurationPropertySource(properties.entrySet().stream()
.collect(Collectors.toMap(entry -> entry.getKey().toString(),
entry -> entry.getValue().toString())))).bind("", objectToMerge.getClass())
.get();
BeanUtils.copyProperties(props, objectToMerge);
return objectToMerge;
}
catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
throw new IllegalStateException(e);
}
}
@@ -207,23 +232,27 @@ class MyFilter extends SimpleBeanPropertyFilter implements Serializable {
writer.serializeAsField(pojo, jgen, provider);
return;
}
Object defaultInstance = CACHE.computeIfAbsent(pojo.getClass(), this::defaultInstance);
Object defaultInstance = defaultInstance(pojo);
if (defaultInstance instanceof CantInstantiateThisClass
|| !valueSameAsDefault(pojo, defaultInstance, writer.getName())) {
writer.serializeAsField(pojo, jgen, provider);
}
}
Object defaultInstance(Object pojo) {
return CACHE.computeIfAbsent(pojo.getClass(), this::defaultInstance);
}
private Object defaultInstance(Class aClass) {
try {
return aClass.newInstance();
return aClass.getDeclaredConstructor().newInstance();
}
catch (Exception e) {
return new CantInstantiateThisClass();
}
}
private boolean valueSameAsDefault(Object pojo, Object defaultInstance, String fieldName) {
boolean valueSameAsDefault(Object pojo, Object defaultInstance, String fieldName) {
Field field = ReflectionUtils.findField(pojo.getClass(), fieldName);
if (field == null) {
return false;

View File

@@ -225,7 +225,7 @@ class SpringTestMethodBodyBuildersSpec extends Specification implements WireMock
}
private String singleTestGenerator(Contract contractDsl) {
return singleTestGenerator([contractDsl])
return singleTestGenerator(Collections.singletonList(contractDsl))
}
private String singleTestGenerator(Collection<Contract> contractDsls) {
@@ -245,7 +245,7 @@ class SpringTestMethodBodyBuildersSpec extends Specification implements WireMock
}
})
}
}.buildClass(configProperties, [contractMetadata(contractDsls)], "foo", generatedClassData)
}.buildClass(configProperties, Collections.singletonList(contractMetadata(contractDsls)), "foo", generatedClassData)
}
ContractMetadata contractMetadata(Collection<Contract> contractDsls) {

View File

@@ -136,22 +136,22 @@ class YamlContractConverterSpec extends Specification {
contract.request.url.clientValue == "/foo"
contract.request.cookies.entries.find { it.key == "foo" && it.serverValue == "bar" }
contract.request.cookies.entries.find {
it.key == "fooRegex" && ((Pattern) it.clientValue).pattern == "reg" && it.serverValue == "reg"
it.key == "fooRegex" && ((Pattern) it.clientValue).pattern() == "reg" && it.serverValue == "reg"
}
contract.request.cookies.entries.find {
it.key == "fooPredefinedRegex" && ((Pattern) it.clientValue).pattern == "(true|false)" && it.serverValue == true
it.key == "fooPredefinedRegex" && ((Pattern) it.clientValue).pattern() == "(true|false)" && it.serverValue == true
}
and:
contract.response.status.clientValue == 200
contract.response.cookies.entries.find { it.key == "foo" && it.clientValue == "baz" }
contract.response.cookies.entries.find {
it.key == "fooRegex" && ((Pattern) it.serverValue).pattern == "[0-9]+" && it.clientValue == 123
it.key == "fooRegex" && ((Pattern) it.serverValue).pattern() == "[0-9]+" && it.clientValue == 123
}
contract.response.cookies.entries.find {
it.key == "source" && ((Pattern) it.serverValue).pattern == "ip_address" && it.clientValue == "ip_address"
it.key == "source" && ((Pattern) it.serverValue).pattern() == "ip_address" && it.clientValue == "ip_address"
}
contract.response.cookies.entries.find {
it.key == "fooPredefinedRegex" && ((Pattern) it.serverValue).pattern == "(true|false)" && it.clientValue == true
it.key == "fooPredefinedRegex" && ((Pattern) it.serverValue).pattern() == "(true|false)" && it.clientValue == true
}
MapConverter.getStubSideValues(contract.response.body) == ["status": "OK"]
}
@@ -178,7 +178,7 @@ class YamlContractConverterSpec extends Specification {
contract.request.method.clientValue == "PUT"
contract.request.headers.entries.find {
it.name == "foo" &&
((Pattern) it.clientValue).pattern == "bar" && it.serverValue == "bar"
((Pattern) it.clientValue).pattern() == "bar" && it.serverValue == "bar"
}
contract.request.headers.entries.find {
it.name == "fooReq" &&
@@ -198,7 +198,7 @@ class YamlContractConverterSpec extends Specification {
}
contract.response.headers.entries.find {
it.name == "foo2" &&
((Pattern) it.serverValue).pattern == "bar" && it.clientValue == "bar"
((Pattern) it.serverValue).pattern() == "bar" && it.clientValue == "bar"
}
contract.response.headers.entries.find {
it.name == "foo3" &&
@@ -269,7 +269,7 @@ class YamlContractConverterSpec extends Specification {
Contract contract = contracts.first()
contract.request.headers.entries.find {
it.name == "Content-Type" &&
((Pattern) it.clientValue).pattern == "application/json.*" && it.serverValue == "application/json"
((Pattern) it.clientValue).pattern() == "application/json.*" && it.serverValue == "application/json"
}
((Pattern) contract.request.urlPath.clientValue).pattern() == "/get/[0-9]"
contract.request.urlPath.serverValue == "/get/1"
@@ -404,7 +404,7 @@ class YamlContractConverterSpec extends Specification {
Contract contract = contracts.first()
contract.input.messageHeaders.entries.find {
it.name == "contentType" &&
((Pattern) it.clientValue).pattern == "application/json.*" && it.serverValue == "application/json"
((Pattern) it.clientValue).pattern() == "application/json.*" && it.serverValue == "application/json"
}
contract.input.bodyMatchers.matchers[0].path() == '$.duck'
contract.input.bodyMatchers.matchers[0].matchingType() == REGEX
@@ -539,7 +539,7 @@ class YamlContractConverterSpec extends Specification {
contract.input.triggeredBy.toString() == "foo()"
contract.input.messageHeaders.entries.find {
it.name == "foo" &&
((Pattern) it.clientValue).pattern == "bar" && it.serverValue == "bar"
((Pattern) it.clientValue).pattern() == "bar" && it.serverValue == "bar"
}
contract.input.messageBody.clientValue == [foo: "bar"]
contract.input.bodyMatchers.matchers[0].path() == '$.bar'
@@ -549,7 +549,7 @@ class YamlContractConverterSpec extends Specification {
contract.outputMessage.assertThat.toString() == "baz()"
contract.outputMessage.headers.entries.find {
it.name == "foo2" &&
((Pattern) it.serverValue).pattern == "bar" && it.clientValue == "bar"
((Pattern) it.serverValue).pattern() == "bar" && it.clientValue == "bar"
}
contract.outputMessage.headers.entries.find {
it.name == "foo3" &&

View File

@@ -25,7 +25,7 @@ import static org.assertj.core.api.BDDAssertions.then;
class AmqpMetadataTests {
YAMLMapper mapper = new YAMLMapper();
@Test
void should_parse_the_metadata_entry() throws JsonProcessingException {
// @formatter:off