View Javadoc
1   package org.apache.maven.plugins.checkstyle;
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.File;
23  import java.util.Arrays;
24  
25  import org.apache.maven.model.Build;
26  import org.apache.maven.plugin.Mojo;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.MojoFailureException;
29  import org.apache.maven.plugin.descriptor.PluginDescriptor;
30  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
31  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
32  
33  /**
34   * @author Edwin Punzalan
35   *
36   */
37  public class CheckstyleViolationCheckMojoTest
38      extends AbstractMojoTestCase
39  {
40      public void testDefaultConfig()
41          throws Exception
42      {
43          File pluginXmlFile = new File( getBasedir(), "src/test/resources/plugin-configs/check-plugin-config.xml" );
44  
45          CheckstyleViolationCheckMojo mojo = (CheckstyleViolationCheckMojo) lookupMojo( "check", pluginXmlFile );
46  
47          mojoSetup( mojo );
48  
49          assertNotNull( "Mojo found.", mojo );
50  
51          assertNotNull( "project null.", mojo.project );
52  
53          try
54          {
55              mojo.execute();
56  
57              fail( "Must throw an exception on violations" );
58          }
59          catch ( MojoFailureException e )
60          {
61              //expected
62          }
63      }
64  
65      public void testInvalidFormatWithSkipExec()
66          throws Exception
67      {
68          File pluginXmlFile = new File( getBasedir(), "src/test/resources/plugin-configs/check-plugin-config.xml" );
69  
70          Mojo mojo = lookupMojo( "check", pluginXmlFile );
71  
72          assertNotNull( "Mojo found.", mojo );
73  
74          mojoSetup( mojo );
75  
76          setVariableValueToObject( mojo, "outputFileFormat", "plain" );
77  
78          try
79          {
80              mojo.execute();
81  
82              fail( "Must throw an exception invalid format: plain" );
83          }
84          catch ( MojoExecutionException e )
85          {
86              //expected
87          }
88      }
89  
90      public void testNoOutputFile()
91          throws Exception
92      {
93          File pluginXmlFile = new File( getBasedir(), "src/test/resources/plugin-configs/check-plugin-config.xml" );
94  
95          Mojo mojo = lookupMojo( "check", pluginXmlFile );
96  
97          assertNotNull( "Mojo found.", mojo );
98  
99          mojoSetup( mojo );
100 
101         setVariableValueToObject( mojo, "outputFile", new File( "target/NoSuchFile.xml" ) );
102 
103         mojo.execute();
104     }
105 
106     private void doTestPlainOutputFile( boolean failsOnError )
107         throws Exception
108     {
109         File pluginXmlFile = new File( getBasedir(), "src/test/resources/plugin-configs/check-plugin-plain-output.xml" );
110 
111         Mojo mojo = lookupMojo( "check", pluginXmlFile );
112 
113         assertNotNull( "Mojo found.", mojo );
114 
115         PluginDescriptor descriptorStub = new PluginDescriptor();
116         descriptorStub.setGroupId( "org.apache.maven.plugins" );
117         descriptorStub.setArtifactId( "maven-checkstyle-plugin" );
118         setVariableValueToObject( mojo, "plugin", descriptorStub );
119 
120         setVariableValueToObject( mojo, "failsOnError", failsOnError );
121 
122         mojo.execute();
123     }
124 
125     public void testPlainOutputFileFailOnError()
126         throws Exception
127     {
128         try
129         {
130             doTestPlainOutputFile( true );
131 
132             fail( "Must fail on violations" );
133         }
134         catch ( MojoExecutionException e )
135         {
136             // expected
137         }
138     }
139 
140     public void testPlainOutputFile()
141         throws Exception
142     {
143         doTestPlainOutputFile( false );
144     }
145 
146     public void testNoFail()
147         throws Exception
148     {
149         File pluginXmlFile = new File( getBasedir(), "src/test/resources/plugin-configs/check-plugin-config.xml" );
150 
151         Mojo mojo = lookupMojo( "check", pluginXmlFile );
152 
153         assertNotNull( "Mojo found.", mojo );
154 
155         mojoSetup( mojo );
156 
157         setVariableValueToObject( mojo, "failOnViolation", Boolean.FALSE );
158 
159         mojo.execute();
160     }
161 
162     protected void mojoSetup( Mojo mojo )
163         throws Exception
164     {
165         // mojo setup
166 
167         setVariableValueToObject( mojo, "project", new MavenProjectStub()
168         {
169 
170             public File getFile()
171             {
172                 return new File( getBasedir(), "target/classes" );
173             }
174 
175             public Build getBuild()
176             {
177                 return new Build()
178                 {
179                     private static final long serialVersionUID = -743084937617131258L;
180 
181                     public String getDirectory()
182                     {
183                         return getBasedir() + "/target/classes";
184                     }
185                 };
186             }
187 
188         } );
189 
190         setVariableValueToObject( mojo, "configLocation", "sun_checks.xml" );
191         setVariableValueToObject( mojo, "cacheFile", getBasedir() + "/target/classes/checkstyle-cachefile" );
192         setVariableValueToObject( mojo, "sourceDirectories", Arrays.asList( getBasedir() + "/src/test/plugin-configs/src" ));// new File( getBasedir() + "/target" ) );
193         setVariableValueToObject( mojo, "inputEncoding", "UTF-8" );
194         setVariableValueToObject( mojo, "skipExec", Boolean.TRUE );
195 
196     }
197 }