]> source.dussan.org Git - archiva.git/blob
50fdb0c3147523fd431a7e3896f98a24151cf2f5
[archiva.git] /
1 package org.apache.maven.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.RepositoryConversionException;
25 import org.apache.archiva.converter.legacy.LegacyRepositoryConverter;
26 import org.apache.commons.io.FileUtils;
27 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.maven.artifact.repository.ArtifactRepository;
29 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
30 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.springframework.test.context.ContextConfiguration;
35 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
36
37 import javax.inject.Inject;
38 import javax.inject.Named;
39 import java.io.File;
40 import java.io.IOException;
41 import java.util.ArrayList;
42 import java.util.List;
43
44 /**
45  * Test the repository converter.
46  *
47  * @todo what about deletions from the source repository?
48  * @todo use artifact-test instead
49  * @todo should reject if dependencies are missing - rely on reporting?
50  * @todo group metadata
51  */
52 @RunWith( SpringJUnit4ClassRunner.class )
53 @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml","classpath:/spring-context.xml"} )
54 public class RepositoryConverterTest
55     extends TestCase
56 {
57     private ArtifactRepository sourceRepository;
58
59     private ManagedRepositoryConfiguration targetRepository;
60
61     @Inject
62     @Named(value = "legacyRepositoryConverter#default")
63     private LegacyRepositoryConverter repositoryConverter;
64
65     @Inject
66     PlexusSisuBridge plexusSisuBridge;
67
68     @Before
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     protected void tearDown()
100         throws Exception
101     {
102         super.tearDown();
103     }
104
105     private void copyDirectoryStructure( File sourceDirectory, File destinationDirectory )
106         throws IOException
107     {
108         if ( !sourceDirectory.exists() )
109         {
110             throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
111         }
112
113         File[] files = sourceDirectory.listFiles();
114
115         String sourcePath = sourceDirectory.getAbsolutePath();
116
117         for ( int i = 0; i < files.length; i++ )
118         {
119             File file = files[i];
120
121             String dest = file.getAbsolutePath();
122
123             dest = dest.substring( sourcePath.length() + 1 );
124
125             File destination = new File( destinationDirectory, dest );
126
127             if ( file.isFile() )
128             {
129                 destination = destination.getParentFile();
130
131                 FileUtils.copyFileToDirectory( file, destination );
132             }
133             else if ( file.isDirectory() )
134             {
135                 if ( !".svn".equals( file.getName() ) )
136                 {
137                     if ( !destination.exists() && !destination.mkdirs() )
138                     {
139                         throw new IOException( "Could not create destination directory '"
140                             + destination.getAbsolutePath() + "'." );
141                     }
142                     copyDirectoryStructure( file, destination );
143                 }
144             }
145             else
146             {
147                 throw new IOException( "Unknown file type: " + file.getAbsolutePath() );
148             }
149         }
150     }
151
152     @Test
153     public void testLegacyConversion()
154         throws IOException, RepositoryConversionException
155     {
156         File legacyRepoDir = new File( sourceRepository.getBasedir() );
157         File destRepoDir = new File( targetRepository.getLocation() );
158         List<String> excludes = new ArrayList<String>();
159         repositoryConverter.convertLegacyRepository( legacyRepoDir, destRepoDir, excludes );
160     }
161 }