Author: khmarbaise Date: Sun Dec 27 19:25:30 2015 New Revision: 1721834 URL: http://svn.apache.org/viewvc?rev=1721834&view=rev Log: More refactoring.
Added: maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugins/ejb/IncludesExcludes.java maven/plugins/trunk/maven-ejb-plugin/src/test/java/org/apache/maven/plugins/ejb/IncludesExcludesTest.java Modified: maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugins/ejb/EjbHelper.java maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugins/ejb/EjbMojo.java maven/plugins/trunk/maven-ejb-plugin/src/test/java/org/apache/maven/plugins/ejb/EjbHelperTest.java Modified: maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugins/ejb/EjbHelper.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugins/ejb/EjbHelper.java?rev=1721834&r1=1721833&r2=1721834&view=diff ============================================================================== --- maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugins/ejb/EjbHelper.java (original) +++ maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugins/ejb/EjbHelper.java Sun Dec 27 19:25:30 2015 @@ -24,24 +24,34 @@ import java.io.File; /** * This class contains some helper methods which do not belong to {@link EjbMojo}. * + * <pre> + * Think about this helper class, cause i've got the impression this can be made better. + * </pre> + * * @author Karl Heinz Marbaise <khmarba...@apache.org> */ -public class EjbHelper +public final class EjbHelper { + private EjbHelper() + { + // prevent instantiation + } + /** * Check if a <code>classifier</code> is valid or not. * * @param classifier The classifier which should be checked. - * @return true in case of a valid classifier false otherwise. + * @return true in case of a valid <code>classifier</code> false otherwise which includes the case where + * <code>classifier</code> is {@code null}. */ public static boolean isClassifierValid( String classifier ) { - // @FIXME: Check classifier and clientClassifier for leading "-" ?? + // @FIXME: Check classifier for trailing dash? "a-0" valid? // What are the rules for a valid classifier? Somewhere documented? which can be used as a reference? boolean result = false; // The following check is only based on an educated guess ;-) - if ( classifier.matches( "^[a-zA-Z]+[0-9a-zA-Z\\-]*" ) ) + if ( hasClassifier( classifier ) && classifier.matches( "^[a-zA-Z]+[0-9a-zA-Z\\-]*" ) ) { result = true; } @@ -76,6 +86,15 @@ public class EjbHelper */ public static File getJarFileName( File basedir, String finalName, String classifier ) { + if ( basedir == null ) + { + throw new IllegalArgumentException( "basedir is not allowed to be null" ); + } + if ( finalName == null ) + { + throw new IllegalArgumentException( "finalName is not allowed to be null" ); + } + StringBuilder fileName = new StringBuilder( finalName ); if ( hasClassifier( classifier ) ) Modified: maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugins/ejb/EjbMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugins/ejb/EjbMojo.java?rev=1721834&r1=1721833&r2=1721834&view=diff ============================================================================== --- maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugins/ejb/EjbMojo.java (original) +++ maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugins/ejb/EjbMojo.java Sun Dec 27 19:25:30 2015 @@ -21,6 +21,7 @@ package org.apache.maven.plugins.ejb; import java.io.File; import java.io.IOException; +import java.util.Collections; import java.util.List; import org.apache.commons.io.IOUtils; @@ -48,6 +49,8 @@ import org.codehaus.plexus.archiver.jar. import org.codehaus.plexus.archiver.jar.ManifestException; import org.codehaus.plexus.util.FileUtils; +import com.google.inject.internal.util.Lists; + /** * Build an EJB (and optional client) from the current project. * @@ -60,11 +63,12 @@ import org.codehaus.plexus.util.FileUtil public class EjbMojo extends AbstractMojo { - // TODO: will null work instead? - private static final String[] DEFAULT_INCLUDES = new String[] { "**/**" }; + private static final List<String> DEFAULT_INCLUDES_LIST = + Collections.unmodifiableList( Lists.newArrayList( "**/**" ) ); - private static final String[] DEFAULT_CLIENT_EXCLUDES = - new String[] { "**/*Bean.class", "**/*CMP.class", "**/*Session.class", "**/package.html" }; + private static final List<String> DEFAULT_CLIENT_EXCLUDES_LIST = + Collections.unmodifiableList( Lists.newArrayList( "**/*Bean.class", "**/*CMP.class", "**/*Session.class", + "**/package.html" ) ); /** * Default value for {@link #clientClassifier} @@ -337,16 +341,13 @@ public class EjbMojo try { - // TODO: This should be handled different. - String[] mainJarExcludes = new String[] { ejbJar, "**/package.html" }; + List<String> defaultExcludes = Lists.newArrayList( ejbJar, "**/package.html" ); + List<String> defaultIncludes = DEFAULT_INCLUDES_LIST; - if ( excludes != null && !excludes.isEmpty() ) - { - excludes.add( ejbJar ); - mainJarExcludes = (String[]) excludes.toArray( new String[excludes.size()] ); - } + IncludesExcludes ie = + new IncludesExcludes( Collections.<String>emptyList(), excludes, defaultIncludes, defaultExcludes ); - archiver.getArchiver().addDirectory( sourceDirectory, DEFAULT_INCLUDES, mainJarExcludes ); + archiver.getArchiver().addDirectory( sourceDirectory, ie.resultingIncludes(), ie.resultingExcludes() ); // FIXME: We should be able to filter more than just the deployment descriptor? if ( deploymentDescriptor.exists() ) @@ -395,19 +396,6 @@ public class EjbMojo getLog().info( "Building EJB client " + clientJarFile.getPath() ); - String[] excludes = DEFAULT_CLIENT_EXCLUDES; - String[] includes = DEFAULT_INCLUDES; - - if ( clientIncludes != null && !clientIncludes.isEmpty() ) - { - includes = (String[]) clientIncludes.toArray( new String[clientIncludes.size()] ); - } - - if ( clientExcludes != null && !clientExcludes.isEmpty() ) - { - excludes = (String[]) clientExcludes.toArray( new String[clientExcludes.size()] ); - } - MavenArchiver clientArchiver = new MavenArchiver(); clientArchiver.setArchiver( clientJarArchiver ); @@ -416,9 +404,15 @@ public class EjbMojo try { - clientArchiver.getArchiver().addDirectory( sourceDirectory, includes, excludes ); + List<String> defaultExcludes = DEFAULT_CLIENT_EXCLUDES_LIST; + List<String> defaultIncludes = DEFAULT_INCLUDES_LIST; + + IncludesExcludes ie = + new IncludesExcludes( clientIncludes, clientExcludes, defaultIncludes, defaultExcludes ); + + clientArchiver.getArchiver().addDirectory( sourceDirectory, ie.resultingIncludes(), + ie.resultingExcludes() ); - // create archive clientArchiver.createArchive( session, project, archive ); } @@ -538,4 +532,5 @@ public class EjbMojo { return clientClassifier; } + } Added: maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugins/ejb/IncludesExcludes.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugins/ejb/IncludesExcludes.java?rev=1721834&view=auto ============================================================================== --- maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugins/ejb/IncludesExcludes.java (added) +++ maven/plugins/trunk/maven-ejb-plugin/src/main/java/org/apache/maven/plugins/ejb/IncludesExcludes.java Sun Dec 27 19:25:30 2015 @@ -0,0 +1,90 @@ +package org.apache.maven.plugins.ejb; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.util.Collections; +import java.util.List; + +/** + * @author Karl Heinz Marbaise <khmarba...@apache.org> + */ +public class IncludesExcludes +{ + private List<String> includes; + + private List<String> defaultIncludes; + + private List<String> excludes; + + private List<String> defaultExcludes; + + public IncludesExcludes( List<String> includes, List<String> excludes, List<String> defaultIncludes, + List<String> defaultExcludes ) + { + this.includes = makeNonNullList( includes ); + this.excludes = makeNonNullList( excludes ); + this.defaultIncludes = makeNonNullList( defaultIncludes ); + this.defaultExcludes = makeNonNullList( defaultExcludes ); + } + + public String[] resultingIncludes() + { + String[] result = new String[0]; + if ( includes.isEmpty() ) + { + result = defaultIncludes.toArray( new String[defaultIncludes.size()] ); + } + else + { + result = includes.toArray( new String[includes.size()] ); + } + + return result; + } + + public String[] resultingExcludes() + { + String[] result = new String[0]; + if ( excludes.isEmpty() ) + { + result = defaultExcludes.toArray( new String[defaultExcludes.size()] ); + } + else + { + result = excludes.toArray( new String[excludes.size()] ); + } + + return result; + + } + + private List<String> makeNonNullList( List<String> in ) + { + if ( in == null ) + { + return Collections.<String>emptyList(); + } + else + { + return in; + } + } + +} Modified: maven/plugins/trunk/maven-ejb-plugin/src/test/java/org/apache/maven/plugins/ejb/EjbHelperTest.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ejb-plugin/src/test/java/org/apache/maven/plugins/ejb/EjbHelperTest.java?rev=1721834&r1=1721833&r2=1721834&view=diff ============================================================================== --- maven/plugins/trunk/maven-ejb-plugin/src/test/java/org/apache/maven/plugins/ejb/EjbHelperTest.java (original) +++ maven/plugins/trunk/maven-ejb-plugin/src/test/java/org/apache/maven/plugins/ejb/EjbHelperTest.java Sun Dec 27 19:25:30 2015 @@ -48,12 +48,18 @@ public class EjbHelperTest } @Test - public void invalidClassifier() + public void isClassifierValidShouldReturnFalseIfClassifierIsPrefixedByDash() { assertThat( EjbHelper.isClassifierValid( "-anton" ), is( Boolean.FALSE ) ); } @Test + public void isClassifierValidShouldReturnFalseIfClassifierIsNull() + { + assertThat( EjbHelper.isClassifierValid( null ), is( Boolean.FALSE ) ); + } + + @Test public void hasClassifierShouldReturnFalseForNull() { assertThat( EjbHelper.hasClassifier( null ), is( Boolean.FALSE ) ); Added: maven/plugins/trunk/maven-ejb-plugin/src/test/java/org/apache/maven/plugins/ejb/IncludesExcludesTest.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ejb-plugin/src/test/java/org/apache/maven/plugins/ejb/IncludesExcludesTest.java?rev=1721834&view=auto ============================================================================== --- maven/plugins/trunk/maven-ejb-plugin/src/test/java/org/apache/maven/plugins/ejb/IncludesExcludesTest.java (added) +++ maven/plugins/trunk/maven-ejb-plugin/src/test/java/org/apache/maven/plugins/ejb/IncludesExcludesTest.java Sun Dec 27 19:25:30 2015 @@ -0,0 +1,42 @@ +package org.apache.maven.plugins.ejb; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.util.Collections; + +import org.junit.Test; + +public class IncludesExcludesTest +{ + + @Test + public void emptyListsShouldResultInZeroSizeResults() + { + IncludesExcludes ie = new IncludesExcludes( Collections.<String>emptyList(), Collections.<String>emptyList(), + Collections.<String>emptyList(), Collections.<String>emptyList() ); + + assertThat( ie.resultingIncludes(), is( new String[0] ) ); + assertThat( ie.resultingExcludes(), is( new String[0] ) ); + } + + @Test + public void nullForInclucesShouldResultInZeroSizeResults() + { + IncludesExcludes ie = new IncludesExcludes( null, Collections.<String>emptyList(), + Collections.<String>emptyList(), Collections.<String>emptyList() ); + + assertThat( ie.resultingIncludes(), is( new String[0] ) ); + assertThat( ie.resultingExcludes(), is( new String[0] ) ); + } + + @Test + public void nullForExclucesShouldResultInZeroSizeResults() + { + IncludesExcludes ie = new IncludesExcludes( Collections.<String>emptyList(), null, + Collections.<String>emptyList(), Collections.<String>emptyList() ); + + assertThat( ie.resultingIncludes(), is( new String[0] ) ); + assertThat( ie.resultingExcludes(), is( new String[0] ) ); + } +}