View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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 }