001package org.eclipse.aether.util.graph.versions; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import static org.junit.Assert.*; 023 024import java.util.Iterator; 025 026import org.eclipse.aether.DefaultRepositorySystemSession; 027import org.eclipse.aether.artifact.DefaultArtifact; 028import org.eclipse.aether.collection.VersionFilter; 029import org.eclipse.aether.graph.Dependency; 030import org.eclipse.aether.internal.test.util.TestUtils; 031import org.eclipse.aether.resolution.VersionRangeRequest; 032import org.eclipse.aether.resolution.VersionRangeResult; 033import org.eclipse.aether.util.version.GenericVersionScheme; 034import org.eclipse.aether.version.InvalidVersionSpecificationException; 035import org.eclipse.aether.version.Version; 036import org.eclipse.aether.version.VersionScheme; 037import org.junit.After; 038import org.junit.Before; 039 040public abstract class AbstractVersionFilterTest 041{ 042 043 protected DefaultRepositorySystemSession session; 044 045 @Before 046 public void setUp() 047 { 048 session = TestUtils.newSession(); 049 } 050 051 @After 052 public void tearDown() 053 { 054 session = null; 055 } 056 057 protected VersionFilter.VersionFilterContext newContext( String gav, String... versions ) 058 { 059 VersionRangeRequest request = new VersionRangeRequest(); 060 request.setArtifact( new DefaultArtifact( gav ) ); 061 VersionRangeResult result = new VersionRangeResult( request ); 062 VersionScheme scheme = new GenericVersionScheme(); 063 try 064 { 065 result.setVersionConstraint( scheme.parseVersionConstraint( request.getArtifact().getVersion() ) ); 066 for ( String version : versions ) 067 { 068 result.addVersion( scheme.parseVersion( version ) ); 069 } 070 } 071 catch ( InvalidVersionSpecificationException e ) 072 { 073 throw new IllegalArgumentException( e ); 074 } 075 return TestUtils.newVersionFilterContext( session, result ); 076 } 077 078 protected VersionFilter derive( VersionFilter filter, String gav ) 079 { 080 return filter.deriveChildFilter( TestUtils.newCollectionContext( session, 081 new Dependency( new DefaultArtifact( gav ), "" ), 082 null ) ); 083 } 084 085 protected void assertVersions( VersionFilter.VersionFilterContext context, String... versions ) 086 { 087 assertEquals( versions.length, context.getCount() ); 088 Iterator<Version> it = context.iterator(); 089 for ( String version : versions ) 090 { 091 assertTrue( it.hasNext() ); 092 assertEquals( version, it.next().toString() ); 093 } 094 } 095 096}