1 package org.apache.maven.archiva.converter;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
37
38
39
40
41
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 }