View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.internal.test.util;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.eclipse.aether.artifact.Artifact;
29  import org.eclipse.aether.artifact.DefaultArtifact;
30  import org.eclipse.aether.graph.DefaultDependencyNode;
31  import org.eclipse.aether.graph.Dependency;
32  import org.eclipse.aether.graph.DependencyNode;
33  import org.eclipse.aether.version.InvalidVersionSpecificationException;
34  import org.eclipse.aether.version.VersionScheme;
35  
36  /**
37   * A builder to create dependency nodes for unit testing.
38   */
39  public class NodeBuilder {
40  
41      private String groupId = "test";
42  
43      private String artifactId = "";
44  
45      private String version = "0.1";
46  
47      private String range;
48  
49      private String ext = "jar";
50  
51      private final String classifier = "";
52  
53      private String scope = "compile";
54  
55      private boolean optional = false;
56  
57      private String context;
58  
59      private final List<Artifact> relocations = new ArrayList<>();
60  
61      private final VersionScheme versionScheme = new TestVersionScheme();
62  
63      private Map<String, String> properties = new HashMap<>(0);
64  
65      public NodeBuilder artifactId(String artifactId) {
66          this.artifactId = artifactId;
67          return this;
68      }
69  
70      public NodeBuilder groupId(String groupId) {
71          this.groupId = groupId;
72          return this;
73      }
74  
75      public NodeBuilder ext(String ext) {
76          this.ext = ext;
77          return this;
78      }
79  
80      public NodeBuilder version(String version) {
81          this.version = version;
82          this.range = null;
83          return this;
84      }
85  
86      public NodeBuilder range(String range) {
87          this.range = range;
88          return this;
89      }
90  
91      public NodeBuilder scope(String scope) {
92          this.scope = scope;
93          return this;
94      }
95  
96      public NodeBuilder optional(boolean optional) {
97          this.optional = optional;
98          return this;
99      }
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.length() > 0) {
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.length() > 0) {
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 }