You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RepositoryConverterTest.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package org.apache.archiva.converter;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import junit.framework.TestCase;
  21. import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
  22. import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
  23. import org.apache.archiva.converter.artifact.LegacyRepositoryLayout;
  24. import org.apache.archiva.converter.legacy.LegacyRepositoryConverter;
  25. import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
  26. import org.apache.commons.io.FileUtils;
  27. import org.apache.maven.artifact.repository.ArtifactRepository;
  28. import org.apache.maven.artifact.repository.MavenArtifactRepository;
  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 javax.inject.Inject;
  35. import javax.inject.Named;
  36. import java.io.IOException;
  37. import java.nio.file.Files;
  38. import java.nio.file.Path;
  39. import java.nio.file.Paths;
  40. import java.util.ArrayList;
  41. import java.util.List;
  42. /**
  43. * Test the repository converter.
  44. *
  45. * @todo what about deletions from the source repository?
  46. * @todo use artifact-test instead
  47. * @todo should reject if dependencies are missing - rely on reporting?
  48. * @todo group metadata
  49. */
  50. @RunWith( ArchivaSpringJUnit4ClassRunner.class )
  51. @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml","classpath:/spring-context.xml"} )
  52. public class RepositoryConverterTest
  53. extends TestCase
  54. {
  55. private ArtifactRepository sourceRepository;
  56. private ManagedRepositoryConfiguration targetRepository;
  57. @Inject
  58. @Named(value = "legacyRepositoryConverter#default")
  59. private LegacyRepositoryConverter repositoryConverter;
  60. @Inject
  61. PlexusSisuBridge plexusSisuBridge;
  62. @Before
  63. @Override
  64. public void setUp()
  65. throws Exception
  66. {
  67. super.setUp();
  68. // ArtifactRepositoryFactory factory = plexusSisuBridge.lookup( ArtifactRepositoryFactory.class );
  69. //(ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
  70. ArtifactRepositoryLayout layout = new LegacyRepositoryLayout();
  71. //(ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "legacy" );
  72. Path sourceBase = Paths.get( "src/test/source-repository" );
  73. sourceRepository = new MavenArtifactRepository( "source", sourceBase.toUri().toURL().toString(), layout, null,
  74. null );
  75. layout = plexusSisuBridge.lookup( ArtifactRepositoryLayout.class, "default" );
  76. //(ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
  77. Path targetBase = Paths.get( "target/test-target-repository" );
  78. copyDirectoryStructure( Paths.get( "src/test/target-repository" ), targetBase );
  79. targetRepository = new ManagedRepositoryConfiguration();
  80. targetRepository.setId( "target" );
  81. targetRepository.setName( "Target Repo" );
  82. targetRepository.setLocation( targetBase.toAbsolutePath().toString() );
  83. targetRepository.setLayout( "default" );
  84. //repositoryConverter = (LegacyRepositoryConverter) lookup( LegacyRepositoryConverter.ROLE, "default" );
  85. }
  86. @Override
  87. protected void tearDown()
  88. throws Exception
  89. {
  90. super.tearDown();
  91. }
  92. private void copyDirectoryStructure( Path sourceDirectory, Path destinationDirectory )
  93. throws IOException
  94. {
  95. if ( !Files.exists(sourceDirectory) )
  96. {
  97. throw new IOException( "Source directory doesn't exists (" + sourceDirectory.toAbsolutePath() + ")." );
  98. }
  99. Path[] files = Files.list(sourceDirectory).toArray(Path[]::new);
  100. String sourcePath = sourceDirectory.toAbsolutePath().toString();
  101. for ( int i = 0; i < files.length; i++ )
  102. {
  103. Path file = files[i];
  104. String dest = file.toAbsolutePath().toString();
  105. dest = dest.substring( sourcePath.length() + 1 );
  106. Path destination = destinationDirectory.resolve( dest );
  107. if ( Files.isRegularFile(file) )
  108. {
  109. destination = destination.getParent();
  110. FileUtils.copyFileToDirectory( file.toFile(), destination.toFile() );
  111. }
  112. else if ( Files.isDirectory(file) )
  113. {
  114. if ( !".svn".equals( file.getFileName().toString() ) )
  115. {
  116. if ( !Files.exists(destination))
  117. {
  118. Files.createDirectories( destination );
  119. }
  120. copyDirectoryStructure( file, destination );
  121. }
  122. }
  123. else
  124. {
  125. throw new IOException( "Unknown file type: " + file.toAbsolutePath() );
  126. }
  127. }
  128. }
  129. @Test
  130. public void testLegacyConversion()
  131. throws IOException, RepositoryConversionException
  132. {
  133. Path legacyRepoDir = Paths.get( sourceRepository.getBasedir() );
  134. Path destRepoDir = Paths.get( targetRepository.getLocation() );
  135. List<String> excludes = new ArrayList<>();
  136. repositoryConverter.convertLegacyRepository( legacyRepoDir, destRepoDir, excludes );
  137. }
  138. }