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.HashMap;
027import java.util.List;
028import java.util.Map;
029import java.util.concurrent.ThreadFactory;
030
031import org.eclipse.aether.ConfigurationProperties;
032import org.junit.Test;
033
034public class PrioritizedComponentsTest
035{
036
037    @Test
038    public void testGetConfigKeys()
039    {
040        String[] keys =
041            { ConfigurationProperties.PREFIX_PRIORITY + "java.lang.String",
042                ConfigurationProperties.PREFIX_PRIORITY + "String" };
043        assertArrayEquals( keys, PrioritizedComponents.getConfigKeys( String.class ) );
044
045        keys =
046            new String[] { ConfigurationProperties.PREFIX_PRIORITY + "java.util.concurrent.ThreadFactory",
047                ConfigurationProperties.PREFIX_PRIORITY + "ThreadFactory",
048                ConfigurationProperties.PREFIX_PRIORITY + "Thread" };
049        assertArrayEquals( keys, PrioritizedComponents.getConfigKeys( ThreadFactory.class ) );
050    }
051
052    @Test
053    public void testAdd_PriorityOverride()
054    {
055        Exception comp1 = new IllegalArgumentException();
056        Exception comp2 = new NullPointerException();
057        Map<Object, Object> config = new HashMap<Object, Object>();
058        config.put( ConfigurationProperties.PREFIX_PRIORITY + comp1.getClass().getName(), 6 );
059        config.put( ConfigurationProperties.PREFIX_PRIORITY + comp2.getClass().getName(), 7 );
060        PrioritizedComponents<Exception> components = new PrioritizedComponents<Exception>( config );
061        components.add( comp1, 1 );
062        components.add( comp2, 0 );
063        List<PrioritizedComponent<Exception>> sorted = components.getEnabled();
064        assertEquals( 2, sorted.size() );
065        assertSame( comp2, sorted.get( 0 ).getComponent() );
066        assertEquals( 7, sorted.get( 0 ).getPriority(), 0.1f );
067        assertSame( comp1, sorted.get( 1 ).getComponent() );
068        assertEquals( 6, sorted.get( 1 ).getPriority(), 0.1f );
069    }
070
071    @Test
072    public void testAdd_ImplicitPriority()
073    {
074        Exception comp1 = new IllegalArgumentException();
075        Exception comp2 = new NullPointerException();
076        Map<Object, Object> config = new HashMap<Object, Object>();
077        config.put( ConfigurationProperties.IMPLICIT_PRIORITIES, true );
078        PrioritizedComponents<Exception> components = new PrioritizedComponents<Exception>( config );
079        components.add( comp1, 1 );
080        components.add( comp2, 2 );
081        List<PrioritizedComponent<Exception>> sorted = components.getEnabled();
082        assertEquals( 2, sorted.size() );
083        assertSame( comp1, sorted.get( 0 ).getComponent() );
084        assertSame( comp2, sorted.get( 1 ).getComponent() );
085    }
086
087    @Test
088    public void testAdd_Disabled()
089    {
090        Exception comp1 = new IllegalArgumentException();
091        Exception comp2 = new NullPointerException();
092        Map<Object, Object> config = new HashMap<Object, Object>();
093        PrioritizedComponents<Exception> components = new PrioritizedComponents<Exception>( config );
094
095        components.add( new UnsupportedOperationException(), Float.NaN );
096        List<PrioritizedComponent<Exception>> sorted = components.getEnabled();
097        assertEquals( 0, sorted.size() );
098
099        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}