001package org.eclipse.aether.internal.impl;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import static org.junit.Assert.assertArrayEquals;
023import static org.junit.Assert.assertEquals;
024import static org.junit.Assert.assertSame;
025
026import java.util.Collections;
027import java.util.HashMap;
028import java.util.List;
029import java.util.Map;
030import java.util.concurrent.ThreadFactory;
031
032import org.eclipse.aether.ConfigurationProperties;
033import org.junit.Test;
034
035public class PrioritizedComponentsTest
036{
037
038    @Test
039    public void testGetConfigKeys()
040    {
041        String[] keys =
042            { ConfigurationProperties.PREFIX_PRIORITY + "java.lang.String",
043                ConfigurationProperties.PREFIX_PRIORITY + "String" };
044        assertArrayEquals( keys, PrioritizedComponents.getConfigKeys( String.class ) );
045
046        keys =
047            new String[] { ConfigurationProperties.PREFIX_PRIORITY + "java.util.concurrent.ThreadFactory",
048                ConfigurationProperties.PREFIX_PRIORITY + "ThreadFactory",
049                ConfigurationProperties.PREFIX_PRIORITY + "Thread" };
050        assertArrayEquals( keys, PrioritizedComponents.getConfigKeys( ThreadFactory.class ) );
051    }
052
053    @Test
054    public void testAdd_PriorityOverride()
055    {
056        Exception comp1 = new IllegalArgumentException();
057        Exception comp2 = new NullPointerException();
058        Map<Object, Object> config = new HashMap<>();
059        config.put( ConfigurationProperties.PREFIX_PRIORITY + comp1.getClass().getName(), 6 );
060        config.put( ConfigurationProperties.PREFIX_PRIORITY + comp2.getClass().getName(), 7 );
061        PrioritizedComponents<Exception> components = new PrioritizedComponents<>( config );
062        components.add( comp1, 1 );
063        components.add( comp2, 0 );
064        List<PrioritizedComponent<Exception>> sorted = components.getEnabled();
065        assertEquals( 2, sorted.size() );
066        assertSame( comp2, sorted.get( 0 ).getComponent() );
067        assertEquals( 7, sorted.get( 0 ).getPriority(), 0.1f );
068        assertSame( comp1, sorted.get( 1 ).getComponent() );
069        assertEquals( 6, sorted.get( 1 ).getPriority(), 0.1f );
070    }
071
072    @Test
073    public void testAdd_ImplicitPriority()
074    {
075        Exception comp1 = new IllegalArgumentException();
076        Exception comp2 = new NullPointerException();
077        Map<Object, Object> config = new HashMap<>();
078        config.put( ConfigurationProperties.IMPLICIT_PRIORITIES, true );
079        PrioritizedComponents<Exception> components = new PrioritizedComponents<>( config );
080        components.add( comp1, 1 );
081        components.add( comp2, 2 );
082        List<PrioritizedComponent<Exception>> sorted = components.getEnabled();
083        assertEquals( 2, sorted.size() );
084        assertSame( comp1, sorted.get( 0 ).getComponent() );
085        assertSame( comp2, sorted.get( 1 ).getComponent() );
086    }
087
088    @Test
089    public void testAdd_Disabled()
090    {
091        Exception comp1 = new IllegalArgumentException();
092        Exception comp2 = new NullPointerException();
093        Map<Object, Object> config = new HashMap<>();
094        PrioritizedComponents<Exception> components = new PrioritizedComponents<>( config );
095
096        components.add( new UnsupportedOperationException(), Float.NaN );
097        List<PrioritizedComponent<Exception>> sorted = components.getEnabled();
098        assertEquals( 0, sorted.size() );
099
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}