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 java.util.Collections;
025
026import org.eclipse.aether.version.InvalidVersionSpecificationException;
027import org.eclipse.aether.version.VersionRange;
028import org.junit.Test;
029
030public class UnionVersionRangeTest
031{
032
033    private VersionRange newRange( String range )
034    {
035        try
036        {
037            return new GenericVersionScheme().parseVersionRange( range );
038        }
039        catch ( InvalidVersionSpecificationException e )
040        {
041            throw new IllegalArgumentException( e );
042        }
043    }
044
045    private void assertBound( String version, boolean inclusive, VersionRange.Bound bound )
046    {
047        if ( version == null )
048        {
049            assertNull( bound );
050        }
051        else
052        {
053            assertNotNull( bound );
054            assertNotNull( bound.getVersion() );
055            assertEquals( inclusive, bound.isInclusive() );
056            try
057            {
058                assertEquals( new GenericVersionScheme().parseVersion( version ), bound.getVersion() );
059            }
060            catch ( InvalidVersionSpecificationException e )
061            {
062                throw new IllegalArgumentException( e );
063            }
064        }
065    }
066
067    @Test
068    public void testGetLowerBound()
069    {
070        VersionRange range = UnionVersionRange.from( Collections.<VersionRange> emptySet() );
071        assertBound( null, false, range.getLowerBound() );
072
073        range = UnionVersionRange.from( newRange( "[1,2]" ), newRange( "[3,4]" ) );
074        assertBound( "1", true, range.getLowerBound() );
075
076        range = UnionVersionRange.from( newRange( "[1,2]" ), newRange( "(,4]" ) );
077        assertBound( null, false, range.getLowerBound() );
078
079        range = UnionVersionRange.from( newRange( "[1,2]" ), newRange( "(1,4]" ) );
080        assertBound( "1", true, range.getLowerBound() );
081
082        range = UnionVersionRange.from( newRange( "[1,2]" ), newRange( "(0,4]" ) );
083        assertBound( "0", false, range.getLowerBound() );
084    }
085
086    @Test
087    public void testGetUpperBound()
088    {
089        VersionRange range = UnionVersionRange.from( Collections.<VersionRange> emptySet() );
090        assertBound( null, false, range.getUpperBound() );
091
092        range = UnionVersionRange.from( newRange( "[1,2]" ), newRange( "[3,4]" ) );
093        assertBound( "4", true, range.getUpperBound() );
094
095        range = UnionVersionRange.from( newRange( "[1,2]" ), newRange( "[3,)" ) );
096        assertBound( null, false, range.getUpperBound() );
097
098        range = UnionVersionRange.from( newRange( "[1,2]" ), newRange( "[1,2)" ) );
099        assertBound( "2", true, range.getUpperBound() );
100
101        range = UnionVersionRange.from( newRange( "[1,2]" ), newRange( "[1,3)" ) );
102        assertBound( "3", false, range.getUpperBound() );
103    }
104
105}