Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.baeldung.hashcode.application;

import com.baeldung.hashcode.standard.User;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

public class ApplicationUnitTest {

Expand All @@ -23,4 +26,14 @@ public void main_NoInputState_TextPrintedToConsole() throws Exception {

assertTrue(users.containsKey(user1));
}

@Test
public void givenOverriddenHashCode_whenUsingIdentityHashCode_thenJvmIdentityIsPreserved() {
User user1 = new User(1L, "John", "[email protected]");
User user2 = new User(1L, "John", "[email protected]");

assertEquals(user1.hashCode(), user2.hashCode());

assertNotEquals(System.identityHashCode(user1), System.identityHashCode(user2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.FormatFlagsConversionMismatchException;
import java.util.IllegalFormatException;
import java.util.IllegalFormatPrecisionException;
import java.util.Locale;
import java.util.MissingFormatArgumentException;
Expand Down Expand Up @@ -74,6 +73,12 @@ public void whenMissingFormatArgument_thenMissingFormatArgumentExceptionThrown()
});
}

@Test
public void givenAnInteger_whenPaddingWithZeros_thanIntegerGetsPadded() {
assertEquals("00000001", padIntegerWithZeros(1, 8));
assertEquals("-0000001", padIntegerWithZeros(-1, 8));
}

@Test
public void whenNumberFormatWithLocales_thenCorrect() {
String frenchFormatted = String.format(Locale.FRANCE, "%,f", 1234567.89);
Expand Down Expand Up @@ -103,4 +108,9 @@ public void whenCurrencyFormatWithLocales_thenCorrect() {
String frenchFormatted = frenchCurrencyFormat.format(1000);
assertEquals("1 000,00 €", frenchFormatted);
}

private String padIntegerWithZeros(int number, int width) {
return String.format("%0" + width + "d", number);
}

}