Skip to content

Commit

Permalink
optimize code structure and add errType to segment errors (#7)
Browse files Browse the repository at this point in the history
optimize code structure and add errType to segment errors
  • Loading branch information
coryhh authored Aug 3, 2022
1 parent d112cdd commit 82ee2bc
Show file tree
Hide file tree
Showing 35 changed files with 1,577 additions and 1,140 deletions.
121 changes: 85 additions & 36 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,51 @@
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<log4j2.version>2.17.1</log4j2.version>
</properties>

<name>${project.groupId}:${project.artifactId}</name>
<description>Arex-compare-sdk is a Java library that can be used to compare two JSON messages.</description>
<url>https://github.com/arextest/arex-compare-sdk</url>

<licenses>
<license>
<name>The Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>

<developers>
<developer>
<name>AREX</name>
<email>arex.test.com@gmail.com</email>
<organization>AREX group</organization>
<organizationUrl>https://www.arextest.com</organizationUrl>
</developer>
</developers>

<scm>
<connection>scm:git:git://github.com/arextest/arex-compare-sdk.git</connection>
<developerConnection>scm:git:ssh://github.com:arextest/arex-compare-sdk.git</developerConnection>
<url>https://github.com/arextest/arex-compare-sdk</url>
</scm>

<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>org.json</artifactId>
<version>2.0</version>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
Expand Down Expand Up @@ -48,7 +84,7 @@
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>org.json</artifactId>
<artifactId>json</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand All @@ -60,16 +96,6 @@
</dependency>
</dependencies>

<!-- <distributionManagement>-->
<!-- <repository>-->
<!-- <id>releases</id>-->
<!-- <url>${releases.repo}</url>-->
<!-- </repository>-->
<!-- <snapshotRepository>-->
<!-- <id>snapshots</id>-->
<!-- <url>${snapshots.repo}</url>-->
<!-- </snapshotRepository>-->
<!-- </distributionManagement>-->

<build>
<plugins>
Expand All @@ -83,39 +109,62 @@
</configuration>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<configuration>
<altDeploymentRepository>
maven.repo::default::file://${project.build.directory}/mvn-repo
</altDeploymentRepository>
</configuration>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>generate-source-jar</id>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.github</groupId>
<artifactId>site-maven-plugin</artifactId>
<version>0.12</version>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.4.0</version>
<configuration>
<server>github</server>
<message>Maven artifacts for ${project.artifactId}-${project.version}</message>
<outputDirectory>${project.build.directory}/mvn-repo</outputDirectory>
<branch>refs/heads/main</branch>
<merge>true</merge>
<includes>
<include>**/*</include>
</includes>
<repositoryName>arex-jar-respository</repositoryName>
<repositoryOwner>arextest</repositoryOwner>
<doclint>none</doclint>
<source>8</source>
<detectJavaApiLink>false</detectJavaApiLink>
</configuration>
<executions>
<execution>
<id>generate-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>site</goal>
<goal>sign</goal>
</goals>
<phase>deploy</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.13</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
129 changes: 129 additions & 0 deletions src/main/java/com/arextest/diff/compare/ArrayCompare.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package com.arextest.diff.compare;

import com.arextest.diff.compare.feature.IndexSelector;
import com.arextest.diff.compare.feature.IndexSelectorFactory;
import com.arextest.diff.handler.log.LogMarker;
import com.arextest.diff.handler.log.LogRegister;
import com.arextest.diff.model.compare.IndexPair;
import com.arextest.diff.model.log.NodeEntity;
import com.arextest.diff.utils.ListUti;
import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* Created by rchen9 on 2022/7/25.
*/
public class ArrayCompare {

public static void arrayCompare(Object obj1, Object obj2, CompareContext compareContext) throws JSONException {

JSONArray obj1Array = (JSONArray) obj1;
JSONArray obj2Array = (JSONArray) obj2;

if (obj1Array.length() != obj2Array.length()) {
LogRegister.register(obj1Array.length(), obj2Array.length(), LogMarker.DIFF_ARRAY_COUNT, compareContext);
}

List<Integer> leftComparedIndexes = new ArrayList<>();
List<Integer> rightComparedIndexes = new ArrayList<>();


// decide to use which indexSelector
IndexSelector indexSelector = IndexSelectorFactory.getIndexSelector(compareContext.currentNodeLeft, compareContext.currentNodeRight, compareContext);

String currentListPath = ListUti.convertPathToStringForShow(compareContext.currentNodeLeft);

for (int i = 0; i < obj1Array.length(); i++) {
leftComparedIndexes.add(i);

compareContext.currentNodeLeft.add(new NodeEntity(null, i));
compareContext.currentListKeysLeft.add(currentListPath);
compareContext.currentListKeysLeft.add(indexSelector.judgeLeftIndexStandard(i));

int correspondRightIndex = indexSelector.findCorrespondRightIndex(i, rightComparedIndexes, obj1Array, obj2Array);

Object element1 = obj1Array.get(i);
Object element2 = null;
boolean rightExist = false;

if (correspondRightIndex == -1) {
LogRegister.register(element1, element2, LogMarker.RIGHT_ARRAY_MISSING_KEY, compareContext);
} else {
element2 = obj2Array.get(correspondRightIndex);
rightExist = true;
rightComparedIndexes.add(correspondRightIndex);

compareContext.currentNodeRight.add(new NodeEntity(null, correspondRightIndex));
compareContext.currentListKeysRight.add(currentListPath);
compareContext.currentListKeysRight.add(indexSelector.judgeRightIndexStandard(correspondRightIndex));

boolean needCompare = !isComparedByRefer(i, correspondRightIndex, compareContext.pkListIndexPair, compareContext.currentNodeLeft);
if (needCompare && element1 != null && element2 != null) {
GenericCompare.jsonCompare(element1, element2, compareContext);
}
}
ListUti.removeLast(compareContext.currentListKeysLeft);
ListUti.removeLast(compareContext.currentListKeysLeft);
ListUti.removeLast(compareContext.currentNodeLeft);
if (rightExist) {
ListUti.removeLast(compareContext.currentListKeysRight);
ListUti.removeLast(compareContext.currentListKeysRight);
ListUti.removeLast(compareContext.currentNodeRight);
}
}

for (int i = 0; i < obj2Array.length(); i++) {
if (rightComparedIndexes.contains(i)) {
continue;
}
compareContext.currentNodeRight.add(new NodeEntity(null, i));
compareContext.currentListKeysRight.add(currentListPath);
compareContext.currentListKeysRight.add(indexSelector.judgeRightIndexStandard(i));

int correspondLeftIndex = indexSelector.findCorrespondLeftIndex(i, leftComparedIndexes, obj1Array, obj2Array);

Object element1 = null;
Object element2 = obj2Array.get(i);
boolean leftExist = false;
if (correspondLeftIndex == -1) {
LogRegister.register(element1, element2, LogMarker.LEFT_ARRAY_MISSING_KEY, compareContext);
} else {
element1 = obj1Array.get(correspondLeftIndex);
leftExist = true;
compareContext.currentListKeysLeft.add(currentListPath);
compareContext.currentListKeysLeft.add(indexSelector.judgeLeftIndexStandard(correspondLeftIndex));
compareContext.currentNodeLeft.add(new NodeEntity(null, correspondLeftIndex));

boolean needCompare = !isComparedByRefer(correspondLeftIndex, i, compareContext.pkListIndexPair, compareContext.currentNodeRight);
if (needCompare && element1 != null && element2 != null) {
GenericCompare.jsonCompare(element1, element2, compareContext);
}
}

if (leftExist) {
ListUti.removeLast(compareContext.currentListKeysLeft);
ListUti.removeLast(compareContext.currentListKeysLeft);
ListUti.removeLast(compareContext.currentNodeLeft);
}
ListUti.removeLast(compareContext.currentListKeysRight);
ListUti.removeLast(compareContext.currentListKeysRight);
ListUti.removeLast(compareContext.currentNodeRight);
}
}

private static boolean isComparedByRefer(int leftIndex, int rightIndex, Map<List<String>, List<IndexPair>> pkListIndexPair, List<NodeEntity> currentListNode) {
boolean result = false;
IndexPair indexPair = new IndexPair(leftIndex, rightIndex);
List<IndexPair> comparedIndexPairs = pkListIndexPair.get(ListUti.convertToStringList(currentListNode));
if (comparedIndexPairs != null && comparedIndexPairs.contains(indexPair)) {
result = true;
} else if (comparedIndexPairs != null) {
comparedIndexPairs.add(indexPair);
}
return result;
}
}
Loading

0 comments on commit 82ee2bc

Please sign in to comment.