View Javadoc

1   package org.apache.maven.archiva.consumers.database;
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.io.File;
23  
24  import org.apache.maven.archiva.configuration.ArchivaConfiguration;
25  import org.apache.maven.archiva.configuration.Configuration;
26  import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
27  import org.apache.maven.archiva.consumers.ConsumerException;
28  import org.apache.maven.archiva.database.ArchivaDatabaseException;
29  import org.apache.maven.archiva.database.ObjectNotFoundException;
30  import org.apache.maven.archiva.database.ProjectModelDAO;
31  import org.apache.maven.archiva.database.updater.DatabaseUnprocessedArtifactConsumer;
32  import org.apache.maven.archiva.model.ArchivaArtifact;
33  import org.apache.maven.archiva.model.ArchivaArtifactModel;
34  import org.apache.maven.archiva.model.ArchivaProjectModel;
35  import org.apache.maven.archiva.model.Dependency;
36  import org.apache.maven.archiva.model.Keys;
37  import org.codehaus.plexus.spring.PlexusInSpringTestCase;
38  
39  /**
40   * Test for ProjectModelToDatabaseConsumerTest
41   * 
42   */
43  public class ProjectModelToDatabaseConsumerTest
44      extends PlexusInSpringTestCase
45  {
46      private ProjectModelToDatabaseConsumer consumer;
47  
48      private ProjectModelDAO modelDao;
49  
50      public void setUp()
51          throws Exception
52      {
53          super.setUp();
54  
55          ArchivaConfiguration archivaConfig = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
56  
57          Configuration configuration = archivaConfig.getConfiguration();
58          ManagedRepositoryConfiguration repo = configuration.findManagedRepositoryById( "internal" );
59          repo.setLocation( new File( getBasedir(), "src/test/resources/test-repo" ).toString() );
60  
61          consumer =
62              (ProjectModelToDatabaseConsumer) lookup( DatabaseUnprocessedArtifactConsumer.class, "update-db-project" );
63          modelDao = (ProjectModelDAO) lookup( ProjectModelDAO.class, "jdo" );
64      }
65  
66      public void testProcess()
67          throws Exception
68      {
69          ArchivaProjectModel model = processAndGetModel( "test-project", "test-project-endpoint-pom", "2.4.4" );
70          assertNotNull( model.getParentProject() );
71          assertEquals( "test-project:test-project:2.4.4", Keys.toKey( model.getParentProject() ) );
72  
73          assertFalse( model.getDependencyManagement().isEmpty() );
74  
75          model = processAndGetModel( "test-project", "test-project-endpoint-ejb", "2.4.4" );
76          assertNotNull( model.getParentProject() );
77          assertEquals( "test-project:test-project-endpoint-pom:2.4.4", Keys.toKey( model.getParentProject() ) );
78          assertTrue( hasDependency( model, "test-project:test-project-api:2.4.4" ) );
79          assertTrue( hasDependency( model, "commons-id:commons-id:0.1-dev" ) );
80  
81          model = processAndGetModel( "test-project", "test-project", "2.4.4" );
82          assertFalse( model.getDependencyManagement().isEmpty() );
83      }
84  
85      private boolean hasDependency( ArchivaProjectModel model, String key )
86      {
87          for ( Dependency dependency : model.getDependencies() )
88          {
89              if ( key.equals( Keys.toKey( dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion() ) ) )
90              {
91                  return true;
92              }
93          }
94          return false;
95      }
96  
97      private ArchivaProjectModel processAndGetModel( String group, String artifactId, String version )
98          throws ConsumerException, ObjectNotFoundException, ArchivaDatabaseException
99      {
100         ArchivaArtifact artifact = createArtifact( group, artifactId, version, "pom" );
101         consumer.processArchivaArtifact( artifact );
102 
103         ArchivaProjectModel model = modelDao.getProjectModel( group, artifactId, version );
104         assertEquals( group, model.getGroupId() );
105         assertEquals( artifactId, model.getArtifactId() );
106         assertEquals( version, model.getVersion() );
107         return model;
108     }
109 
110     protected ArchivaArtifact createArtifact( String group, String artifactId, String version, String type )
111     {
112         ArchivaArtifactModel model = new ArchivaArtifactModel();
113         model.setGroupId( group );
114         model.setArtifactId( artifactId );
115         model.setVersion( version );
116         model.setType( type );
117         model.setRepositoryId( "internal" );
118 
119         return new ArchivaArtifact( model );
120     }
121 
122 }