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.DeclarativeConstraint;
24  import org.apache.maven.archiva.model.ArchivaArtifact;
25  import org.apache.maven.archiva.model.ArchivaProjectModel;
26  import org.apache.maven.archiva.model.ArtifactReference;
27  import org.apache.maven.archiva.model.Dependency;
28  import org.apache.maven.archiva.model.VersionedReference;
29  
30  import java.util.Date;
31  import java.util.List;
32  
33  /**
34   * ProjectsByArtifactUsageConstraintTest 
35   *
36   * @version $Id: ProjectsByArtifactUsageConstraintTest.java 755266 2009-03-17 14:28:40Z brett $
37   */
38  public class ProjectsByArtifactUsageConstraintTest
39      extends AbstractArchivaDatabaseTestCase
40  {
41      @Override
42      protected void setUp()
43          throws Exception
44      {
45          super.setUp();
46      }
47  
48      private void saveModel( String modelId, String deps[] )
49          throws Exception
50      {
51          ArchivaProjectModel model = new ArchivaProjectModel();
52          // Piece together a simple model.
53          VersionedReference ref = toVersionedReference( modelId );
54          model.setGroupId( ref.getGroupId() );
55          model.setArtifactId( ref.getArtifactId() );
56          model.setVersion( ref.getVersion() );
57          model.setPackaging( "jar" );
58          model.setOrigin( "testcase" );
59  
60          if ( deps != null )
61          {
62              for ( int i = 0; i < deps.length; i++ )
63              {
64                  ArtifactReference artiref = toArtifactReference( deps[i] );
65                  Dependency dep = new Dependency();
66                  dep.setGroupId( artiref.getGroupId() );
67                  dep.setArtifactId( artiref.getArtifactId() );
68                  dep.setVersion( artiref.getVersion() );
69                  dep.setClassifier( artiref.getClassifier() );
70                  dep.setClassifier( artiref.getType() );
71  
72                  model.addDependency( dep );
73              }
74          }
75  
76          dao.getProjectModelDAO().saveProjectModel( model );
77      }
78  
79      public ArchivaArtifact toArtifact( String id )
80      {
81          ArtifactReference ref = toArtifactReference( id );
82  
83          ArchivaArtifact artifact = new ArchivaArtifact( ref.getGroupId(), ref.getArtifactId(), ref.getVersion(), ref
84              .getClassifier(), ref.getType(), "testable_repo" );
85          artifact.getModel().setLastModified( new Date() );
86          artifact.getModel().setRepositoryId( "testable_repo" );
87          return artifact;
88      }
89  
90      public void testContraint()
91          throws Exception
92      {
93          saveModel( "org.apache.maven.archiva:archiva-configuration:1.0",
94                     new String[] { "org.codehaus.plexus:plexus-digest:1.0::jar:" } );
95  
96          saveModel( "org.apache.maven.archiva:archiva-common:1.0", new String[] {
97              "org.codehaus.plexus:plexus-digest:1.0::jar:",
98              "junit:junit:3.8.1::jar:" } );
99  
100         ArchivaArtifact artifact;
101 
102         artifact = toArtifact( "org.foo:bar:4.0::jar:" );
103         assertConstraint( 0, new ProjectsByArtifactUsageConstraint( artifact ) );
104         artifact = toArtifact( "org.codehaus.plexus:plexus-digest:1.0::jar:testable_repo" );
105         assertConstraint( 2, new ProjectsByArtifactUsageConstraint( artifact ) );
106     }
107 
108     private void assertConstraint( int expectedHits, DeclarativeConstraint constraint )
109         throws Exception
110     {
111         List<ArchivaProjectModel> results = dao.getProjectModelDAO().queryProjectModels( constraint );
112         assertNotNull( "Projects By Artifact Usage: Not Null", results );
113         assertEquals( "Projects By Artifact Usage: Results.size", expectedHits, results.size() );
114     }
115 }