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.727
 
 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$
 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  
      * @see org.apache.maven.reporting.AbstractMavenReport#canGenerateReport()
 75  
      */
 76  
     public boolean canGenerateReport()
 77  
     {
 78  0
         return validateIfIssueManagementComplete();
 79  
     }
 80  
 
 81  
     public void executeReport( Locale locale )
 82  
         throws MavenReportException
 83  
     {
 84  0
         if ( !canGenerateReport() )
 85  
         {
 86  0
             throw new MavenReportException( "Issue Management is out of order." );
 87  
         }
 88  
 
 89  0
         parseTracUrl();
 90  
 
 91  0
         XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
 92  
 
 93  
         try
 94  
         {
 95  0
             config.setServerURL( new URL( project.getIssueManagement().getUrl() + "/login/xmlrpc" ) );
 96  
         }
 97  0
         catch ( MalformedURLException e1 )
 98  
         {
 99  
 
 100  0
             throw new MavenReportException( "The Trac URL is incorrect." );
 101  
 
 102  0
         }
 103  0
         config.setBasicUserName( tracUser );
 104  0
         config.setBasicPassword( tracPassword );
 105  
 
 106  0
         Object[] queryResult = null;
 107  0
         XmlRpcClient client = new XmlRpcClient();
 108  
 
 109  0
         client.setConfig( config );
 110  
 
 111  0
         String qstr = "";
 112  
 
 113  0
         if ( !StringUtils.isEmpty( query ) )
 114  
         {
 115  0
             qstr = query;
 116  
         }
 117  
 
 118  0
         Object[] params = new Object[] { new String( qstr ) };
 119  
         try
 120  
         {
 121  0
             queryResult = (Object[]) client.execute( "ticket.query", params );
 122  
         }
 123  0
         catch ( XmlRpcException e )
 124  
         {
 125  0
             throw new MavenReportException( "XmlRpc Error.", e );
 126  0
         }
 127  
 
 128  0
         ArrayList ticketList = new ArrayList();
 129  
         TracTicket matchTicket;
 130  
 
 131  0
         TracReportGenerator report = new TracReportGenerator();
 132  
 
 133  0
         if ( queryResult.length == 0 )
 134  
         {
 135  
 
 136  0
             report.doGenerateEmptyReport( getBundle( locale ), getSink() );
 137  0
             getLog().warn( "No ticket has matched." );
 138  
 
 139  
         }
 140  
         else
 141  
         {
 142  
 
 143  0
             for ( int i = 0; i < queryResult.length; i++ )
 144  
             {
 145  0
                 params = new Object[] { queryResult[i] };
 146  
                 try
 147  
                 {
 148  0
                     Object[] Ticketresult = null;
 149  0
                     matchTicket = new TracTicket();
 150  0
                     Ticketresult = (Object[]) client.execute( "ticket.get", params );
 151  0
                     ticketList.add( setQueryResult( Ticketresult, matchTicket ) );
 152  
 
 153  
                 }
 154  0
                 catch ( XmlRpcException e )
 155  
                 {
 156  0
                     throw new MavenReportException( "XmlRpc Error.", e );
 157  0
                 }
 158  
             }
 159  
             try
 160  
             {
 161  
 
 162  0
                 report.doGenerateReport( getBundle( locale ), getSink(), ticketList );
 163  
 
 164  
             }
 165  0
             catch ( Exception e )
 166  
 
 167  
             {
 168  0
                 e.printStackTrace();
 169  
 
 170  0
             }
 171  
 
 172  
         }
 173  
 
 174  0
     }
 175  
 
 176  
     public String getName( Locale locale )
 177  
     {
 178  0
         return "Trac Report";
 179  
     }
 180  
 
 181  
     public String getDescription( Locale locale )
 182  
     {
 183  0
         return "Report on Ticket from the Trac.";
 184  
     }
 185  
 
 186  
     protected Renderer getSiteRenderer()
 187  
     {
 188  0
         return siteRenderer;
 189  
     }
 190  
 
 191  
     protected MavenProject getProject()
 192  
     {
 193  0
         return project;
 194  
     }
 195  
 
 196  
     public String getOutputName()
 197  
     {
 198  0
         return "trac-report";
 199  
     }
 200  
 
 201  
     private ResourceBundle getBundle( Locale locale )
 202  
     {
 203  0
         return ResourceBundle.getBundle( "trac-report", locale, this.getClass().getClassLoader() );
 204  
     }
 205  
 
 206  
     private void parseTracUrl()
 207  
     {
 208  
 
 209  0
         String tracUrl = project.getIssueManagement().getUrl();
 210  
 
 211  0
         if ( tracUrl.endsWith( "/" ) )
 212  
         {
 213  0
             project.getIssueManagement().setUrl( tracUrl.substring( 0, tracUrl.length() - 1 ) );
 214  
         }
 215  
 
 216  0
     }
 217  
 
 218  
     private TracTicket setQueryResult( Object[] ticketObj, TracTicket ticket )
 219  
     {
 220  
 
 221  0
         ticket.setId( String.valueOf( ticketObj[0] ) );
 222  
 
 223  0
         ticket.setLink( project.getIssueManagement().getUrl() + "/ticket/" + String.valueOf( ticketObj[0] ) );
 224  
 
 225  0
         ticket.setTimeCreated( String.valueOf( ticketObj[1] ) );
 226  
 
 227  0
         ticket.setTimeChanged( String.valueOf( ticketObj[2] ) );
 228  
 
 229  0
         Map attributes = (Map) ticketObj[3];
 230  
 
 231  0
         ticket.setType( (String) attributes.get( "type" ) );
 232  
 
 233  0
         ticket.setSummary( (String) attributes.get( "summary" ) );
 234  
 
 235  0
         ticket.setStatus( (String) attributes.get( "status" ) );
 236  
 
 237  0
         ticket.setResolution( (String) attributes.get( "resolution" ) );
 238  
 
 239  0
         ticket.setOwner( (String) attributes.get( "owner" ) );
 240  
 
 241  0
         ticket.setMilestone( (String) attributes.get( "milestone" ) );
 242  
 
 243  0
         ticket.setPriority( (String) attributes.get( "priority" ) );
 244  
 
 245  0
         ticket.setReporter( (String) attributes.get( "reporter" ) );
 246  
 
 247  0
         ticket.setComponent( (String) attributes.get( "component" ) );
 248  
 
 249  0
         return ticket;
 250  
     }
 251  
 
 252  
     private boolean validateIfIssueManagementComplete()
 253  
     {
 254  0
         if ( project.getIssueManagement() == null )
 255  
         {
 256  0
             getLog().error( "No Issue Management set. No Trac Report will be generated." );
 257  
 
 258  0
             return false;
 259  
         }
 260  0
         else if ( ( project.getIssueManagement().getUrl() == null )
 261  
             || ( project.getIssueManagement().getUrl().trim().equals( "" ) ) )
 262  
         {
 263  0
             getLog().error( "No URL set in Issue Management. No Trac Report will be generated." );
 264  
 
 265  0
             return false;
 266  
         }
 267  0
         else if ( ( project.getIssueManagement().getSystem() != null )
 268  
             && !( project.getIssueManagement().getSystem().equalsIgnoreCase( "trac" ) ) )
 269  
         {
 270  0
             getLog().error( "The Trac Report only supports Trac.  No Trac Report will be generated." );
 271  
 
 272  0
             return false;
 273  
         }
 274  0
         return true;
 275  
     }
 276  
 
 277  
 }