From 9da93237fb7e2e7c288efce4c4e93dfa4e6a4b19 Mon Sep 17 00:00:00 2001 From: astrapi69 Date: Fri, 19 Apr 2019 12:07:57 +0200 Subject: [PATCH 1/2] initial version --- .coveralls.yml | 22 ++ .github/CODE_OF_CONDUCT.md | 84 +++++ .github/CONTRIBUTING.md | 3 + .github/PULL_REQUEST_TEMPLATE.md | 7 + .gitignore | 90 ++++- .travis.yml | 18 + CHANGELOG.md | 16 + README.md | 124 ++++++- pom.xml | 142 ++++++++ src/launch/jobj-differ clean deploy.launch | 16 + src/launch/jobj-differ clean install.launch | 16 + ...clean eclipse eclipse clean install.launch | 16 + src/launch/jobj-differ javadoc.launch | 16 + .../jobj-differ update-license-header.launch | 16 + ...versions display-dependency-updates.launch | 16 + .../diff/beans/ChangedAttributeResult.java | 56 +++ .../diff/beans/GenericChangedAttribute.java | 57 +++ .../SerializedChangedAttributeResult.java | 81 +++++ .../de/alpharogroup/diff/beans/package.html | 11 + .../diff/object/DiffObjectExtensions.java | 201 +++++++++++ .../de/alpharogroup/diff/object/package.html | 11 + .../beans/ChangedAttributeResultTest.java | 103 ++++++ .../beans/GenericChangedAttributeTest.java | 96 +++++ .../SerializedChangedAttributeResultTest.java | 136 +++++++ .../diff/object/DiffObjectExtensionsTest.java | 334 ++++++++++++++++++ 25 files changed, 1673 insertions(+), 15 deletions(-) create mode 100644 .coveralls.yml create mode 100644 .github/CODE_OF_CONDUCT.md create mode 100644 .github/CONTRIBUTING.md create mode 100644 .github/PULL_REQUEST_TEMPLATE.md create mode 100644 .travis.yml create mode 100644 CHANGELOG.md create mode 100644 pom.xml create mode 100644 src/launch/jobj-differ clean deploy.launch create mode 100644 src/launch/jobj-differ clean install.launch create mode 100644 src/launch/jobj-differ eclipse clean eclipse eclipse clean install.launch create mode 100644 src/launch/jobj-differ javadoc.launch create mode 100644 src/launch/jobj-differ update-license-header.launch create mode 100644 src/launch/jobj-differ versions display-dependency-updates.launch create mode 100644 src/main/java/de/alpharogroup/diff/beans/ChangedAttributeResult.java create mode 100644 src/main/java/de/alpharogroup/diff/beans/GenericChangedAttribute.java create mode 100644 src/main/java/de/alpharogroup/diff/beans/SerializedChangedAttributeResult.java create mode 100644 src/main/java/de/alpharogroup/diff/beans/package.html create mode 100644 src/main/java/de/alpharogroup/diff/object/DiffObjectExtensions.java create mode 100644 src/main/java/de/alpharogroup/diff/object/package.html create mode 100644 src/test/java/de/alpharogroup/diff/beans/ChangedAttributeResultTest.java create mode 100644 src/test/java/de/alpharogroup/diff/beans/GenericChangedAttributeTest.java create mode 100644 src/test/java/de/alpharogroup/diff/beans/SerializedChangedAttributeResultTest.java create mode 100644 src/test/java/de/alpharogroup/diff/object/DiffObjectExtensionsTest.java diff --git a/.coveralls.yml b/.coveralls.yml new file mode 100644 index 0000000..11657be --- /dev/null +++ b/.coveralls.yml @@ -0,0 +1,22 @@ +# +# The MIT License +# +# Copyright (C) 2015 Asterios Raptis +# +# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and +# associated documentation files (the "Software"), to deal in the Software without restriction, +# including without limitation the rights to use, copy, modify, merge, publish, distribute, +# sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all copies or +# substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT +# NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +# + + diff --git a/.github/CODE_OF_CONDUCT.md b/.github/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..4834460 --- /dev/null +++ b/.github/CODE_OF_CONDUCT.md @@ -0,0 +1,84 @@ +# Code of Conduct + +## 1. Purpose + +A primary goal of jobj-differ is to be inclusive to the largest number of contributors, with the most varied and diverse backgrounds possible. As such, we are committed to providing a friendly, safe and welcoming environment for all, regardless of gender, sexual orientation, ability, ethnicity, socioeconomic status, and religion (or lack thereof). + +This code of conduct outlines our expectations for all those who participate in our community, as well as the consequences for unacceptable behavior. + +We invite all those who participate in jobj-differ to help us create safe and positive experiences for everyone. + +## 2. Open Source Citizenship + +A supplemental goal of this Code of Conduct is to increase open source citizenship by encouraging participants to recognize and strengthen the relationships between our actions and their effects on our community. + +Communities mirror the societies in which they exist and positive action is essential to counteract the many forms of inequality and abuses of power that exist in society. + +If you see someone who is making an extra effort to ensure our community is welcoming, friendly, and encourages all participants to contribute to the fullest extent, we want to know. + +## 3. Expected Behavior + +The following behaviors are expected and requested of all community members: + +* Participate in an authentic and active way. In doing so, you contribute to the health and longevity of this community. +* Exercise consideration and respect in your speech and actions. +* Attempt collaboration before conflict. +* Refrain from demeaning, discriminatory, or harassing behavior and speech. +* Be mindful of your surroundings and of your fellow participants. Alert community leaders if you notice a dangerous situation, someone in distress, or violations of this Code of Conduct, even if they seem inconsequential. +* Remember that community event venues may be shared with members of the public; please be respectful to all patrons of these locations. + +## 4. Unacceptable Behavior + +The following behaviors are considered harassment and are unacceptable within our community: + +* Violence, threats of violence or violent language directed against another person. +* Sexist, racist, homophobic, transphobic, ableist or otherwise discriminatory jokes and language. +* Posting or displaying sexually explicit or violent material. +* Posting or threatening to post other people’s personally identifying information ("doxing"). +* Personal insults, particularly those related to gender, sexual orientation, race, religion, or disability. +* Inappropriate photography or recording. +* Inappropriate physical contact. You should have someone’s consent before touching them. +* Unwelcome sexual attention. This includes, sexualized comments or jokes; inappropriate touching, groping, and unwelcomed sexual advances. +* Deliberate intimidation, stalking or following (online or in person). +* Advocating for, or encouraging, any of the above behavior. +* Sustained disruption of community events, including talks and presentations. + +## 5. Consequences of Unacceptable Behavior + +Unacceptable behavior from any community member, including sponsors and those with decision-making authority, will not be tolerated. + +Anyone asked to stop unacceptable behavior is expected to comply immediately. + +If a community member engages in unacceptable behavior, the community organizers may take any action they deem appropriate, up to and including a temporary ban or permanent expulsion from the community without warning (and without refund in the case of a paid event). + +## 6. Reporting Guidelines + +If you are subject to or witness unacceptable behavior, or have any other concerns, please notify a community organizer as soon as possible. asterios.raptis@web.de. + + + +Additionally, community organizers are available to help community members engage with local law enforcement or to otherwise help those experiencing unacceptable behavior feel safe. In the context of in-person events, organizers will also provide escorts as desired by the person experiencing distress. + +## 7. Addressing Grievances + +If you feel you have been falsely or unfairly accused of violating this Code of Conduct, you should notify astrapi69 with a concise description of your grievance. Your grievance will be handled in accordance with our existing governing policies. + + + +## 8. Scope + +We expect all community participants (contributors, paid or otherwise; sponsors; and other guests) to abide by this Code of Conduct in all community venues–online and in-person–as well as in all one-on-one communications pertaining to community business. + +This code of conduct and its related procedures also applies to unacceptable behavior occurring outside the scope of community activities when such behavior has the potential to adversely affect the safety and well-being of community members. + +## 9. Contact info + +asterios.raptis@web.de + +## 10. License and attribution + +This Code of Conduct is distributed under a [Creative Commons Attribution-ShareAlike license](http://creativecommons.org/licenses/by-sa/3.0/). + +Portions of text derived from the [Django Code of Conduct](https://www.djangoproject.com/conduct/) and the [Geek Feminism Anti-Harassment Policy](http://geekfeminism.wikia.com/wiki/Conference_anti-harassment/Policy). + +Retrieved on November 22, 2016 from [http://citizencodeofconduct.org/](http://citizencodeofconduct.org/) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md new file mode 100644 index 0000000..95219ce --- /dev/null +++ b/.github/CONTRIBUTING.md @@ -0,0 +1,3 @@ +# How to Contribute + +Your are welcome to create pull requests or join in our mailing list for bugfix, doc, example, suggestion or anything else. diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..99a868d --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,7 @@ +Fixes # . + +Changes proposed in this pull request: + +- + +@astrapi69 diff --git a/.gitignore b/.gitignore index a1c2a23..88f9ec2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,23 +1,87 @@ -# Compiled class file +################## +# Compiled files # +################## *.class -# Log file -*.log +################## +# intellij files # +################## +*.iws +*.iml +*.ipr +!.idea/runConfigurations +.idea/inspectionProfiles +.idea/libraries +.idea/codeStyles +.idea/*.* + +################# +# eclipse files # +################# +/.project +/.classpath +/.settings +/.tern-project + +########### +### STS ### +########### +.apt_generated +.factorypath +.springBeans +.sts4-cache -# BlueJ files -*.ctxt -# Mobile Tools for Java (J2ME) -.mtj.tmp/ +################ +### NetBeans ### +################ +/nbproject/private/ +/nbbuild/ +/nbdist/ +/.nb-gradle/ +/build/ -# Package Files # +######################### +# maven generated files # +######################### +/target +/.mvn/ +*.versionsBackup + +############# +# Zip files # +############# +*.tar +*.tar.gz +*.zip +*.7z +*.dmg +*.gz +*.iso *.jar *.war -*.nar *.ear -*.zip -*.tar.gz *.rar -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* +############## +# Logs files # +############## +*.log + +################# +# test-ng files # +################# +/test-output + +############################ +# Binaries generated files # +############################ +/bin + +################ +# gradle files # +################ +/build +/.gradle +/gradle +/pom.xml.bak diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f4efe2c --- /dev/null +++ b/.travis.yml @@ -0,0 +1,18 @@ +# jobj-differ .travis.yml + +language: java + +cache: + directories: + - $HOME/.m2 + +jdk: + - oraclejdk8 + +notifications: + email: + recipients: + - asterios.raptis@web.de + on_failure: always +after_success: + - mvn clean cobertura:cobertura org.eluder.coveralls:coveralls-maven-plugin:report diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..58a6a7b --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,16 @@ +## Change log +---------------------- + +Version 3-SNAPSHOT +------------- + +ADDED: + +- this changelog file +- created PULL_REQUEST_TEMPLATE.md file +- created CODE_OF_CONDUCT.md file +- created CONTRIBUTING.md file +- provide package.html for the javadoc of packages +- moved classes from obsolet jobject-diff project + + diff --git a/README.md b/README.md index 58220de..3fda916 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,122 @@ -# jobj-differ -Resolve differences in java objects +# Overview + +
+ +[![Build Status](https://travis-ci.org/astrapi69/jobj-differ.svg?branch=develop)](https://travis-ci.org/astrapi69/jobj-differ) +[![Coverage Status](https://coveralls.io/repos/github/astrapi69/jobj-differ/badge.svg?branch=develop)](https://coveralls.io/github/astrapi69/jobj-differ?branch=develop) +[![Open Issues](https://img.shields.io/github/issues/astrapi69/jobj-differ.svg?style=flat)](https://github.com/astrapi69/jobj-differ/issues) +[![Maven Central](https://maven-badges.herokuapp.com/maven-central/de.alpharogroup/jobj-differ/badge.svg)](https://maven-badges.herokuapp.com/maven-central/de.alpharogroup/jobj-differ) +[![Javadocs](http://www.javadoc.io/badge/de.alpharogroup/jobj-differ.svg)](http://www.javadoc.io/doc/de.alpharogroup/jobj-differ) +[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](http://opensource.org/licenses/MIT) + +
+ +Utility library for resolving the differences in java objects. + +If you like this project put a ⭐ and donate. + +# Donations + +If you like this library, please consider a donation through paypal: +PayPal this + + +or over bitcoin or bitcoin-cash with: + +36JxRRDfRazLNqUV6NsywCw1q7TK38ukpC + +or over ether with: + +0x588Aa02De98B1Ef70afeDC3ec5290130a3E5e273 + +or over flattr: + + +Flattr this + + +## Note + +No animals were harmed in the making of this library. + +## License + +The source code comes under the liberal MIT License, making jobj-differ great for all types of applications. + +## Maven dependency + +Maven dependency is now on sonatype. +Check out [sonatype repository](https://oss.sonatype.org/index.html#nexus-search;gav~de.alpharogroup~jobj-differ~~~) for latest snapshots and releases. + +Add the following maven dependency to your project `pom.xml` if you want to import the core functionality of jobj-differ: + +Than you can add the dependency to your dependencies: + + + ... + + 3 + ... + + ... + + ... + + + de.alpharogroup + jobj-differ + ${jobj-differ.version} + + ... + + +## Semantic Versioning + +The versions of jobj-differ are maintained with the Simplified Semantic Versioning guidelines. + +Release version numbers will be incremented in the following format: + +`..` + +For detailed information on versioning for this project you can visit this [wiki page](https://github.com/lightblueseas/mvn-parent-projects/wiki/Simplified-Semantic-Versioning). + +## Want to Help and improve it? ### + +The source code for jobj-differ are on GitHub. Please feel free to fork and send pull requests! + +Create your own fork of [astrapi69/jobj-differ/fork](https://github.com/astrapi69/jobj-differ/fork) + +To share your changes, [submit a pull request](https://github.com/astrapi69/jobj-differ/pull/new/develop). + +Don't forget to add new units tests on your changes. + +## Contacting the Developers + +Do not hesitate to contact the jobj-differ developers with your questions, concerns, comments, bug reports, or feature requests. +- Feature requests, questions and bug reports can be reported at the [issues page](https://github.com/astrapi69/jobj-differ/issues). + +## Credits + +|**Travis CI**| +| :---: | +|[![Travis CI](https://travis-ci.com/images/logos/TravisCI-Full-Color.png)](https://coveralls.io/github/astrapi69/jobj-differ?branch=master)| +|Special thanks to [Travis CI](https://travis-ci.org) for providing a free continuous integration service for open source projects| +| | + +|**Nexus Sonatype repositories**| +| :---: | +|[![sonatype repository](https://img.shields.io/nexus/r/https/oss.sonatype.org/de.alpharogroup/jobj-differ.svg?style=for-the-badge)](https://oss.sonatype.org/index.html#nexus-search;gav~de.alpharogroup~jobj-differ~~~)| +|Special thanks to [sonatype repository](https://www.sonatype.com) for providing a free maven repository service for open source projects| +| | + +|**coveralls.io**| +| :---: | +|[![Coverage Status](https://coveralls.io/repos/github/astrapi69/jobj-differ/badge.svg?branch=develop)](https://coveralls.io/github/astrapi69/jobj-differ?branch=master)| +|Special thanks to [coveralls.io](https://coveralls.io) for providing a free code coverage for open source projects| +| | + +|**javadoc.io**| +| :---: | +|[![Javadocs](http://www.javadoc.io/badge/de.alpharogroup/jobj-differ.svg)](http://www.javadoc.io/doc/de.alpharogroup/jobj-differ)| +|Special thanks to [javadoc.io](http://www.javadoc.io) for providing a free javadoc documentation for open source projects| +| | diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..f45b166 --- /dev/null +++ b/pom.xml @@ -0,0 +1,142 @@ + + + 4.0.0 + + + de.alpharogroup + mvn-java-parent + 4.8 + + + jobj-differ + 3-SNAPSHOT + + ${project.artifactId} + + Resolve differences in java objects + + https://github.com/astrapi69/${project.artifactId} + + + + 1.9.3 + + 3.1 + + 2.6.4 + + 5.0.1 + + 3 + + + + + MIT License + http://www.opensource.org/licenses/mit-license.php + repo + + + + + scm:git:git:@github.com:astrapi69/${project.artifactId}.git + scm:git:git@github.com:astrapi69/${project.artifactId}.git + git:@github.com:astrapi69/${project.artifactId}.git + + + + github + https://github.com/astrapi69/${project.artifactId}/issues + + + + travis-ci + https://travis-ci.org/astrapi69/${project.artifactId} + + + + + + + + commons-beanutils + commons-beanutils + + + commons-logging + commons-logging + + + ${commons-beanutils.version} + + + + + de.alpharogroup + test-objects + ${test-objects.version} + + + + + de.alpharogroup + jobject-core + ${jobject-core.version} + + + + de.alpharogroup + jobj-compare + ${jobj-compare.version} + + + + de.alpharogroup + jobj-contract-verifier + ${jobj-contract-verifier.version} + + + + + + + + + + commons-beanutils + commons-beanutils + + + + de.alpharogroup + jobj-compare + + + + de.alpharogroup + jobject-core + + + + de.alpharogroup + test-objects + test + + + + de.alpharogroup + jobj-contract-verifier + test + + + + org.meanbean + meanbean + test + + + + + \ No newline at end of file diff --git a/src/launch/jobj-differ clean deploy.launch b/src/launch/jobj-differ clean deploy.launch new file mode 100644 index 0000000..4ee692f --- /dev/null +++ b/src/launch/jobj-differ clean deploy.launch @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/src/launch/jobj-differ clean install.launch b/src/launch/jobj-differ clean install.launch new file mode 100644 index 0000000..a2a2c1a --- /dev/null +++ b/src/launch/jobj-differ clean install.launch @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/src/launch/jobj-differ eclipse clean eclipse eclipse clean install.launch b/src/launch/jobj-differ eclipse clean eclipse eclipse clean install.launch new file mode 100644 index 0000000..031bfa2 --- /dev/null +++ b/src/launch/jobj-differ eclipse clean eclipse eclipse clean install.launch @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/src/launch/jobj-differ javadoc.launch b/src/launch/jobj-differ javadoc.launch new file mode 100644 index 0000000..f273ed5 --- /dev/null +++ b/src/launch/jobj-differ javadoc.launch @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/src/launch/jobj-differ update-license-header.launch b/src/launch/jobj-differ update-license-header.launch new file mode 100644 index 0000000..592a240 --- /dev/null +++ b/src/launch/jobj-differ update-license-header.launch @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/src/launch/jobj-differ versions display-dependency-updates.launch b/src/launch/jobj-differ versions display-dependency-updates.launch new file mode 100644 index 0000000..f91cb7e --- /dev/null +++ b/src/launch/jobj-differ versions display-dependency-updates.launch @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/src/main/java/de/alpharogroup/diff/beans/ChangedAttributeResult.java b/src/main/java/de/alpharogroup/diff/beans/ChangedAttributeResult.java new file mode 100644 index 0000000..dc2b96d --- /dev/null +++ b/src/main/java/de/alpharogroup/diff/beans/ChangedAttributeResult.java @@ -0,0 +1,56 @@ +/** + * The MIT License + * + * Copyright (C) 2015 Asterios Raptis + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package de.alpharogroup.diff.beans; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.ToString; + +/** + * The class {@link ChangedAttributeResult} is a bean class that is used for compare objects and see + * what changes are made. + */ +@Getter +@Setter +@EqualsAndHashCode +@ToString +@NoArgsConstructor +@AllArgsConstructor +@Builder(toBuilder = true) +public class ChangedAttributeResult +{ + /** The attribute name. */ + private Object attributeName; + + /** The changed attribute. */ + private Object changedAttribute; + + /** The parent if exists. */ + private ChangedAttributeResult parent; + + /** The source attribute. */ + private Object sourceAttribute; + +} diff --git a/src/main/java/de/alpharogroup/diff/beans/GenericChangedAttribute.java b/src/main/java/de/alpharogroup/diff/beans/GenericChangedAttribute.java new file mode 100644 index 0000000..540823f --- /dev/null +++ b/src/main/java/de/alpharogroup/diff/beans/GenericChangedAttribute.java @@ -0,0 +1,57 @@ +/** + * The MIT License + * + * Copyright (C) 2015 Asterios Raptis + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package de.alpharogroup.diff.beans; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.ToString; + +/** + * The class {@link GenericChangedAttribute} is a bean class that encapsulated the difference of the + * attribute from two objects. + * + * @param + * the generic type of the source attribute + * @param + * the generic type of the changed attribute + */ +@Getter +@Setter +@EqualsAndHashCode +@ToString +@NoArgsConstructor +@AllArgsConstructor +@Builder(toBuilder = true) +public class GenericChangedAttribute +{ + /** The attribute name. */ + private String attributeName; + + /** The changed attribute. */ + private CHANGED changedAttribute; + + /** The source attribute. */ + private SOURCE sourceAttribute; +} diff --git a/src/main/java/de/alpharogroup/diff/beans/SerializedChangedAttributeResult.java b/src/main/java/de/alpharogroup/diff/beans/SerializedChangedAttributeResult.java new file mode 100644 index 0000000..11fa328 --- /dev/null +++ b/src/main/java/de/alpharogroup/diff/beans/SerializedChangedAttributeResult.java @@ -0,0 +1,81 @@ +/** + * The MIT License + * + * Copyright (C) 2015 Asterios Raptis + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package de.alpharogroup.diff.beans; + +import java.io.Serializable; + +import lombok.Builder; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.ToString; + +/** + * The class {@link SerializedChangedAttributeResult} is a bean class that is used for compare + * objects and see what changes are made. + */ +@Getter +@Setter +@EqualsAndHashCode +@ToString +@NoArgsConstructor +@Builder(toBuilder = true) +public class SerializedChangedAttributeResult implements Serializable +{ + + /** The Constant serialVersionUID. */ + private static final long serialVersionUID = 1L; + + /** The attribute name. */ + private Object attributeName; + + /** The changed attribute. */ + private Object changedAttribute; + + /** The source attribute. */ + private Object sourceAttribute; + + /** + * Instantiates a new changed attribute result. + * + * @param attributeName + * the attribute name + * @param sourceAttribute + * the source attribute + * @param changedAttribute + * the changed attribute + */ + public SerializedChangedAttributeResult(final Object attributeName, + final Object sourceAttribute, final Object changedAttribute) + { + if (!(attributeName instanceof Serializable) || !(sourceAttribute instanceof Serializable) + || !(changedAttribute instanceof Serializable)) + { + throw new IllegalArgumentException( + "Arguments should implement the Serializable interface."); + } + this.attributeName = attributeName; + this.sourceAttribute = sourceAttribute; + this.changedAttribute = changedAttribute; + } + +} diff --git a/src/main/java/de/alpharogroup/diff/beans/package.html b/src/main/java/de/alpharogroup/diff/beans/package.html new file mode 100644 index 0000000..cb8ad8b --- /dev/null +++ b/src/main/java/de/alpharogroup/diff/beans/package.html @@ -0,0 +1,11 @@ + + + + + de.alpharogroup.diff.beans + + + + This package provides bean classes for find changed data in objects + + diff --git a/src/main/java/de/alpharogroup/diff/object/DiffObjectExtensions.java b/src/main/java/de/alpharogroup/diff/object/DiffObjectExtensions.java new file mode 100644 index 0000000..729c19b --- /dev/null +++ b/src/main/java/de/alpharogroup/diff/object/DiffObjectExtensions.java @@ -0,0 +1,201 @@ +/** + * The MIT License + * + * Copyright (C) 2015 Asterios Raptis + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package de.alpharogroup.diff.object; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.beanutils.BeanUtils; + +import de.alpharogroup.check.Check; +import de.alpharogroup.compare.object.CompareObjectExtensions; +import de.alpharogroup.diff.beans.ChangedAttributeResult; +import lombok.experimental.UtilityClass; + +/** + * The class {@link DiffObjectExtensions} can find changed data from objects in other words the + * difference of them. + */ +@UtilityClass +public class DiffObjectExtensions +{ + + /** + * Compares the given two objects and gets the changed data. + * + * @param sourceOjbect + * the source ojbect + * @param objectToCompare + * the object to compare + * @return the changed data + * @throws IllegalAccessException + * Thrown if this method or object is enforcing java language access control and the + * underlying method or object is inaccessible. + * @throws InvocationTargetException + * Thrown if the property accessor method throws an exception + * @throws NoSuchMethodException + * Thrown if this {@code Method} object is enforcing Java language access control + * and the underlying method is inaccessible. + */ + @SuppressWarnings("rawtypes") + public static Map getChangedDataMap(final Object sourceOjbect, + final Object objectToCompare) + throws IllegalAccessException, InvocationTargetException, NoSuchMethodException + { + Check.get().notNull(sourceOjbect, "sourceOjbect").notNull(objectToCompare, + "objectToCompare"); + if (!sourceOjbect.getClass().equals(objectToCompare.getClass())) + { + throw new IllegalArgumentException( + "Given sourceObject should be the same type as objectToCompare."); + } + final Map beanDescription = BeanUtils.describe(sourceOjbect); + beanDescription.remove("class"); + final Map clonedBeanDescription = BeanUtils.describe(objectToCompare); + clonedBeanDescription.remove("class"); + final Map changedData = new HashMap<>(); + for (final Object key : beanDescription.keySet()) + { + final Object sourceAttribute = beanDescription.get(key); + final Object changedAttribute = clonedBeanDescription.get(key); + if (CompareObjectExtensions.compareTo(sourceOjbect, objectToCompare, + key.toString()) != 0) + { + final ChangedAttributeResult result = new ChangedAttributeResult(); + result.setAttributeName(key); + result.setSourceAttribute(sourceAttribute); + result.setChangedAttribute(changedAttribute); + changedData.put(key, result); + } + } + return changedData; + } + + /** + * Gets the changed data in a list. + * + * @param sourceOjbect + * the source ojbect + * @param objectToCompare + * the object to compare + * @return the changed data + * @throws IllegalAccessException + * Thrown if this method or object is enforcing java language access control and the + * underlying method or object is inaccessible. + * @throws InvocationTargetException + * Thrown if the property accessor method throws an exception + * @throws NoSuchMethodException + * Thrown if this {@code Method} object is enforcing Java language access control + * and the underlying method is inaccessible. + */ + @SuppressWarnings("rawtypes") + public static List getChangedData(final Object sourceOjbect, + final Object objectToCompare) + throws IllegalAccessException, InvocationTargetException, NoSuchMethodException + { + Check.get().notNull(sourceOjbect, "sourceOjbect").notNull(objectToCompare, + "objectToCompare"); + if (!sourceOjbect.getClass().equals(objectToCompare.getClass())) + { + throw new IllegalArgumentException( + "Given sourceObject should be the same type as objectToCompare."); + } + final Map beanDescription = BeanUtils.describe(sourceOjbect); + beanDescription.remove("class"); + final Map clonedBeanDescription = BeanUtils.describe(objectToCompare); + clonedBeanDescription.remove("class"); + final List changedData = new ArrayList<>(); + for (final Object key : beanDescription.keySet()) + { + if (CompareObjectExtensions.compareTo(sourceOjbect, objectToCompare, + key.toString()) != 0) + { + final Object sourceAttribute = beanDescription.get(key); + final Object changedAttribute = clonedBeanDescription.get(key); + changedData.add(ChangedAttributeResult.builder().attributeName(key) + .sourceAttribute(sourceAttribute).changedAttribute(changedAttribute).build()); + } + } + return changedData; + } + + /** + * Gets the changed data recursively in a list from the given source object and the object to + * compare. + * + * @param sourceOjbect + * the source ojbect + * @param objectToCompare + * the object to compare + * @param changedData + * the changed data in a list. This can be initially null, the list will be than + * created. + * @param parent + * the parent of the changed data. This is initially null. + * @return the list with the changed data + * @throws IllegalAccessException + * Thrown if this method or object is enforcing java language access control and the + * underlying method or object is inaccessible. + */ + public static List getChangedDataWithReflection( + final Object sourceOjbect, final Object objectToCompare, + List changedData, final ChangedAttributeResult parent) + throws IllegalAccessException + { + if (changedData == null) + { + changedData = new ArrayList<>(); + } + if (sourceOjbect == null || objectToCompare == null + || !sourceOjbect.getClass().equals(objectToCompare.getClass())) + { + return changedData; + } + final Field[] fields = sourceOjbect.getClass().getDeclaredFields(); + for (final Field field : fields) + { + if (Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers())) + { + continue; + } + field.setAccessible(true); + final Object sourceFieldValue = field.get(sourceOjbect); + final Object toCompareFieldValue = field.get(objectToCompare); + if ((sourceFieldValue == null && toCompareFieldValue != null) + || (toCompareFieldValue == null && sourceFieldValue != null) + || (sourceFieldValue != null && !sourceFieldValue.equals(toCompareFieldValue))) + { + final ChangedAttributeResult changedAttribute = ChangedAttributeResult.builder() + .parent(parent).attributeName(field.getName()).sourceAttribute(sourceFieldValue) + .changedAttribute(toCompareFieldValue).build(); + changedData.add(changedAttribute); + getChangedDataWithReflection(sourceFieldValue, toCompareFieldValue, changedData, + changedAttribute); + } + } + return changedData; + } +} diff --git a/src/main/java/de/alpharogroup/diff/object/package.html b/src/main/java/de/alpharogroup/diff/object/package.html new file mode 100644 index 0000000..682432d --- /dev/null +++ b/src/main/java/de/alpharogroup/diff/object/package.html @@ -0,0 +1,11 @@ + + + + + de.alpharogroup.diff.object + + + + This package provides utility classes for finding deltas of objects + + diff --git a/src/test/java/de/alpharogroup/diff/beans/ChangedAttributeResultTest.java b/src/test/java/de/alpharogroup/diff/beans/ChangedAttributeResultTest.java new file mode 100644 index 0000000..40fdf95 --- /dev/null +++ b/src/test/java/de/alpharogroup/diff/beans/ChangedAttributeResultTest.java @@ -0,0 +1,103 @@ +/** + * The MIT License + * + * Copyright (C) 2015 Asterios Raptis + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package de.alpharogroup.diff.beans; + +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertNotSame; +import static org.testng.AssertJUnit.assertTrue; + +import org.testng.annotations.Test; + +import de.alpharogroup.evaluate.object.evaluators.EqualsEvaluator; +import de.alpharogroup.evaluate.object.evaluators.HashcodeEvaluator; +import de.alpharogroup.evaluate.object.evaluators.ToStringEvaluator; + +/** + * The unit test class for the class {@link ChangedAttributeResult} + */ +public class ChangedAttributeResultTest +{ + + /** + * Test method for {@link ChangedAttributeResult#equals(Object)} + */ + @Test + public void testEqualsObject() + { + final ChangedAttributeResult expected = ChangedAttributeResult.builder() + .attributeName("foo").build(); + final ChangedAttributeResult actual = new ChangedAttributeResult(); + + assertNotSame(expected, actual); + final ChangedAttributeResult attributeResult = new ChangedAttributeResult(); + attributeResult.setAttributeName("foo"); + assertEquals(expected, attributeResult); + assertTrue( + EqualsEvaluator.evaluateReflexivityNonNullSymmetricAndConsistency(expected, actual)); + assertTrue( + EqualsEvaluator.evaluateReflexivityNonNullSymmetricConsistencyAndTransitivity(expected, + attributeResult, ChangedAttributeResult.builder().attributeName("foo").build())); + } + + /** + * Test method for {@link ChangedAttributeResult#hashCode()} + */ + @Test + public void testHashcode() + { + boolean expected; + boolean actual; + final ChangedAttributeResult attributeResult1 = ChangedAttributeResult.builder().build(); + final ChangedAttributeResult attributeResult2 = ChangedAttributeResult.builder().build(); + actual = HashcodeEvaluator.evaluateEquality(attributeResult1, attributeResult2); + expected = true; + assertEquals(expected, actual); + + expected = true; + actual = HashcodeEvaluator.evaluateUnequality(attributeResult1, + ChangedAttributeResult.builder().attributeName("foo").build()); + assertEquals(expected, actual); + + actual = HashcodeEvaluator.evaluateConsistency(attributeResult1); + expected = true; + assertEquals(expected, actual); + } + + /** + * Test method for {@link ChangedAttributeResult#toString()} + */ + @Test + public void testToString() + { + boolean expected; + boolean actual; + actual = ToStringEvaluator.evaluate(ChangedAttributeResult.class); + expected = true; + assertEquals(expected, actual); + + final ChangedAttributeResult integerBox = ChangedAttributeResult.builder().build(); + + actual = ToStringEvaluator.evaluateConsistency(integerBox); + expected = true; + assertEquals(expected, actual); + } + +} diff --git a/src/test/java/de/alpharogroup/diff/beans/GenericChangedAttributeTest.java b/src/test/java/de/alpharogroup/diff/beans/GenericChangedAttributeTest.java new file mode 100644 index 0000000..9b170b2 --- /dev/null +++ b/src/test/java/de/alpharogroup/diff/beans/GenericChangedAttributeTest.java @@ -0,0 +1,96 @@ +/** + * The MIT License + * + * Copyright (C) 2015 Asterios Raptis + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package de.alpharogroup.diff.beans; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.util.Optional; + +import org.meanbean.test.BeanTester; +import org.testng.annotations.Test; + +import de.alpharogroup.evaluate.object.api.ContractViolation; +import de.alpharogroup.evaluate.object.checkers.EqualsHashCodeAndToStringCheck; + +/** + * The unit test class for the class {@link GenericChangedAttribute} + */ +public class GenericChangedAttributeTest +{ + + /** + * Test method for {@link GenericChangedAttribute} constructors and builders + */ + @Test + public final void testConstructors() + { + GenericChangedAttribute model = new GenericChangedAttribute<>(); + assertNotNull(model); + model = new GenericChangedAttribute<>("foo", "name", "value"); + assertNotNull(model); + model = GenericChangedAttribute. builder().build(); + assertNotNull(model); + } + + /** + * Test method for {@link GenericChangedAttribute#equals(Object)} , + * {@link GenericChangedAttribute#hashCode()} and {@link GenericChangedAttribute#toString()} + * + * @throws NoSuchMethodException + * if an accessor method for this property cannot be found + * @throws IllegalAccessException + * if the caller does not have access to the property accessor method + * @throws InvocationTargetException + * if the property accessor method throws an exception + * @throws InstantiationException + * if a new instance of the bean's class cannot be instantiated + * @throws IOException + * Signals that an I/O exception has occurred + * @throws ClassNotFoundException + * occurs if a given class cannot be located by the specified class loader + */ + @Test + public void testEqualsHashcodeAndToStringWithClass() + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, + InstantiationException, IOException, ClassNotFoundException + { + Optional expected; + Optional actual; + actual = EqualsHashCodeAndToStringCheck + .equalsHashcodeAndToString(GenericChangedAttribute.class); + expected = Optional.empty(); + assertEquals(expected, actual); + } + + /** + * Test method for {@link GenericChangedAttribute} with {@link BeanTester} + */ + @Test + public void testWithBeanTester() + { + BeanTester beanTester = new BeanTester(); + beanTester.testBean(GenericChangedAttribute.class); + } + +} diff --git a/src/test/java/de/alpharogroup/diff/beans/SerializedChangedAttributeResultTest.java b/src/test/java/de/alpharogroup/diff/beans/SerializedChangedAttributeResultTest.java new file mode 100644 index 0000000..ec2278a --- /dev/null +++ b/src/test/java/de/alpharogroup/diff/beans/SerializedChangedAttributeResultTest.java @@ -0,0 +1,136 @@ +/** + * The MIT License + * + * Copyright (C) 2015 Asterios Raptis + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package de.alpharogroup.diff.beans; + +import static org.testng.Assert.assertNotNull; +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertNotSame; +import static org.testng.AssertJUnit.assertTrue; + +import org.testng.annotations.Test; + +import de.alpharogroup.evaluate.object.evaluators.EqualsEvaluator; +import de.alpharogroup.evaluate.object.evaluators.HashcodeEvaluator; +import de.alpharogroup.evaluate.object.evaluators.ToStringEvaluator; +import de.alpharogroup.test.objects.Permission; + +/** + * The unit test class for the class {@link SerializedChangedAttributeResult}. + */ +public class SerializedChangedAttributeResultTest +{ + + /** + * Test method for {@link SerializedChangedAttributeResult} constructors + */ + @Test + public final void testConstructors() + { + SerializedChangedAttributeResult model = new SerializedChangedAttributeResult(); + assertNotNull(model); + model = new SerializedChangedAttributeResult("foo", "name", "value"); + assertNotNull(model); + model = SerializedChangedAttributeResult.builder().attributeName("foo").sourceAttribute("") + .changedAttribute("").build(); + assertNotNull(model); + } + + /** + * Test method for {@link SerializedChangedAttributeResult} constructors that throws an + * IllegalArgumentException + */ + @Test(enabled = true, expectedExceptions = IllegalArgumentException.class) + public final void testConstructorsThrowIllegalArgumentException01() + { + new SerializedChangedAttributeResult(Permission.builder().build(), "name", "value"); + } + + /** + * Test method for {@link SerializedChangedAttributeResult#equals(Object)} + */ + @Test + public void testEqualsObject() + { + final SerializedChangedAttributeResult expected = SerializedChangedAttributeResult.builder() + .attributeName("foo").sourceAttribute("").changedAttribute("").build(); + final SerializedChangedAttributeResult actual = new SerializedChangedAttributeResult(); + + assertNotSame(expected, actual); + final SerializedChangedAttributeResult attributeResult = new SerializedChangedAttributeResult(); + attributeResult.setAttributeName("foo"); + attributeResult.setSourceAttribute(""); + attributeResult.setChangedAttribute(""); + assertEquals(expected, attributeResult); + assertTrue( + EqualsEvaluator.evaluateReflexivityNonNullSymmetricAndConsistency(expected, actual)); + assertTrue(EqualsEvaluator.evaluateReflexivityNonNullSymmetricConsistencyAndTransitivity( + expected, attributeResult, SerializedChangedAttributeResult.builder() + .attributeName("foo").sourceAttribute("").changedAttribute("").build())); + } + + /** + * Test method for {@link SerializedChangedAttributeResult#hashCode()} + */ + @Test + public void testHashcode() + { + boolean expected; + boolean actual; + final SerializedChangedAttributeResult attributeResult1 = SerializedChangedAttributeResult + .builder().attributeName("foo").sourceAttribute("").changedAttribute("").build(); + final SerializedChangedAttributeResult attributeResult2 = SerializedChangedAttributeResult + .builder().attributeName("foo").sourceAttribute("").changedAttribute("").build(); + actual = HashcodeEvaluator.evaluateEquality(attributeResult1, attributeResult2); + expected = true; + assertEquals(expected, actual); + + expected = true; + actual = HashcodeEvaluator.evaluateUnequality(attributeResult1, + SerializedChangedAttributeResult.builder().attributeName("bar").sourceAttribute("") + .changedAttribute("").build()); + assertEquals(expected, actual); + + actual = HashcodeEvaluator.evaluateConsistency(attributeResult1); + expected = true; + assertEquals(expected, actual); + } + + /** + * Test method for {@link SerializedChangedAttributeResult#toString()} + */ + @Test + public void testToString() + { + boolean expected; + boolean actual; + actual = ToStringEvaluator.evaluate(SerializedChangedAttributeResult.class); + expected = true; + assertEquals(expected, actual); + + final SerializedChangedAttributeResult integerBox = SerializedChangedAttributeResult + .builder().attributeName("foo").sourceAttribute("").changedAttribute("").build(); + + actual = ToStringEvaluator.evaluateConsistency(integerBox); + expected = true; + assertEquals(expected, actual); + } + +} diff --git a/src/test/java/de/alpharogroup/diff/object/DiffObjectExtensionsTest.java b/src/test/java/de/alpharogroup/diff/object/DiffObjectExtensionsTest.java new file mode 100644 index 0000000..3a39c32 --- /dev/null +++ b/src/test/java/de/alpharogroup/diff/object/DiffObjectExtensionsTest.java @@ -0,0 +1,334 @@ +/** + * The MIT License + * + * Copyright (C) 2015 Asterios Raptis + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package de.alpharogroup.diff.object; + +import static org.testng.AssertJUnit.assertFalse; +import static org.testng.AssertJUnit.assertTrue; + +import java.lang.reflect.InvocationTargetException; +import java.util.List; +import java.util.Map; + +import org.junit.Rule; +import org.junit.rules.ExpectedException; +import org.meanbean.test.BeanTestException; +import org.meanbean.test.BeanTester; +import org.testng.annotations.Test; + +import de.alpharogroup.diff.beans.ChangedAttributeResult; +import de.alpharogroup.test.objects.Employee; +import de.alpharogroup.test.objects.Person; +import de.alpharogroup.test.objects.enums.Gender; + +/** + * The unit test class for the class {@link DiffObjectExtensions}. + */ +public class DiffObjectExtensionsTest +{ + + /** + * A rule for expecting exceptions + */ + @Rule + public ExpectedException throwable = ExpectedException.none(); + + /** + * Test method for {@link DiffObjectExtensions#getChangedData(Object, Object)}. + * + * @throws IllegalAccessException + * Thrown if this method or object is enforcing java language access control and the + * underlying method or object is inaccessible. + * @throws InvocationTargetException + * Thrown if the property accessor method throws an exception + * @throws NoSuchMethodException + * Thrown if this {@code Method} object is enforcing Java language access control + * and the underlying method is inaccessible. + */ + @Test + public void testGetChangedData() + throws IllegalAccessException, InvocationTargetException, NoSuchMethodException + { + final Person sourceOjbect = Person.builder().gender(Gender.MALE).name("obelix").build(); + + final Person objectToCompare = Person.builder().gender(Gender.MALE).name("obelix").build(); + + List result = DiffObjectExtensions.getChangedData(sourceOjbect, + objectToCompare); + assertTrue("Size should be 0 but is " + result.size(), result.size() == 0); + // Change the gender from the objectToCompare... + objectToCompare.setGender(Gender.FEMALE); + // and get the changed data... + result = DiffObjectExtensions.getChangedData(sourceOjbect, objectToCompare); + assertFalse("Size should be 1 but is " + result.size(), result.size() == 0); + assertTrue("Size should be 1 but is " + result.size(), result.size() == 1); + + final ChangedAttributeResult changed = result.get(0); + final Object sourceAttribute = changed.getSourceAttribute(); + final Object changedAttribute = changed.getChangedAttribute(); + assertTrue("", sourceAttribute.equals(Gender.MALE.name())); + assertTrue("", changedAttribute.equals(Gender.FEMALE.name())); + } + + /** + * Test method for {@link DiffObjectExtensions#getChangedData(Object, Object)} with source as + * null value. + * + * @throws IllegalAccessException + * Thrown if this method or object is enforcing java language access control and the + * underlying method or object is inaccessible. + * @throws InvocationTargetException + * Thrown if the property accessor method throws an exception + * @throws NoSuchMethodException + * Thrown if this {@code Method} object is enforcing Java language access control + * and the underlying method is inaccessible. + */ + @Test(enabled = true, expectedExceptions = IllegalArgumentException.class) + public void testGetChangedDataCompareNullValue() + throws IllegalAccessException, InvocationTargetException, NoSuchMethodException + { + final Person sourceOjbect = Person.builder().build(); + final Person objectToCompare = null; + throwable.expect(IllegalArgumentException.class); + throwable.expectMessage("Given argument 'objectToCompare' may not be null."); + DiffObjectExtensions.getChangedData(sourceOjbect, objectToCompare); + } + + /** + * Test method for {@link DiffObjectExtensions#getChangedData(Object, Object)} with different + * class of source and compare object. + * + * @throws IllegalAccessException + * Thrown if this method or object is enforcing java language access control and the + * underlying method or object is inaccessible. + * @throws InvocationTargetException + * Thrown if the property accessor method throws an exception + * @throws NoSuchMethodException + * Thrown if this {@code Method} object is enforcing Java language access control + * and the underlying method is inaccessible. + */ + @Test(enabled = true, expectedExceptions = IllegalArgumentException.class) + public void testGetChangedDataDifferentClass() + throws IllegalAccessException, InvocationTargetException, NoSuchMethodException + { + final Person sourceOjbect = Person.builder().build(); + final Employee objectToCompare = Employee.builder().build(); + throwable.expect(IllegalArgumentException.class); + throwable.expectMessage("Object should not be null and be the same type."); + DiffObjectExtensions.getChangedData(sourceOjbect, objectToCompare); + } + + /** + * Test method for {@link DiffObjectExtensions#getChangedDataMap(Object, Object)}. + * + * @throws IllegalAccessException + * Thrown if this method or object is enforcing java language access control and the + * underlying method or object is inaccessible. + * @throws InvocationTargetException + * Thrown if the property accessor method throws an exception + * @throws NoSuchMethodException + * Thrown if this {@code Method} object is enforcing Java language access control + * and the underlying method is inaccessible. + */ + @Test(enabled = true) + public void testGetChangedDataMap() + throws IllegalAccessException, InvocationTargetException, NoSuchMethodException + { + final Person sourceOjbect = Person.builder().gender(Gender.MALE).name("obelix").build(); + + final Person objectToCompare = Person.builder().gender(Gender.MALE).name("obelix").build(); + + Map result = DiffObjectExtensions + .getChangedDataMap(sourceOjbect, objectToCompare); + assertTrue("Size should be 0 but is " + result.size(), result.size() == 0); + // Change the gender from the objectToCompare... + objectToCompare.setGender(Gender.FEMALE); + // and get the changed data... + result = DiffObjectExtensions.getChangedDataMap(sourceOjbect, objectToCompare); + assertFalse("Size should be 1 but is " + result.size(), result.size() == 0); + assertTrue("", result.containsKey("gender")); + final ChangedAttributeResult changed = result.get("gender"); + final Object sourceAttribute = changed.getSourceAttribute(); + final Object changedAttribute = changed.getChangedAttribute(); + assertTrue("", sourceAttribute.equals(Gender.MALE.name())); + assertTrue("", changedAttribute.equals(Gender.FEMALE.name())); + } + + /** + * Test method for {@link DiffObjectExtensions#getChangedDataMap(Object, Object)} with source as + * null value. + * + * @throws IllegalAccessException + * Thrown if this method or object is enforcing java language access control and the + * underlying method or object is inaccessible. + * @throws InvocationTargetException + * Thrown if the property accessor method throws an exception + * @throws NoSuchMethodException + * Thrown if this {@code Method} object is enforcing Java language access control + * and the underlying method is inaccessible. + */ + @Test(enabled = true, expectedExceptions = IllegalArgumentException.class) + public void testGetChangedDataMapCompareNullValue() + throws IllegalAccessException, InvocationTargetException, NoSuchMethodException + { + final Person sourceOjbect = Person.builder().build(); + final Person objectToCompare = null; + throwable.expect(IllegalArgumentException.class); + throwable.expectMessage("Given argument 'objectToCompare' may not be null."); + DiffObjectExtensions.getChangedDataMap(sourceOjbect, objectToCompare); + } + + + /** + * Test method for {@link DiffObjectExtensions#getChangedDataMap(Object, Object)} with different + * class of source and compare object. + * + * @throws IllegalAccessException + * Thrown if this method or object is enforcing java language access control and the + * underlying method or object is inaccessible. + * @throws InvocationTargetException + * Thrown if the property accessor method throws an exception + * @throws NoSuchMethodException + * Thrown if this {@code Method} object is enforcing Java language access control + * and the underlying method is inaccessible. + */ + @Test(enabled = true, expectedExceptions = IllegalArgumentException.class) + public void testGetChangedDataMapDifferentClass() + throws IllegalAccessException, InvocationTargetException, NoSuchMethodException + { + final Person sourceOjbect = Person.builder().build(); + final Employee objectToCompare = Employee.builder().build(); + throwable.expect(IllegalArgumentException.class); + throwable.expectMessage("Object should not be null and be the same type."); + DiffObjectExtensions.getChangedDataMap(sourceOjbect, objectToCompare); + } + + /** + * Test method for {@link DiffObjectExtensions#getChangedDataMap(Object, Object)} with source as + * null value. + * + * @throws IllegalAccessException + * Thrown if this method or object is enforcing java language access control and the + * underlying method or object is inaccessible. + * @throws InvocationTargetException + * Thrown if the property accessor method throws an exception + * @throws NoSuchMethodException + * Thrown if this {@code Method} object is enforcing Java language access control + * and the underlying method is inaccessible. + */ + @Test(enabled = true, expectedExceptions = IllegalArgumentException.class) + public void testGetChangedDataMapSourceNullValue() + throws IllegalAccessException, InvocationTargetException, NoSuchMethodException + { + final Person sourceOjbect = null; + final Person objectToCompare = Person.builder().build(); + throwable.expect(IllegalArgumentException.class); + throwable.expectMessage("Given argument 'sourceOjbect' may not be null."); + DiffObjectExtensions.getChangedDataMap(sourceOjbect, objectToCompare); + } + + /** + * Test method for {@link DiffObjectExtensions#getChangedData(Object, Object)} with source as + * null value. + * + * @throws IllegalAccessException + * Thrown if this method or object is enforcing java language access control and the + * underlying method or object is inaccessible. + * @throws InvocationTargetException + * Thrown if the property accessor method throws an exception + * @throws NoSuchMethodException + * Thrown if this {@code Method} object is enforcing Java language access control + * and the underlying method is inaccessible. + */ + @Test(enabled = true, expectedExceptions = IllegalArgumentException.class) + public void testGetChangedDataSourceNullValue() + throws IllegalAccessException, InvocationTargetException, NoSuchMethodException + { + final Person sourceOjbect = null; + final Person objectToCompare = Person.builder().build(); + throwable.expect(IllegalArgumentException.class); + throwable.expectMessage("Given argument 'sourceOjbect' may not be null."); + DiffObjectExtensions.getChangedData(sourceOjbect, objectToCompare); + } + + /** + * Test method for {@link DiffObjectExtensions#getChangedDataWithReflection(Object, Object, + * List, ChangedAttributeResult). + * + * @throws IllegalAccessException + * Thrown if this method or object is enforcing java language access control and the + * underlying method or object is inaccessible. + */ + @Test + public void testGetChangedDataWithReflection() throws IllegalAccessException + { + final Person sourceOjbect = Person.builder().build(); + final Person objectToCompare = Person.builder().build(); + final List result = DiffObjectExtensions + .getChangedDataWithReflection(sourceOjbect, objectToCompare, null, null); + assertTrue("Size should be 0 but is " + result.size(), result.size() == 0); + // TODO extend unit test scenarios... + } + + /** + * Test method for + * {@link DiffObjectExtensions#getChangedDataWithReflection(Object, Object, List, ChangedAttributeResult)} + * with different class of source and compare object. + * + * @throws IllegalAccessException + * the illegal access exception + */ + @Test(enabled = true) + public void testGetChangedDataWithReflectionDifferentClass() throws IllegalAccessException + { + Object objectToCompare; + final Person sourceOjbect = Person.builder().build(); + objectToCompare = Employee.builder().build(); + List result = DiffObjectExtensions + .getChangedDataWithReflection(sourceOjbect, objectToCompare, null, null); + assertTrue("Size should be 0 but is " + result.size(), result.size() == 0); + + objectToCompare = Person.builder().build(); + result = DiffObjectExtensions.getChangedDataWithReflection(sourceOjbect, objectToCompare, + result, null); + assertTrue("Size should be 0 but is " + result.size(), result.size() == 0); + // Change the gender from the objectToCompare... + ((Person)objectToCompare).setGender(Gender.FEMALE); + + // and get the changed data... + result = DiffObjectExtensions.getChangedDataWithReflection(sourceOjbect, objectToCompare, + result, null); + assertFalse("Size should be 1 but is " + result.size(), result.size() == 0); + assertTrue("Size should be 1 but is " + result.size(), result.size() == 1); + } + + /** + * Test method for {@link DiffObjectExtensions} with {@link BeanTester} + */ + @Test(expectedExceptions = { BeanTestException.class, InvocationTargetException.class, + UnsupportedOperationException.class }) + public void testWithBeanTester() + { + final BeanTester beanTester = new BeanTester(); + beanTester.testBean(DiffObjectExtensions.class); + } + +} + From f0a9c5d28461d1e050d627015deb79c9f3e0e566 Mon Sep 17 00:00:00 2001 From: astrapi69 Date: Fri, 19 Apr 2019 12:09:35 +0200 Subject: [PATCH 2/2] prepare new major release 3 --- CHANGELOG.md | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 58a6a7b..a893ebd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ ## Change log ---------------------- -Version 3-SNAPSHOT +Version 3 ------------- ADDED: diff --git a/pom.xml b/pom.xml index f45b166..26b3607 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ jobj-differ - 3-SNAPSHOT + 3 ${project.artifactId}