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.internal.impl;
20  
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.concurrent.ThreadFactory;
26  
27  import org.eclipse.aether.ConfigurationProperties;
28  import org.junit.jupiter.api.Test;
29  
30  import static org.junit.jupiter.api.Assertions.*;
31  
32  public class PrioritizedComponentsTest {
33  
34      @Test
35      void testGetConfigKeys() {
36          String[] keys = {
37              ConfigurationProperties.PREFIX_PRIORITY + "java.lang.String",
38              ConfigurationProperties.PREFIX_PRIORITY + "String"
39          };
40          assertArrayEquals(keys, PrioritizedComponents.getConfigKeys(String.class));
41  
42          keys = new String[] {
43              ConfigurationProperties.PREFIX_PRIORITY + "java.util.concurrent.ThreadFactory",
44              ConfigurationProperties.PREFIX_PRIORITY + "ThreadFactory",
45              ConfigurationProperties.PREFIX_PRIORITY + "Thread"
46          };
47          assertArrayEquals(keys, PrioritizedComponents.getConfigKeys(ThreadFactory.class));
48      }
49  
50      @Test
51      void testAdd_PriorityOverride() {
52          Exception comp1 = new IllegalArgumentException();
53          Exception comp2 = new NullPointerException();
54          Map<Object, Object> config = new HashMap<>();
55          config.put(ConfigurationProperties.PREFIX_PRIORITY + comp1.getClass().getName(), 6);
56          config.put(ConfigurationProperties.PREFIX_PRIORITY + comp2.getClass().getName(), 7);
57          PrioritizedComponents<Exception> components = new PrioritizedComponents<>(config);
58          components.add(comp1, 1);
59          components.add(comp2, 0);
60          List<PrioritizedComponent<Exception>> sorted = components.getEnabled();
61          assertEquals(2, sorted.size());
62          assertSame(comp2, sorted.get(0).getComponent());
63          assertEquals(7, sorted.get(0).getPriority(), 0.1f);
64          assertSame(comp1, sorted.get(1).getComponent());
65          assertEquals(6, sorted.get(1).getPriority(), 0.1f);
66      }
67  
68      @Test
69      void testAdd_ImplicitPriority() {
70          Exception comp1 = new IllegalArgumentException();
71          Exception comp2 = new NullPointerException();
72          Map<Object, Object> config = new HashMap<>();
73          config.put(ConfigurationProperties.IMPLICIT_PRIORITIES, true);
74          PrioritizedComponents<Exception> components = new PrioritizedComponents<>(config);
75          components.add(comp1, 1);
76          components.add(comp2, 2);
77          List<PrioritizedComponent<Exception>> sorted = components.getEnabled();
78          assertEquals(2, sorted.size());
79          assertSame(comp1, sorted.get(0).getComponent());
80          assertSame(comp2, sorted.get(1).getComponent());
81      }
82  
83      @Test
84      void testAdd_Disabled() {
85          Exception comp1 = new IllegalArgumentException();
86          Exception comp2 = new NullPointerException();
87          Map<Object, Object> config = new HashMap<>();
88          PrioritizedComponents<Exception> components = new PrioritizedComponents<>(config);
89  
90          components.add(new UnsupportedOperationException(), Float.NaN);
91          List<PrioritizedComponent<Exception>> sorted = components.getEnabled();
92          assertEquals(0, sorted.size());
93  
94          components.add(comp1, 1);
95          sorted = components.getEnabled();
96          assertEquals(1, sorted.size());
97          assertSame(comp1, sorted.get(0).getComponent());
98  
99          components.add(new Exception(), Float.NaN);
100         sorted = components.getEnabled();
101         assertEquals(1, sorted.size());
102         assertSame(comp1, sorted.get(0).getComponent());
103 
104         components.add(comp2, 0);
105         sorted = components.getEnabled();
106         assertEquals(2, sorted.size());
107         assertSame(comp1, sorted.get(0).getComponent());
108         assertSame(comp2, sorted.get(1).getComponent());
109     }
110 
111     @Test
112     void testList() {
113         Exception comp1 = new IllegalArgumentException();
114         Exception comp2 = new NullPointerException();
115 
116         PrioritizedComponents<Exception> components = new PrioritizedComponents<>(Collections.emptyMap());
117         components.add(comp1, 1);
118         components.add(comp2, 0);
119 
120         StringBuilder stringBuilder = new StringBuilder();
121         components.list(stringBuilder);
122 
123         assertEquals("IllegalArgumentException, NullPointerException", stringBuilder.toString());
124     }
125 }