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