From 5fa30be9cf70d93485e33d2b2c8af503a557c343 Mon Sep 17 00:00:00 2001 From: Brett Porter Date: Thu, 25 Jan 2007 13:34:17 +0000 Subject: [PATCH] update test so that it works with future releases on Maven APIs. Test was passing by a side effect of file timestamps that should not have been considered. git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@499784 13f79535-47bb-0310-9956-ffa450edef68 --- .../proxy/DefaultProxyRequestHandler.java | 24 ++++++++++++++ .../proxy/ProxyRequestHandlerTest.java | 33 +++++++++++++------ 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/archiva-proxy/src/main/java/org/apache/maven/archiva/proxy/DefaultProxyRequestHandler.java b/archiva-proxy/src/main/java/org/apache/maven/archiva/proxy/DefaultProxyRequestHandler.java index c06238a75..f4a77f0c7 100644 --- a/archiva-proxy/src/main/java/org/apache/maven/archiva/proxy/DefaultProxyRequestHandler.java +++ b/archiva-proxy/src/main/java/org/apache/maven/archiva/proxy/DefaultProxyRequestHandler.java @@ -28,6 +28,7 @@ import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy; import org.apache.maven.artifact.repository.metadata.Metadata; +import org.apache.maven.artifact.repository.metadata.Versioning; import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader; import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer; import org.apache.maven.model.DistributionManagement; @@ -54,11 +55,15 @@ import java.io.FileWriter; import java.io.IOException; import java.io.Reader; import java.security.NoSuchAlgorithmException; +import java.text.DateFormat; +import java.text.SimpleDateFormat; import java.util.Date; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; +import java.util.Locale; import java.util.Map; +import java.util.TimeZone; /** * An implementation of the proxy handler. This class is not thread safe (the class itself is, but the wagons it uses @@ -97,6 +102,8 @@ public class DefaultProxyRequestHandler */ private Map/**/ wagons; + private static final TimeZone UTC_TIMEZONE = TimeZone.getTimeZone( "UTC" ); + public File get( String path, List proxiedRepositories, ArtifactRepository managedRepository ) throws ProxyException, ResourceDoesNotExistException { @@ -423,6 +430,9 @@ public class DefaultProxyRequestHandler if ( metadata != null ) { + setLastUpdatedIfEmpty( newMetadata, metadataFile ); + setLastUpdatedIfEmpty( metadata, target ); + changed = metadata.merge( newMetadata ); } else @@ -466,6 +476,20 @@ public class DefaultProxyRequestHandler } } + private void setLastUpdatedIfEmpty( Metadata metadata, File metadataFile ) + { + if ( metadata.getVersioning() == null ) + { + metadata.setVersioning( new Versioning() ); + } + if ( metadata.getVersioning().getLastUpdated() == null ) + { + DateFormat fmt = new SimpleDateFormat( "yyyyMMddHHmmss", Locale.US ); + fmt.setTimeZone( UTC_TIMEZONE ); + metadata.getVersioning().setLastUpdated( fmt.format( new Date( metadataFile.lastModified() ) ) ); + } + } + private void getFileFromRepository( String path, ProxiedArtifactRepository repository, String repositoryCachePath, ProxyInfo httpProxy, File target, ArtifactRepositoryPolicy policy, boolean force ) diff --git a/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/ProxyRequestHandlerTest.java b/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/ProxyRequestHandlerTest.java index 6c844894a..13a605fe0 100644 --- a/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/ProxyRequestHandlerTest.java +++ b/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/ProxyRequestHandlerTest.java @@ -39,6 +39,7 @@ import java.io.File; import java.io.IOException; import java.io.StringWriter; import java.net.MalformedURLException; +import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -49,6 +50,7 @@ import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Locale; +import java.util.TimeZone; /** * Test the proxy handler. @@ -88,6 +90,8 @@ public class ProxyRequestHandlerTest private static final ArtifactRepositoryPolicy ALWAYS_UPDATE_POLICY = new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, null ); + private static final TimeZone UTC_TIMEZONE = TimeZone.getTimeZone( "UTC" ); + protected void setUp() throws Exception { @@ -1010,7 +1014,7 @@ public class ProxyRequestHandlerTest assertTrue( "Check file created", file.exists() ); String expectedContents = getExpectedMetadata( "get-merged-metadata", getVersioning( - Arrays.asList( new String[]{"0.9", "1.0", "2.0", "3.0", "5.0", "4.0"} ) ) ); + Arrays.asList( new String[]{"0.9", "1.0", "2.0", "3.0", "5.0", "4.0"} ), file ) ); assertEquals( "Check content matches", expectedContents, FileUtils.readFileToString( file, null ) ); } @@ -1099,8 +1103,8 @@ public class ProxyRequestHandlerTest assertEquals( "Check file matches", expectedFile, file ); assertTrue( "Check file created", file.exists() ); - String expectedContents = - getExpectedMetadata( "get-updated-metadata", getVersioning( Arrays.asList( new String[]{"1.0", "2.0"} ) ) ); + String expectedContents = getExpectedMetadata( "get-updated-metadata", getVersioning( + Arrays.asList( new String[]{"1.0", "2.0"} ), file ) ); assertEquals( "Check content matches", expectedContents, FileUtils.readFileToString( file, null ) ); assertFalse( "Check content doesn't match proxy version", @@ -1126,7 +1130,7 @@ public class ProxyRequestHandlerTest assertTrue( "Check file created", file.exists() ); String expectedContents = - getExpectedMetadata( "get-updated-metadata", "1.0-SNAPSHOT", getVersioning( "20050831.111213", 2 ) ); + getExpectedMetadata( "get-updated-metadata", "1.0-SNAPSHOT", getVersioning( "20050831.111213", 2, file ) ); assertEquals( "Check content matches", expectedContents, FileUtils.readFileToString( file, null ) ); assertFalse( "Check content doesn't match proxy version", @@ -1173,8 +1177,8 @@ public class ProxyRequestHandlerTest assertEquals( "Check file matches", expectedFile, file ); assertTrue( "Check file created", file.exists() ); - String expectedContents = - getExpectedMetadata( "get-updated-metadata", getVersioning( Arrays.asList( new String[]{"1.0", "2.0"} ) ) ); + String expectedContents = getExpectedMetadata( "get-updated-metadata", getVersioning( + Arrays.asList( new String[]{"1.0", "2.0"} ), file ) ); assertEquals( "Check content matches", expectedContents, FileUtils.readFileToString( file, null ) ); assertFalse( "Check content doesn't match old version", unexpectedContents.equals( FileUtils.readFileToString( file, null ) ) ); @@ -1194,8 +1198,8 @@ public class ProxyRequestHandlerTest assertEquals( "Check file matches", expectedFile, file ); assertTrue( "Check file created", file.exists() ); - String expectedContents = - getExpectedMetadata( "get-updated-metadata", getVersioning( Arrays.asList( new String[]{"1.0", "2.0"} ) ) ); + String expectedContents = getExpectedMetadata( "get-updated-metadata", getVersioning( + Arrays.asList( new String[]{"1.0", "2.0"} ), file ) ); assertEquals( "Check content matches", expectedContents, FileUtils.readFileToString( file, null ) ); assertFalse( "Check content doesn't match old version", @@ -1767,7 +1771,7 @@ public class ProxyRequestHandlerTest assertEquals( "Check file matches", expectedFile, file ); } - private static Versioning getVersioning( List versions ) + private static Versioning getVersioning( List versions, File file ) { Versioning versioning = new Versioning(); for ( Iterator i = versions.iterator(); i.hasNext(); ) @@ -1775,6 +1779,7 @@ public class ProxyRequestHandlerTest String v = (String) i.next(); versioning.addVersion( v ); } + setLastUpdatedTimestamp( versioning, file ); return versioning; } @@ -1806,15 +1811,23 @@ public class ProxyRequestHandlerTest return getExpectedMetadata( artifactId, version, null ); } - private static Versioning getVersioning( String timestamp, int buildNumber ) + private static Versioning getVersioning( String timestamp, int buildNumber, File file ) { Versioning versioning = new Versioning(); versioning.setSnapshot( new Snapshot() ); versioning.getSnapshot().setTimestamp( timestamp ); versioning.getSnapshot().setBuildNumber( buildNumber ); + setLastUpdatedTimestamp( versioning, file ); return versioning; } + private static void setLastUpdatedTimestamp( Versioning versioning, File file ) + { + DateFormat fmt = new SimpleDateFormat( "yyyyMMddHHmmss", Locale.US ); + fmt.setTimeZone( UTC_TIMEZONE ); + versioning.setLastUpdated( fmt.format( new Date( file.lastModified() ) ) ); + } + private static Date getPastDate() throws ParseException { -- 2.39.5