Skip to content

Commit 0c39531

Browse files
committed
Update internals of ProblemImpl
1 parent c45f3e2 commit 0c39531

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

src/main/java/io/github/malczuuu/problem4j/core/ProblemImpl.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.util.Set;
1111
import java.util.stream.Collectors;
1212

13-
class ProblemImpl implements Problem {
13+
final class ProblemImpl implements Problem {
1414

1515
private static final long serialVersionUID = 1L;
1616

@@ -94,11 +94,14 @@ public boolean hasExtension(String extension) {
9494
}
9595

9696
@Override
97-
public boolean equals(Object o) {
98-
if (!(o instanceof ProblemImpl)) {
97+
public boolean equals(Object obj) {
98+
if (this == obj) {
99+
return true;
100+
}
101+
if (obj == null || getClass() != obj.getClass()) {
99102
return false;
100103
}
101-
ProblemImpl problem = (ProblemImpl) o;
104+
ProblemImpl problem = (ProblemImpl) obj;
102105
return Objects.equals(getType(), problem.getType())
103106
&& Objects.equals(getTitle(), problem.getTitle())
104107
&& getStatus() == problem.getStatus()
@@ -187,6 +190,9 @@ public Object setValue(Object value) {
187190

188191
@Override
189192
public boolean equals(Object obj) {
193+
if (this == obj) {
194+
return true;
195+
}
190196
if (obj == null || getClass() != obj.getClass()) {
191197
return false;
192198
}

src/test/java/io/github/malczuuu/problem4j/core/ProblemExceptionTests.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertNull;
5+
import static org.junit.jupiter.api.Assertions.assertSame;
56

67
import org.junit.jupiter.api.Test;
78

@@ -144,4 +145,53 @@ void givenProblemWithSpecialCharacters_whenCreatingException_thenSpecialCharacte
144145
"Validation Error: Required Fields Missing: Fields 'name' & 'email' are required (check docs) (code: 422)",
145146
exception.getMessage());
146147
}
148+
149+
@Test
150+
void givenCtor_whenCreatingProblemException_problemIsNotRecreated() {
151+
Problem problem = Problem.builder().title("Bad Request").status(400).build();
152+
153+
ProblemException exception = new ProblemException(problem);
154+
155+
assertSame(problem, exception.getProblem());
156+
}
157+
158+
@Test
159+
void givenCtorWithMessage_whenCreatingProblemException_problemIsNotRecreated() {
160+
Problem problem = Problem.builder().title("Bad Request").status(400).build();
161+
162+
ProblemException exception = new ProblemException("this is a message", problem);
163+
164+
assertSame(problem, exception.getProblem());
165+
}
166+
167+
@Test
168+
void givenCtorWithCause_whenCreatingProblemException_problemIsNotRecreated() {
169+
Problem problem = Problem.builder().title("Bad Request").status(400).build();
170+
Throwable cause = new RuntimeException("root cause");
171+
172+
ProblemException exception = new ProblemException(problem, cause);
173+
174+
assertSame(problem, exception.getProblem());
175+
}
176+
177+
@Test
178+
void givenCtorWithMessageAndCause_whenCreatingProblemException_problemIsNotRecreated() {
179+
Problem problem = Problem.builder().title("Bad Request").status(400).build();
180+
Throwable cause = new RuntimeException("root cause");
181+
182+
ProblemException exception = new ProblemException("this is a message", problem, cause);
183+
184+
assertSame(problem, exception.getProblem());
185+
}
186+
187+
@Test
188+
void givenCtorWithParameters_whenCreatingProblemException_problemIsNotRecreated() {
189+
Problem problem = Problem.builder().title("Bad Request").status(400).build();
190+
Throwable cause = new RuntimeException("root cause");
191+
192+
ProblemException exception =
193+
new ProblemException("this is a message", problem, cause, false, true);
194+
195+
assertSame(problem, exception.getProblem());
196+
}
147197
}

0 commit comments

Comments
 (0)