From b92af0e49fded18b67a3a3cd9492ef79b022c6b1 Mon Sep 17 00:00:00 2001 From: John Dennis Casey Date: Fri, 1 Sep 2006 15:29:52 +0000 Subject: [PATCH] [MNG-2408] Modification to keep metadata files from writing when the main version is LATEST or RELEASE (these are meta-versions). git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@439352 13f79535-47bb-0310-9956-ffa450edef68 --- maven-artifact-manager/pom.xml | 4 + .../metadata/AbstractRepositoryMetadata.java | 8 + .../AbstractRepositoryMetadataTest.java | 158 ++++++++++++ .../maven/artifact/testutils/MockManager.java | 39 +++ .../artifact/testutils/TestFileManager.java | 231 ++++++++++++++++++ pom.xml | 6 + 6 files changed, 446 insertions(+) create mode 100644 maven-artifact-manager/src/test/java/org/apache/maven/artifact/repository/metadata/AbstractRepositoryMetadataTest.java create mode 100644 maven-artifact-manager/src/test/java/org/apache/maven/artifact/testutils/MockManager.java create mode 100644 maven-artifact-manager/src/test/java/org/apache/maven/artifact/testutils/TestFileManager.java diff --git a/maven-artifact-manager/pom.xml b/maven-artifact-manager/pom.xml index f0908b7cb0..4c819fab53 100644 --- a/maven-artifact-manager/pom.xml +++ b/maven-artifact-manager/pom.xml @@ -54,5 +54,9 @@ org.apache.maven.wagon wagon-provider-api + + easymock + easymock + diff --git a/maven-artifact-manager/src/main/java/org/apache/maven/artifact/repository/metadata/AbstractRepositoryMetadata.java b/maven-artifact-manager/src/main/java/org/apache/maven/artifact/repository/metadata/AbstractRepositoryMetadata.java index 3b092527f7..603dfea082 100644 --- a/maven-artifact-manager/src/main/java/org/apache/maven/artifact/repository/metadata/AbstractRepositoryMetadata.java +++ b/maven-artifact-manager/src/main/java/org/apache/maven/artifact/repository/metadata/AbstractRepositoryMetadata.java @@ -113,6 +113,14 @@ public abstract class AbstractRepositoryMetadata { changed = metadata.merge( this.metadata ); } + + // beware meta-versions! + String version = metadata.getVersion(); + if ( version != null && ( Artifact.LATEST_VERSION.equals( version ) || Artifact.RELEASE_VERSION.equals( version ) ) ) + { + // meta-versions are not valid values...don't write them. + changed = false; + } if ( changed ) { diff --git a/maven-artifact-manager/src/test/java/org/apache/maven/artifact/repository/metadata/AbstractRepositoryMetadataTest.java b/maven-artifact-manager/src/test/java/org/apache/maven/artifact/repository/metadata/AbstractRepositoryMetadataTest.java new file mode 100644 index 0000000000..e35f479b5c --- /dev/null +++ b/maven-artifact-manager/src/test/java/org/apache/maven/artifact/repository/metadata/AbstractRepositoryMetadataTest.java @@ -0,0 +1,158 @@ +package org.apache.maven.artifact.repository.metadata; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.testutils.MockManager; +import org.apache.maven.artifact.testutils.TestFileManager; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; +import org.easymock.MockControl; + +import java.io.File; +import java.io.IOException; + +import junit.framework.TestCase; + +public class AbstractRepositoryMetadataTest + extends TestCase +{ + + private MockManager mm = new MockManager(); + private TestFileManager fileManager = new TestFileManager( "AbstractRepositoryMetadataTest.test.", "" ); + + public void tearDown() throws IOException + { + fileManager.cleanUp(); + } + + public void testUpdateRepositoryMetadata_ShouldNotStoreIfMainVersionIsLATEST() + throws IOException, XmlPullParserException + { + MockAndControlForArtifactRepository local = new MockAndControlForArtifactRepository(); + MockAndControlForArtifactRepository remote = new MockAndControlForArtifactRepository(); + + File basedir = fileManager.createTempDir(); + + String path = "metadata.xml"; + + Metadata m = new Metadata(); + m.setVersion( Artifact.LATEST_VERSION ); + + TestRepoMetadata trm = new TestRepoMetadata( m ); + + local.expectGetBasedir( basedir ); + local.expectPathOfLocalRepositoryMetadata( trm, remote.repository, path ); + + mm.replayAll(); + + trm.updateRepositoryMetadata( local.repository, remote.repository ); + + fileManager.assertFileExistence( basedir, path, false ); + + mm.verifyAll(); + } + + public void testUpdateRepositoryMetadata_ShouldNotStoreIfMainVersionIsRELEASE() + throws IOException, XmlPullParserException + { + MockAndControlForArtifactRepository local = new MockAndControlForArtifactRepository(); + MockAndControlForArtifactRepository remote = new MockAndControlForArtifactRepository(); + + File basedir = fileManager.createTempDir(); + + String path = "metadata.xml"; + + Metadata m = new Metadata(); + m.setVersion( Artifact.RELEASE_VERSION ); + + TestRepoMetadata trm = new TestRepoMetadata( m ); + + local.expectGetBasedir( basedir ); + local.expectPathOfLocalRepositoryMetadata( trm, remote.repository, path ); + + mm.replayAll(); + + trm.updateRepositoryMetadata( local.repository, remote.repository ); + + fileManager.assertFileExistence( basedir, path, false ); + + mm.verifyAll(); + } + + private final class MockAndControlForArtifactRepository + { + MockControl control; + + ArtifactRepository repository; + + public MockAndControlForArtifactRepository() + { + control = MockControl.createControl( ArtifactRepository.class ); + mm.add( control ); + + repository = ( ArtifactRepository ) control.getMock(); + } + + public void expectPathOfLocalRepositoryMetadata( TestRepoMetadata trm, ArtifactRepository remote, String path ) + { + repository.pathOfLocalRepositoryMetadata( trm, remote ); + control.setReturnValue( path, MockControl.ONE_OR_MORE ); + } + + public void expectGetBasedir( File basedir ) + { + repository.getBasedir(); + control.setReturnValue( basedir.getAbsolutePath(), MockControl.ONE_OR_MORE ); + } + } + + private static final class TestRepoMetadata + extends AbstractRepositoryMetadata + { + + protected TestRepoMetadata( Metadata metadata ) + { + super( metadata ); + } + + public boolean isSnapshot() + { + return false; + } + + public void setRepository( ArtifactRepository remoteRepository ) + { + } + + public String getArtifactId() + { + return null; + } + + public String getBaseVersion() + { + return null; + } + + public String getGroupId() + { + return null; + } + + public Object getKey() + { + return null; + } + + public boolean storedInArtifactVersionDirectory() + { + return false; + } + + public boolean storedInGroupDirectory() + { + return false; + } + + } + +} diff --git a/maven-artifact-manager/src/test/java/org/apache/maven/artifact/testutils/MockManager.java b/maven-artifact-manager/src/test/java/org/apache/maven/artifact/testutils/MockManager.java new file mode 100644 index 0000000000..99e19f2cf7 --- /dev/null +++ b/maven-artifact-manager/src/test/java/org/apache/maven/artifact/testutils/MockManager.java @@ -0,0 +1,39 @@ +package org.apache.maven.artifact.testutils; + +import org.easymock.MockControl; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class MockManager +{ + + private List mockControls = new ArrayList(); + + public void add( MockControl control ) + { + mockControls.add( control ); + } + + public void replayAll() + { + for ( Iterator it = mockControls.iterator(); it.hasNext(); ) + { + MockControl control = ( MockControl ) it.next(); + + control.replay(); + } + } + + public void verifyAll() + { + for ( Iterator it = mockControls.iterator(); it.hasNext(); ) + { + MockControl control = ( MockControl ) it.next(); + + control.verify(); + } + } + +} diff --git a/maven-artifact-manager/src/test/java/org/apache/maven/artifact/testutils/TestFileManager.java b/maven-artifact-manager/src/test/java/org/apache/maven/artifact/testutils/TestFileManager.java new file mode 100644 index 0000000000..68e0a9cb71 --- /dev/null +++ b/maven-artifact-manager/src/test/java/org/apache/maven/artifact/testutils/TestFileManager.java @@ -0,0 +1,231 @@ +package org.apache.maven.artifact.testutils; + +import org.codehaus.plexus.util.FileUtils; +import org.codehaus.plexus.util.IOUtil; + +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import junit.framework.Assert; + +public class TestFileManager +{ + + public static final String TEMP_DIR_PATH = System.getProperty( "java.io.tmpdir" ); + + private List filesToDelete = new ArrayList(); + + private final String baseFilename; + + private final String fileSuffix; + + private StackTraceElement callerInfo; + + private Thread cleanupWarning; + + private boolean warnAboutCleanup = false; + + public TestFileManager( String baseFilename, String fileSuffix ) + { + this.baseFilename = baseFilename; + this.fileSuffix = fileSuffix; + + initializeCleanupMonitoring(); + } + + private void initializeCleanupMonitoring() + { + callerInfo = new NullPointerException().getStackTrace()[2]; + + Runnable warning = new Runnable() + { + + public void run() + { + maybeWarnAboutCleanUp(); + } + + }; + + cleanupWarning = new Thread( warning ); + + Runtime.getRuntime().addShutdownHook( cleanupWarning ); + } + + private void maybeWarnAboutCleanUp() + { + if ( warnAboutCleanup ) + { + System.out.println( "[WARNING] TestFileManager from: " + callerInfo.getClassName() + " not cleaned up!" ); + } + } + + public void markForDeletion( File toDelete ) + { + filesToDelete.add( toDelete ); + warnAboutCleanup = true; + } + + public synchronized File createTempDir() + { + try + { + Thread.sleep( 20 ); + } + catch ( InterruptedException e ) + { + } + + File dir = new File( TEMP_DIR_PATH, baseFilename + System.currentTimeMillis() ); + + dir.mkdirs(); + markForDeletion( dir ); + + return dir; + } + + public synchronized File createTempFile() + throws IOException + { + File tempFile = File.createTempFile( baseFilename, fileSuffix ); + tempFile.deleteOnExit(); + markForDeletion( tempFile ); + + return tempFile; + } + + public void cleanUp() + throws IOException + { + for ( Iterator it = filesToDelete.iterator(); it.hasNext(); ) + { + File file = ( File ) it.next(); + + if ( file.exists() ) + { + if ( file.isDirectory() ) + { + FileUtils.deleteDirectory( file ); + } + else + { + file.delete(); + } + } + + it.remove(); + } + + warnAboutCleanup = false; + } + + public void assertFileExistence( File dir, String filename, boolean shouldExist ) + { + File file = new File( dir, filename ); + + if ( shouldExist ) + { + Assert.assertTrue( file.exists() ); + } + else + { + Assert.assertFalse( file.exists() ); + } + } + + public void assertFileContents( File dir, String filename, String contentsTest ) + throws IOException + { + assertFileExistence( dir, filename, true ); + + File file = new File( dir, filename ); + + FileReader reader = null; + StringWriter writer = new StringWriter(); + + try + { + reader = new FileReader( file ); + + IOUtil.copy( reader, writer ); + } + finally + { + IOUtil.close( reader ); + } + + Assert.assertEquals( contentsTest, writer.toString() ); + } + + public File createFile( File dir, String filename, String contents ) + throws IOException + { + File file = new File( dir, filename ); + + file.getParentFile().mkdirs(); + + FileWriter writer = null; + + try + { + writer = new FileWriter( file ); + + IOUtil.copy( new StringReader( contents ), writer ); + } + finally + { + IOUtil.close( writer ); + } + + markForDeletion( file ); + + return file; + } + + public String getFileContents( File file ) + throws IOException + { + String result = null; + + FileReader reader = null; + try + { + reader = new FileReader( file ); + + StringWriter writer = new StringWriter(); + + IOUtil.copy( reader, writer ); + + result = writer.toString(); + } + finally + { + IOUtil.close( reader ); + } + + return result; + } + + protected void finalize() + throws Throwable + { + maybeWarnAboutCleanUp(); + + super.finalize(); + } + + public File createFile( String filename, String content ) + throws IOException + { + File dir = createTempDir(); + return createFile( dir, filename, content ); + } + +} diff --git a/pom.xml b/pom.xml index 35b5471579..23877b636d 100644 --- a/pom.xml +++ b/pom.xml @@ -183,6 +183,12 @@ wagon-http-lightweight 1.0-alpha-6 + + easymock + easymock + 1.2_Java1.3 + test +