View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.checkstyle;
20  
21  import java.io.File;
22  import java.util.Arrays;
23  
24  import org.apache.maven.model.Build;
25  import org.apache.maven.plugin.Mojo;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugin.MojoFailureException;
28  import org.apache.maven.plugin.descriptor.PluginDescriptor;
29  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
30  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
31  
32  /**
33   * @author Edwin Punzalan
34   *
35   */
36  public class CheckstyleViolationCheckMojoTest extends AbstractMojoTestCase {
37      public void testDefaultConfig() throws Exception {
38          File pluginXmlFile = new File(getBasedir(), "src/test/resources/plugin-configs/check-plugin-config.xml");
39  
40          CheckstyleViolationCheckMojo mojo = (CheckstyleViolationCheckMojo) lookupMojo("check", pluginXmlFile);
41  
42          mojoSetup(mojo);
43  
44          assertNotNull("Mojo not found.", mojo);
45  
46          assertNotNull("project null.", mojo.project);
47  
48          try {
49              mojo.execute();
50  
51              fail("Must throw an exception on violations");
52          } catch (MojoFailureException e) {
53              // expected
54          }
55      }
56  
57      public void testInvalidFormatWithSkipExec() throws Exception {
58          File pluginXmlFile = new File(getBasedir(), "src/test/resources/plugin-configs/check-plugin-config.xml");
59  
60          Mojo mojo = lookupMojo("check", pluginXmlFile);
61  
62          assertNotNull("Mojo not found.", mojo);
63  
64          mojoSetup(mojo);
65  
66          setVariableValueToObject(mojo, "outputFileFormat", "plain");
67  
68          try {
69              mojo.execute();
70  
71              fail("Must throw an exception invalid format: plain");
72          } catch (MojoExecutionException e) {
73              // expected
74          }
75      }
76  
77      public void testNoOutputFile() throws Exception {
78          File pluginXmlFile = new File(getBasedir(), "src/test/resources/plugin-configs/check-plugin-config.xml");
79  
80          Mojo mojo = lookupMojo("check", pluginXmlFile);
81  
82          assertNotNull("Mojo not found.", mojo);
83  
84          mojoSetup(mojo);
85  
86          setVariableValueToObject(mojo, "outputFile", new File("target/NoSuchFile.xml"));
87  
88          mojo.execute();
89      }
90  
91      private void doTestPlainOutputFile(boolean failsOnError) throws Exception {
92          File pluginXmlFile = new File(getBasedir(), "src/test/resources/plugin-configs/check-plugin-plain-output.xml");
93  
94          Mojo mojo = lookupMojo("check", pluginXmlFile);
95  
96          assertNotNull("Mojo not found.", mojo);
97  
98          PluginDescriptor descriptorStub = new PluginDescriptor();
99          descriptorStub.setGroupId("org.apache.maven.plugins");
100         descriptorStub.setArtifactId("maven-checkstyle-plugin");
101         setVariableValueToObject(mojo, "plugin", descriptorStub);
102 
103         setVariableValueToObject(mojo, "failsOnError", failsOnError);
104 
105         mojo.execute();
106     }
107 
108     public void testPlainOutputFileFailOnError() throws Exception {
109         try {
110             doTestPlainOutputFile(true);
111 
112             fail("Must fail on violations");
113         } catch (MojoExecutionException e) {
114             // expected
115         }
116     }
117 
118     public void testPlainOutputFile() throws Exception {
119         doTestPlainOutputFile(false);
120     }
121 
122     public void testNoFail() throws Exception {
123         File pluginXmlFile = new File(getBasedir(), "src/test/resources/plugin-configs/check-plugin-config.xml");
124 
125         Mojo mojo = lookupMojo("check", pluginXmlFile);
126 
127         assertNotNull("Mojo not found.", mojo);
128 
129         mojoSetup(mojo);
130 
131         setVariableValueToObject(mojo, "failOnViolation", Boolean.FALSE);
132 
133         mojo.execute();
134     }
135 
136     protected void mojoSetup(Mojo mojo) throws Exception {
137         // mojo setup
138 
139         setVariableValueToObject(mojo, "project", new MavenProjectStub() {
140 
141             public File getFile() {
142                 return new File(getBasedir(), "target/classes");
143             }
144 
145             public Build getBuild() {
146                 return new Build() {
147                     private static final long serialVersionUID = -743084937617131258L;
148 
149                     public String getDirectory() {
150                         return getBasedir() + "/target/classes";
151                     }
152                 };
153             }
154         });
155 
156         setVariableValueToObject(mojo, "configLocation", "sun_checks.xml");
157         setVariableValueToObject(mojo, "cacheFile", getBasedir() + "/target/classes/checkstyle-cachefile");
158         setVariableValueToObject(
159                 mojo,
160                 "sourceDirectories",
161                 Arrays.asList(
162                         getBasedir() + "/src/test/plugin-configs/src")); // new File( getBasedir() + "/target" ) );
163         setVariableValueToObject(mojo, "inputEncoding", "UTF-8");
164         setVariableValueToObject(mojo, "skipExec", Boolean.TRUE);
165     }
166 }