From 4870b411eda7d625a3d5dcea6bd1d15d596b7f7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20St=C3=B6cker?= <39899210+DerStoecki@users.noreply.github.com> Date: Wed, 1 Nov 2023 17:52:27 +0100 Subject: [PATCH] Added hashcode to domain and changed equals method a bit, added Test (#2412) This commit is one of many prepare PRs / commits for a bigger PR: SumStateAlert #2260 By Seperating the big #2260 into smaller commits we would like to make the review easier. Co-authored-by: Kai Jeschek <99220919+da-Kai@users.noreply.github.com> --- .../backend/metadata/odoo/odoo/Domain.java | 15 ++++++--- .../metadata/odoo/odoo/DomainTest.java | 32 +++++++++++++------ 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/io.openems.backend.metadata.odoo/src/io/openems/backend/metadata/odoo/odoo/Domain.java b/io.openems.backend.metadata.odoo/src/io/openems/backend/metadata/odoo/odoo/Domain.java index f03793a5a28..68902aef24e 100644 --- a/io.openems.backend.metadata.odoo/src/io/openems/backend/metadata/odoo/odoo/Domain.java +++ b/io.openems.backend.metadata.odoo/src/io/openems/backend/metadata/odoo/odoo/Domain.java @@ -122,16 +122,21 @@ public Domain(Field[] fields, Operator operator, Object value) { this.value = value; } + @Override + public int hashCode() { + return Objects.hash(this.field, this.operator, this.value); + } + @Override public boolean equals(Object obj) { if (this == obj) { return true; - } - if (!Domain.class.isInstance(obj)) { + } else if (obj instanceof Domain other) { + return Objects.equals(this.field, other.field) // + && Objects.equals(this.operator, other.operator) // + && Objects.equals(this.value, other.value); + } else { return false; } - var other = (Domain) obj; - return Objects.equals(this.field, other.field) && Objects.equals(this.operator, other.operator) - && Objects.equals(this.value, other.value); } } diff --git a/io.openems.backend.metadata.odoo/test/io/openems/backend/metadata/odoo/odoo/DomainTest.java b/io.openems.backend.metadata.odoo/test/io/openems/backend/metadata/odoo/odoo/DomainTest.java index e3a36d168df..1b4c2605ed7 100644 --- a/io.openems.backend.metadata.odoo/test/io/openems/backend/metadata/odoo/odoo/DomainTest.java +++ b/io.openems.backend.metadata.odoo/test/io/openems/backend/metadata/odoo/odoo/DomainTest.java @@ -2,6 +2,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertThrows; import org.junit.Test; @@ -33,20 +34,31 @@ public boolean isQuery() { } }; + @Test + public void testDummy() { + assertThrows(UnsupportedOperationException.class, TEST_FIELD::name); + assertThrows(UnsupportedOperationException.class, TEST_FIELD::index); + assertThrows(UnsupportedOperationException.class, TEST_FIELD::isQuery); + } + @Test public void testConstructor() { - final var domain_1 = new Domain(TEST_FIELD, Operator.LIKE, "Lorem ipsum"); + final var domain1 = new Domain(TEST_FIELD, Operator.LIKE, "Lorem ipsum"); + + assertEquals("like", domain1.operator); + assertEquals("Lorem ipsum", domain1.value); + assertEquals(TEST_FIELD.id(), domain1.field); - assertEquals("like", domain_1.operator); - assertEquals("Lorem ipsum", domain_1.value); - assertEquals(TEST_FIELD.id(), domain_1.field); + final var domain2 = new Domain(new Field[] { TEST_FIELD, TEST_FIELD }, Operator.EQ, "Lorem ipsum"); + final var domain3 = new Domain(TEST_FIELD_ID + "." + TEST_FIELD_ID, Operator.EQ, "Lorem ipsum"); - final var domain_3 = new Domain(new Field[] { TEST_FIELD, TEST_FIELD }, Operator.EQ, "Lorem ipsum"); - final var domain_4 = new Domain(TEST_FIELD_ID + "." + TEST_FIELD_ID, Operator.EQ, "Lorem ipsum"); + assertNotEquals(domain1, domain3); + assertEquals(domain3, domain3); + assertEquals(domain2, domain3); + assertNotEquals(domain3, null); + assertNotEquals(domain3, 1); - assertEquals(domain_3, domain_3); - assertEquals(domain_3, domain_4); - assertNotEquals(domain_3, null); - assertNotEquals(domain_3, 1); + assertEquals(domain3.hashCode(), domain2.hashCode()); + assertNotEquals(domain1.hashCode(), domain2.hashCode()); } }