diff --git a/arex-compare-core/pom.xml b/arex-compare-core/pom.xml index ce338ed..f0c843e 100644 --- a/arex-compare-core/pom.xml +++ b/arex-compare-core/pom.xml @@ -5,7 +5,7 @@ arex-compare-parent com.arextest - 0.2.16 + 0.2.17 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 ee25fbc..38d22d6 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 @@ -6,6 +6,7 @@ import com.arextest.diff.handler.log.register.LogRegister; import com.arextest.diff.model.compare.CompareContext; import com.arextest.diff.model.compare.IndexPair; +import com.arextest.diff.model.enumeration.ParentNodeType; import com.arextest.diff.model.log.NodeEntity; import com.arextest.diff.utils.ListUti; import com.fasterxml.jackson.databind.node.ArrayNode; @@ -55,7 +56,8 @@ public static void arrayCompare(Object obj1, Object obj2, CompareContext compare boolean rightExist = false; if (correspondRightIndex == -1) { - LogRegister.register(element1, element2, LogMarker.RIGHT_ARRAY_MISSING_KEY, compareContext); + compareContext.parentNodeType = ParentNodeType.ARRAY; + GenericCompare.jsonCompare(element1, element2, compareContext); } else { element2 = obj2Array.get(correspondRightIndex); rightExist = true; @@ -70,6 +72,7 @@ public static void arrayCompare(Object obj1, Object obj2, CompareContext compare compareContext.pkListIndexPair, compareContext.currentNodeLeft); if (needCompare && element1 != null && element2 != null) { + compareContext.parentNodeType = ParentNodeType.ARRAY; GenericCompare.jsonCompare(element1, element2, compareContext); } } @@ -98,7 +101,8 @@ public static void arrayCompare(Object obj1, Object obj2, CompareContext compare Object element2 = obj2Array.get(i); boolean leftExist = false; if (correspondLeftIndex == -1) { - LogRegister.register(element1, element2, LogMarker.LEFT_ARRAY_MISSING_KEY, compareContext); + compareContext.parentNodeType = ParentNodeType.ARRAY; + GenericCompare.jsonCompare(element1, element2, compareContext); } else { element1 = obj1Array.get(correspondLeftIndex); leftExist = true; @@ -111,6 +115,7 @@ public static void arrayCompare(Object obj1, Object obj2, CompareContext compare compareContext.pkListIndexPair, compareContext.currentNodeRight); if (needCompare && element1 != null && element2 != null) { + compareContext.parentNodeType = ParentNodeType.ARRAY; GenericCompare.jsonCompare(element1, element2, compareContext); } } diff --git a/arex-compare-core/src/main/java/com/arextest/diff/compare/GenericCompare.java b/arex-compare-core/src/main/java/com/arextest/diff/compare/GenericCompare.java index abe800c..e6acaa2 100644 --- a/arex-compare-core/src/main/java/com/arextest/diff/compare/GenericCompare.java +++ b/arex-compare-core/src/main/java/com/arextest/diff/compare/GenericCompare.java @@ -3,6 +3,7 @@ import com.arextest.diff.handler.log.LogMarker; import com.arextest.diff.handler.log.register.LogRegister; import com.arextest.diff.model.compare.CompareContext; +import com.arextest.diff.model.enumeration.ParentNodeType; import com.arextest.diff.model.log.NodeEntity; import com.arextest.diff.utils.IgnoreUtil; import com.arextest.diff.utils.ListUti; @@ -10,6 +11,7 @@ import com.fasterxml.jackson.databind.node.NullNode; import com.fasterxml.jackson.databind.node.ObjectNode; import java.util.List; +import java.util.Objects; public class GenericCompare { @@ -34,8 +36,9 @@ public static void jsonCompare(Object obj1, Object obj2, CompareContext compareC } // not compare by exclusions - if (IgnoreUtil.ignoreProcessor(fuzzyPath, currentNode, compareContext.exclusions, - compareContext.expressionExclusions, compareContext.ignoreNodeSet)) { + if (IgnoreUtil.ignoreProcessor(fuzzyPath, compareContext.currentNodeLeft, + compareContext.currentNodeRight, compareContext.exclusions, + compareContext.conditionExclusions, compareContext.ignoreNodeSet)) { return; } @@ -43,10 +46,16 @@ public static void jsonCompare(Object obj1, Object obj2, CompareContext compareC if (obj1 == null && obj2 == null) { return; } else if (obj1 == null) { - LogRegister.register(obj1, obj2, LogMarker.LEFT_OBJECT_MISSING, compareContext); + LogRegister.register(obj1, obj2, + Objects.equals(compareContext.parentNodeType, ParentNodeType.OBJECT) + ? LogMarker.LEFT_OBJECT_MISSING + : LogMarker.LEFT_ARRAY_MISSING, compareContext); return; } else if (obj2 == null) { - LogRegister.register(obj1, obj2, LogMarker.RIGHT_OBJECT_MISSING, compareContext); + LogRegister.register(obj1, obj2, + Objects.equals(compareContext.parentNodeType, ParentNodeType.OBJECT) + ? LogMarker.LEFT_OBJECT_MISSING + : LogMarker.RIGHT_ARRAY_MISSING, compareContext); return; } diff --git a/arex-compare-core/src/main/java/com/arextest/diff/compare/ObjectCompare.java b/arex-compare-core/src/main/java/com/arextest/diff/compare/ObjectCompare.java index c0a0e0f..8e2d4da 100644 --- a/arex-compare-core/src/main/java/com/arextest/diff/compare/ObjectCompare.java +++ b/arex-compare-core/src/main/java/com/arextest/diff/compare/ObjectCompare.java @@ -1,6 +1,7 @@ package com.arextest.diff.compare; import com.arextest.diff.model.compare.CompareContext; +import com.arextest.diff.model.enumeration.ParentNodeType; import com.arextest.diff.model.log.NodeEntity; import com.arextest.diff.utils.JacksonHelperUtil; import com.arextest.diff.utils.ListUti; @@ -34,6 +35,7 @@ public static void objectCompare(Object obj1, Object obj2, CompareContext compar compareContext.currentNodeRight.add(new NodeEntity(fieldName, 0)); } + compareContext.parentNodeType = ParentNodeType.OBJECT; GenericCompare.jsonCompare(obj1FieldValue, obj2FieldValue, compareContext); ListUti.removeLast(compareContext.currentNodeLeft); @@ -57,6 +59,7 @@ public static void objectCompare(Object obj1, Object obj2, CompareContext compar compareContext.currentNodeLeft.add(new NodeEntity(fieldName, 0)); } + compareContext.parentNodeType = ParentNodeType.OBJECT; GenericCompare.jsonCompare(obj1FieldValue, obj2FieldValue, compareContext); if (leftExist) { diff --git a/arex-compare-core/src/main/java/com/arextest/diff/eigen/EigenHandler.java b/arex-compare-core/src/main/java/com/arextest/diff/eigen/EigenHandler.java index dddec1c..f74ada2 100644 --- a/arex-compare-core/src/main/java/com/arextest/diff/eigen/EigenHandler.java +++ b/arex-compare-core/src/main/java/com/arextest/diff/eigen/EigenHandler.java @@ -10,9 +10,10 @@ import com.arextest.diff.model.pathparse.ExpressionNodeEntity; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ObjectNode; -import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; +import java.util.List; +import java.util.Map; import java.util.Objects; public class EigenHandler { @@ -41,13 +42,13 @@ public EigenResult doHandler(RulesConfig rulesConfig) { obj = null; } - LinkedList> expressionExclusions = - jsonPathExpressionHandler.doMultiExpressionParse(rulesConfig.getExpressionExclusions(), + List> expressionExclusions = new LinkedList<>(); + Map, LinkedList>> listLinkedListMap = + jsonPathExpressionHandler.doMultiExpressionParse( + rulesConfig.getExpressionExclusions(), obj); - if (expressionExclusions != null) { - rulesConfig.setExpressionExclusions(new ArrayList<>(expressionExclusions)); - } - + listLinkedListMap.values().forEach(expressionExclusions::addAll); + rulesConfig.setExpressionExclusions(expressionExclusions); return eigenMapCalculate.doCalculate(obj, rulesConfig, new HashMap<>()); } diff --git a/arex-compare-core/src/main/java/com/arextest/diff/eigen/EigenMapCalculate.java b/arex-compare-core/src/main/java/com/arextest/diff/eigen/EigenMapCalculate.java index 98fb276..1a6a9dc 100644 --- a/arex-compare-core/src/main/java/com/arextest/diff/eigen/EigenMapCalculate.java +++ b/arex-compare-core/src/main/java/com/arextest/diff/eigen/EigenMapCalculate.java @@ -12,7 +12,6 @@ import java.time.Instant; import java.time.ZoneId; import java.time.format.DateTimeFormatter; -import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; @@ -54,7 +53,8 @@ private void doCalculateJsonNode(Object obj, CalculateContext calculateContext, Map eigenMap) { // ignore by node name and node path - if (IgnoreUtil.ignoreProcessor(calculateContext.nodePath, calculateContext.currentNodeEntity, + if (IgnoreUtil.ignoreProcessorEngine(calculateContext.nodePath, + calculateContext.currentNodeEntity, calculateContext.exclusions, calculateContext.expressionExclusions, calculateContext.ignoreNodeSet)) { return; diff --git a/arex-compare-core/src/main/java/com/arextest/diff/handler/CompareHandler.java b/arex-compare-core/src/main/java/com/arextest/diff/handler/CompareHandler.java index 101a94a..7eeb7ab 100644 --- a/arex-compare-core/src/main/java/com/arextest/diff/handler/CompareHandler.java +++ b/arex-compare-core/src/main/java/com/arextest/diff/handler/CompareHandler.java @@ -29,7 +29,7 @@ public List doHandler(RulesConfig rulesConfig, KeyComputeResponse key compareContext.setResponseReferences(keyComputeResponse.getAllReferenceEntities()); compareContext.exclusions = rulesConfig.getExclusions(); - compareContext.expressionExclusions = rulesConfig.getExpressionExclusions(); + compareContext.conditionExclusions = rulesConfig.getConditionExclusions(); compareContext.ignoreNodeSet = rulesConfig.getIgnoreNodeSet(); compareContext.notDistinguishNullAndEmpty = rulesConfig.isNullEqualsEmpty(); diff --git a/arex-compare-core/src/main/java/com/arextest/diff/handler/parse/sqlparse/SqlParse.java b/arex-compare-core/src/main/java/com/arextest/diff/handler/parse/sqlparse/SqlParse.java index a6355f2..4a0ffad 100644 --- a/arex-compare-core/src/main/java/com/arextest/diff/handler/parse/sqlparse/SqlParse.java +++ b/arex-compare-core/src/main/java/com/arextest/diff/handler/parse/sqlparse/SqlParse.java @@ -76,6 +76,9 @@ public ParsedResult sqlParse(ObjectNode jsonObj, boolean nameToLower) { return new ParsedResult(null, false); } + // additional: Handling multiple selections + databaseBody = processMultipleSelect(databaseBody); + boolean successParse = true; ArrayNode parsedSql = JacksonHelperUtil.getArrayNode(); List isSelect = new ArrayList<>(); @@ -245,6 +248,41 @@ private void fillOriginalSql(ObjectNode objectNode, JsonNode databaseBody) { objectNode.set(DbParseConstants.PARSED_SQL, parsedSql); } + private JsonNode processMultipleSelect(JsonNode databaseBody) { + JsonNode result = databaseBody; + if (databaseBody instanceof TextNode) { + String sql = databaseBody.asText(); + if (sql.contains(";")) { + String[] sqls = sql.split(";"); + ArrayNode arrayNode = JacksonHelperUtil.getArrayNode(); + for (String s : sqls) { + if (!startsWithSelect(s)) { + break; + } + arrayNode.add(s); + } + result = arrayNode; + } + + } + return result; + } + + public static boolean startsWithSelect(String str) { + if (str == null) { + return false; + } + int len = str.length(); + int i = 0; + // Skip leading whitespace + while (i < len && Character.isWhitespace(str.charAt(i))) { + i++; + } + // Check if the remaining string starts with "select" or "SELECT" + return str.regionMatches(true, i, "select", 0, 6); + } + + private static class ParsedResult { private List isSelect; diff --git a/arex-compare-core/src/main/java/com/arextest/diff/handler/pathparse/JsonPathExpressionHandler.java b/arex-compare-core/src/main/java/com/arextest/diff/handler/pathparse/JsonPathExpressionHandler.java index 2f3a7e0..c94f231 100644 --- a/arex-compare-core/src/main/java/com/arextest/diff/handler/pathparse/JsonPathExpressionHandler.java +++ b/arex-compare-core/src/main/java/com/arextest/diff/handler/pathparse/JsonPathExpressionHandler.java @@ -11,9 +11,12 @@ import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.TextNode; +import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import java.util.Objects; import java.util.Set; import java.util.concurrent.CompletableFuture; @@ -29,16 +32,20 @@ public class JsonPathExpressionHandler { public void doExpressionParse(RulesConfig rulesConfig, Object baseObj, Object testObj) throws ExecutionException, InterruptedException { - Set> result = new HashSet<>(); + Map>, LinkedList>> + conditionExclusions = null; + try { List> expressionExclusions = rulesConfig.getExpressionExclusions(); - CompletableFuture>> future1 = CompletableFuture.supplyAsync( + CompletableFuture, LinkedList>>> + future1 = CompletableFuture.supplyAsync( () -> doMultiExpressionParse(expressionExclusions, baseObj), TaskThreadFactory.jsonObjectThreadPool ); - CompletableFuture>> future2 = CompletableFuture.supplyAsync( + CompletableFuture, LinkedList>>> + future2 = CompletableFuture.supplyAsync( () -> doMultiExpressionParse(expressionExclusions, testObj), TaskThreadFactory.jsonObjectThreadPool ); @@ -46,19 +53,20 @@ public void doExpressionParse(RulesConfig rulesConfig, CompletableFuture voidCompletableFuture = CompletableFuture.allOf(future1, future2); voidCompletableFuture.get(Constant.JSON_PATH_PARSE_MINUTES_TIME, TimeUnit.MINUTES); - result.addAll(future1.get()); - result.addAll(future2.get()); + Map, LinkedList>> baseExpression = future1.get(); + Map, LinkedList>> testExpression = future2.get(); + conditionExclusions = mergeExpression(baseExpression, testExpression); } catch (RuntimeException | TimeoutException e) { LOGGER.warning("doExpressionParse error: " + e.getMessage()); } - rulesConfig.setExpressionExclusions(new LinkedList<>(result)); + rulesConfig.setConditionExclusions(conditionExclusions); } - public LinkedList> doMultiExpressionParse( + public Map, LinkedList>> doMultiExpressionParse( List> expressionExclusions, Object object) { - - LinkedList> result = new LinkedList<>(); + Map, LinkedList>> result = + new HashMap<>(); try { if (ListUti.isEmpty(expressionExclusions)) { @@ -69,7 +77,7 @@ public LinkedList> doMultiExpressionParse( LinkedList> linkedLists = doSinglePathExpressionParse( expressionNodeEntityList, 0, expressionNodeEntityList.size(), object, false); if (linkedLists != null) { - result.addAll(linkedLists); + result.put(expressionNodeEntityList, linkedLists); } } } catch (RuntimeException exception) { @@ -279,5 +287,40 @@ private String getValueFormPath(Object objectNode, List pathList) { return null; } + + private Map>, LinkedList>> mergeExpression( + Map, LinkedList>> baseExpression, + Map, LinkedList>> testExpression) { + + Map>, LinkedList>> result = new HashMap<>(); + + Set> baseExpressionKeySet = baseExpression.keySet(); + Set> testExpresssionKeyset = testExpression.keySet(); + + // get the intersection of the two sets + Set> temp = new HashSet<>(baseExpressionKeySet); + temp.retainAll(testExpresssionKeyset); + + for (Entry, LinkedList>> entry : baseExpression.entrySet()) { + List key = entry.getKey(); + LinkedList> baseValue = entry.getValue(); + if (temp.contains(key)) { + LinkedList> testValue = testExpression.get(key); + result.put(baseValue, testValue); + } else { + result.put(baseValue, null); + } + } + + for (Entry, LinkedList>> entry : testExpression.entrySet()) { + List key = entry.getKey(); + LinkedList> testValue = entry.getValue(); + if (!temp.contains(key)) { + result.put(null, testValue); + } + } + return result; + } + } diff --git a/arex-compare-core/src/main/java/com/arextest/diff/model/RulesConfig.java b/arex-compare-core/src/main/java/com/arextest/diff/model/RulesConfig.java index f3075eb..fe47bf0 100644 --- a/arex-compare-core/src/main/java/com/arextest/diff/model/RulesConfig.java +++ b/arex-compare-core/src/main/java/com/arextest/diff/model/RulesConfig.java @@ -6,9 +6,11 @@ import com.arextest.diff.model.key.ReferenceEntity; import com.arextest.diff.model.pathparse.ExpressionNodeEntity; import java.util.Collections; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; +import org.apache.commons.lang3.tuple.Pair; public class RulesConfig { @@ -60,6 +62,8 @@ public class RulesConfig { //region: inner processed class private List> expressionExclusions; + private Map>, LinkedList>> conditionExclusions; + //endregion public RulesConfig() { @@ -231,8 +235,16 @@ public List> getExpressionExclusions() { return expressionExclusions; } - public void setExpressionExclusions( - List> expressionExclusions) { + public void setExpressionExclusions(List> expressionExclusions) { this.expressionExclusions = expressionExclusions; } + + public Map>, LinkedList>> getConditionExclusions() { + return conditionExclusions; + } + + public void setConditionExclusions( + Map>, LinkedList>> conditionExclusions) { + this.conditionExclusions = conditionExclusions; + } } diff --git a/arex-compare-core/src/main/java/com/arextest/diff/model/compare/CompareContext.java b/arex-compare-core/src/main/java/com/arextest/diff/model/compare/CompareContext.java index 64e84a8..1d26f30 100644 --- a/arex-compare-core/src/main/java/com/arextest/diff/model/compare/CompareContext.java +++ b/arex-compare-core/src/main/java/com/arextest/diff/model/compare/CompareContext.java @@ -9,6 +9,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; @@ -24,7 +25,7 @@ public class CompareContext { public Object currentTestObj; public List> exclusions; - public List> expressionExclusions; + public Map>, LinkedList>> conditionExclusions; public Set ignoreNodeSet; public boolean notDistinguishNullAndEmpty = false; public boolean nullEqualsNotExist = false; @@ -62,6 +63,9 @@ public class CompareContext { public byte ignoreReferenceNotFound = 0; + // 0:object 1:array + public int parentNodeType; + public CompareContext() { this.currentTraceLeft = new ArrayList<>(); this.currentTraceRight = new ArrayList<>(); diff --git a/arex-compare-core/src/main/java/com/arextest/diff/model/enumeration/ParentNodeType.java b/arex-compare-core/src/main/java/com/arextest/diff/model/enumeration/ParentNodeType.java new file mode 100644 index 0000000..4a04422 --- /dev/null +++ b/arex-compare-core/src/main/java/com/arextest/diff/model/enumeration/ParentNodeType.java @@ -0,0 +1,8 @@ +package com.arextest.diff.model.enumeration; + +public interface ParentNodeType { + + int OBJECT = 1; + int ARRAY = 2; + +} diff --git a/arex-compare-core/src/main/java/com/arextest/diff/utils/ArexStringEqualsUtil.java b/arex-compare-core/src/main/java/com/arextest/diff/utils/ArexStringEqualsUtil.java index a64bdff..b39be80 100644 --- a/arex-compare-core/src/main/java/com/arextest/diff/utils/ArexStringEqualsUtil.java +++ b/arex-compare-core/src/main/java/com/arextest/diff/utils/ArexStringEqualsUtil.java @@ -40,29 +40,21 @@ public static boolean isEqualsWithoutPrefix(String baseStr, String testStr) { int baseIndex = 0; int testIndex = 0; - if (baseValueLen == 0) { - while (testIndex < testValueLen) { - if (isPrefix(testIndex, testStr)) { - testIndex = testIndex + 5; - } else { - return false; - } + while (testIndex < testValueLen) { + if (isPrefix(testIndex, testStr)) { + testIndex = testIndex + 5; + continue; } - return true; - } else { - while (baseIndex < baseValueLen && testIndex < testValueLen) { - if (isPrefix(testIndex, testStr)) { - testIndex = testIndex + 5; - continue; - } - if (baseStr.charAt(baseIndex) != testStr.charAt(testIndex)) { - return false; - } - baseIndex++; - testIndex++; + if (baseIndex >= baseValueLen) { + return false; } - return true; + if (baseStr.charAt(baseIndex) != testStr.charAt(testIndex)) { + return false; + } + baseIndex++; + testIndex++; } + return true; } private static boolean isPrefix(int testIndex, String testValue) { diff --git a/arex-compare-core/src/main/java/com/arextest/diff/utils/ExpressionNodeParser.java b/arex-compare-core/src/main/java/com/arextest/diff/utils/ExpressionNodeParser.java index a28affa..3d5b5a7 100644 --- a/arex-compare-core/src/main/java/com/arextest/diff/utils/ExpressionNodeParser.java +++ b/arex-compare-core/src/main/java/com/arextest/diff/utils/ExpressionNodeParser.java @@ -69,7 +69,11 @@ private static MutablePair, Boolean> registerExpressi String substring = item.substring(1, item.length() - 1); String[] split = substring.split(EQUAL_SYMBOL, 2); if (split.length != 2) { - result.add(new ExpressionNodeEntity(split[0], ExpressionNodeType.INDEX_NODE)); + Integer index = toInteger(split[0]); + if (index == null) { + return null; + } + result.add(new ExpressionNodeEntity(index, ExpressionNodeType.INDEX_NODE)); } else { String path = split[0]; List stringPath = new ArrayList<>( @@ -86,4 +90,12 @@ private static MutablePair, Boolean> registerExpressi return new MutablePair<>(result, isExpression); } + private static Integer toInteger(String str) { + try { + return Integer.parseInt(str); + } catch (NumberFormatException e) { + return null; + } + } + } diff --git a/arex-compare-core/src/main/java/com/arextest/diff/utils/IgnoreUtil.java b/arex-compare-core/src/main/java/com/arextest/diff/utils/IgnoreUtil.java index ad0f222..72011fc 100644 --- a/arex-compare-core/src/main/java/com/arextest/diff/utils/IgnoreUtil.java +++ b/arex-compare-core/src/main/java/com/arextest/diff/utils/IgnoreUtil.java @@ -4,7 +4,9 @@ import com.arextest.diff.model.log.NodeEntity; import com.arextest.diff.model.pathparse.ExpressionNodeEntity; import com.arextest.diff.model.pathparse.ExpressionNodeType; +import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.Set; @@ -13,8 +15,37 @@ */ public class IgnoreUtil { + public static boolean ignoreProcessor(List fuzzyNodePath, + List baseNodePath, List testNodePath, + List> exclusions, + Map>, LinkedList>> conditionExclusions, + Set ignoreNodeSet) { + if (ignoreNodeProcessor(fuzzyNodePath, ignoreNodeSet)) { + return true; + } + + if (exclusions != null && !exclusions.isEmpty()) { + for (List ignoreNodePath : exclusions) { + if (ignoreMatch(fuzzyNodePath, ignoreNodePath)) { + return true; + } + } + } - public static boolean ignoreProcessor(List fuzzyNodePath, List nodePath, + if (conditionExclusions != null && !conditionExclusions.isEmpty()) { + for (Map.Entry>, LinkedList>> entry : conditionExclusions.entrySet()) { + LinkedList> key = entry.getKey(); + LinkedList> value = entry.getValue(); + if (multiIgnoreExpressionMatch(baseNodePath, key) && multiIgnoreExpressionMatch( + testNodePath, value)) { + return true; + } + } + } + return false; + } + + public static boolean ignoreProcessorEngine(List fuzzyNodePath, List nodePath, List> exclusions, List> expressionExclusions, Set ignoreNodeSet) { @@ -77,6 +108,23 @@ private static boolean ignoreNodeProcessor(List nodePath, Set ig return false; } + private static boolean multiIgnoreExpressionMatch(List nodePath, + LinkedList> expressionNodeEntityListNodePath) { + + boolean result = false; + if (expressionNodeEntityListNodePath == null) { + result = true; + } else { + for (LinkedList expressionNodeEntityList : expressionNodeEntityListNodePath) { + if (ignoreExpressionMatch(nodePath, expressionNodeEntityList)) { + result = true; + break; + } + } + } + return result; + } + private static boolean ignoreExpressionMatch(List nodePath, List expressionNodeEntityListNodePath) { diff --git a/arex-compare-core/src/test/java/com/arextest/diff/handler/parse/sqlparse/SelectExampleTest.java b/arex-compare-core/src/test/java/com/arextest/diff/handler/parse/sqlparse/SelectExampleTest.java index b251880..ceac0d8 100644 --- a/arex-compare-core/src/test/java/com/arextest/diff/handler/parse/sqlparse/SelectExampleTest.java +++ b/arex-compare-core/src/test/java/com/arextest/diff/handler/parse/sqlparse/SelectExampleTest.java @@ -383,4 +383,11 @@ public void testSelect19() throws JSQLParserException { Statement parse = CCJSqlParserUtil.parse(sql); System.out.println(); } + + @Test + public void testSelectList() throws JSQLParserException { + String sql = " SELECT st_id from students union select st_id from student_skill;"; + Statement statement = CCJSqlParserUtil.parse(sql); + testSqlParse(statement); + } } 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 48b7ca0..678d9b5 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 @@ -649,5 +649,99 @@ public static String readStringFromFile(String filePath) throws IOException { return sb.toString(); } + @Test + public void testArexPrefixFilter() { + CompareSDK sdk = new CompareSDK(); + CompareOptions compareOptions = CompareOptions.options(); + String baseMsg = "{\n" + + " \"time\": \"2024-10-18 14:32:26\"\n" + + "}"; + String testMsg = "{\n" + + " \"time\": \"2024-10-18 14:32:26xxxxx\"\n" + + "}"; + CompareResult result = sdk.compare(baseMsg, testMsg, compareOptions); + Assertions.assertEquals(1, result.getCode()); + } + + + @Test + public void testArrayListMissingExpressMissing() { + CompareSDK sdk = new CompareSDK(); + String baseMsg = "{\n" + + " \"students\": [\n" + + " {\n" + + " \"key\": \"xiaoming\",\n" + + " \"value\": \"18\"\n" + + " },\n" + + " {\n" + + " \"key\": \"xiaomei\",\n" + + " \"value\": \"19\"\n" + + " }\n" + + " ]\n" + + "}"; + String testMsg = "{\n" + + " \"students\": [\n" + + " {\n" + + " \"key\": \"xiaomei\",\n" + + " \"value\": \"18\"\n" + + " }\n" + + " ]\n" + + "}"; + + CompareOptions compareOptions = CompareOptions.options() + .putListSortConfig(Arrays.asList("students"), Arrays.asList(Arrays.asList("key"))) + .putExclusions(Arrays.asList("students", "[key=xiaoming]")) +// .putExclusions(Arrays.asList("students","[index=0]")); + ; + CompareResult result = sdk.compare(baseMsg, testMsg, compareOptions); + Assertions.assertEquals(1, result.getLogs().size()); + } + + + @Test + public void testConditionIgnore() { + CompareSDK sdk = new CompareSDK(); + String baseMsg = "{\n" + + " \"students\": [\n" + + " {\n" + + " \"key\": \"xiaoming\",\n" + + " \"value\": \"18\"\n" + + " },\n" + + " {\n" + + " \"key\": \"xiaomei\",\n" + + " \"value\": \"19\"\n" + + " }\n" + + " ]\n" + + "}"; + String testMsg = "{\n" + + " \"students\": [\n" + + " {\n" + + " \"key\": \"xiaomei\",\n" + + " \"value\": \"18\"\n" + + " }\n" + + " ]\n" + + "}"; + + CompareOptions compareOptions = CompareOptions.options() + .putListSortConfig(Arrays.asList("students"), Arrays.asList(Arrays.asList("key"))) + .putExclusions(Arrays.asList("students", "[key=xiaoming]")); + CompareResult result = sdk.compare(baseMsg, testMsg, compareOptions); + Assertions.assertEquals(1, result.getLogs().size()); + } + + @Test + public void testArexPrefix() { + CompareSDK compareSDK = new CompareSDK(); + String baseMsg = "{\n" + + " \"time\": \"2024-10-18 14:32:26\"\n" + + "}"; + + String testMsg = "{\n" + + " \"time\": \"2024-10-18 14:32:26xxxxx\"\n" + + "}"; + CompareResult result = compareSDK.compare(baseMsg, testMsg); + Assertions.assertEquals(1, result.getCode()); + } + } diff --git a/arex-compare-core/src/test/java/com/arextest/diff/sdk/CompareSDKTest.java b/arex-compare-core/src/test/java/com/arextest/diff/sdk/CompareSDKTest.java index a4464ed..03793e1 100644 --- a/arex-compare-core/src/test/java/com/arextest/diff/sdk/CompareSDKTest.java +++ b/arex-compare-core/src/test/java/com/arextest/diff/sdk/CompareSDKTest.java @@ -891,4 +891,29 @@ public void testOutPutErrorValueType() { Assertions.assertEquals("18", compare.getLogs().get(0).getBaseValue()); } + @Test + public void testMultiCompare() { + String baseMsg = "{\n" + + " \"parameters\": [\n" + + " {}\n" + + " ],\n" + + " \"body\": \"select * from table where ID = 1;select * from table where ID = 2;\",\n" + + " \"dbname\": \"db\"\n" + + "}"; + String testMsg = "{\n" + + " \"parameters\": [\n" + + " {}\n" + + " ],\n" + + " \"body\": \"select * from table where ID = 2;select * from table where ID = 1;\",\n" + + " \"dbname\": \"db\"\n" + + "}"; + CompareSDK sdk = new CompareSDK(); + CompareOptions compareOptions = CompareOptions.options() + .putCategoryType(CategoryType.DATABASE) + .putSelectIgnoreCompare(true); + + CompareResult compare = sdk.compare(baseMsg, testMsg, compareOptions); + Assertions.assertEquals(0, compare.getCode()); + } + } \ No newline at end of file diff --git a/pom.xml b/pom.xml index 73e3cfe..07ae0cf 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.arextest arex-compare-parent pom - 0.2.16 + 0.2.17 arex-compare-extension arex-compare-core