View Javadoc
1   package org.apache.maven.plugins.pmd;
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.io.IOException;
24  import java.io.Writer;
25  import java.util.Locale;
26  
27  import org.apache.maven.doxia.site.decoration.DecorationModel;
28  import org.apache.maven.doxia.siterenderer.DocumentContent;
29  import org.apache.maven.doxia.siterenderer.RendererException;
30  import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
31  import org.apache.maven.execution.DefaultMavenExecutionRequest;
32  import org.apache.maven.execution.MavenExecutionRequest;
33  import org.apache.maven.execution.MavenSession;
34  import org.apache.maven.plugin.Mojo;
35  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
36  import org.codehaus.plexus.util.ReflectionUtils;
37  import org.codehaus.plexus.util.WriterFactory;
38  
39  /**
40   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
41   * @version $Id$
42   * @since 2.5
43   */
44  public abstract class AbstractPmdReportTest
45      extends AbstractMojoTestCase
46  {
47      @Override
48      protected void setUp()
49          throws Exception
50      {
51          super.setUp();
52          CapturingPrintStream.init( true );
53      }
54  
55      /**
56       * Renderer the sink from the report mojo.
57       *
58       * @param mojo not null
59       * @param outputHtml not null
60       * @throws RendererException if any
61       * @throws IOException if any
62       */
63      protected void renderer( AbstractPmdReport mojo, File outputHtml )
64          throws RendererException, IOException
65      {
66          SiteRenderingContext context = new SiteRenderingContext();
67          context.setDecoration( new DecorationModel() );
68          context.setTemplateName( "org/apache/maven/doxia/siterenderer/resources/default-site.vm" );
69          context.setLocale( Locale.ENGLISH );
70          
71          outputHtml.getParentFile().mkdirs();
72  
73          try ( Writer writer = WriterFactory.newXmlWriter( outputHtml ) )
74          {
75              mojo.getSiteRenderer().mergeDocumentIntoSite( writer, (DocumentContent) mojo.getSink(), context );
76          }
77      }
78  
79      /**
80       * Checks, whether the string <code>contained</code> is contained in
81       * the given <code>text</code> ignoring case.
82       *
83       * @param text the string in which the search is executed
84       * @param contains the string, the should be searched
85       * @return <code>true</code> if the string is contained, otherwise <code>false</code>.
86       */
87      public static boolean lowerCaseContains( String text, String contains )
88      {
89          return text.toLowerCase( Locale.ROOT ).contains( contains.toLowerCase( Locale.ROOT ) );
90      }
91  
92      @Override
93      protected Mojo lookupMojo( String goal, File pom ) throws Exception
94      {
95          Mojo mojo = super.lookupMojo( goal, pom );
96          return mockMavenSession( mojo );
97      }
98  
99      private Mojo mockMavenSession(Mojo mojo) throws IllegalAccessException {
100         String basedir = getBasedir();
101         if ( ReflectionUtils.getFieldByNameIncludingSuperclasses( "session", mojo.getClass() ) != null )
102         {
103             MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest() {
104                 public String getBaseDirectory() {
105                     return basedir;
106                 };
107             };
108             ReflectionUtils.setVariableValueInObject( mojo, "session",
109                     new MavenSession( null, null, executionRequest, null ) );
110         }
111         return mojo;
112     }
113 }