Skip to content

Commit 752086e

Browse files
Alejandro Gervasiozhendrikse
authored andcommitted
Initial Commit (eugenp#2349)
1 parent deb79bb commit 752086e

4 files changed

Lines changed: 125 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.application;
2+
3+
import com.baeldung.entities.User;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
Map<User, User> users = new HashMap<>();
11+
User user1 = new User(1L, "John", "john@domain.com");
12+
User user2 = new User(2L, "Jennifer", "jennifer@domain.com");
13+
User user3 = new User(3L, "Mary", "mary@domain.com");
14+
15+
users.put(user1, user1);
16+
users.put(user2, user2);
17+
users.put(user3, user3);
18+
19+
if (users.containsKey(user1)) {
20+
System.out.print("User found in the collection");
21+
}
22+
}
23+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.entities;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class User {
7+
8+
private final Logger logger = LoggerFactory.getLogger(User.class);
9+
private long id;
10+
private String name;
11+
private String email;
12+
13+
public User(long id, String name, String email) {
14+
this.id = id;
15+
this.name = name;
16+
this.email = email;
17+
}
18+
19+
@Override
20+
public boolean equals(Object o) {
21+
if (this == o) return true;
22+
if (o == null) return false;
23+
if (this.getClass() != o.getClass()) return false;
24+
User user = (User) o;
25+
return id != user.id && (!name.equals(user.name) && !email.equals(user.email));
26+
}
27+
28+
@Override
29+
public int hashCode() {
30+
int hash = 7;
31+
hash = 31 * hash + (int) id;
32+
hash = 31 * hash + (name == null ? 0 : name.hashCode());
33+
hash = 31 * hash + (email == null ? 0 : email.hashCode());
34+
logger.info("hashCode() method called - Computed hash: " + hash);
35+
return hash;
36+
}
37+
// getters and setters here
38+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.application;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import java.io.ByteArrayOutputStream;
7+
import java.io.PrintStream;
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class ApplicationTest {
11+
12+
private ByteArrayOutputStream outContent;
13+
14+
@Before
15+
public void setUpPrintStreamInstance() throws Exception {
16+
this.outContent = new ByteArrayOutputStream();
17+
System.setOut(new PrintStream(outContent));
18+
}
19+
20+
@After
21+
public void tearDownByteArrayOutputStream() throws Exception {
22+
outContent = null;
23+
}
24+
25+
@Test
26+
public void main_NoInputState_TextPrintedToConsole() throws Exception {
27+
Application.main(new String[]{});
28+
assertEquals("User found in the collection", outContent.toString());
29+
}
30+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.entities;
2+
3+
import org.junit.After;
4+
import org.junit.Assert;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
public class UserTest {
9+
10+
private User user;
11+
private User comparisonUser;
12+
13+
@Before
14+
public void setUpUserInstances() {
15+
this.user = new User(1L, "test", "test@domain.com");
16+
this.comparisonUser = this.user;
17+
}
18+
19+
@After
20+
public void tearDownUserInstances() {
21+
user = null;
22+
comparisonUser = null;
23+
}
24+
25+
@Test
26+
public void equals_EqualUserInstance_TrueAssertion(){
27+
Assert.assertTrue(user.equals(comparisonUser));
28+
}
29+
30+
@Test
31+
public void hashCode_UserHash_TrueAssertion() {
32+
Assert.assertEquals(1792276941, user.hashCode());
33+
}
34+
}

0 commit comments

Comments
 (0)