1   package org.apache.maven.plugin.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 org.apache.maven.plugin.Mojo;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugin.MojoFailureException;
25  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
26  
27  import java.io.File;
28  
29  /**
30   * @author Edwin Punzalan
31   */
32  public class CheckstyleViolationCheckMojoTest
33      extends AbstractMojoTestCase
34  {
35      public void testDefaultConfig()
36          throws Exception
37      {
38          File pluginXmlFile = new File( getBasedir(), "src/test/plugin-configs/check-plugin-config.xml" );
39  
40          Mojo mojo = lookupMojo( "check", pluginXmlFile );
41  
42          assertNotNull( "Mojo found.", mojo );
43  
44          try
45          {
46              mojo.execute();
47  
48              fail( "Must throw an exception on violations" );
49          }
50          catch ( MojoFailureException e )
51          {
52              //expected
53          }
54      }
55  
56      public void testInvalidFormat()
57          throws Exception
58      {
59          File pluginXmlFile = new File( getBasedir(), "src/test/plugin-configs/check-plugin-config.xml" );
60  
61          Mojo mojo = lookupMojo( "check", pluginXmlFile );
62  
63          assertNotNull( "Mojo found.", mojo );
64  
65          setVariableValueToObject( mojo, "outputFileFormat", "plain" );
66  
67          try
68          {
69              mojo.execute();
70  
71              fail( "Must throw an exception invalid format: plain" );
72          }
73          catch ( MojoExecutionException e )
74          {
75              //expected
76          }
77      }
78  
79      public void testNoOutputFile()
80          throws Exception
81      {
82          File pluginXmlFile = new File( getBasedir(), "src/test/plugin-configs/check-plugin-config.xml" );
83  
84          Mojo mojo = lookupMojo( "check", pluginXmlFile );
85  
86          assertNotNull( "Mojo found.", mojo );
87  
88          setVariableValueToObject( mojo, "outputFile", new File( "target/NoSuchFile.xml" ) );
89  
90          mojo.execute();
91      }
92  
93      public void testNoFail()
94          throws Exception
95      {
96          File pluginXmlFile = new File( getBasedir(), "src/test/plugin-configs/check-plugin-config.xml" );
97  
98          Mojo mojo = lookupMojo( "check", pluginXmlFile );
99  
100         assertNotNull( "Mojo found.", mojo );
101 
102         setVariableValueToObject( mojo, "failOnViolation", Boolean.FALSE );
103 
104         mojo.execute();
105     }
106 }