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.enforcer.rule.api.EnforcerRule;
25  import org.apache.maven.plugin.MojoExecutionException;
26  
27  /**
28   * Exhaustively check the enforcer mojo.
29   * 
30   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
31   * 
32   */
33  public class TestEnforceMojo
34      extends TestCase
35  {
36  
37      public void testEnforceMojo ()
38          throws MojoExecutionException
39      {
40          EnforceMojo mojo = new EnforceMojo();
41          mojo.setFail( false );
42          mojo.setSession( EnforcerTestUtils.getMavenSession() );
43          mojo.setProject( new MockProject() );
44  
45          try
46          {
47              mojo.execute();
48              fail( "Expected a Mojo Execution Exception." );
49          }
50          catch ( MojoExecutionException e )
51          {
52              System.out.println( "Caught Expected Exception:" + e.getLocalizedMessage() );
53          }
54  
55          EnforcerRule[] rules = new EnforcerRule[10];
56          rules[0] = new MockEnforcerRule( true );
57          rules[1] = new MockEnforcerRule( true );
58          mojo.setRules( rules );
59  
60          mojo.execute();
61  
62          try
63          {
64              mojo.setFailFast( false );
65              mojo.setFail( true );
66              mojo.execute();
67              fail( "Expected a Mojo Execution Exception." );
68          }
69          catch ( MojoExecutionException e )
70          {
71              System.out.println( "Caught Expected Exception:" + e.getLocalizedMessage() );
72          }
73  
74          try
75          {
76              mojo.setFailFast( true );
77              mojo.setFail( true );
78              mojo.execute();
79              fail( "Expected a Mojo Execution Exception." );
80          }
81          catch ( MojoExecutionException e )
82          {
83              System.out.println( "Caught Expected Exception:" + e.getLocalizedMessage() );
84          }
85  
86          ( (MockEnforcerRule) rules[0] ).setFailRule( false );
87          ( (MockEnforcerRule) rules[1] ).setFailRule( false );
88          mojo.execute();
89  
90      }
91  
92      public void testCaching () throws MojoExecutionException
93      {
94          EnforceMojo mojo = new EnforceMojo();
95          mojo.setFail( true );
96          mojo.setSession( EnforcerTestUtils.getMavenSession() );
97          mojo.setProject( new MockProject() );
98  
99          MockEnforcerRule[] rules = new MockEnforcerRule[10];
100         
101         //check that basic caching works.
102         rules[0] = new MockEnforcerRule( false, "", true, true );
103         rules[1] = new MockEnforcerRule( false, "", true, true );
104         mojo.setRules( rules );
105 
106         EnforceMojo.cache.clear();
107         mojo.execute();
108         
109         assertTrue( "Expected this rule to be executed.",rules[0].executed );
110         assertFalse( "Expected this rule not to be executed.",rules[1].executed);
111         
112         //check that skip caching works.
113         rules[0] = new MockEnforcerRule( false, "", true, true );
114         rules[1] = new MockEnforcerRule( false, "", true, true );
115         mojo.setRules( rules );
116 
117         EnforceMojo.cache.clear();
118         mojo.ignoreCache = true;
119         mojo.execute();
120         
121         assertTrue( "Expected this rule to be executed.",rules[0].executed );
122         assertTrue( "Expected this rule to be executed.",rules[1].executed );
123         
124         mojo.ignoreCache = false;
125         
126         //check that different ids are compared.
127         rules[0] = new MockEnforcerRule( false, "1", true, true );
128         rules[1] = new MockEnforcerRule( false, "2", true, true );
129         rules[2] = new MockEnforcerRule( false, "2", true, true );
130         mojo.setRules( rules );
131 
132         EnforceMojo.cache.clear();
133         mojo.execute();
134         
135         assertTrue( "Expected this rule to be executed.",rules[0].executed );
136         assertTrue( "Expected this rule to be executed.",rules[1].executed);
137         assertFalse( "Expected this rule not to be executed.",rules[2].executed);
138         
139         //check that future overrides are working
140         rules[0] = new MockEnforcerRule( false, "1", true, true );
141         rules[1] = new MockEnforcerRule( false, "1", false, true );
142         rules[2] = null;
143         mojo.setRules( rules );
144 
145         EnforceMojo.cache.clear();
146         mojo.execute();
147         
148         assertTrue( "Expected this rule to be executed.",rules[0].executed );
149         assertTrue( "Expected this rule to be executed.",rules[1].executed);
150 
151         //check that future isResultValid is used
152         rules[0] = new MockEnforcerRule( false, "1", true, true );
153         rules[1] = new MockEnforcerRule( false, "1", true, false );
154         rules[2] = null;
155         mojo.setRules( rules );
156 
157         EnforceMojo.cache.clear();
158         mojo.execute();
159         
160         assertTrue( "Expected this rule to be executed.",rules[0].executed );
161         assertTrue( "Expected this rule to be executed.",rules[1].executed);
162 
163     }
164     
165     public void testCachePersistence1() throws MojoExecutionException
166     {
167         EnforceMojo mojo = new EnforceMojo();
168         mojo.setFail( true );
169         mojo.setSession( EnforcerTestUtils.getMavenSession() );
170         mojo.setProject( new MockProject() );
171 
172         MockEnforcerRule[] rules = new MockEnforcerRule[10];
173         
174         //check that basic caching works.
175         rules[0] = new MockEnforcerRule( false, "", true, true );
176         rules[1] = new MockEnforcerRule( false, "", true, true );
177         mojo.setRules( rules );
178 
179         EnforceMojo.cache.clear();
180         mojo.execute();
181         
182         assertTrue( "Expected this rule to be executed.",rules[0].executed );
183         assertFalse( "Expected this rule not to be executed.",rules[1].executed);
184         
185     }
186     
187     public void testCachePersistence2() throws MojoExecutionException
188     {
189         EnforceMojo mojo = new EnforceMojo();
190         mojo.setFail( true );
191         mojo.setSession( EnforcerTestUtils.getMavenSession() );
192         mojo.setProject( new MockProject() );
193 
194         MockEnforcerRule[] rules = new MockEnforcerRule[10];
195         
196         //check that basic caching works.
197         rules[0] = new MockEnforcerRule( false, "", true, true );
198         rules[1] = new MockEnforcerRule( false, "", true, true );
199         mojo.setRules( rules );
200 
201         mojo.execute();
202         
203         assertFalse( "Expected this rule not to be executed.",rules[0].executed);
204         assertFalse( "Expected this rule not to be executed.",rules[1].executed);
205         
206     }
207     
208     public void testCachePersistence3() throws MojoExecutionException
209     {
210         System.gc();
211         
212         try
213         {
214             Thread.sleep( 1000 );
215         }
216         catch ( InterruptedException e )
217         {
218         }
219         
220         EnforceMojo mojo = new EnforceMojo();
221         mojo.setFail( true );
222         mojo.setSession( EnforcerTestUtils.getMavenSession() );
223         mojo.setProject( new MockProject() );
224 
225         MockEnforcerRule[] rules = new MockEnforcerRule[10];
226         
227         //check that basic caching works.
228         rules[0] = new MockEnforcerRule( false, "", true, true );
229         rules[1] = new MockEnforcerRule( false, "", true, true );
230         mojo.setRules( rules );
231 
232         mojo.execute();
233         
234         assertFalse( "Expected this rule not to be executed.",rules[0].executed);
235         assertFalse( "Expected this rule not to be executed.",rules[1].executed);
236         
237     }
238 }