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   * RecentArtifactsByAgeConstraintTest 
33   *
34   * @version $Id: RecentArtifactsByAgeConstraintTest.java 755266 2009-03-17 14:28:40Z brett $
35   */
36  public class RecentArtifactsByAgeConstraintTest
37      extends AbstractArchivaDatabaseTestCase
38  {
39      private ArtifactDAO artifactDao;
40  
41      @Override
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, int daysOld )
52      {
53          ArchivaArtifact artifact = artifactDao.createArtifact( "org.apache.maven.archiva.test", artifactId, version,
54                                                                 "", "jar", "testable_repo" );
55          Calendar cal = Calendar.getInstance();
56          cal.add( Calendar.DAY_OF_MONTH, ( -1 ) * daysOld );
57          artifact.getModel().setLastModified( cal.getTime() );
58          artifact.getModel().setRepositoryId( "testable_repo" );
59          return artifact;
60      }
61  
62      public void testConstraint()
63          throws Exception
64      {
65          ArchivaArtifact artifact;
66  
67          // Setup artifacts in fresh DB.
68          artifact = createArtifact( "test-one", "1.0", 200 );
69          artifactDao.saveArtifact( artifact );
70  
71          artifact = createArtifact( "test-one", "1.1", 100 );
72          artifactDao.saveArtifact( artifact );
73  
74          artifact = createArtifact( "test-one", "1.2", 50 );
75          artifactDao.saveArtifact( artifact );
76  
77          artifact = createArtifact( "test-two", "1.0", 200 );
78          artifactDao.saveArtifact( artifact );
79  
80          artifact = createArtifact( "test-two", "2.0", 150 );
81          artifactDao.saveArtifact( artifact );
82  
83          artifact = createArtifact( "test-two", "2.1", 100 );
84          artifactDao.saveArtifact( artifact );
85  
86          artifact = createArtifact( "test-two", "3.0", 5 );
87          artifactDao.saveArtifact( artifact );
88  
89          assertConstraint( 0, new RecentArtifactsByAgeConstraint( 2 ) );
90          assertConstraint( 1, new RecentArtifactsByAgeConstraint( 7 ) );
91          assertConstraint( 2, new RecentArtifactsByAgeConstraint( 90 ) );
92          assertConstraint( 4, new RecentArtifactsByAgeConstraint( 100 ) );
93          assertConstraint( 5, new RecentArtifactsByAgeConstraint( 150 ) );
94          assertConstraint( 7, new RecentArtifactsByAgeConstraint( 9000 ) );
95      }
96  
97      private void assertConstraint( int expectedHits, Constraint constraint )
98          throws Exception
99      {
100         List<ArchivaArtifact> results = artifactDao.queryArtifacts( constraint );
101         assertNotNull( "Recent Artifacts By Age: Not Null", results );
102         assertEquals( "Recent Artifacts By Age: Results.size", expectedHits, results.size() );
103     }
104 }