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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.legacy.LegacyRepositoryConverter;
  24. import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
  25. import org.apache.commons.io.FileUtils;
  26. import org.apache.maven.artifact.repository.ArtifactRepository;
  27. import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
  28. import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
  29. import org.junit.Before;
  30. import org.junit.Test;
  31. import org.junit.runner.RunWith;
  32. import org.springframework.test.context.ContextConfiguration;
  33. import javax.inject.Inject;
  34. import javax.inject.Named;
  35. import java.io.IOException;
  36. import java.nio.file.Files;
  37. import java.nio.file.Path;
  38. import java.nio.file.Paths;
  39. import java.util.ArrayList;
  40. import java.util.List;
  41. /**
  42. * Test the repository converter.
  43. *
  44. * @todo what about deletions from the source repository?
  45. * @todo use artifact-test instead
  46. * @todo should reject if dependencies are missing - rely on reporting?
  47. * @todo group metadata
  48. */
  49. @RunWith( ArchivaSpringJUnit4ClassRunner.class )
  50. @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml","classpath:/spring-context.xml"} )
  51. public class RepositoryConverterTest
  52. extends TestCase
  53. {
  54. private ArtifactRepository sourceRepository;
  55. private ManagedRepositoryConfiguration targetRepository;
  56. @Inject
  57. @Named(value = "legacyRepositoryConverter#default")
  58. private LegacyRepositoryConverter repositoryConverter;
  59. @Inject
  60. PlexusSisuBridge plexusSisuBridge;
  61. @Before
  62. @Override
  63. public void setUp()
  64. throws Exception
  65. {
  66. super.setUp();
  67. ArtifactRepositoryFactory factory = plexusSisuBridge.lookup( ArtifactRepositoryFactory.class );
  68. //(ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
  69. ArtifactRepositoryLayout layout = plexusSisuBridge.lookup( ArtifactRepositoryLayout.class, "legacy" );
  70. //(ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "legacy" );
  71. Path sourceBase = Paths.get( "src/test/source-repository" );
  72. sourceRepository = factory.createArtifactRepository( "source", sourceBase.toUri().toURL().toString(), layout, null,
  73. null );
  74. layout = plexusSisuBridge.lookup( ArtifactRepositoryLayout.class, "default" );
  75. //(ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
  76. Path targetBase = Paths.get( "target/test-target-repository" );
  77. copyDirectoryStructure( Paths.get( "src/test/target-repository" ), targetBase );
  78. targetRepository = new ManagedRepositoryConfiguration();
  79. targetRepository.setId( "target" );
  80. targetRepository.setName( "Target Repo" );
  81. targetRepository.setLocation( targetBase.toAbsolutePath().toString() );
  82. targetRepository.setLayout( "default" );
  83. //repositoryConverter = (LegacyRepositoryConverter) lookup( LegacyRepositoryConverter.ROLE, "default" );
  84. }
  85. @Override
  86. protected void tearDown()
  87. throws Exception
  88. {
  89. super.tearDown();
  90. }
  91. private void copyDirectoryStructure( Path sourceDirectory, Path destinationDirectory )
  92. throws IOException
  93. {
  94. if ( !Files.exists(sourceDirectory) )
  95. {
  96. throw new IOException( "Source directory doesn't exists (" + sourceDirectory.toAbsolutePath() + ")." );
  97. }
  98. Path[] files = Files.list(sourceDirectory).toArray(Path[]::new);
  99. String sourcePath = sourceDirectory.toAbsolutePath().toString();
  100. for ( int i = 0; i < files.length; i++ )
  101. {
  102. Path file = files[i];
  103. String dest = file.toAbsolutePath().toString();
  104. dest = dest.substring( sourcePath.length() + 1 );
  105. Path destination = destinationDirectory.resolve( dest );
  106. if ( Files.isRegularFile(file) )
  107. {
  108. destination = destination.getParent();
  109. FileUtils.copyFileToDirectory( file.toFile(), destination.toFile() );
  110. }
  111. else if ( Files.isDirectory(file) )
  112. {
  113. if ( !".svn".equals( file.getFileName().toString() ) )
  114. {
  115. if ( !Files.exists(destination))
  116. {
  117. Files.createDirectories( destination );
  118. }
  119. copyDirectoryStructure( file, destination );
  120. }
  121. }
  122. else
  123. {
  124. throw new IOException( "Unknown file type: " + file.toAbsolutePath() );
  125. }
  126. }
  127. }
  128. @Test
  129. public void testLegacyConversion()
  130. throws IOException, RepositoryConversionException
  131. {
  132. Path legacyRepoDir = Paths.get( sourceRepository.getBasedir() );
  133. Path destRepoDir = Paths.get( targetRepository.getLocation() );
  134. List<String> excludes = new ArrayList<>();
  135. repositoryConverter.convertLegacyRepository( legacyRepoDir, destRepoDir, excludes );
  136. }
  137. }