View Javadoc

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 java.io.File;
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.testing.AbstractMojoTestCase;
29  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
30  
31  /**
32   * @author Edwin Punzalan
33   * @version $Id: CheckstyleViolationCheckMojoTest.html 816663 2012-05-08 14:00:13Z hboutemy $
34   */
35  public class CheckstyleViolationCheckMojoTest
36      extends AbstractMojoTestCase
37  {
38      
39      
40      
41      public void testDefaultConfig()
42          throws Exception
43      {
44          File pluginXmlFile = new File( getBasedir(), "src/test/plugin-configs/check-plugin-config.xml" );
45  
46          CheckstyleViolationCheckMojo mojo = (CheckstyleViolationCheckMojo) lookupMojo( "check", pluginXmlFile );
47          
48          mojoSetup( mojo );
49          
50          assertNotNull( "Mojo found.", mojo );
51          
52          assertNotNull( "project null.", mojo.project );
53          
54          try
55          {
56              mojo.execute();
57  
58              fail( "Must throw an exception on violations" );
59          }
60          catch ( MojoFailureException e )
61          {
62              //expected
63          }
64      }
65  
66      public void testInvalidFormat()
67          throws Exception
68      {
69          File pluginXmlFile = new File( getBasedir(), "src/test/plugin-configs/check-plugin-config.xml" );
70  
71          Mojo mojo = lookupMojo( "check", pluginXmlFile );
72  
73          assertNotNull( "Mojo found.", mojo );
74  
75          mojoSetup( mojo );
76          
77          setVariableValueToObject( mojo, "outputFileFormat", "plain" );
78  
79          try
80          {
81              mojo.execute();
82  
83              fail( "Must throw an exception invalid format: plain" );
84          }
85          catch ( MojoExecutionException e )
86          {
87              //expected
88          }
89      }
90  
91      public void testNoOutputFile()
92          throws Exception
93      {
94          File pluginXmlFile = new File( getBasedir(), "src/test/plugin-configs/check-plugin-config.xml" );
95  
96          Mojo mojo = lookupMojo( "check", pluginXmlFile );
97  
98          assertNotNull( "Mojo found.", mojo );
99  
100         mojoSetup( mojo );
101         
102         setVariableValueToObject( mojo, "outputFile", new File( "target/NoSuchFile.xml" ) );
103 
104         mojo.execute();
105     }
106 
107     public void testNoFail()
108         throws Exception
109     {
110         File pluginXmlFile = new File( getBasedir(), "src/test/plugin-configs/check-plugin-config.xml" );
111 
112         Mojo mojo = lookupMojo( "check", pluginXmlFile );
113 
114         assertNotNull( "Mojo found.", mojo );
115 
116         mojoSetup( mojo );
117         
118         setVariableValueToObject( mojo, "failOnViolation", Boolean.FALSE );
119 
120         mojo.execute();
121     }
122     
123     protected void mojoSetup( Mojo mojo )
124         throws Exception
125     {
126         // mojo setup
127 
128         setVariableValueToObject( mojo, "project", new MavenProjectStub()
129         {
130 
131             public File getFile()
132             {
133                 return new File( getBasedir(), "target/classes" );
134             }
135 
136             public Build getBuild()
137             {
138                 return new Build()
139                 {
140                     public String getDirectory()
141                     {
142                         return getBasedir() + "/target/classes";
143                     }
144                 };
145             }
146 
147         } );
148 
149         setVariableValueToObject( mojo, "configLocation", "config/sun_checks.xml" );
150         setVariableValueToObject( mojo, "cacheFile", getBasedir() + "/target/classes/checkstyle-cachefile" );
151         setVariableValueToObject( mojo, "sourceDirectory", new File( getBasedir(), "src/test/plugin-configs/src" ));// new File( getBasedir() + "/target" ) );
152         setVariableValueToObject( mojo, "encoding", "UTF-8" );
153         setVariableValueToObject( mojo, "skipExec", Boolean.TRUE );
154 
155     }
156 }