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.LinkedList;
025import java.util.List;
026
027import org.eclipse.aether.DefaultRepositorySystemSession;
028import org.eclipse.aether.collection.DependencyGraphTransformationContext;
029import org.eclipse.aether.collection.DependencyGraphTransformer;
030import org.eclipse.aether.graph.DependencyNode;
031import org.eclipse.aether.internal.test.util.DependencyGraphParser;
032import org.eclipse.aether.internal.test.util.TestUtils;
033import org.junit.After;
034import org.junit.Before;
035
036/**
037 */
038public abstract class AbstractDependencyGraphTransformerTest
039{
040
041    protected DependencyGraphTransformer transformer;
042
043    protected DependencyGraphParser parser;
044
045    protected DefaultRepositorySystemSession session;
046
047    protected DependencyGraphTransformationContext context;
048
049    protected abstract DependencyGraphTransformer newTransformer();
050
051    protected abstract DependencyGraphParser newParser();
052
053    protected DependencyNode transform( DependencyNode root )
054        throws Exception
055    {
056        context = TestUtils.newTransformationContext( session );
057        root = transformer.transformGraph( root, context );
058        assertNotNull( root );
059        return root;
060    }
061
062    protected DependencyNode parseResource( String resource, String... substitutions )
063        throws Exception
064    {
065        parser.setSubstitutions( substitutions );
066        return parser.parseResource( resource );
067    }
068
069    protected DependencyNode parseLiteral( String literal, String... substitutions )
070        throws Exception
071    {
072        parser.setSubstitutions( substitutions );
073        return parser.parseLiteral( literal );
074    }
075
076    protected List<DependencyNode> find( DependencyNode node, String id )
077    {
078        LinkedList<DependencyNode> trail = new LinkedList<>();
079        find( trail, node, id );
080        return trail;
081    }
082
083    private boolean find( LinkedList<DependencyNode> trail, DependencyNode node, String id )
084    {
085        trail.addFirst( node );
086
087        if ( isMatch( node, id ) )
088        {
089            return true;
090        }
091
092        for ( DependencyNode child : node.getChildren() )
093        {
094            if ( find( trail, child, id ) )
095            {
096                return true;
097            }
098        }
099
100        trail.removeFirst();
101
102        return false;
103    }
104
105    private boolean isMatch( DependencyNode node, String id )
106    {
107        if ( node.getDependency() == null )
108        {
109            return false;
110        }
111        return id.equals( node.getDependency().getArtifact().getArtifactId() );
112    }
113
114    @Before
115    public void setUp()
116    {
117        transformer = newTransformer();
118        parser = newParser();
119        session = new DefaultRepositorySystemSession();
120    }
121
122    @After
123    public void tearDown()
124    {
125        transformer = null;
126        parser = null;
127        session = null;
128        context = null;
129    }
130
131}