Coverage Report - org.apache.maven.plugin.trac.TracMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
TracMojo
0%
0/77
0%
0/20
2,909
 
 1  
 package org.apache.maven.plugin.trac;
 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.net.MalformedURLException;
 23  
 import java.net.URL;
 24  
 import java.util.ArrayList;
 25  
 import java.util.Locale;
 26  
 import java.util.Map;
 27  
 import java.util.ResourceBundle;
 28  
 
 29  
 import org.apache.maven.doxia.siterenderer.Renderer;
 30  
 import org.apache.maven.plugin.changes.AbstractChangesReport;
 31  
 import org.apache.maven.project.MavenProject;
 32  
 import org.apache.maven.reporting.MavenReportException;
 33  
 import org.apache.xmlrpc.XmlRpcException;
 34  
 import org.apache.xmlrpc.client.XmlRpcClient;
 35  
 import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
 36  
 import org.codehaus.plexus.util.StringUtils;
 37  
 
 38  
 /**
 39  
  * Goal which downloads issues from the Issue Tracking System and generates a
 40  
  * report.
 41  
  *
 42  
  * @goal trac-report
 43  
  * @author Noriko Kinugasa
 44  
  * @version $Id: org.apache.maven.plugin.trac.TracMojo.html 816595 2012-05-08 12:43:00Z hboutemy $
 45  
  * @since 2.1
 46  
  */
 47  0
 public class TracMojo
 48  
     extends AbstractChangesReport
 49  
 {
 50  
     /**
 51  
      * Defines the Trac username for authentication into a private Trac
 52  
      * installation.
 53  
      *
 54  
      * @parameter default-value=""
 55  
      */
 56  
     private String tracUser;
 57  
 
 58  
     /**
 59  
      * Defines the Trac password for authentication into a private Trac
 60  
      * installation.
 61  
      *
 62  
      * @parameter default-value=""
 63  
      */
 64  
     private String tracPassword;
 65  
 
 66  
     /**
 67  
      * Defines the Trac query for searching ticket.
 68  
      *
 69  
      * @parameter default-value="order=id"
 70  
      */
 71  
     private String query;
 72  
 
 73  
     /**
 74  
      * Sets the column names that you want to show in the report. The columns
 75  
      * will appear in the report in the same order as you specify them here.
 76  
      * Multiple values can be separated by commas.
 77  
      * <p>
 78  
      * Valid columns are: <code>id</code>, <code>type</code>,
 79  
      * <code>summary</code>, <code>status</code>, <code>resolution</code>,
 80  
      * <code>milestone</code>, <code>owner</code>, <code>priority</code>,
 81  
      * <code>reporter</code>, <code>component</code>, <code>created</code>,
 82  
      * <code>changed</code>.
 83  
      * </p>
 84  
      *
 85  
      * @parameter default-value="id,type,summary,owner,reporter,priority,status,resolution,created,changed"
 86  
      * @since 2.2
 87  
      */
 88  
     private String columnNames;
 89  
 
 90  
     /**
 91  
      * @see org.apache.maven.reporting.AbstractMavenReport#canGenerateReport()
 92  
      */
 93  
     public boolean canGenerateReport()
 94  
     {
 95  0
         return validateIfIssueManagementComplete();
 96  
     }
 97  
 
 98  
     public void executeReport( Locale locale )
 99  
         throws MavenReportException
 100  
     {
 101  0
         if ( !canGenerateReport() )
 102  
         {
 103  0
             throw new MavenReportException( "Issue Management is out of order." );
 104  
         }
 105  
 
 106  0
         parseTracUrl();
 107  
 
 108  0
         XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
 109  
 
 110  
         try
 111  
         {
 112  0
             config.setServerURL( new URL( project.getIssueManagement().getUrl() + "/login/xmlrpc" ) );
 113  
         }
 114  0
         catch ( MalformedURLException e1 )
 115  
         {
 116  
 
 117  0
             throw new MavenReportException( "The Trac URL is incorrect." );
 118  
 
 119  0
         }
 120  0
         config.setBasicUserName( tracUser );
 121  0
         config.setBasicPassword( tracPassword );
 122  
 
 123  0
         Object[] queryResult = null;
 124  0
         XmlRpcClient client = new XmlRpcClient();
 125  
 
 126  0
         client.setConfig( config );
 127  
 
 128  0
         String qstr = "";
 129  
 
 130  0
         if ( !StringUtils.isEmpty( query ) )
 131  
         {
 132  0
             qstr = query;
 133  
         }
 134  
 
 135  0
         Object[] params = new Object[] { new String( qstr ) };
 136  
         try
 137  
         {
 138  0
             queryResult = (Object[]) client.execute( "ticket.query", params );
 139  
         }
 140  0
         catch ( XmlRpcException e )
 141  
         {
 142  0
             throw new MavenReportException( "XmlRpc Error.", e );
 143  0
         }
 144  
 
 145  0
         ArrayList ticketList = new ArrayList();
 146  
         TracTicket matchTicket;
 147  
 
 148  0
         TracReportGenerator report = new TracReportGenerator( columnNames );
 149  
 
 150  0
         if ( queryResult.length == 0 )
 151  
         {
 152  
 
 153  0
             report.doGenerateEmptyReport( getBundle( locale ), getSink() );
 154  0
             getLog().warn( "No ticket has matched." );
 155  
 
 156  
         }
 157  
         else
 158  
         {
 159  
 
 160  0
             for ( int i = 0; i < queryResult.length; i++ )
 161  
             {
 162  0
                 params = new Object[] { queryResult[i] };
 163  
                 try
 164  
                 {
 165  0
                     Object[] ticketresult = null;
 166  0
                     matchTicket = new TracTicket();
 167  0
                     ticketresult = (Object[]) client.execute( "ticket.get", params );
 168  0
                     ticketList.add( setQueryResult( ticketresult, matchTicket ) );
 169  
 
 170  
                 }
 171  0
                 catch ( XmlRpcException e )
 172  
                 {
 173  0
                     throw new MavenReportException( "XmlRpc Error.", e );
 174  0
                 }
 175  
             }
 176  
             try
 177  
             {
 178  
 
 179  0
                 report.doGenerateReport( getBundle( locale ), getSink(), ticketList );
 180  
 
 181  
             }
 182  0
             catch ( Exception e )
 183  
 
 184  
             {
 185  0
                 e.printStackTrace();
 186  
 
 187  0
             }
 188  
 
 189  
         }
 190  
 
 191  0
     }
 192  
 
 193  
     public String getName( Locale locale )
 194  
     {
 195  0
         return "Trac Report";
 196  
     }
 197  
 
 198  
     public String getDescription( Locale locale )
 199  
     {
 200  0
         return "Report on Ticket from the Trac.";
 201  
     }
 202  
 
 203  
     protected Renderer getSiteRenderer()
 204  
     {
 205  0
         return siteRenderer;
 206  
     }
 207  
 
 208  
     protected MavenProject getProject()
 209  
     {
 210  0
         return project;
 211  
     }
 212  
 
 213  
     public String getOutputName()
 214  
     {
 215  0
         return "trac-report";
 216  
     }
 217  
 
 218  
     private ResourceBundle getBundle( Locale locale )
 219  
     {
 220  0
         return ResourceBundle.getBundle( "trac-report", locale, this.getClass().getClassLoader() );
 221  
     }
 222  
 
 223  
     private void parseTracUrl()
 224  
     {
 225  
 
 226  0
         String tracUrl = project.getIssueManagement().getUrl();
 227  
 
 228  0
         if ( tracUrl.endsWith( "/" ) )
 229  
         {
 230  0
             project.getIssueManagement().setUrl( tracUrl.substring( 0, tracUrl.length() - 1 ) );
 231  
         }
 232  
 
 233  0
     }
 234  
 
 235  
     private TracTicket setQueryResult( Object[] ticketObj, TracTicket ticket )
 236  
     {
 237  
 
 238  0
         ticket.setId( String.valueOf( ticketObj[0] ) );
 239  
 
 240  0
         ticket.setLink( project.getIssueManagement().getUrl() + "/ticket/" + String.valueOf( ticketObj[0] ) );
 241  
 
 242  0
         ticket.setTimeCreated( String.valueOf( ticketObj[1] ) );
 243  
 
 244  0
         ticket.setTimeChanged( String.valueOf( ticketObj[2] ) );
 245  
 
 246  0
         Map attributes = (Map) ticketObj[3];
 247  
 
 248  0
         ticket.setType( (String) attributes.get( "type" ) );
 249  
 
 250  0
         ticket.setSummary( (String) attributes.get( "summary" ) );
 251  
 
 252  0
         ticket.setStatus( (String) attributes.get( "status" ) );
 253  
 
 254  0
         ticket.setResolution( (String) attributes.get( "resolution" ) );
 255  
 
 256  0
         ticket.setOwner( (String) attributes.get( "owner" ) );
 257  
 
 258  0
         ticket.setMilestone( (String) attributes.get( "milestone" ) );
 259  
 
 260  0
         ticket.setPriority( (String) attributes.get( "priority" ) );
 261  
 
 262  0
         ticket.setReporter( (String) attributes.get( "reporter" ) );
 263  
 
 264  0
         ticket.setComponent( (String) attributes.get( "component" ) );
 265  
 
 266  0
         return ticket;
 267  
     }
 268  
 
 269  
     private boolean validateIfIssueManagementComplete()
 270  
     {
 271  0
         if ( project.getIssueManagement() == null )
 272  
         {
 273  0
             getLog().error( "No Issue Management set. No Trac Report will be generated." );
 274  
 
 275  0
             return false;
 276  
         }
 277  0
         else if ( ( project.getIssueManagement().getUrl() == null )
 278  
             || ( project.getIssueManagement().getUrl().trim().equals( "" ) ) )
 279  
         {
 280  0
             getLog().error( "No URL set in Issue Management. No Trac Report will be generated." );
 281  
 
 282  0
             return false;
 283  
         }
 284  0
         else if ( ( project.getIssueManagement().getSystem() != null )
 285  
             && !( project.getIssueManagement().getSystem().equalsIgnoreCase( "trac" ) ) )
 286  
         {
 287  0
             getLog().error( "The Trac Report only supports Trac.  No Trac Report will be generated." );
 288  
 
 289  0
             return false;
 290  
         }
 291  0
         return true;
 292  
     }
 293  
 
 294  
 }