diff --git a/arex-compare-core/pom.xml b/arex-compare-core/pom.xml index 858ba7a..ce338ed 100644 --- a/arex-compare-core/pom.xml +++ b/arex-compare-core/pom.xml @@ -5,7 +5,7 @@ arex-compare-parent com.arextest - 0.2.15 + 0.2.16 4.0.0 diff --git a/arex-compare-core/src/main/java/com/arextest/diff/compare/ArrayCompare.java b/arex-compare-core/src/main/java/com/arextest/diff/compare/ArrayCompare.java index 877c75c..ee25fbc 100644 --- a/arex-compare-core/src/main/java/com/arextest/diff/compare/ArrayCompare.java +++ b/arex-compare-core/src/main/java/com/arextest/diff/compare/ArrayCompare.java @@ -9,9 +9,10 @@ import com.arextest.diff.model.log.NodeEntity; import com.arextest.diff.utils.ListUti; import com.fasterxml.jackson.databind.node.ArrayNode; -import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; /** * Created by rchen9 on 2022/7/25. @@ -29,8 +30,8 @@ public static void arrayCompare(Object obj1, Object obj2, CompareContext compare compareContext); } - List leftComparedIndexes = new ArrayList<>(); - List rightComparedIndexes = new ArrayList<>(); + Set leftComparedIndexes = new HashSet<>(); + Set rightComparedIndexes = new HashSet<>(); // decide to use which indexSelector IndexSelector indexSelector = IndexSelectorFactory.getIndexSelector( diff --git a/arex-compare-core/src/main/java/com/arextest/diff/compare/feature/GeneralIndexSelector.java b/arex-compare-core/src/main/java/com/arextest/diff/compare/feature/GeneralIndexSelector.java index d83fbec..619db0b 100644 --- a/arex-compare-core/src/main/java/com/arextest/diff/compare/feature/GeneralIndexSelector.java +++ b/arex-compare-core/src/main/java/com/arextest/diff/compare/feature/GeneralIndexSelector.java @@ -1,12 +1,12 @@ package com.arextest.diff.compare.feature; import com.fasterxml.jackson.databind.node.ArrayNode; -import java.util.List; +import java.util.Set; public class GeneralIndexSelector implements IndexSelector { @Override - public int findCorrespondLeftIndex(int curRightIndex, List leftComparedIndex, + public int findCorrespondLeftIndex(int curRightIndex, Set leftComparedIndex, ArrayNode obj1Array, ArrayNode obj2Array) { if (obj1Array == null || obj1Array.isEmpty()) { return -1; @@ -18,7 +18,7 @@ public int findCorrespondLeftIndex(int curRightIndex, List leftCompared } @Override - public int findCorrespondRightIndex(int curLeftIndex, List rightComparedIndex, + public int findCorrespondRightIndex(int curLeftIndex, Set rightComparedIndex, ArrayNode obj1Array, ArrayNode obj2Array) { if (obj2Array == null || obj2Array.isEmpty()) { return -1; diff --git a/arex-compare-core/src/main/java/com/arextest/diff/compare/feature/IndexSelector.java b/arex-compare-core/src/main/java/com/arextest/diff/compare/feature/IndexSelector.java index 0ac4404..5a5ca85 100644 --- a/arex-compare-core/src/main/java/com/arextest/diff/compare/feature/IndexSelector.java +++ b/arex-compare-core/src/main/java/com/arextest/diff/compare/feature/IndexSelector.java @@ -2,14 +2,14 @@ import com.arextest.diff.model.exception.FindErrorException; import com.fasterxml.jackson.databind.node.ArrayNode; -import java.util.List; +import java.util.Set; public interface IndexSelector { - int findCorrespondLeftIndex(int curRightIndex, List leftComparedIndex, + int findCorrespondLeftIndex(int curRightIndex, Set leftComparedIndex, ArrayNode obj1Array, ArrayNode obj2Array) throws FindErrorException, Exception; - int findCorrespondRightIndex(int curLeftIndex, List rightComparedIndex, + int findCorrespondRightIndex(int curLeftIndex, Set rightComparedIndex, ArrayNode obj1Array, ArrayNode obj2Array) throws Exception; String judgeLeftIndexStandard(int leftIndex); diff --git a/arex-compare-core/src/main/java/com/arextest/diff/compare/feature/ListKeyIndexSelector.java b/arex-compare-core/src/main/java/com/arextest/diff/compare/feature/ListKeyIndexSelector.java index 7c84de6..3c46c25 100644 --- a/arex-compare-core/src/main/java/com/arextest/diff/compare/feature/ListKeyIndexSelector.java +++ b/arex-compare-core/src/main/java/com/arextest/diff/compare/feature/ListKeyIndexSelector.java @@ -4,8 +4,8 @@ import com.arextest.diff.handler.log.register.LogRegister; import com.arextest.diff.model.compare.CompareContext; import com.fasterxml.jackson.databind.node.ArrayNode; -import java.util.List; import java.util.Map; +import java.util.Set; public class ListKeyIndexSelector implements IndexSelector { @@ -24,7 +24,7 @@ public ListKeyIndexSelector(Map indexKeysLeft, } @Override - public int findCorrespondLeftIndex(int curRightIndex, List leftComparedIndexes, + public int findCorrespondLeftIndex(int curRightIndex, Set leftComparedIndexes, ArrayNode obj1Array, ArrayNode obj2Array) throws Exception { int correspondLeftIndex = -1; if (indexKeysRight != null) { @@ -33,11 +33,12 @@ public int findCorrespondLeftIndex(int curRightIndex, List leftCompared int cnt = 0; boolean alreadyFind = false; if (indexKeysLeft != null) { - for (Integer index : indexKeysLeft.keySet()) { - if (rightKey.equals(indexKeysLeft.get(index))) { + for (Map.Entry entry : indexKeysLeft.entrySet()) { + if (rightKey.equals(entry.getValue())) { cnt++; - if (correspondLeftIndex == -1 && !leftComparedIndexes.contains(index) && !alreadyFind) { - correspondLeftIndex = index; + if (correspondLeftIndex == -1 && !leftComparedIndexes.contains(entry.getKey()) + && !alreadyFind) { + correspondLeftIndex = entry.getKey(); alreadyFind = true; } } @@ -54,7 +55,7 @@ public int findCorrespondLeftIndex(int curRightIndex, List leftCompared } @Override - public int findCorrespondRightIndex(int curLeftIndex, List rightComparedIndexes, + public int findCorrespondRightIndex(int curLeftIndex, Set rightComparedIndexes, ArrayNode obj1Array, ArrayNode obj2Array) throws Exception { // when indexKeyLef is null, obj1Array must be empty. but the indexKeyLeft also judge null; int correspondRightIndex = -1; @@ -64,16 +65,18 @@ public int findCorrespondRightIndex(int curLeftIndex, List rightCompare int cnt = 0; boolean alreadyFind = false; if (indexKeysRight != null) { - for (Integer index : indexKeysRight.keySet()) { - if (leftKey.equals(indexKeysRight.get(index))) { + for (Map.Entry entry : indexKeysRight.entrySet()) { + if (leftKey.equals(entry.getValue())) { cnt++; - if (correspondRightIndex == -1 && !rightComparedIndexes.contains(index) + if (correspondRightIndex == -1 && !rightComparedIndexes.contains(entry.getKey()) && !alreadyFind) { - correspondRightIndex = index; + correspondRightIndex = entry.getKey(); alreadyFind = true; } } } + + } if (cnt > 1) { diff --git a/arex-compare-core/src/main/java/com/arextest/diff/compare/feature/PrimitiveArrayIndexSelector.java b/arex-compare-core/src/main/java/com/arextest/diff/compare/feature/PrimitiveArrayIndexSelector.java index 6258398..ad1b99b 100644 --- a/arex-compare-core/src/main/java/com/arextest/diff/compare/feature/PrimitiveArrayIndexSelector.java +++ b/arex-compare-core/src/main/java/com/arextest/diff/compare/feature/PrimitiveArrayIndexSelector.java @@ -7,6 +7,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; public class PrimitiveArrayIndexSelector implements IndexSelector { @@ -22,7 +23,7 @@ public PrimitiveArrayIndexSelector(CompareContext compareContext) { } @Override - public int findCorrespondLeftIndex(int curRightIndex, List leftComparedIndex, + public int findCorrespondLeftIndex(int curRightIndex, Set leftComparedIndex, ArrayNode obj1Array, ArrayNode obj2Array) { JsonNode jsonNode = obj2Array.get(curRightIndex); if (leftIndexKeys.containsKey(jsonNode)) { @@ -37,7 +38,7 @@ public int findCorrespondLeftIndex(int curRightIndex, List leftCompared } @Override - public int findCorrespondRightIndex(int curLeftIndex, List rightComparedIndex, + public int findCorrespondRightIndex(int curLeftIndex, Set rightComparedIndex, ArrayNode obj1Array, ArrayNode obj2Array) { JsonNode jsonNode = obj1Array.get(curLeftIndex); if (rightIndexKeys.containsKey(jsonNode)) { diff --git a/arex-compare-core/src/main/java/com/arextest/diff/handler/parse/StringAndCompressParse.java b/arex-compare-core/src/main/java/com/arextest/diff/handler/parse/StringAndCompressParse.java index 5d5d0fe..de63f1d 100644 --- a/arex-compare-core/src/main/java/com/arextest/diff/handler/parse/StringAndCompressParse.java +++ b/arex-compare-core/src/main/java/com/arextest/diff/handler/parse/StringAndCompressParse.java @@ -2,10 +2,10 @@ import com.arextest.diff.model.TransformConfig.TransformMethod; import com.arextest.diff.model.log.NodeEntity; -import com.arextest.diff.utils.TransformUtil; import com.arextest.diff.utils.JacksonHelperUtil; import com.arextest.diff.utils.ListUti; import com.arextest.diff.utils.StringUtil; +import com.arextest.diff.utils.TransformUtil; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; @@ -17,7 +17,6 @@ import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.stream.Collectors; import org.apache.commons.lang3.tuple.MutablePair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -61,7 +60,7 @@ public void getJSONParse(Object obj, Object preObj) { ObjectNode jsonObject = (ObjectNode) obj; List names = JacksonHelperUtil.getNames(jsonObject); for (String fieldName : names) { - currentNode.add(new NodeEntity(fieldName, 0)); + currentNode.add(new NodeEntity(nameToLower ? fieldName.toLowerCase() : fieldName, 0)); Object objFieldValue = jsonObject.get(fieldName); getJSONParse(objFieldValue, obj); ListUti.removeLast(currentNode); @@ -78,16 +77,19 @@ public void getJSONParse(Object obj, Object preObj) { } else { String value = ((JsonNode) obj).asText(); - // TODO: 2022/9/20 improve the method to speed up - List nodePath = nameToLower ? ListUti.convertToStringList(currentNode).stream() - .map(String::toLowerCase).collect(Collectors.toList()) - : ListUti.convertToStringList(currentNode); + MutablePair objectBooleanPair = null; - if (transFormConfigMap != null && transFormConfigMap.containsKey(nodePath)) { - List transformMethodList = this.transFormConfigMap.get(nodePath); - objectBooleanPair = processCompress(value, this.pluginJarUrl, transformMethodList, preObj); - } else { + if (transFormConfigMap == null || transFormConfigMap.isEmpty()) { objectBooleanPair = processStringParse(value, preObj); + } else { + List nodePath = ListUti.convertToStringList(currentNode); + if (transFormConfigMap.containsKey(nodePath)) { + List transformMethodList = this.transFormConfigMap.get(nodePath); + objectBooleanPair = processCompress(value, this.pluginJarUrl, transformMethodList, + preObj); + } else { + objectBooleanPair = processStringParse(value, preObj); + } } if (objectBooleanPair.getKey() == null) { diff --git a/arex-compare-core/src/main/java/com/arextest/diff/utils/JacksonHelperUtil.java b/arex-compare-core/src/main/java/com/arextest/diff/utils/JacksonHelperUtil.java index 9358804..88705e2 100644 --- a/arex-compare-core/src/main/java/com/arextest/diff/utils/JacksonHelperUtil.java +++ b/arex-compare-core/src/main/java/com/arextest/diff/utils/JacksonHelperUtil.java @@ -4,7 +4,6 @@ import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; /** @@ -15,11 +14,8 @@ public class JacksonHelperUtil { public static ObjectMapper objectMapper = new ObjectMapper(); public static List getNames(ObjectNode objectNode) { - List result = new ArrayList<>(); - Iterator stringIterator = objectNode.fieldNames(); - while (stringIterator.hasNext()) { - result.add(stringIterator.next()); - } + List result = new ArrayList<>(objectNode.size()); + objectNode.fieldNames().forEachRemaining(result::add); return result; } diff --git a/arex-compare-core/src/main/java/com/arextest/diff/utils/NameConvertUtil.java b/arex-compare-core/src/main/java/com/arextest/diff/utils/NameConvertUtil.java index 29722ad..a927508 100644 --- a/arex-compare-core/src/main/java/com/arextest/diff/utils/NameConvertUtil.java +++ b/arex-compare-core/src/main/java/com/arextest/diff/utils/NameConvertUtil.java @@ -19,13 +19,12 @@ public static void nameConvert(Object object) { for (String fieldName : names) { JsonNode obj1FieldValue = jsonObj1.get(fieldName); - jsonObj1.set(fieldName.toLowerCase(), obj1FieldValue); - nameConvert(obj1FieldValue); - } - for (String fieldName : names) { - if (containsUpper(fieldName)) { + String lowerCase = fieldName.toLowerCase(); + jsonObj1.set(lowerCase, obj1FieldValue); + if (fieldName != lowerCase) { jsonObj1.remove(fieldName); } + nameConvert(obj1FieldValue); } } else if (object instanceof ArrayNode) { ArrayNode obj1Array = (ArrayNode) object; @@ -35,12 +34,5 @@ public static void nameConvert(Object object) { nameConvert(element); } } - - } - - public static boolean containsUpper(String name) { - return name.chars().anyMatch( - (int ch) -> Character.isUpperCase((char) ch) - ); } } diff --git a/arex-compare-core/src/test/java/com/arextest/diff/compare/feature/PrimitiveArrayIndexSelectorUnitTest.java b/arex-compare-core/src/test/java/com/arextest/diff/compare/feature/PrimitiveArrayIndexSelectorUnitTest.java index 5e6d305..5a05821 100644 --- a/arex-compare-core/src/test/java/com/arextest/diff/compare/feature/PrimitiveArrayIndexSelectorUnitTest.java +++ b/arex-compare-core/src/test/java/com/arextest/diff/compare/feature/PrimitiveArrayIndexSelectorUnitTest.java @@ -33,7 +33,7 @@ public void testFindCorrespondLeftIndex() throws JsonProcessingException { ArrayNode obj2 = objectMapper.readValue("[3,1,2]", ArrayNode.class); int correspondLeftIndex = primitiveArrayIndexSelector.findCorrespondLeftIndex(0, - Collections.emptyList(), obj1, obj2); + Collections.emptySet(), obj1, obj2); Assertions.assertEquals(2, correspondLeftIndex); } @@ -44,7 +44,7 @@ public void testFindCorrespondRightIndex() throws JsonProcessingException { ArrayNode obj2 = objectMapper.readValue("[3,1,2]", ArrayNode.class); int correspondRightIndex = primitiveArrayIndexSelector.findCorrespondRightIndex(0, - Collections.emptyList(), obj1, obj2); + Collections.emptySet(), obj1, obj2); Assertions.assertEquals(1, correspondRightIndex); } diff --git a/arex-compare-core/src/test/java/com/arextest/diff/sdk/CompareProblemTest.java b/arex-compare-core/src/test/java/com/arextest/diff/sdk/CompareProblemTest.java index f424cf6..48b7ca0 100644 --- a/arex-compare-core/src/test/java/com/arextest/diff/sdk/CompareProblemTest.java +++ b/arex-compare-core/src/test/java/com/arextest/diff/sdk/CompareProblemTest.java @@ -5,8 +5,12 @@ import com.arextest.diff.model.DecompressConfig; import com.arextest.diff.model.enumeration.CategoryType; import com.arextest.diff.model.enumeration.DiffResultCode; +import java.io.FileInputStream; +import java.io.IOException; import java.util.Arrays; +import java.util.Collections; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; /** @@ -613,5 +617,37 @@ public void testStringEqualsWithArexPrefix() { Assertions.assertEquals(0, result.getCode()); } + @Disabled + @Test + public void testCpuUsed() throws IOException { + CompareSDK compareSDK = new CompareSDK(); + compareSDK.getGlobalOptions().putNameToLower(true).putNullEqualsEmpty(true).putIgnoreNodeSet( + Collections.singleton("timestamp")); + + CompareOptions compareOptions = CompareOptions.options().putListSortConfig( + Arrays.asList("fareinfolist"), + Arrays.asList(Arrays.asList("id")) + ); + + String baseMsg = readStringFromFile("./baseDecompress.txt"); + String testMsg = readStringFromFile("./testDecompress.txt"); + CompareResult result = compareSDK.compare(baseMsg, testMsg, compareOptions); + Assertions.assertEquals(0, result.getCode()); + + } + + // reading a string from a file + public static String readStringFromFile(String filePath) throws IOException { + FileInputStream fis = new FileInputStream(filePath); + byte[] buffer = new byte[1024]; + StringBuilder sb = new StringBuilder(); + int bytesRead; + while ((bytesRead = fis.read(buffer)) != -1) { + sb.append(new String(buffer, 0, bytesRead)); + } + fis.close(); + return sb.toString(); + } + } diff --git a/pom.xml b/pom.xml index 72ac67d..73e3cfe 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.arextest arex-compare-parent pom - 0.2.15 + 0.2.16 arex-compare-extension arex-compare-core