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$
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          assertNotNull( "locator null.", mojo.locator );
55          
56          try
57          {
58              mojo.execute();
59  
60              fail( "Must throw an exception on violations" );
61          }
62          catch ( MojoFailureException e )
63          {
64              //expected
65          }
66      }
67  
68      public void testInvalidFormat()
69          throws Exception
70      {
71          File pluginXmlFile = new File( getBasedir(), "src/test/plugin-configs/check-plugin-config.xml" );
72  
73          Mojo mojo = lookupMojo( "check", pluginXmlFile );
74  
75          assertNotNull( "Mojo found.", mojo );
76  
77          mojoSetup( mojo );
78          
79          setVariableValueToObject( mojo, "outputFileFormat", "plain" );
80  
81          try
82          {
83              mojo.execute();
84  
85              fail( "Must throw an exception invalid format: plain" );
86          }
87          catch ( MojoExecutionException e )
88          {
89              //expected
90          }
91      }
92  
93      public void testNoOutputFile()
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         mojoSetup( mojo );
103         
104         setVariableValueToObject( mojo, "outputFile", new File( "target/NoSuchFile.xml" ) );
105 
106         mojo.execute();
107     }
108 
109     public void testNoFail()
110         throws Exception
111     {
112         File pluginXmlFile = new File( getBasedir(), "src/test/plugin-configs/check-plugin-config.xml" );
113 
114         Mojo mojo = lookupMojo( "check", pluginXmlFile );
115 
116         assertNotNull( "Mojo found.", mojo );
117 
118         mojoSetup( mojo );
119         
120         setVariableValueToObject( mojo, "failOnViolation", Boolean.FALSE );
121 
122         mojo.execute();
123     }
124     
125     protected void mojoSetup( Mojo mojo )
126         throws Exception
127     {
128         // mojo setup
129 
130         setVariableValueToObject( mojo, "project", new MavenProjectStub()
131         {
132 
133             public File getFile()
134             {
135                 return new File( getBasedir(), "target/classes" );
136             }
137 
138             public Build getBuild()
139             {
140                 return new Build()
141                 {
142                     public String getDirectory()
143                     {
144                         return getBasedir() + "/target/classes";
145                     }
146                 };
147             }
148 
149         } );
150 
151         setVariableValueToObject( mojo, "configLocation", "config/sun_checks.xml" );
152         setVariableValueToObject( mojo, "cacheFile", getBasedir() + "/target/classes/checkstyle-cachefile" );
153         setVariableValueToObject( mojo, "sourceDirectory", new File( getBasedir(), "src/test/plugin-configs/src" ));// new File( getBasedir() + "/target" ) );
154         setVariableValueToObject( mojo, "encoding", "UTF-8" );
155         setVariableValueToObject( mojo, "skipExec", Boolean.TRUE );
156 
157     }
158 }