]> source.dussan.org Git - archiva.git/blob
68fafb4fc2867fb65b996de1893bed097164a9de
[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.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 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
35
36 import javax.inject.Inject;
37 import javax.inject.Named;
38 import java.io.File;
39 import java.io.IOException;
40 import java.util.ArrayList;
41 import java.util.List;
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( SpringJUnit4ClassRunner.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     public void setUp()
69         throws Exception
70     {
71         super.setUp();
72
73         ArtifactRepositoryFactory factory = plexusSisuBridge.lookup( ArtifactRepositoryFactory.class );
74             //(ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
75
76         ArtifactRepositoryLayout layout = plexusSisuBridge.lookup( ArtifactRepositoryLayout.class, "legacy" );
77             //(ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "legacy" );
78
79         File sourceBase = new File( "src/test/source-repository" );
80         sourceRepository = factory.createArtifactRepository( "source", sourceBase.toURL().toString(), layout, null,
81                                                              null );
82
83         layout = plexusSisuBridge.lookup( ArtifactRepositoryLayout.class, "default" );
84             //(ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
85
86         File targetBase = new File( "target/test-target-repository" );
87         copyDirectoryStructure( new File( "src/test/target-repository" ), targetBase );
88
89         targetRepository = new ManagedRepositoryConfiguration();
90         targetRepository.setId( "target" );
91         targetRepository.setName( "Target Repo" );
92         targetRepository.setLocation( targetBase.getAbsolutePath() );
93         targetRepository.setLayout( "default" );
94
95         //repositoryConverter = (LegacyRepositoryConverter) lookup( LegacyRepositoryConverter.ROLE, "default" );
96     }
97
98     protected void tearDown()
99         throws Exception
100     {
101         super.tearDown();
102     }
103
104     private void copyDirectoryStructure( File sourceDirectory, File destinationDirectory )
105         throws IOException
106     {
107         if ( !sourceDirectory.exists() )
108         {
109             throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
110         }
111
112         File[] files = sourceDirectory.listFiles();
113
114         String sourcePath = sourceDirectory.getAbsolutePath();
115
116         for ( int i = 0; i < files.length; i++ )
117         {
118             File file = files[i];
119
120             String dest = file.getAbsolutePath();
121
122             dest = dest.substring( sourcePath.length() + 1 );
123
124             File destination = new File( destinationDirectory, dest );
125
126             if ( file.isFile() )
127             {
128                 destination = destination.getParentFile();
129
130                 FileUtils.copyFileToDirectory( file, destination );
131             }
132             else if ( file.isDirectory() )
133             {
134                 if ( !".svn".equals( file.getName() ) )
135                 {
136                     if ( !destination.exists() && !destination.mkdirs() )
137                     {
138                         throw new IOException( "Could not create destination directory '"
139                             + destination.getAbsolutePath() + "'." );
140                     }
141                     copyDirectoryStructure( file, destination );
142                 }
143             }
144             else
145             {
146                 throw new IOException( "Unknown file type: " + file.getAbsolutePath() );
147             }
148         }
149     }
150
151     @Test
152     public void testLegacyConversion()
153         throws IOException, RepositoryConversionException
154     {
155         File legacyRepoDir = new File( sourceRepository.getBasedir() );
156         File destRepoDir = new File( targetRepository.getLocation() );
157         List<String> excludes = new ArrayList<String>();
158         repositoryConverter.convertLegacyRepository( legacyRepoDir, destRepoDir, excludes );
159     }
160 }