Coder Social home page Coder Social logo

vincemann / ez-compare Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 797 KB

Simple refelctionbased fluent-api-style comparisson tool for dtos and other objects

License: Apache License 2.0

Java 100.00%
java compare-objects test diff fluent-api dto entity spring

ez-compare's Introduction

Ez-Compare

Simple refelction-based Comparisson-Tool for comparing two Objects: "Root" & "Compare".
Highly configurable, yet very simple to use.
Human readable fluent-api-style.
Does not perform deep compare (except for arrays),
but instead iterates over the selected instance variables of both objects and compares them via equal-method.
-> reflection depth is 1.

Features

  • Can Compare Objects of different Types
  • Only uses Root's Properties/ Fields for Comparisson
  • Can ignore missing Fields of Compare
  • Evaluates and returns Differences
  • Can be configured to stop after finding first Difference (Performence-Mode)
  • Full-Compare-Mode (use all Properties of Root and ignore some if needed)
  • Partial-Compare-Mode (explicitly include Properties of Root and only compare theses)
  • Globally & Locally configure Compare-Options
  • ... see Java Docs for more...

Include

Maven

<repositories>  
    <repository>  
        <id>jitpack.io</id>  
        <url>https://jitpack.io</url>  
    </repository>  
</repositories>  
  
<dependency>  
    <groupId>com.github.vincemann</groupId>  
    <artifactId>ez-compare</artifactId>  
    <version>1.0.0-SNAPSHOT.3</version>  
</dependency>  

Simple Example

  @Test
  public void fullCompareEasy(){
      // using default config aka strict mode 
      Person p = Person.builder()
              .name("same")
              .address("same")
              .age(42)
              .tel(42L)
              .creatorIp("8.8.8.8")
              .build();

      PersonDTO dto = PersonDTO.builder()
              .age(42)
              .tel(42L)
              .address("same")
              .name("same")
              .build();

      // normal way
      Assertions.assertEquals(dto.age,p.age);
      Assertions.assertEquals(dto.address,p.address);
      Assertions.assertEquals(dto.tel,p.tel);
      Assertions.assertEquals(dto.name,p.name);

      //ez compare way
      compare(dto).with(p)
              //by default uses only properties of left side (=root) object
              .properties()
              .all()
              .assertEqual();
  }

Advanced Example

    @Test
    public void fullCompareEasy_getDiff(){
        Person p = Person.builder()
                .name("unimportant")
                .address("veryDiff")
                .age(42)
                .tel(null)
                .creatorIp("unimportant")
                .build();

        PersonDTO dto = PersonDTO.builder()
                .age(42)
                .tel(42L)
                //address and name is irrelevant, all other properties must be equal
                .address("diff")
                .name(null)
                .build();

        //only properties that are not null and present in both compare objects are relevant
        RapidEqualsBuilder.Diff diff = compare(p).with(dto)
                .ignoreNotFound(true)       //creatorIp will be ignored
                .ignoreNull(true)           //tel       will be ignored
                .properties()
                .all()
                //we dont care about name neither
                .ignore(p::getName)
                .assertNotEqual()
                .getDiff();

        //just to showcase it worked the way it is expected
        Assertions.assertEquals(1,diff.getDiffNodes().size());
        Assertions.assertEquals(propertyNameOf(p::getAddress),diff.getFirstNode().getProperty());
    }

Check out more showcases in src/test/ShowCaseTests

ez-compare's People

Contributors

vincemann avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

ez-compare's Issues

offer converters for comparisson

i.E.:
compare(readModuleDto).with(savedModule)
.properties()
.all()
useConverter<Long,IdentifiableEntity>((a,b) -> {
if(b.contains("id")){
return a.equals(b.getId().toString())
})
.assertEqual();

will compare longs with propertynames containing "id" to respective entity.getId()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.