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.

ArtifactMissingChecksumsConsumerTest.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package org.apache.archiva.consumers.core;
  2. import org.apache.archiva.checksum.ChecksumAlgorithm;
  3. import org.apache.archiva.checksum.ChecksummedFile;
  4. import org.apache.archiva.common.utils.PathUtil;
  5. import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
  6. import org.apache.archiva.repository.base.managed.BasicManagedRepository;
  7. import org.apache.archiva.repository.EditableManagedRepository;
  8. import org.apache.commons.io.FileUtils;
  9. import org.assertj.core.api.Assertions;
  10. import org.junit.Before;
  11. import org.junit.Test;
  12. import java.nio.file.Files;
  13. import java.nio.file.Path;
  14. import java.nio.file.Paths;
  15. import java.util.Arrays;
  16. import java.util.Calendar;
  17. /*
  18. * Licensed to the Apache Software Foundation (ASF) under one
  19. * or more contributor license agreements. See the NOTICE file
  20. * distributed with this work for additional information
  21. * regarding copyright ownership. The ASF licenses this file
  22. * to you under the Apache License, Version 2.0 (the
  23. * "License"); you may not use this file except in compliance
  24. * with the License. You may obtain a copy of the License at
  25. *
  26. * http://www.apache.org/licenses/LICENSE-2.0
  27. *
  28. * Unless required by applicable law or agreed to in writing,
  29. * software distributed under the License is distributed on an
  30. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  31. * KIND, either express or implied. See the License for the
  32. * specific language governing permissions and limitations
  33. * under the License.
  34. */
  35. public class ArtifactMissingChecksumsConsumerTest
  36. extends AbstractArtifactConsumerTest
  37. {
  38. private EditableManagedRepository repoConfig;
  39. @Before
  40. @Override
  41. public void setUp()
  42. throws Exception
  43. {
  44. super.setUp();
  45. Path basePath = Paths.get("target/test-classes");
  46. repoConfig = BasicManagedRepository.newFilesystemInstance( "test-repo", "Test Repository", basePath.resolve("test-repo"));
  47. repoConfig.setLayout( "default" );
  48. repoConfig.setLocation(basePath.resolve("test-repo/" ).toUri() );
  49. consumer = applicationContext.getBean( "knownRepositoryContentConsumer#create-missing-checksums",
  50. KnownRepositoryContentConsumer.class );
  51. }
  52. @Test
  53. public void testNoExistingChecksums()
  54. throws Exception
  55. {
  56. String path = "no-checksums-artifact/1.0/no-checksums-artifact-1.0.jar";
  57. Path basePath = PathUtil.getPathFromUri( repoConfig.getLocation() );
  58. Path sha1Path = basePath.resolve(path + ".sha1" );
  59. Path md5FilePath = basePath.resolve(path + ".md5" );
  60. Files.deleteIfExists( sha1Path );
  61. Files.deleteIfExists( md5FilePath );
  62. Assertions.assertThat( sha1Path.toFile() ).doesNotExist();
  63. Assertions.assertThat( md5FilePath.toFile() ).doesNotExist();
  64. consumer.beginScan( repoConfig, Calendar.getInstance().getTime() );
  65. consumer.processFile( path );
  66. Assertions.assertThat( sha1Path.toFile() ).exists();
  67. long sha1LastModified = sha1Path.toFile().lastModified();
  68. Assertions.assertThat( md5FilePath.toFile() ).exists();
  69. long md5LastModified = md5FilePath.toFile().lastModified();
  70. Thread.sleep( 1000 );
  71. consumer.processFile( path );
  72. Assertions.assertThat( sha1Path.toFile() ).exists();
  73. Assertions.assertThat( md5FilePath.toFile() ).exists();
  74. Assertions.assertThat( sha1Path.toFile().lastModified() ).isEqualTo( sha1LastModified );
  75. Assertions.assertThat( md5FilePath.toFile().lastModified() ).isEqualTo( md5LastModified );
  76. }
  77. @Test
  78. public void testExistingIncorrectChecksums()
  79. throws Exception
  80. {
  81. Path newLocation = Paths.get( "target/test-repo" );
  82. org.apache.archiva.common.utils.FileUtils.deleteDirectory( newLocation );
  83. FileUtils.copyDirectory( Paths.get(repoConfig.getLocation() ).toFile(), newLocation.toFile() );
  84. repoConfig.setLocation( newLocation.toAbsolutePath().toUri() );
  85. Path basePath = PathUtil.getPathFromUri( repoConfig.getLocation() );
  86. String path = "incorrect-checksums/1.0/incorrect-checksums-1.0.jar";
  87. Path sha1Path = basePath.resolve( path + ".sha1" );
  88. Path md5Path = basePath.resolve( path + ".md5" );
  89. ChecksummedFile checksum = new ChecksummedFile( basePath.resolve( path ) );
  90. Assertions.assertThat( sha1Path.toFile() ).exists();
  91. Assertions.assertThat( md5Path.toFile() ).exists();
  92. Assertions.assertThat(
  93. checksum.isValidChecksums( Arrays.asList(ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 ) ) ) //
  94. .isFalse();
  95. consumer.beginScan( repoConfig, Calendar.getInstance().getTime() );
  96. consumer.processFile( path );
  97. Assertions.assertThat( sha1Path.toFile() ).exists();
  98. Assertions.assertThat( md5Path.toFile() ).exists();
  99. Assertions.assertThat(
  100. checksum.isValidChecksums( Arrays.asList(ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 )) ) //
  101. .isTrue();
  102. }
  103. }