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