Coverage Report - org.apache.maven.plugin.eclipse.EclipseSourceDir
 
Classes in this File Line Coverage Branch Coverage Complexity
EclipseSourceDir
83%
55/66
55%
12/22
1,5
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.maven.plugin.eclipse;
 20  
 
 21  
 import java.util.ArrayList;
 22  
 import java.util.LinkedHashSet;
 23  
 import java.util.List;
 24  
 
 25  
 import org.apache.maven.plugin.MojoExecutionException;
 26  
 import org.apache.maven.plugin.ide.IdeUtils;
 27  
 import org.codehaus.plexus.util.StringUtils;
 28  
 
 29  
 /**
 30  
  * Represent an eclipse source dir. Eclipse has no "main", "test" or "resource" concepts, so two source dirs with the
 31  
  * same path are equal.
 32  
  * <p>
 33  
  * source directories should always have a null output value.
 34  
  * 
 35  
  * @author <a href="mailto:fgiust@users.sourceforge.net">Fabrizio Giustina</a>
 36  
  * @version $Id: EclipseSourceDir.java 798342 2009-07-28 00:17:00Z aheritier $
 37  
  */
 38  
 public class EclipseSourceDir
 39  
     implements Comparable
 40  
 {
 41  
     private static final String PATTERN_SEPARATOR = "|";
 42  
 
 43  
     private String path;
 44  
 
 45  
     /**
 46  
      * source directories should always have a null output value.
 47  
      */
 48  
     private String output;
 49  
 
 50  
     /**
 51  
      * List<String>
 52  
      */
 53  
     private List include;
 54  
 
 55  
     /**
 56  
      * List<String>
 57  
      */
 58  
     private List exclude;
 59  
 
 60  
     private boolean isResource;
 61  
 
 62  
     private boolean test;
 63  
 
 64  
     private boolean filtering;
 65  
 
 66  
     /**
 67  
      * @param path the eclipse source directory
 68  
      * @param output path output directory
 69  
      * @param isResource true if the directory only contains resources, false if a compilation directory
 70  
      * @param test true if is a test directory, false otherwise
 71  
      * @param include a string in the eclipse pattern format for the include filter
 72  
      * @param exclude a string in the eclipse pattern format for the exclude filter
 73  
      * @param filtering true if filtering should be applied, false otherwise. Note: Filtering will only be applied if
 74  
      *            this become a "special directory" by being nested within the default output directory.
 75  
      */
 76  
     public EclipseSourceDir( String path, String output, boolean isResource, boolean test, List include, List exclude,
 77  
                              boolean filtering )
 78  114
     {
 79  114
         setPath( path );
 80  114
         this.output = output;
 81  114
         this.isResource = isResource;
 82  114
         this.test = test;
 83  114
         setInclude( include );
 84  114
         setExclude( exclude );
 85  114
         this.filtering = filtering;
 86  114
     }
 87  
 
 88  
     /**
 89  
      * Getter for <code>exclude</code>.
 90  
      * 
 91  
      * @return Returns the exclude. Never null.
 92  
      */
 93  
     public List getExclude()
 94  
     {
 95  36
         return this.exclude;
 96  
     }
 97  
 
 98  
     /**
 99  
      * Setter for <code>exclude</code>.
 100  
      * 
 101  
      * @param exclude The exclude to set.
 102  
      */
 103  
     public void setExclude( List exclude )
 104  
     {
 105  120
         this.exclude = new ArrayList();
 106  120
         if ( exclude != null )
 107  
         {
 108  84
             this.exclude.addAll( exclude );
 109  
         }
 110  120
     }
 111  
 
 112  
     /**
 113  
      * Getter for <code>include</code>.
 114  
      * 
 115  
      * @return Returns the include. Never null.
 116  
      */
 117  
     public List getInclude()
 118  
     {
 119  36
         return this.include;
 120  
     }
 121  
 
 122  
     /**
 123  
      * Setter for <code>include</code>.
 124  
      * 
 125  
      * @param include The include to set.
 126  
      */
 127  
     public void setInclude( List include )
 128  
     {
 129  120
         this.include = new ArrayList();
 130  120
         if ( include != null )
 131  
         {
 132  60
             this.include.addAll( include );
 133  
         }
 134  120
     }
 135  
 
 136  
     /**
 137  
      * @return Returns the exclude as a string pattern suitable for eclipse
 138  
      */
 139  
     public String getExcludeAsString()
 140  
     {
 141  78
         return StringUtils.join( exclude.iterator(), PATTERN_SEPARATOR );
 142  
     }
 143  
 
 144  
     /**
 145  
      * @return Returns the include as a string pattern suitable for eclipse
 146  
      */
 147  
     public String getIncludeAsString()
 148  
     {
 149  78
         return StringUtils.join( include.iterator(), PATTERN_SEPARATOR );
 150  
     }
 151  
 
 152  
     /**
 153  
      * Getter for <code>output</code>.
 154  
      * <p>
 155  
      * source directories should always have a null output value.
 156  
      * 
 157  
      * @return Returns the output.
 158  
      */
 159  
     public String getOutput()
 160  
     {
 161  108
         return this.output;
 162  
     }
 163  
 
 164  
     /**
 165  
      * Setter for <code>output</code>.
 166  
      * 
 167  
      * @param output The output to set.
 168  
      */
 169  
     public void setOutput( String output )
 170  
     {
 171  0
         this.output = output;
 172  0
     }
 173  
 
 174  
     /**
 175  
      * Getter for <code>path</code>.
 176  
      * 
 177  
      * @return Returns the path.
 178  
      */
 179  
     public String getPath()
 180  
     {
 181  48
         return this.path;
 182  
     }
 183  
 
 184  
     /**
 185  
      * Setter for <code>path</code>. Converts \\ to / in path.
 186  
      * 
 187  
      * @param path The path to set.
 188  
      */
 189  
     public void setPath( String path )
 190  
     {
 191  114
         this.path = IdeUtils.fixSeparator( path );
 192  114
     }
 193  
 
 194  
     /**
 195  
      * Getter for <code>test</code>.
 196  
      * 
 197  
      * @return Returns the test.
 198  
      */
 199  
     public boolean isTest()
 200  
     {
 201  0
         return this.test;
 202  
     }
 203  
 
 204  
     /**
 205  
      * Setter for <code>test</code>.
 206  
      * 
 207  
      * @param test The test to set.
 208  
      */
 209  
     public void setTest( boolean test )
 210  
     {
 211  0
         this.test = test;
 212  0
     }
 213  
 
 214  
     /**
 215  
      * Getter for <code>isResource</code>.
 216  
      * 
 217  
      * @return Returns the isResource.
 218  
      */
 219  
     public boolean isResource()
 220  
     {
 221  12
         return this.isResource;
 222  
     }
 223  
 
 224  
     /**
 225  
      * Wheter this resource should be copied with filtering.
 226  
      */
 227  
     public boolean isFiltering()
 228  
     {
 229  6
         return filtering;
 230  
     }
 231  
 
 232  
     /**
 233  
      * @see java.lang.Object#equals(java.lang.Object)
 234  
      */
 235  
     public boolean equals( Object obj )
 236  
     {
 237  0
         return ( obj != null ) && ( obj instanceof EclipseSourceDir )
 238  
             && this.path.equals( ( (EclipseSourceDir) obj ).path );
 239  
     }
 240  
 
 241  
     /**
 242  
      * @see java.lang.Object#hashCode()
 243  
      */
 244  
     public int hashCode()
 245  
     {
 246  48
         return this.path.hashCode();
 247  
     }
 248  
 
 249  
     /**
 250  
      * @see java.lang.Comparable#compareTo(java.lang.Object)
 251  
      */
 252  
     public int compareTo( Object obj )
 253  
     {
 254  0
         return this.path.compareTo( ( (EclipseSourceDir) obj ).path );
 255  
     }
 256  
 
 257  
     /**
 258  
      * {@inheritDoc}
 259  
      */
 260  
     public String toString()
 261  
     {
 262  54
         StringBuffer buffer = new StringBuffer();
 263  54
         buffer.append( ( isResource ? "re" : "" ) + "source " );
 264  54
         buffer.append( path );
 265  54
         buffer.append( ": " );
 266  54
         buffer.append( "output=" ).append( output ).append( ", " );
 267  54
         buffer.append( "include=[" ).append( getIncludeAsString() ).append( "], " );
 268  54
         buffer.append( "exclude=[" ).append( getExcludeAsString() ).append( "], " );
 269  54
         buffer.append( "test=" ).append( test ).append( ", " );
 270  54
         buffer.append( "filtering=" ).append( filtering );
 271  54
         return buffer.toString();
 272  
     }
 273  
 
 274  
     /**
 275  
      * Merge with the provided directory.
 276  
      * <p>
 277  
      * If one directory is a source and the other is a resource directory then the result will be a source directory and
 278  
      * any includes or excludes will be removed since Eclipse has no "main", "test" or "resource" concepts. The output
 279  
      * directory will be the source directories value.
 280  
      * <p>
 281  
      * If the two directories are the same resources type (i.e isResource is equal) then the result will be the same
 282  
      * resource type with the includes from each merged together (duplicates will be removed), similarly for the
 283  
      * excludes. No effort is made to ensure that the includes and excludes are disjointed sets. Please fix your pom
 284  
      * instead.
 285  
      * <p>
 286  
      * No support for cases where the test, or filtering values are not identical.
 287  
      * 
 288  
      * @param mergeWith the directory to merge with
 289  
      * @throws MojoExecutionException test or filtering values are not identical, or isResource true and output are not
 290  
      *             identical
 291  
      */
 292  
     public void merge( EclipseSourceDir mergeWith )
 293  
         throws MojoExecutionException
 294  
     {
 295  18
         if ( test != mergeWith.test )
 296  
         {
 297  0
             throw new MojoExecutionException( "Request to merge when 'test' is not identical. Original=" + toString()
 298  
                 + ", merging with=" + mergeWith.toString() );
 299  
         }
 300  18
         if ( filtering != mergeWith.filtering )
 301  
         {
 302  0
             throw new MojoExecutionException( "Request to merge when 'filtering' is not identical. Original="
 303  
                 + toString() + ", merging with=" + mergeWith.toString() );
 304  
         }
 305  
 
 306  18
         if ( isResource != mergeWith.isResource )
 307  
         {
 308  6
             if ( isResource )
 309  
             {
 310  
                 // the output directory is set to the source directory's value
 311  0
                 output = mergeWith.output;
 312  
             }
 313  6
             isResource = false;
 314  6
             setInclude( null );
 315  6
             setExclude( null );
 316  
 
 317  
         }
 318  
         else
 319  
         {
 320  12
             if ( !StringUtils.equals( output, mergeWith.output ) )
 321  
             {
 322  0
                 throw new MojoExecutionException( "Request to merge when 'output' is not identical. Original="
 323  
                     + toString() + ", merging with=" + mergeWith.toString() );
 324  
             }
 325  
 
 326  12
             LinkedHashSet includesAsSet = new LinkedHashSet();
 327  12
             includesAsSet.addAll( include );
 328  12
             includesAsSet.addAll( mergeWith.include );
 329  12
             include = new ArrayList( includesAsSet );
 330  
 
 331  12
             LinkedHashSet excludesAsSet = new LinkedHashSet();
 332  12
             excludesAsSet.addAll( exclude );
 333  12
             excludesAsSet.addAll( mergeWith.exclude );
 334  12
             exclude = new ArrayList( excludesAsSet );
 335  
         }
 336  18
     }
 337  
 }