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