View Javadoc

1   package org.apache.maven.archiva.database.constraints;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.archiva.database.AbstractArchivaDatabaseTestCase;
23  import org.apache.maven.archiva.database.ArchivaDAO;
24  import org.apache.maven.archiva.database.ArtifactDAO;
25  import org.apache.maven.archiva.database.Constraint;
26  import org.apache.maven.archiva.model.ArchivaArtifact;
27  
28  import java.util.Calendar;
29  import java.util.List;
30  
31  /**
32   * ArtifactsRelatedConstraintTest 
33   *
34   * @version $Id: ArtifactsRelatedConstraintTest.java 755266 2009-03-17 14:28:40Z brett $
35   */
36  public class ArtifactsRelatedConstraintTest
37      extends AbstractArchivaDatabaseTestCase
38  {
39      private static final String TEST_GROUPID = "org.apache.maven.archiva.test";
40      private ArtifactDAO artifactDao;
41  
42      protected void setUp()
43          throws Exception
44      {
45          super.setUp();
46  
47          ArchivaDAO dao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" );
48          artifactDao = dao.getArtifactDAO();
49      }
50  
51      public ArchivaArtifact createArtifact( String artifactId, String version, String classifier, String type )
52      {
53          ArchivaArtifact artifact = artifactDao.createArtifact( TEST_GROUPID, artifactId, version,
54                                                                 classifier, type, "testable_repo" );
55          Calendar cal = Calendar.getInstance();
56          artifact.getModel().setLastModified( cal.getTime() );
57          artifact.getModel().setRepositoryId( "testable_repo" );
58          return artifact;
59      }
60  
61      public void testConstraint()
62          throws Exception
63      {
64          // Setup artifacts in fresh DB.
65          artifactDao.saveArtifact( createArtifact( "test-one", "1.0", "", "jar" ) );
66          artifactDao.saveArtifact( createArtifact( "test-one", "1.0", "", "pom" ) );
67          artifactDao.saveArtifact( createArtifact( "test-one", "1.0", "javadoc", "jar" ) );
68          artifactDao.saveArtifact( createArtifact( "test-one", "1.0", "sources", "jar" ) );
69  
70          artifactDao.saveArtifact( createArtifact( "test-one", "1.1", "", "jar" ) );
71          artifactDao.saveArtifact( createArtifact( "test-one", "1.2", "", "jar" ) );
72          
73          artifactDao.saveArtifact( createArtifact( "test-two", "1.0", "", "jar" ) );
74          artifactDao.saveArtifact( createArtifact( "test-two", "2.0", "", "jar" ) );
75          artifactDao.saveArtifact( createArtifact( "test-two", "2.1", "", "jar" ) );
76          artifactDao.saveArtifact( createArtifact( "test-two", "3.0", "", "jar" ) );
77  
78          assertConstraint( 4, new ArtifactsRelatedConstraint( TEST_GROUPID, "test-one", "1.0" ) );
79          assertConstraint( 1, new ArtifactsRelatedConstraint( TEST_GROUPID, "test-one", "1.1" ) );
80      }
81  
82      private void assertConstraint( int expectedHits, Constraint constraint )
83          throws Exception
84      {
85          List<ArchivaArtifact> results = artifactDao.queryArtifacts( constraint );
86          assertNotNull( "Related Artifacts: Not Null", results );
87          assertEquals( "Related Artifacts: Results.size", expectedHits, results.size() );
88      }
89  
90  }