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