|
9 | 9 | import org.junit.jupiter.params.ParameterizedTest;
|
10 | 10 | import org.junit.jupiter.params.provider.Arguments;
|
11 | 11 | import org.junit.jupiter.params.provider.MethodSource;
|
| 12 | +import org.junit.jupiter.api.Test; |
12 | 13 |
|
13 | 14 | import java.nio.charset.StandardCharsets;
|
| 15 | +import java.io.UnsupportedEncodingException; |
| 16 | +import java.security.NoSuchAlgorithmException; |
14 | 17 | import java.util.List;
|
15 | 18 | import java.util.stream.Collectors;
|
16 | 19 | import java.util.stream.Stream;
|
17 | 20 |
|
18 | 21 | import static org.assertj.core.api.Assertions.assertThat;
|
| 22 | +import static org.junit.jupiter.api.Assertions.*; |
19 | 23 |
|
20 | 24 | class NamingHelperTest {
|
21 | 25 |
|
@@ -57,34 +61,79 @@ void smoke(String charset, String prefix, String tableName, String referencedTab
|
57 | 61 | .isEqualTo( expectedConstraintName );
|
58 | 62 | }
|
59 | 63 |
|
| 64 | + @Test |
| 65 | + void testHashWithAlgorithm_md5_utf8() throws Exception { |
| 66 | + NamingHelper helper = NamingHelper.withCharset(StandardCharsets.UTF_8.name()); |
| 67 | + String hash = helper.hashWithAlgorithm("table_name", "MD5"); |
| 68 | + assertNotNull(hash); |
| 69 | + assertFalse(hash.isEmpty()); |
| 70 | + // MD5 hash of "table_name" in base 35 should be deterministic |
| 71 | + assertEquals("8q6ok4ne4ufel54crtitkq7ir", hash); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void testHashWithAlgorithm_sha256_utf8() throws Exception { |
| 76 | + NamingHelper helper = NamingHelper.withCharset(StandardCharsets.UTF_8.name()); |
| 77 | + String hash = helper.hashWithAlgorithm("table_name", "SHA-256"); |
| 78 | + assertNotNull(hash); |
| 79 | + assertFalse(hash.isEmpty()); |
| 80 | + // SHA-256 hash of "table_name" in base 35 should be deterministic |
| 81 | + assertEquals("nie2bx5e7mderevrnl4gkuhtmy45nwfvst7dv6cx3pb3yy9ul1", hash); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void testHashWithAlgorithm_md5_iso88591() throws Exception { |
| 86 | + NamingHelper helper = NamingHelper.withCharset(StandardCharsets.ISO_8859_1.name()); |
| 87 | + String hash = helper.hashWithAlgorithm("café", "MD5"); |
| 88 | + assertNotNull(hash); |
| 89 | + assertFalse(hash.isEmpty()); |
| 90 | + assertEquals("hgll69c0qdhsjikniholqfcj4", hash); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void testHashWithAlgorithm_invalidAlgorithm() { |
| 95 | + NamingHelper helper = NamingHelper.withCharset(StandardCharsets.UTF_8.name()); |
| 96 | + assertThrows(NoSuchAlgorithmException.class, () -> { |
| 97 | + helper.hashWithAlgorithm("table_name", "NOPE"); |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void testHashWithAlgorithm_invalidCharset() { |
| 103 | + NamingHelper helper = NamingHelper.withCharset("NOPE-CHARSET"); |
| 104 | + assertThrows(UnsupportedEncodingException.class, () -> { |
| 105 | + helper.hashWithAlgorithm("table_name", "MD5"); |
| 106 | + }); |
| 107 | + } |
| 108 | + |
60 | 109 | private static Stream<Arguments> args() {
|
61 | 110 | // String charset, String prefix, String tableName, String referencedTableName,
|
62 | 111 | // List<String> columnNames, String expectedFkName, String expectedConstraintName
|
63 | 112 | return Stream.of(
|
64 | 113 | Arguments.of(
|
65 | 114 | StandardCharsets.UTF_8.name(),
|
66 | 115 | "fk_", "table_name", "other_table_name", List.of( "col1", "col2", "col3" ),
|
67 |
| - "fk_ka01ji8vk10osbgp6ve604cm1kfsso7byjr6s294jaukhv3ajq", "fk_kv2dq7fp00eyv1vq5yc29rvc3yftq2fmyg9iacv99wrn3nn0l6" ), |
| 116 | + "fk_f4u43ook9b825fxbm3exb18q6", "fk_1o8k3sa4q2a2wb596v4htt8qf" ), |
68 | 117 | Arguments.of(
|
69 | 118 | StandardCharsets.ISO_8859_1.name(),
|
70 | 119 | "fk_", "table_name", "other_table_name", List.of( "col1", "col2", "col3" ),
|
71 |
| - "fk_ka01ji8vk10osbgp6ve604cm1kfsso7byjr6s294jaukhv3ajq", "fk_kv2dq7fp00eyv1vq5yc29rvc3yftq2fmyg9iacv99wrn3nn0l6" ), |
| 120 | + "fk_f4u43ook9b825fxbm3exb18q6", "fk_1o8k3sa4q2a2wb596v4htt8qf" ), |
72 | 121 | Arguments.of(
|
73 | 122 | StandardCharsets.UTF_8.name(),
|
74 | 123 | "fk_", "café", "le_déjeuner", List.of( "col1", "col2", "col3" ),
|
75 |
| - "fk_ih8sokb1hh74aiucascp5pv0dlecescli8httwu7ca8ggbvxx4", "fk_1mlbg6hqesxj797eo16lo82hd491j5ag67833h3i1k7q99wo9b" ), |
| 124 | + "fk_jdvsrk14lxab6a829ok160vyj", "fk_h34kugb2bguwmcn1g5h1q3snf" ), |
76 | 125 | Arguments.of(
|
77 | 126 | StandardCharsets.ISO_8859_1.name(),
|
78 | 127 | "fk_", "café", "le_déjeuner", List.of( "col1", "col2", "col3" ),
|
79 |
| - "fk_i2tnixfnx9ylanksjrn8u41wvg5cdgl8wr7264olc17srxpa95", "fk_h8iedvm0im7uuapsek0b5wsc5goahu7wvjgtc3a5snqi79outg" ), |
| 128 | + "fk_g1py0mkjd1tu46tr8c2e1vm2l", "fk_1pitt5gtytwpy6ea02o7l5men" ), |
80 | 129 | Arguments.of(
|
81 | 130 | StandardCharsets.UTF_8.name(),
|
82 | 131 | "fk_", "abcdefghijklmnopqrstuvwxyzäöüß", "stuvwxyzäöüß", List.of( "col1" ),
|
83 |
| - "fk_eh9134y0qw0bck215ws5kixdw8v41w26nuq76p5p6vdheyvbpk", "fk_megnb1o6em9hrlel3dvyomlmo41my964kfvdudonbumofve1jx" ), |
| 132 | + "fk_q11mlivmrc3sdfnncd2hwkpqp", "fk_gm8xsqu7ayucv5w5w2gj2dfly" ), |
84 | 133 | Arguments.of(
|
85 | 134 | StandardCharsets.ISO_8859_1.name(),
|
86 | 135 | "fk_", "abcdefghijklmnopqrstuvwxyzäöüß", "stuvwxyzäöüß", List.of( "col1" )
|
87 |
| - , "fk_t8yjwdnsr4el6guwpgnxtlsvcgodr9rtaod8uor849w36552h", "fk_x5u4f3i64gnbca1jxu03q2968mn4b66bb6lbqbtf5apo6ux13" ) |
| 136 | + , "fk_fua9hgc6dn6eno8hlqt58j72o", "fk_3iig3yrgsf5bjlbdo05d7mp2" ) |
88 | 137 | );
|
89 | 138 | }
|
90 | 139 |
|
|
0 commit comments