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

fix: the compare of "string" supports the prefix of "arex_" #48

Merged
merged 2 commits into from
Jan 23, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,22 @@

import com.arextest.diff.model.enumeration.UnmatchedType;
import com.arextest.diff.model.log.LogEntity;
import java.util.HashSet;
import java.util.Set;
import com.arextest.diff.utils.ArexStringEqualsUtil;
import java.util.function.Predicate;

/**
* Created by rchen9 on 2023/4/13.
*/
public class ArexPrefixFilter implements Predicate<LogEntity> {

private static final Set<String> PREFIX_SET = new HashSet<String>() {
{
add("arex.");
add("arex_");
add("_arex");
add("AREX.");
add("AREX_");
add("_AREX");
}
};

public static boolean isEqualsWithoutPrefix(String baseStr, String testStr) {

int baseValueLen = baseStr.length();
int testValueLen = testStr.length();
if (testValueLen - baseValueLen <= 0 || (testValueLen - baseValueLen) % 5 != 0) {
return false;
}

int baseIndex = 0;
int testIndex = 0;

if (baseValueLen == 0) {
while (testIndex < testValueLen) {
if (isPrefix(testIndex, testStr)) {
testIndex = testIndex + 5;
} else {
return false;
}
}
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++;
}
return true;
}
}

private static boolean isPrefix(int testIndex, String testValue) {
if (testIndex > testValue.length() - 5) {
return false;
}

if (testValue.charAt(testIndex) != 'a' && testValue.charAt(testIndex) != '_') {
return false;
}

String substring = testValue.substring(testIndex, testIndex + 5);
return PREFIX_SET.contains(substring);
}

@Override
public boolean test(LogEntity logEntity) {
int unmatchedType = logEntity.getPathPair().getUnmatchedType();
if (unmatchedType == UnmatchedType.UNMATCHED) {
Object baseValue = logEntity.getBaseValue();
Object testValue = logEntity.getTestValue();
if (baseValue != null && testValue != null) {
return !isEqualsWithoutPrefix((String) baseValue, (String) testValue);
return !ArexStringEqualsUtil.isEqualsWithoutPrefix((String) baseValue, (String) testValue);
}
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import com.arextest.diff.model.enumeration.UnmatchedType;
import com.arextest.diff.model.log.LogEntity;
import com.arextest.diff.model.log.UnmatchedPairEntity;
import com.arextest.diff.utils.ArexStringEqualsUtil;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Future;

public class CompareBuilder {
Expand Down Expand Up @@ -94,7 +94,7 @@ public CompareBuilder noDiff(String baseMsg, String testMsg) {
public CompareBuilder addEqualsCompare(Object baseMsg, Object testMsg, RulesConfig rulesConfig) {
String baseMsgStr = baseMsg == null ? null : baseMsg.toString();
String testMsgStr = testMsg == null ? null : testMsg.toString();
boolean equals = Objects.equals(baseMsgStr, testMsgStr);
boolean equals = ArexStringEqualsUtil.stringEquals(baseMsgStr, testMsgStr);
this.code(equals ? DiffResultCode.COMPARED_WITHOUT_DIFFERENCE
: DiffResultCode.COMPARED_WITH_DIFFERENCE);
this.message("compare successfully");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.arextest.diff.utils;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public class ArexStringEqualsUtil {

private static final Set<String> PREFIX_SET = new HashSet<String>() {
{
add("arex.");
add("arex_");
add("_arex");
add("AREX.");
add("AREX_");
add("_AREX");
}
};

/**
* The agent will add a prefix to the email or URL.
* compare two string if the string is not equal, then compare without prefix
* @param baseStr
* @param testStr
* @return
*/
public static boolean stringEquals(String baseStr, String testStr) {
boolean result = Objects.equals(baseStr, testStr);
if (!result) {
result = isEqualsWithoutPrefix(baseStr, testStr);
}
return result;
}

public static boolean isEqualsWithoutPrefix(String baseStr, String testStr) {

if (baseStr == null && testStr == null) {
return true;
}

if (baseStr == null || testStr == null) {
return false;
}

int baseValueLen = baseStr.length();
int testValueLen = testStr.length();
if (testValueLen - baseValueLen <= 0 || (testValueLen - baseValueLen) % 5 != 0) {
return false;
}

int baseIndex = 0;
int testIndex = 0;

if (baseValueLen == 0) {
while (testIndex < testValueLen) {
if (isPrefix(testIndex, testStr)) {
testIndex = testIndex + 5;
} else {
return false;
}
}
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++;
}
return true;
}
}

private static boolean isPrefix(int testIndex, String testValue) {
if (testIndex > testValue.length() - 5) {
return false;
}

if (testValue.charAt(testIndex) != 'a' && testValue.charAt(testIndex) != '_') {
return false;
}

String substring = testValue.substring(testIndex, testIndex + 5);
return PREFIX_SET.contains(substring);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ public void testRootDecompress3() {
Assertions.assertEquals(1, result.getCode());

CompareResult quickResult = sdk.quickCompare(baseMsg, testMsg, compareOptions);
Assertions.assertEquals("H4sIAAAAAAAAAKtWSlSyAuJaAMXisGkJAAAA", quickResult.getProcessedTestMsg());
Assertions.assertEquals("H4sIAAAAAAAAAKtWSlSyAuJaAMXisGkJAAAA",
quickResult.getProcessedTestMsg());

}

Expand Down Expand Up @@ -592,7 +593,24 @@ public void testListSortNullPointer() {
+ "}";
CompareResult result = sdk.compare(baseMsg, testMsg, compareOptions);
Assertions.assertEquals(1, result.getLogs().size());
}

@Test
public void testStringEqualsWithArexPrefix() {
CompareSDK sdk = new CompareSDK();
sdk.getGlobalOptions()
.putNameToLower(true)
.putNullEqualsEmpty(true)
.putPluginJarUrl("./lib/arex-compare-sdk-plugin-0.1.0-jar-with-dependencies.jar");

CompareOptions compareOptions = CompareOptions.options()
.putDecompressConfig(new DecompressConfig("Base64",
Arrays.asList(Arrays.asList("arex_root"))));

String baseMsg = "aHR0cHM6Ly9iYWlkdS5jb20=";
String testMsg = "aHR0cHM6Ly9hcmV4LmJhaWR1LmNvbQ==";
CompareResult result = sdk.compare(baseMsg, testMsg, compareOptions);
Assertions.assertEquals(0, result.getCode());
}


Expand Down