Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2013 Shazam Entertainment Limited
*
* 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.
*/
package com.github.karsaig.approvalcrest.matcher;
import com.github.karsaig.approvalcrest.MatcherConfiguration;
import com.github.karsaig.approvalcrest.PathNullPointerException;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import org.hamcrest.Description;
import org.hamcrest.DiagnosingMatcher;
import org.hamcrest.Matcher;
import org.json.JSONException;
import org.skyscreamer.jsonassert.JSONAssert;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Pattern;
import static com.github.karsaig.approvalcrest.BeanFinder.findBeanAt;
import static com.github.karsaig.approvalcrest.CyclicReferenceDetector.getClassesWithCircularReferences;
import static com.github.karsaig.approvalcrest.FieldsIgnorer.MARKER;
import static com.github.karsaig.approvalcrest.FieldsIgnorer.findPaths;
import static com.github.karsaig.approvalcrest.matcher.GsonProvider.gson;
/**
* Extends the functionalities of {@link DiagnosingMatcher} with the possibility to specify fields and object types to
* ignore in the comparison, or fields to be matched with a custom matcher
*/
public class DiagnosingCustomisableMatcher extends AbstractDiagnosingMatcher implements CustomisableMatcher> {
private static final Pattern MARKER_PATTERN = Pattern.compile(MARKER);
protected final Set> circularReferenceTypes = new HashSet<>();
protected final T expected;
private GsonConfiguration configuration;
protected MatcherConfiguration matcherConfiguration = new MatcherConfiguration();
public DiagnosingCustomisableMatcher(T expected) {
this.expected = expected;
}
@Override
public void describeTo(Description description) {
Gson gson = gson(matcherConfiguration, circularReferenceTypes, configuration);
description.appendText(filterJson(gson, expected));
for (String fieldPath : matcherConfiguration.getCustomMatchers().keySet()) {
description.appendText("\nand ")
.appendText(fieldPath).appendText(" ")
.appendDescriptionOf(matcherConfiguration.getCustomMatchers().get(fieldPath));
}
}
@Override
protected boolean matches(Object actual, Description mismatchDescription) {
circularReferenceTypes.addAll(getClassesWithCircularReferences(actual, matcherConfiguration));
circularReferenceTypes.addAll(getClassesWithCircularReferences(expected, matcherConfiguration));
Gson gson = gson(matcherConfiguration, circularReferenceTypes, configuration);
if (!areCustomMatchersMatching(actual, mismatchDescription, gson)) {
return false;
}
String expectedJson = filterJson(gson, expected);
if (actual == null) {
return appendMismatchDescription(mismatchDescription, expectedJson, "null", "actual was null");
}
String actualJson = filterJson(gson, actual);
return assertEquals(expectedJson, actualJson, mismatchDescription);
}
private boolean areCustomMatchersMatching(Object actual, Description mismatchDescription, Gson gson) {
Map