1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether;
20
21 import java.net.URL;
22 import java.net.URLClassLoader;
23 import java.nio.file.Paths;
24
25 import org.junit.jupiter.api.Test;
26
27 import static org.junit.jupiter.api.Assertions.assertEquals;
28 import static org.junit.jupiter.api.Assertions.assertNotEquals;
29 import static org.junit.jupiter.api.Assertions.assertNotSame;
30 import static org.junit.jupiter.api.Assertions.assertThrows;
31
32 public class KeysTest {
33 @Test
34 void noElement() {
35 assertThrows(IllegalArgumentException.class, Keys::of);
36 }
37
38 @Test
39 void nullVararg() {
40 assertThrows(NullPointerException.class, () -> Keys.of((Object[]) null));
41 }
42
43 @Test
44 void globalKeys() {
45 Object key1 = Keys.of("one", "two", "three");
46 Object key2 = Keys.of("one", "two", "three");
47 Object key3 = Keys.of("one", "two");
48 assertNotSame(key1, key2);
49 assertNotSame(key2, key3);
50 assertNotSame(key1, key3);
51
52 assertEquals(key1, key2);
53 assertNotEquals(key1, key3);
54 assertEquals(key1.hashCode(), key2.hashCode());
55 assertNotEquals(key1.hashCode(), key3.hashCode());
56 }
57
58 @Test
59 void classLoaderAwareKeys() throws Exception {
60 String classBinaryName = Keys.class.getName();
61 try (URLClassLoader altLoader = new URLClassLoader(
62 new URL[] {Paths.get("target/classes").toUri().toURL()}, null)) {
63 Class<?> thisClass = Keys.class.getClassLoader().loadClass(classBinaryName);
64 Class<?> otherClass = altLoader.loadClass(classBinaryName);
65
66 Object key1 = Keys.of(thisClass, "one", "two");
67 Object key2 = Keys.of(thisClass, "one", "two");
68 Object key3 = Keys.of(otherClass, "one", "two");
69 assertNotSame(key1, key2);
70 assertNotSame(key2, key3);
71 assertNotSame(key1, key3);
72
73 assertEquals(key1, key2);
74 assertNotEquals(key1, key3);
75 assertEquals(key1.hashCode(), key2.hashCode());
76 assertNotEquals(key1.hashCode(), key3.hashCode());
77 }
78 }
79
80 @Test
81 void privateKeys() {
82 Object one = new Object();
83 Object two = new Object();
84 Object key1 = Keys.of(one, "one", "two");
85 Object key2 = Keys.of(one, "one", "two");
86 Object key3 = Keys.of(two, "one", "two");
87 assertNotSame(key1, key2);
88 assertNotSame(key2, key3);
89 assertNotSame(key1, key3);
90
91 assertEquals(key1, key2);
92 assertNotEquals(key1, key3);
93 assertEquals(key1.hashCode(), key2.hashCode());
94 assertNotEquals(key1.hashCode(), key3.hashCode());
95 }
96
97 @Test
98 void subKey() {
99 Object key1 = Keys.of("one", "two", "three");
100 Object key2 = Keys.of("one", "two");
101 assertNotSame(key1, key2);
102
103 assertNotEquals(key1, key2);
104 assertNotEquals(key1.hashCode(), key2.hashCode());
105
106 Object key3 = Keys.of(key2, "three");
107 assertNotSame(key1, key3);
108
109 assertEquals(key1, key3);
110 assertEquals(key1.hashCode(), key3.hashCode());
111 }
112 }