View Javadoc
1   package org.eclipse.aether.internal.impl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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.Collections;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  import java.util.concurrent.ThreadFactory;
31  
32  import org.eclipse.aether.ConfigurationProperties;
33  import org.junit.Test;
34  
35  public class PrioritizedComponentsTest
36  {
37  
38      @Test
39      public void testGetConfigKeys()
40      {
41          String[] keys =
42              { ConfigurationProperties.PREFIX_PRIORITY + "java.lang.String",
43                  ConfigurationProperties.PREFIX_PRIORITY + "String" };
44          assertArrayEquals( keys, PrioritizedComponents.getConfigKeys( String.class ) );
45  
46          keys =
47              new String[] { ConfigurationProperties.PREFIX_PRIORITY + "java.util.concurrent.ThreadFactory",
48                  ConfigurationProperties.PREFIX_PRIORITY + "ThreadFactory",
49                  ConfigurationProperties.PREFIX_PRIORITY + "Thread" };
50          assertArrayEquals( keys, PrioritizedComponents.getConfigKeys( ThreadFactory.class ) );
51      }
52  
53      @Test
54      public void testAdd_PriorityOverride()
55      {
56          Exception comp1 = new IllegalArgumentException();
57          Exception comp2 = new NullPointerException();
58          Map<Object, Object> config = new HashMap<>();
59          config.put( ConfigurationProperties.PREFIX_PRIORITY + comp1.getClass().getName(), 6 );
60          config.put( ConfigurationProperties.PREFIX_PRIORITY + comp2.getClass().getName(), 7 );
61          PrioritizedComponents<Exception> components = new PrioritizedComponents<>( config );
62          components.add( comp1, 1 );
63          components.add( comp2, 0 );
64          List<PrioritizedComponent<Exception>> sorted = components.getEnabled();
65          assertEquals( 2, sorted.size() );
66          assertSame( comp2, sorted.get( 0 ).getComponent() );
67          assertEquals( 7, sorted.get( 0 ).getPriority(), 0.1f );
68          assertSame( comp1, sorted.get( 1 ).getComponent() );
69          assertEquals( 6, sorted.get( 1 ).getPriority(), 0.1f );
70      }
71  
72      @Test
73      public void testAdd_ImplicitPriority()
74      {
75          Exception comp1 = new IllegalArgumentException();
76          Exception comp2 = new NullPointerException();
77          Map<Object, Object> config = new HashMap<>();
78          config.put( ConfigurationProperties.IMPLICIT_PRIORITIES, true );
79          PrioritizedComponents<Exception> components = new PrioritizedComponents<>( config );
80          components.add( comp1, 1 );
81          components.add( comp2, 2 );
82          List<PrioritizedComponent<Exception>> sorted = components.getEnabled();
83          assertEquals( 2, sorted.size() );
84          assertSame( comp1, sorted.get( 0 ).getComponent() );
85          assertSame( comp2, sorted.get( 1 ).getComponent() );
86      }
87  
88      @Test
89      public void testAdd_Disabled()
90      {
91          Exception comp1 = new IllegalArgumentException();
92          Exception comp2 = new NullPointerException();
93          Map<Object, Object> config = new HashMap<>();
94          PrioritizedComponents<Exception> components = new PrioritizedComponents<>( config );
95  
96          components.add( new UnsupportedOperationException(), Float.NaN );
97          List<PrioritizedComponent<Exception>> sorted = components.getEnabled();
98          assertEquals( 0, sorted.size() );
99  
100         components.add( comp1, 1 );
101         sorted = components.getEnabled();
102         assertEquals( 1, sorted.size() );
103         assertSame( comp1, sorted.get( 0 ).getComponent() );
104 
105         components.add( new Exception(), Float.NaN );
106         sorted = components.getEnabled();
107         assertEquals( 1, sorted.size() );
108         assertSame( comp1, sorted.get( 0 ).getComponent() );
109 
110         components.add( comp2, 0 );
111         sorted = components.getEnabled();
112         assertEquals( 2, sorted.size() );
113         assertSame( comp1, sorted.get( 0 ).getComponent() );
114         assertSame( comp2, sorted.get( 1 ).getComponent() );
115     }
116 
117     @Test
118     public void testList()
119     {
120         Exception comp1 = new IllegalArgumentException();
121         Exception comp2 = new NullPointerException();
122 
123         PrioritizedComponents<Exception> components = new PrioritizedComponents<>( Collections.emptyMap() );
124         components.add( comp1, 1 );
125         components.add( comp2, 0 );
126 
127         StringBuilder stringBuilder = new StringBuilder();
128         components.list( stringBuilder );
129 
130         assertEquals( "IllegalArgumentException, NullPointerException", stringBuilder.toString() );
131     }
132 }