View Javadoc
1   package org.eclipse.aether.util.version;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.junit.Assert.*;
23  
24  import org.eclipse.aether.util.version.GenericVersion;
25  import org.eclipse.aether.util.version.GenericVersionScheme;
26  import org.eclipse.aether.version.InvalidVersionSpecificationException;
27  import org.eclipse.aether.version.VersionConstraint;
28  import org.junit.Before;
29  import org.junit.Test;
30  
31  /**
32   */
33  public class GenericVersionSchemeTest
34  {
35  
36      private GenericVersionScheme scheme;
37  
38      @Before
39      public void setUp()
40      {
41          scheme = new GenericVersionScheme();
42      }
43  
44      private InvalidVersionSpecificationException parseInvalid( String constraint )
45      {
46          try
47          {
48              scheme.parseVersionConstraint( constraint );
49              fail( "expected exception for constraint " + constraint );
50              return null;
51          }
52          catch ( InvalidVersionSpecificationException e )
53          {
54              return e;
55          }
56      }
57  
58      @Test
59      public void testEnumeratedVersions()
60          throws InvalidVersionSpecificationException
61      {
62          VersionConstraint c = scheme.parseVersionConstraint( "1.0" );
63          assertEquals( "1.0", c.getVersion().toString() );
64          assertTrue( c.containsVersion( new GenericVersion( "1.0" ) ) );
65  
66          c = scheme.parseVersionConstraint( "[1.0]" );
67          assertNull( c.getVersion() );
68          assertTrue( c.containsVersion( new GenericVersion( "1.0" ) ) );
69  
70          c = scheme.parseVersionConstraint( "[1.0],[2.0]" );
71          assertTrue( c.containsVersion( new GenericVersion( "1.0" ) ) );
72          assertTrue( c.containsVersion( new GenericVersion( "2.0" ) ) );
73  
74          c = scheme.parseVersionConstraint( "[1.0],[2.0],[3.0]" );
75          assertContains( c, "1.0", "2.0", "3.0" );
76          assertNotContains( c, "1.5" );
77  
78          c = scheme.parseVersionConstraint( "[1,3),(3,5)" );
79          assertContains( c, "1", "2", "4" );
80          assertNotContains( c, "3", "5" );
81  
82          c = scheme.parseVersionConstraint( "[1,3),(3,)" );
83          assertContains( c, "1", "2", "4" );
84          assertNotContains( c, "3" );
85      }
86  
87      private void assertNotContains( VersionConstraint c, String... versions )
88      {
89          assertContains( String.format( "%s: %%s should not be contained\n", c.toString() ), c, false, versions );
90      }
91  
92      private void assertContains( String msg, VersionConstraint c, boolean b, String... versions )
93      {
94          for ( String v : versions )
95          {
96              assertEquals( String.format( msg, v ), b, c.containsVersion( new GenericVersion( v ) ) );
97          }
98      }
99  
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 }