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.util.graph.versions;
20  
21  import java.util.Iterator;
22  
23  import org.eclipse.aether.DefaultRepositorySystemSession;
24  import org.eclipse.aether.artifact.DefaultArtifact;
25  import org.eclipse.aether.collection.VersionFilter;
26  import org.eclipse.aether.graph.Dependency;
27  import org.eclipse.aether.internal.test.util.TestUtils;
28  import org.eclipse.aether.resolution.VersionRangeRequest;
29  import org.eclipse.aether.resolution.VersionRangeResult;
30  import org.eclipse.aether.util.version.GenericVersionScheme;
31  import org.eclipse.aether.version.InvalidVersionSpecificationException;
32  import org.eclipse.aether.version.Version;
33  import org.eclipse.aether.version.VersionScheme;
34  import org.junit.After;
35  import org.junit.Before;
36  
37  import static org.junit.Assert.assertEquals;
38  import static org.junit.Assert.assertTrue;
39  
40  public abstract class AbstractVersionFilterTest {
41  
42      protected DefaultRepositorySystemSession session;
43  
44      @Before
45      public void setUp() {
46          session = TestUtils.newSession();
47      }
48  
49      @After
50      public void tearDown() {
51          session = null;
52      }
53  
54      protected VersionFilter.VersionFilterContext newContext(String gav, String... versions) {
55          VersionRangeRequest request = new VersionRangeRequest();
56          request.setArtifact(new DefaultArtifact(gav));
57          VersionRangeResult result = new VersionRangeResult(request);
58          VersionScheme scheme = new GenericVersionScheme();
59          try {
60              result.setVersionConstraint(
61                      scheme.parseVersionConstraint(request.getArtifact().getVersion()));
62              for (String version : versions) {
63                  result.addVersion(scheme.parseVersion(version));
64              }
65          } catch (InvalidVersionSpecificationException e) {
66              throw new IllegalArgumentException(e);
67          }
68          return TestUtils.newVersionFilterContext(session, result);
69      }
70  
71      protected VersionFilter derive(VersionFilter filter, String gav) {
72          return filter.deriveChildFilter(
73                  TestUtils.newCollectionContext(session, new Dependency(new DefaultArtifact(gav), ""), null));
74      }
75  
76      protected void assertVersions(VersionFilter.VersionFilterContext context, String... versions) {
77          assertEquals(versions.length, context.getCount());
78          Iterator<Version> it = context.iterator();
79          for (String version : versions) {
80              assertTrue(it.hasNext());
81              assertEquals(version, it.next().toString());
82          }
83      }
84  }