Author: jdcasey Date: Wed Jul 19 15:40:46 2006 New Revision: 423644 URL: http://svn.apache.org/viewvc?rev=423644&view=rev Log: Fixed interpolation to make it more consistent for file name mappings and output directory format. Also, added new unit tests...all of org.apache.maven.plugin.assembly.utils should be tested now, with the exception of DigestUtils.
Added: maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/AssemblyFileUtilsTest.java (with props) maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/AssemblyFormatUtilsTest.java (with props) Modified: maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/AssemblyFileUtils.java maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/AssemblyFormatUtils.java maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/DigestUtils.java Modified: maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/AssemblyFileUtils.java URL: http://svn.apache.org/viewvc/maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/AssemblyFileUtils.java?rev=423644&r1=423643&r2=423644&view=diff ============================================================================== --- maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/AssemblyFileUtils.java (original) +++ maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/AssemblyFileUtils.java Wed Jul 19 15:40:46 2006 @@ -19,6 +19,8 @@ /** * NOTE: It is the responsibility of the caller to close the source Reader instance. + * @param lineEndings This is the result of the getLineEndingChars(..) method in this utility class; the actual + * line-ending characters. */ public static void convertLineEndings( Reader source, File dest, String lineEndings ) throws IOException @@ -27,13 +29,13 @@ BufferedReader bufferedSource = null; try { - if ( !( source instanceof BufferedReader ) ) + if ( source instanceof BufferedReader ) { - bufferedSource = new BufferedReader( source ); + bufferedSource = (BufferedReader) source; } else { - bufferedSource = (BufferedReader) source; + bufferedSource = new BufferedReader( source ); } out = new BufferedWriter( new FileWriter( dest ) ); Modified: maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/AssemblyFormatUtils.java URL: http://svn.apache.org/viewvc/maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/AssemblyFormatUtils.java?rev=423644&r1=423643&r2=423644&view=diff ============================================================================== --- maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/AssemblyFormatUtils.java (original) +++ maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/AssemblyFormatUtils.java Wed Jul 19 15:40:46 2006 @@ -1,17 +1,15 @@ package org.apache.maven.plugin.assembly.utils; import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.handler.ArtifactHandler; -import org.apache.maven.model.Build; import org.apache.maven.plugin.assembly.AssemblerConfigurationSource; import org.apache.maven.plugin.assembly.format.AssemblyFormattingException; import org.apache.maven.plugins.assembly.model.Assembly; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.util.StringUtils; -import org.codehaus.plexus.util.introspection.ReflectionValueExtractor; +import org.codehaus.plexus.util.interpolation.ObjectBasedValueSource; +import org.codehaus.plexus.util.interpolation.RegexBasedInterpolator; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import java.util.Properties; public final class AssemblyFormatUtils { @@ -57,12 +55,6 @@ value = ""; } - if ( !value.endsWith( "/" ) && !value.endsWith( "\\" ) ) - { - // TODO: shouldn't archiver do this? - value += '/'; - } - if ( includeBaseDirectory ) { if ( value.startsWith( "/" ) ) @@ -74,23 +66,30 @@ value = finalName + "/" + value; } } - else + + RegexBasedInterpolator interpolator = new RegexBasedInterpolator(); + + Properties specialExpressionOverrides = new Properties(); + + if ( finalName != null ) { - if ( value.startsWith( "/" ) ) - { - value = value.substring( 1 ); - } + specialExpressionOverrides.setProperty( "finalName", finalName ); + specialExpressionOverrides.setProperty( "build.finalName", finalName ); } - + + interpolator.addValueSource( new PropertiesInterpolationValueSource( specialExpressionOverrides ) ); + if ( project != null ) { - value = StringUtils.replace( value, "${groupId}", project.getGroupId() ); - value = StringUtils.replace( value, "${artifactId}", project.getArtifactId() ); - value = StringUtils.replace( value, "${version}", project.getVersion() ); - - Build build = project.getBuild(); - value = StringUtils.replace( value, "${build.finalName}", build.getFinalName() ); - value = StringUtils.replace( value, "${finalName}", build.getFinalName() ); + interpolator.addValueSource( new ObjectBasedValueSource( project ) ); + } + + value = interpolator.interpolate( value, "__project" ); + + if ( value.length() > 0 && !value.endsWith( "/" ) && !value.endsWith( "\\" ) ) + { + // TODO: shouldn't archiver do this? + value += "/"; } return value; @@ -110,6 +109,7 @@ { String value = expression; + // TODO: [jdcasey] What if they *want* to suppress the classifier?! This should be part of the expression, IMO // insert the classifier if exist if ( !StringUtils.isEmpty( artifact.getClassifier() ) ) { @@ -127,44 +127,13 @@ value = value + "-" + artifact.getClassifier(); } } - - // this matches the last ${...} string - Pattern pat = Pattern.compile( "^(.*)\\$\\{([^\\}]+)\\}(.*)$" ); - Matcher mat = pat.matcher( expression ); - - if ( mat.matches() ) - { - Object middle; - String left = evaluateFileNameMapping( mat.group( 1 ), artifact ); - try - { - middle = ReflectionValueExtractor.evaluate( mat.group( 2 ), artifact, false ); - } - catch ( Exception e ) - { - throw new AssemblyFormattingException( "Cannot evaluate filenameMapping: '" + mat.group( 2 ) + "': " - + e.getMessage(), e ); - } - String right = mat.group( 3 ); - - if ( middle == null ) - { - // TODO: There should be a more generic way dealing with that. - // Having magic words is not good at all. - // probe for magic word - if ( "extension".equals( mat.group( 2 ).trim() ) ) - { - ArtifactHandler artifactHandler = artifact.getArtifactHandler(); - middle = artifactHandler.getExtension(); - } - else - { - middle = "${" + mat.group( 2 ) + "}"; - } - } - - value = left + middle + right; - } + + RegexBasedInterpolator interpolator = new RegexBasedInterpolator(); + + interpolator.addValueSource( new ObjectBasedValueSource( artifact ) ); + interpolator.addValueSource( new ObjectBasedValueSource( artifact.getArtifactHandler() ) ); + + value = interpolator.interpolate( value, "__artifact" ); return value; } Modified: maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/DigestUtils.java URL: http://svn.apache.org/viewvc/maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/DigestUtils.java?rev=423644&r1=423643&r2=423644&view=diff ============================================================================== --- maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/DigestUtils.java (original) +++ maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/DigestUtils.java Wed Jul 19 15:40:46 2006 @@ -31,6 +31,7 @@ * Create a digest for a file. Stolen from repository-utils - once released, use that instead. * * @author <a href="mailto:[EMAIL PROTECTED]">Brett Porter</a> + * @todo [jdcasey] This needs unit tests. */ public class DigestUtils { Added: maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/AssemblyFileUtilsTest.java URL: http://svn.apache.org/viewvc/maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/AssemblyFileUtilsTest.java?rev=423644&view=auto ============================================================================== --- maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/AssemblyFileUtilsTest.java (added) +++ maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/AssemblyFileUtilsTest.java Wed Jul 19 15:40:46 2006 @@ -0,0 +1,112 @@ +package org.apache.maven.plugin.assembly.utils; + +import org.apache.maven.plugin.assembly.format.AssemblyFormattingException; +import org.codehaus.plexus.util.IOUtil; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; + +import junit.framework.TestCase; + +public class AssemblyFileUtilsTest + extends TestCase +{ + + public void testGetLineEndingChars_ShouldReturnDosLineEnding() + throws AssemblyFormattingException + { + assertEquals( "\r\n", AssemblyFileUtils.getLineEndingCharacters( "dos" ) ); + assertEquals( "\r\n", AssemblyFileUtils.getLineEndingCharacters( "crlf" ) ); + } + + public void testGetLineEndingChars_ShouldReturnUnixLineEnding() + throws AssemblyFormattingException + { + assertEquals( "\n", AssemblyFileUtils.getLineEndingCharacters( "unix" ) ); + assertEquals( "\n", AssemblyFileUtils.getLineEndingCharacters( "lf" ) ); + } + + public void testGetLineEndingChars_ShouldReturnNullLineEnding() + throws AssemblyFormattingException + { + assertNull( AssemblyFileUtils.getLineEndingCharacters( "keep" ) ); + } + + public void testGetLineEndingChars_ShouldThrowFormattingExceptionWithInvalidHint() + { + try + { + AssemblyFileUtils.getLineEndingCharacters( "invalid" ); + + fail( "Invalid line-ending hint should throw a formatting exception." ); + } + catch ( AssemblyFormattingException e ) + { + } + } + + // TODO: Fix the end-of-document problem with line-ending conversions. + public void testConvertLineEndings_ShouldReplaceLFWithCRLF() throws IOException + { + String test = "This is a \ntest."; + String check = "This is a \r\ntest.\r\n"; + + testConversion( test, check, "\r\n" ); + } + + // TODO: Fix the end-of-document problem with line-ending conversions. + public void testConvertLineEndings_ShouldReplaceCRLFWithLF() throws IOException + { + String test = "This is a \r\ntest."; + String check = "This is a \ntest.\n"; + + testConversion( test, check, "\n" ); + } + + // TODO: Fix the end-of-document problem with line-ending conversions. + public void testConvertLineEndings_ShouldReplaceLFWithLF() throws IOException + { + String test = "This is a \ntest."; + String check = "This is a \ntest.\n"; + + testConversion( test, check, "\n" ); + } + + // TODO: Fix the end-of-document problem with line-ending conversions. + public void testConvertLineEndings_ShouldReplaceCRLFWithCRLF() throws IOException + { + String test = "This is a \r\ntest."; + String check = "This is a \r\ntest.\r\n"; + + testConversion( test, check, "\r\n" ); + } + + private void testConversion( String test, String check, String lineEndingChars ) + throws IOException + { + File dest = File.createTempFile( "line-conversion-test.", "" ); + dest.deleteOnExit(); + + AssemblyFileUtils.convertLineEndings( new StringReader( test ), dest, lineEndingChars ); + + FileReader reader = null; + StringWriter writer = new StringWriter(); + + try + { + reader = new FileReader( dest ); + + IOUtil.copy( reader, writer ); + } + finally + { + IOUtil.close( reader ); + } + + assertEquals( check, writer.toString() ); + } + +} Propchange: maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/AssemblyFileUtilsTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/AssemblyFileUtilsTest.java ------------------------------------------------------------------------------ svn:keywords = "Author Date Id Revision" Added: maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/AssemblyFormatUtilsTest.java URL: http://svn.apache.org/viewvc/maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/AssemblyFormatUtilsTest.java?rev=423644&view=auto ============================================================================== --- maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/AssemblyFormatUtilsTest.java (added) +++ maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/AssemblyFormatUtilsTest.java Wed Jul 19 15:40:46 2006 @@ -0,0 +1,290 @@ +package org.apache.maven.plugin.assembly.utils; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.handler.ArtifactHandler; +import org.apache.maven.model.Model; +import org.apache.maven.plugin.assembly.AssemblerConfigurationSource; +import org.apache.maven.plugin.assembly.format.AssemblyFormattingException; +import org.apache.maven.plugin.assembly.testutils.MockManager; +import org.apache.maven.plugins.assembly.model.Assembly; +import org.apache.maven.project.MavenProject; +import org.easymock.MockControl; + +import junit.framework.TestCase; + + +public class AssemblyFormatUtilsTest + extends TestCase +{ + + private MockManager mockManager = new MockManager(); + + public void testGetDistroName_ShouldUseJustFinalNameWithNoAppendAssemblyIdOrClassifier() + { + verifyDistroName( "assembly", null, "finalName", false, "finalName" ); + } + + public void testGetDistroName_ShouldUseJustFinalNameWhenAppendAssemblyIdAndAssemblyIdIsNull() + { + verifyDistroName( null, "classifier", "finalName", true, "finalName" ); + } + + public void testGetDistroName_ShouldUseFinalNamePlusAssemblyIdNotClassifier() + { + verifyDistroName( "assembly", "classifier", "finalName", true, "finalName-assembly" ); + } + + public void testGetDistroName_ShouldUseFinalNamePlusClassifierWhenAppendAssemblyIdIsNull() + { + verifyDistroName( "assembly", "classifier", "finalName", false, "finalName-classifier" ); + } + + public void testGetOutputDir_ShouldNotAlterOutDirWhenIncludeBaseFalseAndNoExpressions() + { + verifyOutputDir( "dir/", "finalName", false, null, null, null, "dir/" ); + } + + public void testGetOutputDir_ShouldNotAlterOutDirWhenIncludeBaseFalseAndNoExpressions_CheckWithBackslash() + { + verifyOutputDir( "dir\\", "finalName", false, null, null, null, "dir\\" ); + } + + public void testGetOutputDir_ShouldAppendSlashToOutDirWhenMissingAndIncludeBaseFalseAndNoExpressions() + { + verifyOutputDir( "dir", "finalName", false, null, null, null, "dir/" ); + } + + public void testGetOutputDir_ShouldPrependFinalNameWhenIncludeBaseTrue() + { + verifyOutputDir( "dir/", "finalName", true, null, null, null, "finalName/dir/" ); + } + + public void testGetOutputDir_ShouldResolveGroupIdInOutDir() + { + verifyOutputDir( "${groupId}", "finalName", false, "group", null, null, "group/" ); + } + + public void testGetOutputDir_ShouldResolveArtifactIdInOutDir() + { + verifyOutputDir( "${artifactId}", "finalName", false, null, "artifact", null, "artifact/" ); + } + + public void testGetOutputDir_ShouldResolveVersionInOutDir() + { + verifyOutputDir( "${version}", "finalName", false, null, null, "version", "version/" ); + } + + public void testGetOutputDir_ShouldResolveFinalNameInOutDir() + { + verifyOutputDir( "${finalName}", "finalName", false, null, null, null, "finalName/" ); + } + + public void testGetOutputDir_ShouldResolveBuildFinalNameInOutDir() + { + verifyOutputDir( "${build.finalName}", "finalName", false, null, null, null, "finalName/" ); + } + + public void testGetOutputDir_ShouldReturnEmptyPathWhenAllInputIsEmptyAndIncludeBaseFalse() + { + verifyOutputDir( null, null, false, null, null, null, "" ); + } + + public void testEvalFileNameMapping_ShouldPassExpressionThroughUnchanged() throws AssemblyFormattingException + { + verifyEvalFileNameMapping( "filename", null, null, null, null, null, "filename" ); + } + + public void testEvalFileNameMapping_ShouldInsertClassifierAheadOfExtension() throws AssemblyFormattingException + { + verifyEvalFileNameMapping( "filename.ext", "classifier", null, null, null, null, "filename-classifier.ext" ); + } + + public void testEvalFileNameMapping_ShouldAppendClassifier() throws AssemblyFormattingException + { + verifyEvalFileNameMapping( "filename", "classifier", null, null, null, null, "filename-classifier" ); + } + + public void testEvalFileNameMapping_ShouldResolveGroupId() throws AssemblyFormattingException + { + verifyEvalFileNameMapping( "${groupId}", "classifier", "group", null, null, null, "group-classifier" ); + } + + public void testEvalFileNameMapping_ShouldResolveArtifactId() throws AssemblyFormattingException + { + verifyEvalFileNameMapping( "${artifactId}", "classifier", null, "artifact", null, null, "artifact-classifier" ); + } + + public void testEvalFileNameMapping_ShouldResolveVersion() throws AssemblyFormattingException + { + verifyEvalFileNameMapping( "${version}", "classifier", null, null, "version", null, "version-classifier" ); + } + + public void testEvalFileNameMapping_ShouldResolveExtension() throws AssemblyFormattingException + { + verifyEvalFileNameMapping( "file.${extension}", "classifier", null, null, null, "ext", "file-classifier.ext" ); + } + + private void verifyEvalFileNameMapping( String expression, String classifier, String groupId, String artifactId, + String version, String extension, String checkValue ) + throws AssemblyFormattingException + { + MockAndControlForEvalFileNameMapping mac = new MockAndControlForEvalFileNameMapping( groupId, artifactId, version, classifier, extension ); + + mockManager.replayAll(); + + String result = AssemblyFormatUtils.evaluateFileNameMapping( expression, mac.artifact ); + + assertEquals( checkValue, result ); + + mockManager.verifyAll(); + + // clear out for next call. + mockManager.clear(); + } + + private void verifyOutputDir( String outDir, String finalName, boolean includeBasedir, String groupId, String artifactId, String version, String checkValue ) + { + MavenProject project = null; + + if ( groupId != null || artifactId != null || version != null ) + { + Model model = new Model(); + model.setGroupId( groupId ); + model.setArtifactId( artifactId ); + model.setVersion( version ); + + project = new MavenProject( model ); + } + + String result = AssemblyFormatUtils.getOutputDirectory( outDir, project, finalName, includeBasedir ); + + assertEquals( checkValue, result ); + } + + private void verifyDistroName( String assemblyId, String classifier, String finalName, boolean appendAssemblyId, String checkValue ) + { + MockAndControlForGetDistroName mac = new MockAndControlForGetDistroName( finalName, appendAssemblyId, classifier ); + + mockManager.replayAll(); + + Assembly assembly = new Assembly(); + assembly.setId( assemblyId ); + + String result = AssemblyFormatUtils.getDistributionName( assembly, mac.configSource ); + + assertEquals( checkValue, result ); + + mockManager.verifyAll(); + + // clear it out for the next call. + mockManager.clear(); + } + + private final class MockAndControlForGetDistroName + { + MockControl control; + AssemblerConfigurationSource configSource; + + private final String classifier; + private final boolean isAssemblyIdAppended; + private final String finalName; + + public MockAndControlForGetDistroName( String finalName, boolean isAssemblyIdAppended, String classifier ) + { + this.finalName = finalName; + this.isAssemblyIdAppended = isAssemblyIdAppended; + this.classifier = classifier; + + control = MockControl.createControl( AssemblerConfigurationSource.class ); + mockManager.add( control ); + + configSource = (AssemblerConfigurationSource) control.getMock(); + + enableExpectations(); + } + + private void enableExpectations() + { + configSource.getClassifier(); + control.setReturnValue( classifier, MockControl.ONE_OR_MORE ); + + configSource.isAssemblyIdAppended(); + control.setReturnValue( isAssemblyIdAppended, MockControl.ONE_OR_MORE ); + + configSource.getFinalName(); + control.setReturnValue( finalName, MockControl.ONE_OR_MORE ); + } + + } + + private final class MockAndControlForEvalFileNameMapping + { + MockControl artifactControl; + Artifact artifact; + + MockControl handlerControl; + ArtifactHandler handler; + + private final String classifier; + private final String groupId; + private final String artifactId; + private final String version; + private final String extension; + + public MockAndControlForEvalFileNameMapping( String groupId, String artifactId, String version, String classifier, String extension ) + { + this.groupId = groupId; + this.artifactId = artifactId; + this.version = version; + this.classifier = classifier; + this.extension = extension; + + artifactControl = MockControl.createControl( Artifact.class ); + mockManager.add( artifactControl ); + + artifact = (Artifact) artifactControl.getMock(); + + handlerControl = MockControl.createControl( ArtifactHandler.class ); + mockManager.add( handlerControl ); + + handler = (ArtifactHandler) handlerControl.getMock(); + + enableExpectations(); + } + + private void enableExpectations() + { + if ( groupId != null ) + { + artifact.getGroupId(); + artifactControl.setReturnValue( groupId, MockControl.ONE_OR_MORE ); + } + + if ( artifactId != null ) + { + artifact.getArtifactId(); + artifactControl.setReturnValue( artifactId, MockControl.ONE_OR_MORE ); + } + + if ( version != null ) + { + artifact.getVersion(); + artifactControl.setReturnValue( version, MockControl.ONE_OR_MORE ); + } + + if ( extension != null ) + { + handler.getExtension(); + handlerControl.setReturnValue( extension, MockControl.ONE_OR_MORE ); + } + + // this one is always called. + artifact.getClassifier(); + artifactControl.setReturnValue( classifier, MockControl.ONE_OR_MORE ); + + artifact.getArtifactHandler(); + artifactControl.setReturnValue( handler, MockControl.ONE_OR_MORE ); + } + + } +} Propchange: maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/AssemblyFormatUtilsTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/AssemblyFormatUtilsTest.java ------------------------------------------------------------------------------ svn:keywords = "Author Date Id Revision"