View Javadoc
1   package org.apache.maven.plugins.enforcer;
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 junit.framework.TestCase;
23  
24  import org.apache.maven.artifact.versioning.ArtifactVersion;
25  import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
26  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
27  import org.apache.maven.artifact.versioning.VersionRange;
28  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
29  import org.apache.maven.plugin.logging.Log;
30  import org.apache.maven.plugin.logging.SystemStreamLog;
31  
32  /**
33   * The Class TestAbstractVersionEnforcer.
34   *
35   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
36   */
37  public class TestAbstractVersionEnforcer
38      extends TestCase
39  {
40  
41      /**
42       * Test contains version.
43       *
44       * @throws InvalidVersionSpecificationException the invalid version specification exception
45       */
46      public void testContainsVersion()
47          throws InvalidVersionSpecificationException
48      {
49          ArtifactVersion version = new DefaultArtifactVersion( "2.0.5" );
50          // test ranges
51          assertTrue( AbstractVersionEnforcer.containsVersion( VersionRange.createFromVersionSpec( "[2.0.5,)" ), version ) );
52          assertTrue( AbstractVersionEnforcer.containsVersion( VersionRange.createFromVersionSpec( "[2.0.4,)" ), version ) );
53          assertTrue( AbstractVersionEnforcer.containsVersion( VersionRange.createFromVersionSpec( "[2.0.4,2.0.5]" ),
54                                                               version ) );
55          assertTrue( AbstractVersionEnforcer.containsVersion( VersionRange.createFromVersionSpec( "[2.0.4,2.0.6]" ),
56                                                               version ) );
57          assertTrue( AbstractVersionEnforcer.containsVersion( VersionRange.createFromVersionSpec( "[2.0.4,2.0.6)" ),
58                                                               version ) );
59          assertTrue( AbstractVersionEnforcer.containsVersion( VersionRange.createFromVersionSpec( "[2.0,)" ), version ) );
60          assertTrue( AbstractVersionEnforcer.containsVersion( VersionRange.createFromVersionSpec( "[2.0.0,)" ), version ) );
61          // not matching versions
62          assertFalse( AbstractVersionEnforcer.containsVersion( VersionRange.createFromVersionSpec( "[2.0.4,2.0.5)" ),
63                                                                version ) );
64          assertFalse( AbstractVersionEnforcer.containsVersion( VersionRange.createFromVersionSpec( "[2.0.6,)" ), version ) );
65          assertFalse( AbstractVersionEnforcer.containsVersion( VersionRange.createFromVersionSpec( "(2.0.5,)" ), version ) );
66  
67          // test singular versions -> 2.0.5 == [2.0.5,) or x >= 2.0.5
68          assertTrue( AbstractVersionEnforcer.containsVersion( VersionRange.createFromVersionSpec( "2.0" ), version ) );
69          assertTrue( AbstractVersionEnforcer.containsVersion( VersionRange.createFromVersionSpec( "2.0.4" ), version ) );
70          assertTrue( AbstractVersionEnforcer.containsVersion( VersionRange.createFromVersionSpec( "2.0.5" ), version ) );
71  
72          assertFalse( AbstractVersionEnforcer.containsVersion( VersionRange.createFromVersionSpec( "2.0.6" ), version ) );
73  
74          version = new DefaultArtifactVersion( "1.5.0-7" );
75          assertTrue( AbstractVersionEnforcer.containsVersion( VersionRange.createFromVersionSpec( "[1.5.0,)" ), version ) );
76          assertTrue( AbstractVersionEnforcer.containsVersion( VersionRange.createFromVersionSpec( "[1.5,1.6)" ), version ) );
77  
78          version = new DefaultArtifactVersion( RequireJavaVersion.normalizeJDKVersion( "1.5.0-07" ) );
79          assertTrue( AbstractVersionEnforcer.containsVersion( VersionRange.createFromVersionSpec( "[1.5.0,)" ), version ) );
80          assertTrue( AbstractVersionEnforcer.containsVersion( VersionRange.createFromVersionSpec( "[1.5,1.6)" ), version ) );
81  
82          //MENFORCER-50
83          version = new DefaultArtifactVersion ("2.1.0-M1-RC12");
84          assertTrue( AbstractVersionEnforcer.containsVersion( VersionRange.createFromVersionSpec( "[2.1.0-M1-RC12,)" ), version ) );
85          assertFalse( AbstractVersionEnforcer.containsVersion( VersionRange.createFromVersionSpec( "[2.1.0-M1,)" ), version ) );
86  
87      }
88  
89      /**
90       * Enforce false.
91       *
92       * @param rule the rule
93       * @param log the log
94       * @param var the var
95       * @param range the range
96       * @param version the version
97       */
98      private void enforceFalse( AbstractVersionEnforcer rule, Log log, String var, String range, ArtifactVersion version )
99      {
100         try
101         {
102             rule.enforceVersion( log, var, range, version );
103             fail( "Expected to receive EnforcerRuleException because:" + version + " is not contained by " + range );
104         }
105         catch ( Exception e )
106         {
107             if ( e instanceof EnforcerRuleException )
108             {
109                 // log.info( "Caught Expected Exception: " +
110                 // e.getLocalizedMessage() );
111             }
112             else
113             {
114                 fail( "Received wrong exception. Expected EnforcerRuleExeption. Received:" + e.toString() );
115             }
116         }
117     }
118 
119     /**
120      * Test enforce version.
121      */
122     public void testEnforceVersion()
123     {
124         RequireMavenVersion rule = new RequireMavenVersion();
125         ArtifactVersion version = new DefaultArtifactVersion( "2.0.5" );
126         SystemStreamLog log = new SystemStreamLog();
127         // test ranges
128 
129         // not matching versions
130         try
131         {
132             rule.enforceVersion( log, "test", "[2.0.5,)", version );
133             rule.enforceVersion( log, "test", "[2.0.4,)", version );
134             rule.enforceVersion( log, "test", "[2.0.4,2.0.5]", version );
135             rule.enforceVersion( log, "test", "[2.0.4,2.0.6]", version );
136             rule.enforceVersion( log, "test", "[2.0.4,2.0.6)", version );
137             rule.enforceVersion( log, "test", "[2.0,)", version );
138             rule.enforceVersion( log, "test", "[2.0.0,)", version );
139 
140             // test singular versions -> 2.0.5 == [2.0.5,) or x >= 2.0.5
141             rule.enforceVersion( log, "test", "2.0", version );
142             rule.enforceVersion( log, "test", "2.0.4", version );
143             rule.enforceVersion( log, "test", "2.0.5", version );
144         }
145         catch ( Exception e )
146         {
147             fail( "No Exception expected. Caught:" + e.getLocalizedMessage() );
148 
149         }
150 
151         enforceFalse( rule, log, "test", "[2.0.6,)", version );
152         enforceFalse( rule, log, "test", "(2.0.5,)", version );
153         enforceFalse( rule, log, "test", "2.0.6", version );
154 
155         enforceFalse( rule, log, "test", "[2.0.4,2.0.5)", version );
156 
157         // make sure to handle the invalid range specification
158         enforceFalse( rule, log, "test", "[[2.0.4,2.0.5)", version );
159 
160     }
161 }