001package org.eclipse.aether.util.version;
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 org.eclipse.aether.util.version.GenericVersion;
025import org.eclipse.aether.util.version.GenericVersionScheme;
026import org.eclipse.aether.version.InvalidVersionSpecificationException;
027import org.eclipse.aether.version.VersionConstraint;
028import org.junit.Before;
029import org.junit.Test;
030
031/**
032 */
033public class GenericVersionSchemeTest
034{
035
036    private GenericVersionScheme scheme;
037
038    @Before
039    public void setUp()
040        throws Exception
041    {
042        scheme = new GenericVersionScheme();
043    }
044
045    private InvalidVersionSpecificationException parseInvalid( String constraint )
046    {
047        try
048        {
049            scheme.parseVersionConstraint( constraint );
050            fail( "expected exception for constraint " + constraint );
051            return null;
052        }
053        catch ( InvalidVersionSpecificationException e )
054        {
055            return e;
056        }
057    }
058
059    @Test
060    public void testEnumeratedVersions()
061        throws InvalidVersionSpecificationException
062    {
063        VersionConstraint c = scheme.parseVersionConstraint( "1.0" );
064        assertEquals( "1.0", c.getVersion().toString() );
065        assertTrue( c.containsVersion( new GenericVersion( "1.0" ) ) );
066
067        c = scheme.parseVersionConstraint( "[1.0]" );
068        assertEquals( null, c.getVersion() );
069        assertTrue( c.containsVersion( new GenericVersion( "1.0" ) ) );
070
071        c = scheme.parseVersionConstraint( "[1.0],[2.0]" );
072        assertTrue( c.containsVersion( new GenericVersion( "1.0" ) ) );
073        assertTrue( c.containsVersion( new GenericVersion( "2.0" ) ) );
074
075        c = scheme.parseVersionConstraint( "[1.0],[2.0],[3.0]" );
076        assertContains( c, "1.0", "2.0", "3.0" );
077        assertNotContains( c, "1.5" );
078
079        c = scheme.parseVersionConstraint( "[1,3),(3,5)" );
080        assertContains( c, "1", "2", "4" );
081        assertNotContains( c, "3", "5" );
082
083        c = scheme.parseVersionConstraint( "[1,3),(3,)" );
084        assertContains( c, "1", "2", "4" );
085        assertNotContains( c, "3" );
086    }
087
088    private void assertNotContains( VersionConstraint c, String... versions )
089    {
090        assertContains( String.format( "%s: %%s should not be contained\n", c.toString() ), c, false, versions );
091    }
092
093    private void assertContains( String msg, VersionConstraint c, boolean b, String... versions )
094    {
095        for ( String v : versions )
096        {
097            assertEquals( String.format( msg, v ), b, c.containsVersion( new GenericVersion( v ) ) );
098        }
099    }
100
101    private void assertContains( VersionConstraint c, String... versions )
102    {
103        assertContains( String.format( "%s: %%s should be contained\n", c.toString() ), c, true, versions );
104    }
105
106    @Test
107    public void testInvalid()
108    {
109        parseInvalid( "[1," );
110        parseInvalid( "[1,2],(3," );
111        parseInvalid( "[1,2],3" );
112    }
113}