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