001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.internal.test.util;
020
021import java.io.File;
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027
028import org.eclipse.aether.artifact.Artifact;
029import org.eclipse.aether.artifact.DefaultArtifact;
030import org.eclipse.aether.graph.DefaultDependencyNode;
031import org.eclipse.aether.graph.Dependency;
032import org.eclipse.aether.graph.DependencyNode;
033import org.eclipse.aether.version.InvalidVersionSpecificationException;
034import org.eclipse.aether.version.VersionScheme;
035
036/**
037 * A builder to create dependency nodes for unit testing.
038 */
039public class NodeBuilder {
040
041    private String groupId = "test";
042
043    private String artifactId = "";
044
045    private String version = "0.1";
046
047    private String range;
048
049    private String ext = "jar";
050
051    private final String classifier = "";
052
053    private String scope = "compile";
054
055    private boolean optional = false;
056
057    private String context;
058
059    private final List<Artifact> relocations = new ArrayList<>();
060
061    private final VersionScheme versionScheme = new TestVersionScheme();
062
063    private Map<String, String> properties = new HashMap<>(0);
064
065    public NodeBuilder artifactId(String artifactId) {
066        this.artifactId = artifactId;
067        return this;
068    }
069
070    public NodeBuilder groupId(String groupId) {
071        this.groupId = groupId;
072        return this;
073    }
074
075    public NodeBuilder ext(String ext) {
076        this.ext = ext;
077        return this;
078    }
079
080    public NodeBuilder version(String version) {
081        this.version = version;
082        this.range = null;
083        return this;
084    }
085
086    public NodeBuilder range(String range) {
087        this.range = range;
088        return this;
089    }
090
091    public NodeBuilder scope(String scope) {
092        this.scope = scope;
093        return this;
094    }
095
096    public NodeBuilder optional(boolean optional) {
097        this.optional = optional;
098        return this;
099    }
100
101    public NodeBuilder context(String context) {
102        this.context = context;
103        return this;
104    }
105
106    public NodeBuilder reloc(String artifactId) {
107        Artifact relocation = new DefaultArtifact(groupId, artifactId, classifier, ext, version);
108        relocations.add(relocation);
109        return this;
110    }
111
112    public NodeBuilder reloc(String groupId, String artifactId, String version) {
113        Artifact relocation = new DefaultArtifact(groupId, artifactId, classifier, ext, version);
114        relocations.add(relocation);
115        return this;
116    }
117
118    public NodeBuilder properties(Map<String, String> properties) {
119        this.properties = properties != null ? properties : Collections.<String, String>emptyMap();
120        return this;
121    }
122
123    public DependencyNode build() {
124        Dependency dependency = null;
125        if (artifactId != null && !artifactId.isEmpty()) {
126            Artifact artifact =
127                    new DefaultArtifact(groupId, artifactId, classifier, ext, version, properties, (File) null);
128            dependency = new Dependency(artifact, scope, optional);
129        }
130        DefaultDependencyNode node = new DefaultDependencyNode(dependency);
131        if (artifactId != null && !artifactId.isEmpty()) {
132            try {
133                node.setVersion(versionScheme.parseVersion(version));
134                node.setVersionConstraint(versionScheme.parseVersionConstraint(range != null ? range : version));
135            } catch (InvalidVersionSpecificationException e) {
136                throw new IllegalArgumentException("bad version: " + e.getMessage(), e);
137            }
138        }
139        node.setRequestContext(context);
140        node.setRelocations(relocations);
141        return node;
142    }
143}