]> source.dussan.org Git - archiva.git/commitdiff
[MRM-1327] make sure getArtifactsByDateRange and getArtifactsByChecksum query is...
authorBrett Porter <brett@apache.org>
Thu, 30 Dec 2010 01:41:07 +0000 (01:41 +0000)
committerBrett Porter <brett@apache.org>
Thu, 30 Dec 2010 01:41:07 +0000 (01:41 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1053783 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/metadata/metadata-repository-api/src/test/java/org/apache/archiva/metadata/repository/AbstractMetadataRepositoryTest.java
archiva-modules/plugins/metadata-store-jcr/src/main/java/org/apache/archiva/metadata/repository/jcr/JcrMetadataRepository.java

index 204c6cbe4d91da65425412566f3e64c87c87bce5..466f9261392aaca32ee433b77774194c07fd2074 100644 (file)
@@ -457,7 +457,10 @@ public abstract class AbstractMetadataRepositoryTest
         artifacts = repository.getArtifacts( TEST_REPO_ID );
         assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
 
-        artifacts = repository.getArtifactsByChecksum( TEST_REPO_ID, artifactMetadata.getSha1() );
+        artifacts = repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 );
+        assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
+
+        artifacts = repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 );
         assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
 
         artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, null );
@@ -827,6 +830,44 @@ public abstract class AbstractMetadataRepositoryTest
         assertEquals( Collections.singletonList( secondArtifact ), repository.getArtifacts( OTHER_REPO_ID ) );
     }
 
+    public void testGetArtifactsByDateRangeMultipleCopies()
+        throws Exception
+    {
+        ArtifactMetadata artifact = createArtifact();
+        repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
+
+        ArtifactMetadata secondArtifact = createArtifact();
+        secondArtifact.setRepositoryId( OTHER_REPO_ID );
+        repository.updateArtifact( OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact );
+
+        // test it restricts to the appropriate repository
+        assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByDateRange( TEST_REPO_ID, null,
+                                                                                                 null ) );
+        assertEquals( Collections.singletonList( secondArtifact ), repository.getArtifactsByDateRange( OTHER_REPO_ID,
+                                                                                                       null, null ) );
+    }
+
+    public void testGetArtifactsByChecksumMultipleCopies()
+        throws Exception
+    {
+        ArtifactMetadata artifact = createArtifact();
+        repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
+
+        ArtifactMetadata secondArtifact = createArtifact();
+        secondArtifact.setRepositoryId( OTHER_REPO_ID );
+        repository.updateArtifact( OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact );
+
+        // test it restricts to the appropriate repository
+        assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByChecksum( TEST_REPO_ID,
+                                                                                                TEST_SHA1 ) );
+        assertEquals( Collections.singletonList( secondArtifact ), repository.getArtifactsByChecksum( OTHER_REPO_ID,
+                                                                                                      TEST_SHA1 ) );
+        assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByChecksum( TEST_REPO_ID,
+                                                                                                TEST_MD5 ) );
+        assertEquals( Collections.singletonList( secondArtifact ), repository.getArtifactsByChecksum( OTHER_REPO_ID,
+                                                                                                      TEST_MD5 ) );
+    }
+
     public void testGetNamespacesWithSparseDepth()
         throws Exception
     {
@@ -868,6 +909,8 @@ public abstract class AbstractMetadataRepositoryTest
 
         assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByChecksum( TEST_REPO_ID,
                                                                                                 TEST_SHA1 ) );
+        assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByChecksum( TEST_REPO_ID,
+                                                                                                TEST_MD5 ) );
     }
 
     public void testGetArtifactsByChecksumMultipleResult()
@@ -885,6 +928,10 @@ public abstract class AbstractMetadataRepositoryTest
             TEST_REPO_ID, TEST_SHA1 ) );
         Collections.sort( artifacts, new ArtifactMetadataComparator() );
         assertEquals( Arrays.asList( artifact2, artifact1 ), artifacts );
+
+        artifacts = new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) );
+        Collections.sort( artifacts, new ArtifactMetadataComparator() );
+        assertEquals( Arrays.asList( artifact2, artifact1 ), artifacts );
     }
 
     public void testGetArtifactsByChecksumNoResult()
@@ -894,7 +941,7 @@ public abstract class AbstractMetadataRepositoryTest
         repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
 
         assertEquals( Collections.<ArtifactMetadata>emptyList(), repository.getArtifactsByChecksum( TEST_REPO_ID,
-                                                                                                    "not checksum" ) );
+                                                                                                    "not checksum" ) );
     }
 
     public void testDeleteArtifact()
index 22923eabaed5f9052c5840e8d0c41e44317dedb2..4c329b39e53532501c14e6d942ad05ff9fcb4c51 100644 (file)
@@ -79,8 +79,6 @@ public class JcrMetadataRepository
 
     static final String FACET_NODE_TYPE = "archiva:facet";
 
-    private static final String QUERY_ARTIFACTS = "SELECT * FROM [" + ARTIFACT_NODE_TYPE + "] AS artifact";
-
     private final Map<String, MetadataFacetFactory> metadataFacetFactories;
 
     private static final Logger log = LoggerFactory.getLogger( JcrMetadataRepository.class );
@@ -489,17 +487,15 @@ public class JcrMetadataRepository
     {
         List<ArtifactMetadata> artifacts;
 
-        String q = QUERY_ARTIFACTS;
+        String q = getArtifactQuery( repoId );
 
-        String clause = " WHERE";
         if ( startTime != null )
         {
-            q += clause + " [whenGathered] >= $start";
-            clause = " AND";
+            q += " AND [whenGathered] >= $start";
         }
         if ( endTime != null )
         {
-            q += clause + " [whenGathered] <= $end";
+            q += " AND [whenGathered] <= $end";
         }
 
         try
@@ -566,7 +562,7 @@ public class JcrMetadataRepository
     {
         List<ArtifactMetadata> artifacts;
 
-        String q = QUERY_ARTIFACTS + " WHERE [sha1] = $checksum OR [md5] = $checksum";
+        String q = getArtifactQuery( repositoryId ) + " AND ([sha1] = $checksum OR [md5] = $checksum)";
 
         try
         {
@@ -630,8 +626,7 @@ public class JcrMetadataRepository
     {
         List<ArtifactMetadata> artifacts;
 
-        String q = QUERY_ARTIFACTS + " WHERE ISDESCENDANTNODE(artifact,'/" + getRepositoryContentPath( repositoryId ) +
-            "')";
+        String q = getArtifactQuery( repositoryId );
 
         try
         {
@@ -654,6 +649,12 @@ public class JcrMetadataRepository
         return artifacts;
     }
 
+    private static String getArtifactQuery( String repositoryId )
+    {
+        return "SELECT * FROM [" + ARTIFACT_NODE_TYPE + "] AS artifact WHERE ISDESCENDANTNODE(artifact,'/" +
+            getRepositoryContentPath( repositoryId ) + "')";
+    }
+
     public ProjectMetadata getProject( String repositoryId, String namespace, String projectId )
         throws MetadataResolutionException
     {