View Javadoc
1   package org.apache.maven.it;
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 org.apache.maven.it.util.ResourceExtractor;
23  
24  import java.io.File;
25  import java.util.Properties;
26  
27  /**
28   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-4615">MNG-4615</a>.
29   *
30   * @author Benjamin Bentmann
31   */
32  public class MavenITmng4615ValidateRequiredPluginParameterTest
33      extends AbstractMavenIntegrationTestCase
34  {
35  
36      public MavenITmng4615ValidateRequiredPluginParameterTest()
37      {
38          super( "[2.0.3,3.0-alpha-1),[3.0-beta-2,)" );
39      }
40  
41      /**
42       * Verify that Maven validates required mojo parameters (and doesn't just have the plugins die with NPEs).
43       * This scenario checks the case of all required parameters being set via plugin configuration.
44       *
45       * @throws Exception in case of failure
46       */
47      public void testitAllSet()
48          throws Exception
49      {
50          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-4615/test-0" );
51  
52          Verifier verifier = newVerifier( testDir.getAbsolutePath() );
53          verifier.setAutoclean( false );
54          verifier.deleteDirectory( "target" );
55          verifier.executeGoal( "validate" );
56          verifier.verifyErrorFreeLog();
57          verifier.resetStreams();
58  
59          Properties props = verifier.loadProperties( "target/config.properties" );
60          assertEquals( "one", props.get( "requiredParam" ) );
61          assertEquals( "two", props.get( "requiredParamWithDefault" ) );
62      }
63  
64      /**
65       * Verify that Maven validates required mojo parameters (and doesn't just have the plugins die with NPEs).
66       * This scenario checks the case of a parameter missing its backing system property.
67       *
68       * @throws Exception in case of failure
69       */
70      public void testitExprMissing()
71          throws Exception
72      {
73          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-4615/test-1" );
74  
75          Verifier verifier = newVerifier( testDir.getAbsolutePath() );
76          verifier.setAutoclean( false );
77          verifier.deleteDirectory( "target" );
78          verifier.setLogFileName( "log-a.txt" );
79          try
80          {
81              verifier.executeGoal( "validate" );
82              verifier.verifyErrorFreeLog();
83              fail( "Build did not fail despite required plugin parameter missing" );
84          }
85          catch ( VerificationException e )
86          {
87              // expected
88          }
89  
90          verifier.resetStreams();
91      }
92  
93      /**
94       * Verify that Maven validates required mojo parameters (and doesn't just have the plugins die with NPEs).
95       * This scenario checks the case of a parameter having its backing system property set.
96       *
97       * @throws Exception in case of failure
98       */
99      public void testitExprSet()
100         throws Exception
101     {
102         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-4615/test-1" );
103 
104         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
105         verifier.setAutoclean( false );
106         verifier.deleteDirectory( "target" );
107         verifier.setSystemProperty( "config.requiredParam", "CLI" );
108         verifier.setLogFileName( "log-b.txt" );
109         verifier.executeGoal( "validate" );
110         verifier.verifyErrorFreeLog();
111         verifier.resetStreams();
112 
113         Properties props = verifier.loadProperties( "target/config.properties" );
114         assertEquals( "CLI", props.get( "requiredParam" ) );
115         assertEquals( "two", props.get( "requiredParamWithDefault" ) );
116     }
117 
118     /**
119      * Verify that Maven validates required mojo parameters (and doesn't just have the plugins die with NPEs).
120      * This scenario checks the case of a parameter missing its backing POM value.
121      *
122      * @throws Exception in case of failure
123      */
124     public void testitPomValMissing()
125         throws Exception
126     {
127         // cf. MNG-4764
128         requiresMavenVersion( "[3.0-beta-2,)" );
129 
130         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-4615/test-2a" );
131 
132         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
133         verifier.setAutoclean( false );
134         verifier.deleteDirectory( "target" );
135         try
136         {
137             verifier.executeGoal( "validate" );
138             verifier.verifyErrorFreeLog();
139             fail( "Build did not fail despite required plugin parameter missing" );
140         }
141         catch ( VerificationException e )
142         {
143             // expected
144         }
145 
146         verifier.resetStreams();
147     }
148 
149     /**
150      * Verify that Maven validates required mojo parameters (and doesn't just have the plugins die with NPEs).
151      * This scenario checks the case of a parameter having its backing POM value set.
152      *
153      * @throws Exception in case of failure
154      */
155     public void testitPomValSet()
156         throws Exception
157     {
158         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-4615/test-2b" );
159 
160         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
161         verifier.setAutoclean( false );
162         verifier.deleteDirectory( "target" );
163         verifier.executeGoal( "validate" );
164         verifier.verifyErrorFreeLog();
165         verifier.resetStreams();
166 
167         Properties props = verifier.loadProperties( "target/config.properties" );
168         assertEquals( "one", props.get( "requiredParam" ) );
169         assertEquals( "http://foo.bar/", props.get( "requiredParamWithDefault" ) );
170     }
171 
172 }