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