001package org.eclipse.aether.util.graph.transformer;
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.*;
023
024import java.util.Collection;
025import java.util.LinkedList;
026import java.util.List;
027import java.util.Queue;
028
029import org.eclipse.aether.collection.DependencyGraphTransformer;
030import org.eclipse.aether.graph.DependencyNode;
031import org.eclipse.aether.internal.test.util.DependencyGraphParser;
032import org.eclipse.aether.util.graph.transformer.ConflictIdSorter;
033import org.eclipse.aether.util.graph.transformer.TransformationContextKeys;
034import org.junit.Test;
035
036/**
037 */
038public class ConflictIdSorterTest
039    extends AbstractDependencyGraphTransformerTest
040{
041
042    @Override
043    protected DependencyGraphTransformer newTransformer()
044    {
045        return new ChainedDependencyGraphTransformer( new SimpleConflictMarker(), new ConflictIdSorter() );
046    }
047
048    @Override
049    protected DependencyGraphParser newParser()
050    {
051        return new DependencyGraphParser( "transformer/conflict-id-sorter/" );
052    }
053
054    private void expectOrder( List<String> sorted, String... ids )
055    {
056        Queue<String> queue = new LinkedList<>( sorted );
057
058        for ( String id : ids )
059        {
060            String item = queue.poll();
061            assertNotNull( String.format( "not enough conflict groups (no match for '%s'", id ), item );
062
063            if ( !"*".equals( id ) )
064            {
065                assertEquals( id, item );
066            }
067        }
068
069        assertTrue( String.format( "leftover conflict groups (remaining: '%s')", queue ), queue.isEmpty() );
070    }
071
072    private void expectOrder( String... id )
073    {
074        @SuppressWarnings( "unchecked" )
075        List<String> sorted = (List<String>) context.get( TransformationContextKeys.SORTED_CONFLICT_IDS );
076        expectOrder( sorted, id );
077    }
078
079    private void expectCycle( boolean cycle )
080    {
081        Collection<?> cycles = (Collection<?>) context.get( TransformationContextKeys.CYCLIC_CONFLICT_IDS );
082        assertEquals( cycle, !cycles.isEmpty() );
083    }
084
085    @Test
086    public void testSimple()
087        throws Exception
088    {
089        DependencyNode node = parseResource( "simple.txt" );
090        assertSame( node, transform( node ) );
091
092        expectOrder( "gid2:aid::jar", "gid:aid::jar", "gid:aid2::jar" );
093        expectCycle( false );
094    }
095
096    @Test
097    public void testCycle()
098        throws Exception
099    {
100        DependencyNode node = parseResource( "cycle.txt" );
101        assertSame( node, transform( node ) );
102
103        expectOrder( "gid:aid::jar", "gid2:aid::jar" );
104        expectCycle( true );
105    }
106
107    @Test
108    public void testCycles()
109        throws Exception
110    {
111        DependencyNode node = parseResource( "cycles.txt" );
112        assertSame( node, transform( node ) );
113
114        expectOrder( "*", "*", "*", "gid:aid::jar" );
115        expectCycle( true );
116    }
117
118    @Test
119    public void testNoConflicts()
120        throws Exception
121    {
122        DependencyNode node = parseResource( "no-conflicts.txt" );
123        assertSame( node, transform( node ) );
124
125        expectOrder( "gid:aid::jar", "gid3:aid::jar", "gid2:aid::jar", "gid4:aid::jar" );
126        expectCycle( false );
127    }
128
129}