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