You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@archiva.apache.org by jo...@apache.org on 2007/10/31 18:34:20 UTC

svn commit: r590764 - in /maven/archiva/branches/archiva-backend-security/archiva-database/src: main/java/org/apache/maven/archiva/database/constraints/ test/java/org/apache/maven/archiva/database/constraints/

Author: joakime
Date: Wed Oct 31 10:34:18 2007
New Revision: 590764

URL: http://svn.apache.org/viewvc?rev=590764&view=rev
Log:
Allow for sql constraints to specify selected repositories list.


Added:
    maven/archiva/branches/archiva-backend-security/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/SqlBuilder.java   (with props)
Modified:
    maven/archiva/branches/archiva-backend-security/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/UniqueArtifactIdConstraint.java
    maven/archiva/branches/archiva-backend-security/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/UniqueGroupIdConstraint.java
    maven/archiva/branches/archiva-backend-security/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/UniqueVersionConstraint.java
    maven/archiva/branches/archiva-backend-security/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/UniqueGroupIdConstraintTest.java
    maven/archiva/branches/archiva-backend-security/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/UniqueVersionConstraintTest.java

Added: maven/archiva/branches/archiva-backend-security/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/SqlBuilder.java
URL: http://svn.apache.org/viewvc/maven/archiva/branches/archiva-backend-security/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/SqlBuilder.java?rev=590764&view=auto
==============================================================================
--- maven/archiva/branches/archiva-backend-security/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/SqlBuilder.java (added)
+++ maven/archiva/branches/archiva-backend-security/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/SqlBuilder.java Wed Oct 31 10:34:18 2007
@@ -0,0 +1,80 @@
+package org.apache.maven.archiva.database.constraints;
+
+/*
+ * 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 org.apache.commons.lang.StringUtils;
+
+import java.util.List;
+
+/**
+ * SqlBuilder - common sql building mechanisms. 
+ *
+ * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
+ * @version $Id$
+ */
+public class SqlBuilder
+{
+    /**
+     * Append a sql specific where clause within <code>"()"</code> braces that selects the specific
+     * repository ids provided. 
+     * 
+     * NOTE: This does not append the "WHERE" statement itself.
+     * 
+     * @param sql the sql buffer to append to.
+     * @param fieldId the field id for the repository Id.
+     * @param selectedRepositoryIds the list of repository ids to provide.
+     */
+    public static void appendWhereSelectedRepositories( StringBuffer sql, String fieldId,
+                                                        List<String> selectedRepositoryIds )
+    {
+        if ( fieldId == null )
+        {
+            throw new NullPointerException( "Null field id is not allowed." );
+        }
+
+        if ( StringUtils.isBlank( fieldId ) )
+        {
+            throw new IllegalArgumentException( "Blank field id is not allowed." );
+        }
+
+        if ( selectedRepositoryIds == null )
+        {
+            throw new NullPointerException( "Selected repositories cannot be null." );
+        }
+
+        if ( selectedRepositoryIds.isEmpty() )
+        {
+            throw new IllegalArgumentException( "Selected repositories cannot be null." );
+        }
+
+        sql.append( " (" );
+        boolean multiple = false;
+        for ( String repo : selectedRepositoryIds )
+        {
+            if ( multiple )
+            {
+                sql.append( " || " );
+            }
+            sql.append( " " ).append( fieldId ).append( " == \"" ).append( repo ).append( "\"" );
+            multiple = true;
+        }
+        sql.append( " )" );
+    }
+}

Propchange: maven/archiva/branches/archiva-backend-security/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/SqlBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/archiva/branches/archiva-backend-security/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/SqlBuilder.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Propchange: maven/archiva/branches/archiva-backend-security/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/SqlBuilder.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: maven/archiva/branches/archiva-backend-security/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/UniqueArtifactIdConstraint.java
URL: http://svn.apache.org/viewvc/maven/archiva/branches/archiva-backend-security/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/UniqueArtifactIdConstraint.java?rev=590764&r1=590763&r2=590764&view=diff
==============================================================================
--- maven/archiva/branches/archiva-backend-security/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/UniqueArtifactIdConstraint.java (original)
+++ maven/archiva/branches/archiva-backend-security/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/UniqueArtifactIdConstraint.java Wed Oct 31 10:34:18 2007
@@ -22,6 +22,8 @@
 import org.apache.maven.archiva.database.Constraint;
 import org.apache.maven.archiva.model.ArchivaArtifactModel;
 
+import java.util.List;
+
 /**
  * Obtain a set of unique ArtifactIds for the specified groupId.
  *
@@ -32,7 +34,24 @@
     extends AbstractSimpleConstraint
     implements Constraint
 {
-    private String sql;
+    private StringBuffer sql = new StringBuffer();
+
+    /**
+     * Obtain a set of unique ArtifactIds for the specified groupId.
+     * 
+     * @param groupId the groupId to search for artifactIds within.
+     */
+    public UniqueArtifactIdConstraint( List<String> selectedRepositoryIds, String groupId )
+    {
+        appendSelect( sql );
+        sql.append( " WHERE " );
+        SqlBuilder.appendWhereSelectedRepositories( sql, "repositoryId", selectedRepositoryIds );
+        sql.append( " && " );
+        appendWhereSelectedGroupId( sql );
+        appendGroupBy( sql );
+
+        super.params = new Object[] { groupId };
+    }
 
     /**
      * Obtain a set of unique ArtifactIds for the specified groupId.
@@ -41,13 +60,15 @@
      */
     public UniqueArtifactIdConstraint( String groupId )
     {
-        sql = "SELECT artifactId FROM " + ArchivaArtifactModel.class.getName()
-            + " WHERE groupId == selectedGroupId PARAMETERS String selectedGroupId"
-            + " GROUP BY artifactId ORDER BY artifactId ASCENDING";
+        appendSelect( sql );
+        sql.append( " WHERE " );
+        appendWhereSelectedGroupId( sql );
+        appendGroupBy( sql );
 
         super.params = new Object[] { groupId };
     }
 
+    @SuppressWarnings("unchecked")
     public Class getResultClass()
     {
         return String.class;
@@ -55,6 +76,22 @@
 
     public String getSelectSql()
     {
-        return sql;
+        return sql.toString();
     }
+
+    private void appendGroupBy( StringBuffer buf )
+    {
+        buf.append( " GROUP BY artifactId ORDER BY artifactId ASCENDING" );
+    }
+
+    private void appendSelect( StringBuffer buf )
+    {
+        buf.append( "SELECT artifactId FROM " ).append( ArchivaArtifactModel.class.getName() );
+    }
+
+    private void appendWhereSelectedGroupId( StringBuffer buf )
+    {
+        buf.append( " groupId == selectedGroupId PARAMETERS String selectedGroupId" );
+    }
+
 }

Modified: maven/archiva/branches/archiva-backend-security/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/UniqueGroupIdConstraint.java
URL: http://svn.apache.org/viewvc/maven/archiva/branches/archiva-backend-security/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/UniqueGroupIdConstraint.java?rev=590764&r1=590763&r2=590764&view=diff
==============================================================================
--- maven/archiva/branches/archiva-backend-security/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/UniqueGroupIdConstraint.java (original)
+++ maven/archiva/branches/archiva-backend-security/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/UniqueGroupIdConstraint.java Wed Oct 31 10:34:18 2007
@@ -22,6 +22,8 @@
 import org.apache.maven.archiva.database.Constraint;
 import org.apache.maven.archiva.model.ArchivaArtifactModel;
 
+import java.util.List;
+
 /**
  * UniqueGroupIdConstraint 
  *
@@ -32,24 +34,46 @@
     extends AbstractSimpleConstraint
     implements Constraint
 {
-    private String sql;
+    private StringBuffer sql = new StringBuffer();
 
     public UniqueGroupIdConstraint()
     {
         /* this assumes search for no groupId prefix */
-        sql = "SELECT groupId FROM " + ArchivaArtifactModel.class.getName()
-            + " GROUP BY groupId ORDER BY groupId ASCENDING";
+        appendSelect( sql );
+        appendGroupBy( sql );
+    }
+
+    public UniqueGroupIdConstraint( List<String> selectedRepositories )
+    {
+        appendSelect( sql );
+        sql.append( " WHERE " );
+        SqlBuilder.appendWhereSelectedRepositories( sql, "repositoryId", selectedRepositories );
+        appendGroupBy( sql );
+    }
+
+    public UniqueGroupIdConstraint( List<String> selectedRepositories, String groupIdPrefix )
+    {
+        appendSelect( sql );
+        sql.append( " WHERE " );
+        SqlBuilder.appendWhereSelectedRepositories( sql, "repositoryId", selectedRepositories );
+        sql.append( " && " );
+        appendWhereGroupIdStartsWith( sql );
+        appendGroupBy( sql );
+
+        super.params = new Object[] { groupIdPrefix };
     }
 
     public UniqueGroupIdConstraint( String groupIdPrefix )
     {
-        sql = "SELECT groupId FROM " + ArchivaArtifactModel.class.getName()
-            + " WHERE groupId.startsWith(groupIdPrefix) PARAMETERS String groupIdPrefix"
-            + " GROUP BY groupId ORDER BY groupId ASCENDING";
+        appendSelect( sql );
+        sql.append( " WHERE " );
+        appendWhereGroupIdStartsWith( sql );
+        appendGroupBy( sql );
 
         super.params = new Object[] { groupIdPrefix };
     }
 
+    @SuppressWarnings("unchecked")
     public Class getResultClass()
     {
         return String.class;
@@ -57,6 +81,21 @@
 
     public String getSelectSql()
     {
-        return sql;
+        return sql.toString();
+    }
+
+    private void appendGroupBy( StringBuffer buf )
+    {
+        buf.append( " GROUP BY groupId ORDER BY groupId ASCENDING" );
+    }
+
+    private void appendSelect( StringBuffer buf )
+    {
+        buf.append( "SELECT groupId FROM " ).append( ArchivaArtifactModel.class.getName() );
+    }
+
+    private void appendWhereGroupIdStartsWith( StringBuffer buf )
+    {
+        buf.append( " groupId.startsWith(groupIdPrefix) PARAMETERS String groupIdPrefix" );
     }
 }

Modified: maven/archiva/branches/archiva-backend-security/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/UniqueVersionConstraint.java
URL: http://svn.apache.org/viewvc/maven/archiva/branches/archiva-backend-security/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/UniqueVersionConstraint.java?rev=590764&r1=590763&r2=590764&view=diff
==============================================================================
--- maven/archiva/branches/archiva-backend-security/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/UniqueVersionConstraint.java (original)
+++ maven/archiva/branches/archiva-backend-security/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/UniqueVersionConstraint.java Wed Oct 31 10:34:18 2007
@@ -23,6 +23,8 @@
 import org.apache.maven.archiva.database.Constraint;
 import org.apache.maven.archiva.model.ArchivaArtifactModel;
 
+import java.util.List;
+
 /**
  * Obtain the list of version's for specific GroupId and ArtifactId.
  *
@@ -33,7 +35,36 @@
     extends AbstractSimpleConstraint
     implements Constraint
 {
-    private String sql;
+    private StringBuffer sql = new StringBuffer();
+
+    /**
+     * Obtain the list of version's for specific GroupId and ArtifactId.
+     * 
+     * @param selectedRepositoryIds the selected repository ids.
+     * @param groupId the selected groupId.
+     * @param artifactId the selected artifactId.
+     */
+    public UniqueVersionConstraint( List<String> selectedRepositoryIds, String groupId, String artifactId )
+    {
+        if ( StringUtils.isBlank( groupId ) )
+        {
+            throw new IllegalArgumentException( "A blank groupId is not allowed." );
+        }
+
+        if ( StringUtils.isBlank( artifactId ) )
+        {
+            throw new IllegalArgumentException( "A blank artifactId is not allowed." );
+        }
+
+        appendSelect( sql );
+        sql.append( " WHERE " );
+        SqlBuilder.appendWhereSelectedRepositories( sql, "repositoryId", selectedRepositoryIds );
+        sql.append( " && " );
+        appendWhereSelectedGroupIdArtifactId( sql );
+        appendGroupBy( sql );
+
+        super.params = new Object[] { groupId, artifactId };
+    }
 
     /**
      * Obtain the list of version's for specific GroupId and ArtifactId.
@@ -53,14 +84,15 @@
             throw new IllegalArgumentException( "A blank artifactId is not allowed." );
         }
 
-        sql = "SELECT version FROM " + ArchivaArtifactModel.class.getName()
-            + " WHERE groupId == selectedGroupId && artifactId == selectedArtifactId"
-            + " PARAMETERS String selectedGroupId, String selectedArtifactId"
-            + " GROUP BY version ORDER BY version ASCENDING";
+        appendSelect( sql );
+        sql.append( " WHERE " );
+        appendWhereSelectedGroupIdArtifactId( sql );
+        appendGroupBy( sql );
 
         super.params = new Object[] { groupId, artifactId };
     }
 
+    @SuppressWarnings("unchecked")
     public Class getResultClass()
     {
         return String.class;
@@ -68,6 +100,22 @@
 
     public String getSelectSql()
     {
-        return sql;
+        return sql.toString();
+    }
+
+    private void appendGroupBy( StringBuffer buf )
+    {
+        buf.append( " GROUP BY version ORDER BY version ASCENDING" );
+    }
+
+    private void appendSelect( StringBuffer buf )
+    {
+        buf.append( "SELECT version FROM " ).append( ArchivaArtifactModel.class.getName() );
+    }
+
+    private void appendWhereSelectedGroupIdArtifactId( StringBuffer buf )
+    {
+        buf.append( " groupId == selectedGroupId && artifactId == selectedArtifactId" );
+        buf.append( " PARAMETERS String selectedGroupId, String selectedArtifactId" );
     }
 }

Modified: maven/archiva/branches/archiva-backend-security/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/UniqueGroupIdConstraintTest.java
URL: http://svn.apache.org/viewvc/maven/archiva/branches/archiva-backend-security/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/UniqueGroupIdConstraintTest.java?rev=590764&r1=590763&r2=590764&view=diff
==============================================================================
--- maven/archiva/branches/archiva-backend-security/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/UniqueGroupIdConstraintTest.java (original)
+++ maven/archiva/branches/archiva-backend-security/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/UniqueGroupIdConstraintTest.java Wed Oct 31 10:34:18 2007
@@ -26,6 +26,7 @@
 import org.apache.maven.archiva.database.SimpleConstraint;
 import org.apache.maven.archiva.model.ArchivaArtifact;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Date;
 import java.util.Iterator;
@@ -42,91 +43,268 @@
 {
     private ArtifactDAO artifactDao;
 
-    protected void setUp()
+    public void testConstraintGroupIdParamCommonsLang()
         throws Exception
     {
-        super.setUp();
+        setupArtifacts();
 
-        ArchivaDAO dao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" );
-        artifactDao = dao.getArtifactDAO();
+        assertConstraint( new String[] { "commons-lang" }, new UniqueGroupIdConstraint( "commons-lang" ) );
     }
 
-    public ArchivaArtifact createArtifact( String groupId, String artifactId, String version )
+    public void testConstraintGroupIdParamNoRepos()
+        throws Exception
     {
-        ArchivaArtifact artifact = artifactDao.createArtifact( groupId, artifactId, version, "", "jar" );
-        artifact.getModel().setLastModified( new Date() ); // mandatory field.
-        artifact.getModel().setRepositoryId( "testable_repo" );
-        return artifact;
+        try
+        {
+            List<String> selectedRepos = new ArrayList<String>();
+            new UniqueGroupIdConstraint( selectedRepos, "org" );
+            fail( "Should have thrown an IllegalArgumentException due to lack of specified repos." );
+        }
+        catch ( IllegalArgumentException e )
+        {
+            // expected path.
+        }
     }
 
-    public void testConstraint()
+    public void testConstraintGroupIdParamNullRepos()
         throws Exception
     {
-        ArchivaArtifact artifact;
+        try
+        {
+            new UniqueGroupIdConstraint( (List<String>) null, "org" );
+            fail( "Should have thrown an NullPointerException due to lack of specified repos." );
+        }
+        catch ( NullPointerException e )
+        {
+            // expected path.
+        }
+    }
 
-        // Setup artifacts in fresh DB.
-        artifact = createArtifact( "commons-lang", "commons-lang", "2.0" );
-        artifactDao.saveArtifact( artifact );
+    public void testConstraintGroupIdParamOrg()
+        throws Exception
+    {
+        setupArtifacts();
 
-        artifact = createArtifact( "commons-lang", "commons-lang", "2.1" );
-        artifactDao.saveArtifact( artifact );
+        assertConstraint( new String[] {
+            "org.apache.maven.test",
+            "org.apache.maven.test.foo",
+            "org.apache.maven.shared",
+            "org.apache.archiva",
+            "org.codehaus.modello",
+            "org.codehaus.mojo" }, new UniqueGroupIdConstraint( "org" ) );
+    }
 
-        artifact = createArtifact( "org.apache.maven.test", "test-one", "1.2" );
-        artifactDao.saveArtifact( artifact );
+    public void testConstraintGroupIdParamOrgApache()
+        throws Exception
+    {
+        setupArtifacts();
 
-        artifact = createArtifact( "org.apache.maven.test.foo", "test-two", "1.0" );
-        artifactDao.saveArtifact( artifact );
+        assertConstraint( new String[] {
+            "org.apache.maven.test",
+            "org.apache.maven.test.foo",
+            "org.apache.maven.shared",
+            "org.apache.archiva" }, new UniqueGroupIdConstraint( "org.apache" ) );
+    }
 
-        artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.0" );
-        artifactDao.saveArtifact( artifact );
+    public void testConstraintGroupIdParamOrgApacheMaven()
+        throws Exception
+    {
+        setupArtifacts();
 
-        artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.1" );
-        artifactDao.saveArtifact( artifact );
+        assertConstraint( new String[] {
+            "org.apache.maven.test",
+            "org.apache.maven.test.foo",
+            "org.apache.maven.shared" }, new UniqueGroupIdConstraint( "org.apache.maven" ) );
+    }
 
-        artifact = createArtifact( "org.codehaus.modello", "test-two", "3.0" );
-        artifactDao.saveArtifact( artifact );
+    public void testConstraintGroupIdParamOrgApacheSnapshotsOnly()
+        throws Exception
+    {
+        setupArtifacts();
+
+        List<String> observableRepositories = new ArrayList<String>();
+        observableRepositories.add( "snapshots" );
+
+        assertConstraint( new String[] { "org.apache.archiva" }, new UniqueGroupIdConstraint( observableRepositories,
+                                                                                              "org.apache" ) );
+    }
+
+    public void testConstraintGroupIdParamOrgSnapshotsOnly()
+        throws Exception
+    {
+        setupArtifacts();
+
+        List<String> observableRepositories = new ArrayList<String>();
+        observableRepositories.add( "snapshots" );
+
+        assertConstraint( new String[] { "org.apache.archiva", "org.codehaus.modello", "org.codehaus.mojo" },
+                          new UniqueGroupIdConstraint( observableRepositories, "org" ) );
+    }
+
+    public void testConstraintNoGroupIdParam()
+        throws Exception
+    {
+        setupArtifacts();
 
         assertConstraint( new String[] {
             "commons-lang",
             "org.apache.maven.test",
             "org.apache.maven.test.foo",
             "org.apache.maven.shared",
-            "org.codehaus.modello" }, new UniqueGroupIdConstraint() );
-        assertConstraint( new String[] { "commons-lang" }, new UniqueGroupIdConstraint( "commons-lang" ) );
-        assertConstraint( new String[] {
-            "org.apache.maven.test",
-            "org.apache.maven.test.foo",
-            "org.apache.maven.shared" }, new UniqueGroupIdConstraint( "org.apache.maven" ) );
+            "org.codehaus.modello",
+            "org.codehaus.mojo",
+            "org.apache.archiva" }, new UniqueGroupIdConstraint() );
+    }
+
+    public void testConstraintNoGroupIdParamCentralAndSnapshots()
+        throws Exception
+    {
+        setupArtifacts();
+
+        List<String> observableRepositories = new ArrayList<String>();
+        observableRepositories.add( "central" );
+        observableRepositories.add( "snapshots" );
+
         assertConstraint( new String[] {
+            "commons-lang",
             "org.apache.maven.test",
             "org.apache.maven.test.foo",
-            "org.apache.maven.shared" }, new UniqueGroupIdConstraint( "org.apache" ) );
+            "org.apache.maven.shared",
+            "org.codehaus.modello",
+            "org.codehaus.mojo",
+            "org.apache.archiva" }, new UniqueGroupIdConstraint( observableRepositories ) );
+    }
+
+    public void testConstraintNoGroupIdParamCentralOnly()
+        throws Exception
+    {
+        setupArtifacts();
+
+        List<String> observableRepositories = new ArrayList<String>();
+        observableRepositories.add( "central" );
+
         assertConstraint( new String[] {
+            "commons-lang",
             "org.apache.maven.test",
             "org.apache.maven.test.foo",
             "org.apache.maven.shared",
-            "org.codehaus.modello" }, new UniqueGroupIdConstraint( "org" ) );
+            "org.codehaus.modello" }, new UniqueGroupIdConstraint( observableRepositories ) );
+    }
+
+    public void testConstraintNoGroupIdParamNoRepos()
+        throws Exception
+    {
+        try
+        {
+            List<String> selectedRepos = new ArrayList<String>();
+            new UniqueGroupIdConstraint( selectedRepos );
+            fail( "Should have thrown an IllegalArgumentException due to lack of specified repos." );
+        }
+        catch ( IllegalArgumentException e )
+        {
+            // expected path.
+        }
+    }
+
+    public void testConstraintNoGroupIdParamNullRepos()
+        throws Exception
+    {
+        try
+        {
+            new UniqueGroupIdConstraint( (List<String>) null );
+            fail( "Should have thrown an NullPointerException due to lack of specified repos." );
+        }
+        catch ( NullPointerException e )
+        {
+            // expected path.
+        }
+    }
+
+    public void testConstraintNoGroupIdParamSnapshotsOnly()
+        throws Exception
+    {
+        setupArtifacts();
+
+        List<String> observableRepositories = new ArrayList<String>();
+        observableRepositories.add( "snapshots" );
+
+        assertConstraint( new String[] { "org.codehaus.modello", "org.codehaus.mojo", "org.apache.archiva" },
+                          new UniqueGroupIdConstraint( observableRepositories ) );
     }
 
     private void assertConstraint( String[] expectedGroupIds, SimpleConstraint constraint )
         throws Exception
     {
         String prefix = "Unique Group IDs: ";
-        
-        List results = dao.query( constraint );
+
+        List<String> results = dao.query( constraint );
         assertNotNull( prefix + "Not Null", results );
         assertEquals( prefix + "Results.size", expectedGroupIds.length, results.size() );
 
-        List groupIdList = Arrays.asList( expectedGroupIds );
+        List<String> groupIdList = Arrays.asList( expectedGroupIds );
 
-        Iterator it = results.iterator();
+        Iterator<String> it = results.iterator();
         while ( it.hasNext() )
         {
             String actualGroupId = (String) it.next();
             assertTrue( prefix + "groupId result should not be blank.", StringUtils.isNotBlank( actualGroupId ) );
-            assertTrue( prefix + " groupId result <" + actualGroupId + "> exists in expected GroupIds.",
-                        groupIdList.contains( actualGroupId ) );
+            assertTrue( prefix + " groupId result <" + actualGroupId + "> exists in expected GroupIds.", groupIdList
+                .contains( actualGroupId ) );
         }
     }
 
+    private ArchivaArtifact createArtifact( String repoId, String groupId, String artifactId, String version )
+    {
+        ArchivaArtifact artifact = artifactDao.createArtifact( groupId, artifactId, version, "", "jar" );
+        artifact.getModel().setLastModified( new Date() ); // mandatory field.
+        artifact.getModel().setRepositoryId( repoId );
+        return artifact;
+    }
+
+    private void setupArtifacts()
+        throws Exception
+    {
+        ArchivaArtifact artifact;
+
+        // Setup artifacts in fresh DB.
+        artifact = createArtifact( "central", "commons-lang", "commons-lang", "2.0" );
+        artifactDao.saveArtifact( artifact );
+
+        artifact = createArtifact( "central", "commons-lang", "commons-lang", "2.1" );
+        artifactDao.saveArtifact( artifact );
+
+        artifact = createArtifact( "central", "org.apache.maven.test", "test-one", "1.2" );
+        artifactDao.saveArtifact( artifact );
+
+        artifact = createArtifact( "central", "org.apache.maven.test.foo", "test-two", "1.0" );
+        artifactDao.saveArtifact( artifact );
+
+        artifact = createArtifact( "central", "org.apache.maven.shared", "test-two", "2.0" );
+        artifactDao.saveArtifact( artifact );
+
+        artifact = createArtifact( "central", "org.apache.maven.shared", "test-two", "2.1" );
+        artifactDao.saveArtifact( artifact );
+
+        artifact = createArtifact( "central", "org.codehaus.modello", "test-two", "3.0" );
+        artifactDao.saveArtifact( artifact );
+
+        // Snapshots repository artifacts
+        artifact = createArtifact( "snapshots", "org.codehaus.modello", "test-three", "1.0-SNAPSHOT" );
+        artifactDao.saveArtifact( artifact );
+
+        artifact = createArtifact( "snapshots", "org.codehaus.mojo", "testable-maven-plugin", "2.1-SNAPSHOT" );
+        artifactDao.saveArtifact( artifact );
+
+        artifact = createArtifact( "snapshots", "org.apache.archiva", "testable", "1.1-alpha-1-20070822.033400-43" );
+        artifactDao.saveArtifact( artifact );
+    }
+
+    protected void setUp()
+        throws Exception
+    {
+        super.setUp();
+
+        ArchivaDAO dao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" );
+        artifactDao = dao.getArtifactDAO();
+    }
 }

Modified: maven/archiva/branches/archiva-backend-security/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/UniqueVersionConstraintTest.java
URL: http://svn.apache.org/viewvc/maven/archiva/branches/archiva-backend-security/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/UniqueVersionConstraintTest.java?rev=590764&r1=590763&r2=590764&view=diff
==============================================================================
--- maven/archiva/branches/archiva-backend-security/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/UniqueVersionConstraintTest.java (original)
+++ maven/archiva/branches/archiva-backend-security/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/UniqueVersionConstraintTest.java Wed Oct 31 10:34:18 2007
@@ -26,9 +26,9 @@
 import org.apache.maven.archiva.database.SimpleConstraint;
 import org.apache.maven.archiva.model.ArchivaArtifact;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Date;
-import java.util.Iterator;
 import java.util.List;
 
 /**
@@ -42,87 +42,152 @@
 {
     private ArtifactDAO artifactDao;
 
-    protected void setUp()
+    public void testConstraintGroupIdArtifactIdCommonsLang()
         throws Exception
     {
-        super.setUp();
+        setupArtifacts();
 
-        ArchivaDAO dao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" );
-        artifactDao = dao.getArtifactDAO();
+        assertConstraint( new String[] { "2.0", "2.1" }, new UniqueVersionConstraint( "commons-lang", "commons-lang" ) );
+    }
+
+    public void testConstraintGroupIdArtifactIdInvalid()
+        throws Exception
+    {
+        setupArtifacts();
+
+        assertConstraint( new String[] {}, new UniqueVersionConstraint( "org.apache", "invalid" ) );
+        assertConstraint( new String[] {}, new UniqueVersionConstraint( "org.apache.test", "invalid" ) );
+        assertConstraint( new String[] {}, new UniqueVersionConstraint( "invalid", "test-two" ) );
+    }
+
+    public void testConstraintGroupIdArtifactIdMavenSharedTestTwo()
+        throws Exception
+    {
+        setupArtifacts();
+
+        assertConstraint( new String[] { "2.0", "2.1-SNAPSHOT", "2.1.1", "2.1-alpha-1" },
+                          new UniqueVersionConstraint( "org.apache.maven.shared", "test-two" ) );
+    }
+
+    public void testConstraintGroupIdArtifactIdMavenSharedTestTwoCentralOnly()
+        throws Exception
+    {
+        setupArtifacts();
+
+        List<String> observableRepositories = new ArrayList<String>();
+        observableRepositories.add( "central" );
+
+        assertConstraint( new String[] { "2.0", "2.1.1", "2.1-alpha-1" },
+                          new UniqueVersionConstraint( observableRepositories, "org.apache.maven.shared", "test-two" ) );
+    }
+
+    public void testConstraintGroupIdArtifactIdMavenSharedTestTwoSnapshotsOnly()
+        throws Exception
+    {
+        setupArtifacts();
+
+        List<String> observableRepositories = new ArrayList<String>();
+        observableRepositories.add( "snapshots" );
+
+        assertConstraint( new String[] { "2.1-SNAPSHOT" }, 
+                          new UniqueVersionConstraint( observableRepositories, "org.apache.maven.shared", "test-two" ) );
+    }
+
+    public void testConstraintGroupIdArtifactIdMavenTestOne()
+        throws Exception
+    {
+        setupArtifacts();
+
+        assertConstraint( new String[] { "1.2" }, new UniqueVersionConstraint( "org.apache.maven.test", "test-one" ) );
     }
 
-    public ArchivaArtifact createArtifact( String groupId, String artifactId, String version )
+    public void testConstraintGroupIdArtifactIdModelloLong()
+        throws Exception
+    {
+        setupArtifacts();
+
+        assertConstraint( new String[] { "3.0" }, new UniqueVersionConstraint( "org.codehaus.modello", "modellong" ) );
+    }
+
+    private void assertConstraint( String[] versions, SimpleConstraint constraint )
+    {
+        String prefix = "Unique Versions: ";
+
+        List<String> results = dao.query( constraint );
+        assertNotNull( prefix + "Not Null", results );
+        assertEquals( prefix + "Results.size", versions.length, results.size() );
+
+        List<String> expectedVersions = Arrays.asList( versions );
+
+        for ( String actualVersion : results )
+        {
+            assertTrue( prefix + "version result should not be blank.", StringUtils.isNotBlank( actualVersion ) );
+            assertTrue( prefix + "version result <" + actualVersion + "> exists in expected versions.",
+                        expectedVersions.contains( actualVersion ) );
+        }
+    }
+
+    private ArchivaArtifact createArtifact( String repoId, String groupId, String artifactId, String version )
     {
         ArchivaArtifact artifact = artifactDao.createArtifact( groupId, artifactId, version, "", "jar" );
         artifact.getModel().setLastModified( new Date() ); // mandatory field.
-        artifact.getModel().setRepositoryId( "testable_repo" );
+        artifact.getModel().setRepositoryId( repoId );
         return artifact;
     }
 
-    public void testConstraint()
+    private void setupArtifacts()
         throws Exception
     {
         ArchivaArtifact artifact;
 
         // Setup artifacts in fresh DB.
-        artifact = createArtifact( "commons-lang", "commons-lang", "2.0" );
+        artifact = createArtifact( "central", "commons-lang", "commons-lang", "2.0" );
         artifactDao.saveArtifact( artifact );
 
-        artifact = createArtifact( "commons-lang", "commons-lang", "2.1" );
+        artifact = createArtifact( "central", "commons-lang", "commons-lang", "2.1" );
         artifactDao.saveArtifact( artifact );
 
-        artifact = createArtifact( "org.apache.maven.test", "test-one", "1.2" );
+        artifact = createArtifact( "central", "org.apache.maven.test", "test-one", "1.2" );
         artifactDao.saveArtifact( artifact );
 
-        artifact = createArtifact( "org.apache.maven.test.foo", "test-two", "1.0" );
+        artifact = createArtifact( "central", "org.apache.maven.test.foo", "test-two", "1.0" );
         artifactDao.saveArtifact( artifact );
 
-        artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.0" );
+        artifact = createArtifact( "central", "org.apache.maven.shared", "test-two", "2.0" );
         artifactDao.saveArtifact( artifact );
 
-        artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.1-SNAPSHOT" );
+        artifact = createArtifact( "central", "org.apache.maven.shared", "test-two", "2.1.1" );
         artifactDao.saveArtifact( artifact );
 
-        artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.1.1" );
+        artifact = createArtifact( "central", "org.apache.maven.shared", "test-two", "2.1-alpha-1" );
         artifactDao.saveArtifact( artifact );
 
-        artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.1-alpha-1" );
+        artifact = createArtifact( "central", "org.apache.maven.shared", "test-bar", "2.1" );
         artifactDao.saveArtifact( artifact );
 
-        artifact = createArtifact( "org.apache.maven.shared", "test-bar", "2.1" );
+        artifact = createArtifact( "central", "org.codehaus.modello", "modellong", "3.0" );
         artifactDao.saveArtifact( artifact );
 
-        artifact = createArtifact( "org.codehaus.modello", "modellong", "3.0" );
+        // Snapshots repository artifacts
+        artifact = createArtifact( "snapshots", "org.apache.maven.shared", "test-two", "2.1-SNAPSHOT" );
         artifactDao.saveArtifact( artifact );
 
-        assertConstraint( new String[] {}, new UniqueVersionConstraint( "org.apache", "invalid" ) );
-        assertConstraint( new String[] {}, new UniqueVersionConstraint( "org.apache.test", "invalid" ) );
-        assertConstraint( new String[] {}, new UniqueVersionConstraint( "invalid", "test-two" ) );
+        artifact = createArtifact( "snapshots", "org.codehaus.modello", "test-three", "1.0-SNAPSHOT" );
+        artifactDao.saveArtifact( artifact );
 
-        assertConstraint( new String[] { "2.0", "2.1" }, new UniqueVersionConstraint( "commons-lang", "commons-lang" ) );
-        assertConstraint( new String[] { "1.2" }, new UniqueVersionConstraint( "org.apache.maven.test", "test-one" ) );
-        assertConstraint( new String[] { "2.0", "2.1-SNAPSHOT", "2.1.1", "2.1-alpha-1" },
-                          new UniqueVersionConstraint( "org.apache.maven.shared", "test-two" ) );
-        assertConstraint( new String[] { "3.0" }, new UniqueVersionConstraint( "org.codehaus.modello", "modellong" ) );
+        artifact = createArtifact( "snapshots", "org.codehaus.mojo", "testable-maven-plugin", "2.1-SNAPSHOT" );
+        artifactDao.saveArtifact( artifact );
+
+        artifact = createArtifact( "snapshots", "org.apache.archiva", "testable", "1.1-alpha-1-20070822.033400-43" );
+        artifactDao.saveArtifact( artifact );
     }
 
-    private void assertConstraint( String[] versions, SimpleConstraint constraint )
+    protected void setUp()
+        throws Exception
     {
-        String prefix = "Unique Versions: ";
-
-        List results = dao.query( constraint );
-        assertNotNull( prefix + "Not Null", results );
-        assertEquals( prefix + "Results.size", versions.length, results.size() );
-
-        List expectedVersions = Arrays.asList( versions );
+        super.setUp();
 
-        Iterator it = results.iterator();
-        while ( it.hasNext() )
-        {
-            String actualVersion = (String) it.next();
-            assertTrue( prefix + "version result should not be blank.", StringUtils.isNotBlank( actualVersion ) );
-            assertTrue( prefix + "version result <" + actualVersion + "> exists in expected versions.",
-                        expectedVersions.contains( actualVersion ) );
-        }
+        ArchivaDAO dao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" );
+        artifactDao = dao.getArtifactDAO();
     }
 }