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.*;
38  
39  public abstract class AbstractVersionFilterTest {
40  
41      protected DefaultRepositorySystemSession session;
42  
43      @Before
44      public void setUp() {
45          session = TestUtils.newSession();
46      }
47  
48      @After
49      public void tearDown() {
50          session = null;
51      }
52  
53      protected VersionFilter.VersionFilterContext newContext(String gav, String... versions) {
54          VersionRangeRequest request = new VersionRangeRequest();
55          request.setArtifact(new DefaultArtifact(gav));
56          VersionRangeResult result = new VersionRangeResult(request);
57          VersionScheme scheme = new GenericVersionScheme();
58          try {
59              result.setVersionConstraint(
60                      scheme.parseVersionConstraint(request.getArtifact().getVersion()));
61              for (String version : versions) {
62                  result.addVersion(scheme.parseVersion(version));
63              }
64          } catch (InvalidVersionSpecificationException e) {
65              throw new IllegalArgumentException(e);
66          }
67          return TestUtils.newVersionFilterContext(session, result);
68      }
69  
70      protected VersionFilter derive(VersionFilter filter, String gav) {
71          return filter.deriveChildFilter(
72                  TestUtils.newCollectionContext(session, new Dependency(new DefaultArtifact(gav), ""), null));
73      }
74  
75      protected void assertVersions(VersionFilter.VersionFilterContext context, String... versions) {
76          assertEquals(versions.length, context.getCount());
77          Iterator<Version> it = context.iterator();
78          for (String version : versions) {
79              assertTrue(it.hasNext());
80              assertEquals(version, it.next().toString());
81          }
82      }
83  }