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 NPE hashcode in LwM2mPath #1505

Merged
merged 3 commits into from
Sep 1, 2023
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
5 changes: 5 additions & 0 deletions leshan-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ Contributors:
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.eclipse.leshan.core.util.Validate;

Expand Down Expand Up @@ -331,56 +332,21 @@ public String toString() {

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + objectId;
result = prime * result + ((objectInstanceId == null) ? 0 : objectInstanceId.hashCode());
result = prime * result + ((resourceId == null) ? 0 : resourceId.hashCode());
result = prime * result + ((resourceInstanceId == null) ? 0 : resourceInstanceId.hashCode());
return result;
return Objects.hash(objectId, objectInstanceId, resourceId, resourceInstanceId);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
if (this == obj)
return true;
}
if (obj == null) {
if (obj == null)
return false;
}
if (getClass() != obj.getClass()) {
if (getClass() != obj.getClass())
return false;
}
LwM2mPath other = (LwM2mPath) obj;
if (objectId == null) {
if (other.objectId != null) {
return false;
}
} else if (!objectId.equals(other.objectId)) {
return false;
}
if (objectInstanceId == null) {
if (other.objectInstanceId != null) {
return false;
}
} else if (!objectInstanceId.equals(other.objectInstanceId)) {
return false;
}
if (resourceId == null) {
if (other.resourceId != null) {
return false;
}
} else if (!resourceId.equals(other.resourceId)) {
return false;
}
if (resourceInstanceId == null) {
if (other.resourceInstanceId != null) {
return false;
}
} else if (!resourceInstanceId.equals(other.resourceInstanceId)) {
return false;
}
return true;
return Objects.equals(objectId, other.objectId) && Objects.equals(objectInstanceId, other.objectInstanceId)
&& Objects.equals(resourceId, other.resourceId)
&& Objects.equals(resourceInstanceId, other.resourceInstanceId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

import org.junit.jupiter.api.Test;

public class LwM2mParhTest {
import nl.jqno.equalsverifier.EqualsVerifier;

public class LwM2mPathTest {

@Test
public void test_compare_path() {
Expand Down Expand Up @@ -73,4 +75,12 @@ private void assertFirstSmaller(String path1, String path2) {
assertTrue(new LwM2mPath(path2).compareTo(new LwM2mPath(path1)) == 1);

}

@Test
public void assertEqualsHashcode() {
// TODO we should not use EqualsVerifier.simple()
// But implement a right hashcode/equals way
// see : https://github.com/eclipse-leshan/leshan/issues/1504
EqualsVerifier.simple().forClass(LwM2mPath.class).verify();
}
}
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@ Contributors:
<version>3.24.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<version>3.15.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down