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 java.util.Date;
23  import java.util.List;
24  
25  import org.apache.maven.archiva.database.AbstractArchivaDatabaseTestCase;
26  import org.apache.maven.archiva.database.ArtifactDAO;
27  import org.apache.maven.archiva.model.ArchivaArtifact;
28  
29  /**
30   * ArtifactsProcessedConstraintTest 
31   *
32   * @version $Id: ArtifactsProcessedConstraintTest.java 755266 2009-03-17 14:28:40Z brett $
33   */
34  public class ArtifactsProcessedConstraintTest
35      extends AbstractArchivaDatabaseTestCase
36  {
37      public ArchivaArtifact createArtifact( String groupId, String artifactId, String version, String whenProcessed )
38          throws Exception
39      {
40          ArchivaArtifact artifact = dao.getArtifactDAO().createArtifact( groupId, artifactId, version, "", "jar", "testrepo" );
41          assertNotNull( "Artifact should not be null.", artifact );
42          Date dateWhenProcessed = null;
43  
44          if ( whenProcessed != null )
45          {
46              dateWhenProcessed = toDate( whenProcessed );
47          }
48  
49          artifact.getModel().setWhenProcessed( dateWhenProcessed );
50  
51          // Satisfy table / column requirements.
52          artifact.getModel().setLastModified( new Date() );
53  
54          return artifact;
55      }
56  
57      public void assertResults( String type, List<ArchivaArtifact> results, String expectedArtifacts[] )
58      {
59          assertNotNull( "Results[" + type + "] should not be null.", results );
60          assertEquals( "Results[" + type + "].size", expectedArtifacts.length, results.size() );
61  
62          for ( int i = 0; i < expectedArtifacts.length; i++ )
63          {
64              String artifactId = expectedArtifacts[i];
65  
66              int found = 0;
67              for ( ArchivaArtifact artifact : results )
68              {
69                  if ( artifactId.equals( artifact.getArtifactId() ) )
70                  {
71                      found++;
72                  }
73              }
74  
75              if ( found <= 0 )
76              {
77                  fail( "Results[" + type + "] - Did not find expected artifact ID [" + artifactId + "]" );
78              }
79  
80              if ( found > 1 )
81              {
82                  fail( "Results[" + type + "] - Expected to find 1 copy of artifact ID [" + artifactId
83                      + "], yet found <" + found + "> instead." );
84              }
85          }
86      }
87  
88      @Override
89      protected void setUp()
90          throws Exception
91      {
92          super.setUp();
93  
94          ArtifactDAO adao = dao.getArtifactDAO();
95          assertNotNull( "Artifact DAO should not be null.", adao );
96  
97          adao.saveArtifact( createArtifact( "org.apache.maven.archiva", "archiva-common", "1.0-SNAPSHOT", null ) );
98          adao.saveArtifact( createArtifact( "org.apache.maven.archiva", "archiva-utils", "1.0-SNAPSHOT",
99                                             "2006/08/22 19:01:00" ) );
100         adao.saveArtifact( createArtifact( "org.apache.maven.archiva", "archiva-old", "0.1", "2004/02/15 9:01:00" ) );
101         adao.saveArtifact( createArtifact( "org.apache.maven.archiva", "archiva-database", "1.0-SNAPSHOT", null ) );
102     }
103 
104     public void testNotProcessed()
105         throws Exception
106     {
107         List<ArchivaArtifact> results = dao.getArtifactDAO().queryArtifacts( new ArtifactsProcessedConstraint( false ) );
108         assertResults( "not-processed", results, new String[] { "archiva-common", "archiva-database" } );
109     }
110 
111     public void testProcessed()
112         throws Exception
113     {
114         List<ArchivaArtifact> results = dao.getArtifactDAO().queryArtifacts( new ArtifactsProcessedConstraint( true ) );
115         assertResults( "processed", results, new String[] { "archiva-utils", "archiva-old" } );
116     }
117 
118     public void testSinceRecent()
119         throws Exception
120     {
121         Date since = toDate( "2006/01/01 12:00:00" );
122         List<ArchivaArtifact> results = dao.getArtifactDAO().queryArtifacts( new ArtifactsProcessedConstraint( since ) );
123         assertResults( "processed", results, new String[] { "archiva-utils" } );
124     }
125 
126     public void testSinceOld()
127         throws Exception
128     {
129         Date since = toDate( "2001/01/01 12:00:00" );
130         List<ArchivaArtifact> results = dao.getArtifactDAO().queryArtifacts( new ArtifactsProcessedConstraint( since ) );
131         assertResults( "processed", results, new String[] { "archiva-utils", "archiva-old" } );
132     }
133 }