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          throws Exception
41      {
42          scheme = new GenericVersionScheme();
43      }
44  
45      private InvalidVersionSpecificationException parseInvalid( String constraint )
46      {
47          try
48          {
49              scheme.parseVersionConstraint( constraint );
50              fail( "expected exception for constraint " + constraint );
51              return null;
52          }
53          catch ( InvalidVersionSpecificationException e )
54          {
55              return e;
56          }
57      }
58  
59      @Test
60      public void testEnumeratedVersions()
61          throws InvalidVersionSpecificationException
62      {
63          VersionConstraint c = scheme.parseVersionConstraint( "1.0" );
64          assertEquals( "1.0", c.getVersion().toString() );
65          assertTrue( c.containsVersion( new GenericVersion( "1.0" ) ) );
66  
67          c = scheme.parseVersionConstraint( "[1.0]" );
68          assertEquals( null, c.getVersion() );
69          assertTrue( c.containsVersion( new GenericVersion( "1.0" ) ) );
70  
71          c = scheme.parseVersionConstraint( "[1.0],[2.0]" );
72          assertTrue( c.containsVersion( new GenericVersion( "1.0" ) ) );
73          assertTrue( c.containsVersion( new GenericVersion( "2.0" ) ) );
74  
75          c = scheme.parseVersionConstraint( "[1.0],[2.0],[3.0]" );
76          assertContains( c, "1.0", "2.0", "3.0" );
77          assertNotContains( c, "1.5" );
78  
79          c = scheme.parseVersionConstraint( "[1,3),(3,5)" );
80          assertContains( c, "1", "2", "4" );
81          assertNotContains( c, "3", "5" );
82  
83          c = scheme.parseVersionConstraint( "[1,3),(3,)" );
84          assertContains( c, "1", "2", "4" );
85          assertNotContains( c, "3" );
86      }
87  
88      private void assertNotContains( VersionConstraint c, String... versions )
89      {
90          assertContains( String.format( "%s: %%s should not be contained\n", c.toString() ), c, false, versions );
91      }
92  
93      private void assertContains( String msg, VersionConstraint c, boolean b, String... versions )
94      {
95          for ( String v : versions )
96          {
97              assertEquals( String.format( msg, v ), b, c.containsVersion( new GenericVersion( v ) ) );
98          }
99      }
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 }