]> source.dussan.org Git - archiva.git/blob
b52f9518e946b83c884dfe92017fa2a73992e0dc
[archiva.git] /
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.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;
36
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;
45
46 /**
47  * Test the repository converter.
48  *
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
53  */
54 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
55 @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml","classpath:/spring-context.xml"} )
56 public class RepositoryConverterTest
57     extends TestCase
58 {
59     private ArtifactRepository sourceRepository;
60
61     private ManagedRepositoryConfiguration targetRepository;
62
63     @Inject
64     @Named(value = "legacyRepositoryConverter#default")
65     private LegacyRepositoryConverter repositoryConverter;
66
67     @Inject
68     PlexusSisuBridge plexusSisuBridge;
69
70     @Before
71     @Override
72     public void setUp()
73         throws Exception
74     {
75         super.setUp();
76
77         // ArtifactRepositoryFactory factory = plexusSisuBridge.lookup( ArtifactRepositoryFactory.class );
78             //(ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
79
80         ArtifactRepositoryLayout layout = new LegacyRepositoryLayout();
81             //(ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "legacy" );
82
83         Path sourceBase = Paths.get( "src/test/source-repository" );
84         sourceRepository = new MavenArtifactRepository( "source", sourceBase.toUri().toURL().toString(), layout, null,
85                                                              null );
86
87         layout = plexusSisuBridge.lookup( ArtifactRepositoryLayout.class, "default" );
88             //(ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
89
90         Path targetBase = Paths.get( "target/test-target-repository" );
91         copyDirectoryStructure( Paths.get( "src/test/target-repository" ), targetBase );
92
93         targetRepository = new ManagedRepositoryConfiguration();
94         targetRepository.setId( "target" );
95         targetRepository.setName( "Target Repo" );
96         targetRepository.setLocation( targetBase.toAbsolutePath().toString() );
97         targetRepository.setLayout( "default" );
98
99         //repositoryConverter = (LegacyRepositoryConverter) lookup( LegacyRepositoryConverter.ROLE, "default" );
100     }
101
102     @Override
103     protected void tearDown()
104         throws Exception
105     {
106         super.tearDown();
107     }
108
109     private void copyDirectoryStructure( Path sourceDirectory, Path destinationDirectory )
110         throws IOException
111     {
112         if ( !Files.exists(sourceDirectory) )
113         {
114             throw new IOException( "Source directory doesn't exists (" + sourceDirectory.toAbsolutePath() + ")." );
115         }
116
117         Path[] files = Files.list(sourceDirectory).toArray(Path[]::new);
118
119         String sourcePath = sourceDirectory.toAbsolutePath().toString();
120
121         for ( int i = 0; i < files.length; i++ )
122         {
123             Path file = files[i];
124
125             String dest = file.toAbsolutePath().toString();
126
127             dest = dest.substring( sourcePath.length() + 1 );
128
129             Path destination = destinationDirectory.resolve( dest );
130
131             if ( Files.isRegularFile(file) )
132             {
133                 destination = destination.getParent();
134
135                 FileUtils.copyFileToDirectory( file.toFile(), destination.toFile() );
136             }
137             else if ( Files.isDirectory(file) )
138             {
139                 if ( !".svn".equals( file.getFileName().toString() ) )
140                 {
141                     if ( !Files.exists(destination))
142                     {
143                         Files.createDirectories( destination );
144                     }
145                     copyDirectoryStructure( file, destination );
146                 }
147             }
148             else
149             {
150                 throw new IOException( "Unknown file type: " + file.toAbsolutePath() );
151             }
152         }
153     }
154
155     @Test
156     public void testLegacyConversion()
157         throws IOException, RepositoryConversionException
158     {
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 );
163     }
164 }