View Javadoc

1   package org.apache.maven.archiva.converter;
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  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.commons.io.FileUtils;
28  import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
29  import org.apache.maven.archiva.converter.legacy.LegacyRepositoryConverter;
30  import org.apache.maven.artifact.repository.ArtifactRepository;
31  import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
32  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
33  import org.codehaus.plexus.spring.PlexusInSpringTestCase;
34  
35  /**
36   * Test the repository converter.
37   *
38   * @todo what about deletions from the source repository?
39   * @todo use artifact-test instead
40   * @todo should reject if dependencies are missing - rely on reporting?
41   * @todo group metadata
42   */
43  public class RepositoryConverterTest
44      extends PlexusInSpringTestCase
45  {
46      private ArtifactRepository sourceRepository;
47  
48      private ManagedRepositoryConfiguration targetRepository;
49  
50      private LegacyRepositoryConverter repositoryConverter;
51  
52      protected void setUp()
53          throws Exception
54      {
55          super.setUp();
56  
57          ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
58  
59          ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "legacy" );
60  
61          File sourceBase = getTestFile( "src/test/source-repository" );
62          sourceRepository = factory.createArtifactRepository( "source", sourceBase.toURL().toString(), layout, null,
63                                                               null );
64  
65          layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
66  
67          File targetBase = getTestFile( "target/test-target-repository" );
68          copyDirectoryStructure( getTestFile( "src/test/target-repository" ), targetBase );
69  
70          targetRepository = new ManagedRepositoryConfiguration();
71          targetRepository.setId( "target" );
72          targetRepository.setName( "Target Repo" );
73          targetRepository.setLocation( targetBase.getAbsolutePath() );
74          targetRepository.setLayout( "default" );
75  
76          repositoryConverter = (LegacyRepositoryConverter) lookup( LegacyRepositoryConverter.ROLE, "default" );
77      }
78  
79      protected void tearDown()
80          throws Exception
81      {
82          super.tearDown();
83      }
84  
85      private void copyDirectoryStructure( File sourceDirectory, File destinationDirectory )
86          throws IOException
87      {
88          if ( !sourceDirectory.exists() )
89          {
90              throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
91          }
92  
93          File[] files = sourceDirectory.listFiles();
94  
95          String sourcePath = sourceDirectory.getAbsolutePath();
96  
97          for ( int i = 0; i < files.length; i++ )
98          {
99              File file = files[i];
100 
101             String dest = file.getAbsolutePath();
102 
103             dest = dest.substring( sourcePath.length() + 1 );
104 
105             File destination = new File( destinationDirectory, dest );
106 
107             if ( file.isFile() )
108             {
109                 destination = destination.getParentFile();
110 
111                 FileUtils.copyFileToDirectory( file, destination );
112             }
113             else if ( file.isDirectory() )
114             {
115                 if ( !".svn".equals( file.getName() ) )
116                 {
117                     if ( !destination.exists() && !destination.mkdirs() )
118                     {
119                         throw new IOException( "Could not create destination directory '"
120                             + destination.getAbsolutePath() + "'." );
121                     }
122                     copyDirectoryStructure( file, destination );
123                 }
124             }
125             else
126             {
127                 throw new IOException( "Unknown file type: " + file.getAbsolutePath() );
128             }
129         }
130     }
131 
132     public void testLegacyConversion()
133         throws IOException, RepositoryConversionException
134     {
135         File legacyRepoDir = new File( sourceRepository.getBasedir() );
136         File destRepoDir = new File( targetRepository.getLocation() );
137         List<String> excludes = new ArrayList<String>();
138         repositoryConverter.convertLegacyRepository( legacyRepoDir, destRepoDir, excludes );
139     }
140 }