001package org.eclipse.aether.internal.test.util;
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 java.io.File;
023import java.util.ArrayList;
024import java.util.Collections;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028
029import org.eclipse.aether.artifact.Artifact;
030import org.eclipse.aether.artifact.DefaultArtifact;
031import org.eclipse.aether.graph.DefaultDependencyNode;
032import org.eclipse.aether.graph.Dependency;
033import org.eclipse.aether.graph.DependencyNode;
034import org.eclipse.aether.version.InvalidVersionSpecificationException;
035import org.eclipse.aether.version.VersionScheme;
036
037/**
038 * A builder to create dependency nodes for unit testing.
039 */
040public class NodeBuilder
041{
042
043    private String groupId = "test";
044
045    private String artifactId = "";
046
047    private String version = "0.1";
048
049    private String range;
050
051    private String ext = "jar";
052
053    private final String classifier = "";
054
055    private String scope = "compile";
056
057    private boolean optional = false;
058
059    private String context;
060
061    private final List<Artifact> relocations = new ArrayList<>();
062
063    private final VersionScheme versionScheme = new TestVersionScheme();
064
065    private Map<String, String> properties = new HashMap<>( 0 );
066
067    public NodeBuilder artifactId( String artifactId )
068    {
069        this.artifactId = artifactId;
070        return this;
071    }
072
073    public NodeBuilder groupId( String groupId )
074    {
075        this.groupId = groupId;
076        return this;
077
078    }
079
080    public NodeBuilder ext( String ext )
081    {
082        this.ext = ext;
083        return this;
084    }
085
086    public NodeBuilder version( String version )
087    {
088        this.version = version;
089        this.range = null;
090        return this;
091    }
092
093    public NodeBuilder range( String range )
094    {
095        this.range = range;
096        return this;
097    }
098
099    public NodeBuilder scope( String scope )
100    {
101        this.scope = scope;
102        return this;
103    }
104
105    public NodeBuilder optional( boolean optional )
106    {
107        this.optional = optional;
108        return this;
109    }
110
111    public NodeBuilder context( String context )
112    {
113        this.context = context;
114        return this;
115    }
116
117    public NodeBuilder reloc( String artifactId )
118    {
119        Artifact relocation = new DefaultArtifact( groupId, artifactId, classifier, ext, version );
120        relocations.add( relocation );
121        return this;
122    }
123
124    public NodeBuilder reloc( String groupId, String artifactId, String version )
125    {
126        Artifact relocation = new DefaultArtifact( groupId, artifactId, classifier, ext, version );
127        relocations.add( relocation );
128        return this;
129    }
130
131    public NodeBuilder properties( Map<String, String> properties )
132    {
133        this.properties = properties != null ? properties : Collections.<String, String>emptyMap();
134        return this;
135    }
136
137    public DependencyNode build()
138    {
139        Dependency dependency = null;
140        if ( artifactId != null && artifactId.length() > 0 )
141        {
142            Artifact artifact =
143                new DefaultArtifact( groupId, artifactId, classifier, ext, version, properties, (File) null );
144            dependency = new Dependency( artifact, scope, optional );
145        }
146        DefaultDependencyNode node = new DefaultDependencyNode( dependency );
147        if ( artifactId != null && artifactId.length() > 0 )
148        {
149            try
150            {
151                node.setVersion( versionScheme.parseVersion( version ) );
152                node.setVersionConstraint( versionScheme.parseVersionConstraint( range != null ? range : version ) );
153            }
154            catch ( InvalidVersionSpecificationException e )
155            {
156                throw new IllegalArgumentException( "bad version: " + e.getMessage(), e );
157            }
158        }
159        node.setRequestContext( context );
160        node.setRelocations( relocations );
161        return node;
162    }
163
164}