1 package org.apache.archiva.converter;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import junit.framework.TestCase;
23 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
24 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.apache.archiva.converter.artifact.LegacyRepositoryLayout;
26 import org.apache.archiva.converter.legacy.LegacyRepositoryConverter;
27 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
28 import org.apache.commons.io.FileUtils;
29 import org.apache.maven.artifact.repository.ArtifactRepository;
30 import org.apache.maven.artifact.repository.MavenArtifactRepository;
31 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.springframework.test.context.ContextConfiguration;
37 import javax.inject.Inject;
38 import javax.inject.Named;
39 import java.io.IOException;
40 import java.nio.file.Files;
41 import java.nio.file.Path;
42 import java.nio.file.Paths;
43 import java.util.ArrayList;
44 import java.util.List;
47 * Test the repository converter.
49 * @todo what about deletions from the source repository?
50 * @todo use artifact-test instead
51 * @todo should reject if dependencies are missing - rely on reporting?
52 * @todo group metadata
54 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
55 @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml","classpath:/spring-context.xml"} )
56 public class RepositoryConverterTest
59 private ArtifactRepository sourceRepository;
61 private ManagedRepositoryConfiguration targetRepository;
64 @Named(value = "legacyRepositoryConverter#default")
65 private LegacyRepositoryConverter repositoryConverter;
68 PlexusSisuBridge plexusSisuBridge;
77 // ArtifactRepositoryFactory factory = plexusSisuBridge.lookup( ArtifactRepositoryFactory.class );
78 //(ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
80 ArtifactRepositoryLayout layout = new LegacyRepositoryLayout();
81 //(ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "legacy" );
83 Path sourceBase = Paths.get( "src/test/source-repository" );
84 sourceRepository = new MavenArtifactRepository( "source", sourceBase.toUri().toURL().toString(), layout, null,
87 layout = plexusSisuBridge.lookup( ArtifactRepositoryLayout.class, "default" );
88 //(ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
90 Path targetBase = Paths.get( "target/test-target-repository" );
91 copyDirectoryStructure( Paths.get( "src/test/target-repository" ), targetBase );
93 targetRepository = new ManagedRepositoryConfiguration();
94 targetRepository.setId( "target" );
95 targetRepository.setName( "Target Repo" );
96 targetRepository.setLocation( targetBase.toAbsolutePath().toString() );
97 targetRepository.setLayout( "default" );
99 //repositoryConverter = (LegacyRepositoryConverter) lookup( LegacyRepositoryConverter.ROLE, "default" );
103 protected void tearDown()
109 private void copyDirectoryStructure( Path sourceDirectory, Path destinationDirectory )
112 if ( !Files.exists(sourceDirectory) )
114 throw new IOException( "Source directory doesn't exists (" + sourceDirectory.toAbsolutePath() + ")." );
117 Path[] files = Files.list(sourceDirectory).toArray(Path[]::new);
119 String sourcePath = sourceDirectory.toAbsolutePath().toString();
121 for ( int i = 0; i < files.length; i++ )
123 Path file = files[i];
125 String dest = file.toAbsolutePath().toString();
127 dest = dest.substring( sourcePath.length() + 1 );
129 Path destination = destinationDirectory.resolve( dest );
131 if ( Files.isRegularFile(file) )
133 destination = destination.getParent();
135 FileUtils.copyFileToDirectory( file.toFile(), destination.toFile() );
137 else if ( Files.isDirectory(file) )
139 if ( !".svn".equals( file.getFileName().toString() ) )
141 if ( !Files.exists(destination))
143 Files.createDirectories( destination );
145 copyDirectoryStructure( file, destination );
150 throw new IOException( "Unknown file type: " + file.toAbsolutePath() );
156 public void testLegacyConversion()
157 throws IOException, RepositoryConversionException
159 Path legacyRepoDir = Paths.get( sourceRepository.getBasedir() );
160 Path destRepoDir = Paths.get( targetRepository.getLocation() );
161 List<String> excludes = new ArrayList<>();
162 repositoryConverter.convertLegacyRepository( legacyRepoDir, destRepoDir, excludes );