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 java.io.IOException;
23  import java.util.Set;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
29  import org.apache.maven.plugin.testing.ArtifactStubFactory;
30  import org.apache.maven.plugins.enforcer.utils.TestEnforcerRuleUtils;
31  import org.apache.maven.project.MavenProject;
32  
33  /**
34   * The Class TestNoSnapshots.
35   *
36   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
37   */
38  public class TestRequireReleaseDeps
39      extends TestCase
40  {
41  
42      /**
43       * Test rule.
44       *
45       * @throws IOException Signals that an I/O exception has occurred.
46       */
47      public void testRule()
48          throws IOException
49      {
50  
51          ArtifactStubFactory factory = new ArtifactStubFactory();
52          MockProject project = new MockProject();
53          EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( project );
54          project.setArtifacts( factory.getMixedArtifacts() );
55          project.setDependencyArtifacts( factory.getScopedArtifacts() );
56          RequireReleaseDeps rule = newRequireReleaseDeps();
57          rule.setSearchTransitive( false );
58  
59          TestEnforcerRuleUtils.execute( rule, helper, false );
60  
61          rule.setSearchTransitive( true );
62  
63          TestEnforcerRuleUtils.execute( rule, helper, true );
64  
65          // test onlyWhenRelease in each case
66  
67          project.setArtifact( factory.getSnapshotArtifact() );
68  
69          TestEnforcerRuleUtils.execute( rule, helper, true );
70  
71          rule.setOnlyWhenRelease( true );
72  
73          TestEnforcerRuleUtils.execute( rule, helper, false );
74  
75          project.setArtifact( factory.getReleaseArtifact() );
76  
77          TestEnforcerRuleUtils.execute( rule, helper, true );
78  
79          MockProject parent = new MockProject();
80          parent.setArtifact( factory.getSnapshotArtifact() );
81          project.setParent( parent );
82          project.setArtifacts( null );
83          project.setDependencyArtifacts( null );
84          helper = EnforcerTestUtils.getHelper(project);
85  
86          rule.setFailWhenParentIsSnapshot( true );
87          TestEnforcerRuleUtils.execute( rule, helper, true );
88  
89          rule.setFailWhenParentIsSnapshot( false );
90          TestEnforcerRuleUtils.execute( rule, helper, false );
91  
92  
93      }
94  
95      private RequireReleaseDeps newRequireReleaseDeps()
96      {
97          RequireReleaseDeps rule = new RequireReleaseDeps()
98          {
99              protected Set<Artifact> getDependenciesToCheck( MavenProject project )
100             {
101                 // the integration with dependencyGraphTree is verified with the integration tests
102                 // for unit-testing 
103                 return isSearchTransitive() ? project.getArtifacts() : project.getDependencyArtifacts();
104             }
105         };        
106         return rule;
107     }
108 
109     /**
110      * Test id.
111      */
112     public void testId()
113     {
114         RequireReleaseDeps rule = newRequireReleaseDeps();
115         rule.getCacheId();
116     }
117 }