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.

FilesystemAssetTest.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package org.apache.archiva.repository.storage;
  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 org.apache.archiva.common.filelock.DefaultFileLockManager;
  21. import org.apache.commons.io.FileUtils;
  22. import org.apache.commons.io.IOUtils;
  23. import org.junit.After;
  24. import org.junit.Assert;
  25. import org.junit.Before;
  26. import org.junit.Test;
  27. import java.io.IOException;
  28. import java.io.InputStream;
  29. import java.io.OutputStream;
  30. import java.nio.file.Files;
  31. import java.nio.file.Path;
  32. import java.nio.file.Paths;
  33. import java.time.Instant;
  34. public class FilesystemAssetTest {
  35. Path assetPathFile;
  36. Path assetPathDir;
  37. FilesystemStorage filesystemStorage;
  38. @Before
  39. public void init() throws IOException {
  40. assetPathDir = Files.createTempDirectory("assetDir");
  41. assetPathFile = Files.createTempFile(assetPathDir,"assetFile", "dat");
  42. filesystemStorage = new FilesystemStorage(assetPathDir, new DefaultFileLockManager());
  43. }
  44. @After
  45. public void cleanup() {
  46. try {
  47. Files.deleteIfExists(assetPathFile);
  48. } catch (IOException e) {
  49. e.printStackTrace();
  50. }
  51. FileUtils.deleteQuietly(assetPathDir.toFile());
  52. }
  53. @Test
  54. public void getPath() {
  55. FilesystemAsset asset = new FilesystemAsset(filesystemStorage, assetPathFile.getFileName().toString(), assetPathFile);
  56. Assert.assertEquals("/"+assetPathFile.getFileName().toString(), asset.getPath());
  57. }
  58. @Test
  59. public void getName() {
  60. FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/"+assetPathFile.getFileName().toString(), assetPathFile);
  61. Assert.assertEquals(assetPathFile.getFileName().toString(), asset.getName());
  62. }
  63. @Test
  64. public void getModificationTime() throws IOException {
  65. Instant modTime = Files.getLastModifiedTime(assetPathFile).toInstant();
  66. FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/test123", assetPathFile);
  67. Assert.assertTrue(modTime.equals(asset.getModificationTime()));
  68. }
  69. @Test
  70. public void isContainer() {
  71. FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/test1323", assetPathFile);
  72. Assert.assertFalse(asset.isContainer());
  73. FilesystemAsset asset2 = new FilesystemAsset(filesystemStorage, "/test1234", assetPathDir);
  74. Assert.assertTrue(asset2.isContainer());
  75. }
  76. @Test
  77. public void list() throws IOException {
  78. FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/test1234", assetPathFile);
  79. Assert.assertEquals(0, asset.list().size());
  80. FilesystemAsset asset2 = new FilesystemAsset(filesystemStorage, "/test1235", assetPathDir);
  81. Assert.assertEquals(1, asset2.list().size());
  82. Path f1 = Files.createTempFile(assetPathDir, "testfile", "dat");
  83. Path f2 = Files.createTempFile(assetPathDir, "testfile", "dat");
  84. Path d1 = Files.createTempDirectory(assetPathDir, "testdir");
  85. Assert.assertEquals(4, asset2.list().size());
  86. Assert.assertTrue(asset2.list().stream().anyMatch(p -> p.getName().equals(f1.getFileName().toString())));
  87. Assert.assertTrue(asset2.list().stream().anyMatch(p -> p.getName().equals(f2.getFileName().toString())));
  88. Assert.assertTrue(asset2.list().stream().anyMatch(p -> p.getName().equals(d1.getFileName().toString())));
  89. Files.deleteIfExists(f1);
  90. Files.deleteIfExists(f2);
  91. Files.deleteIfExists(d1);
  92. }
  93. @Test
  94. public void getSize() throws IOException {
  95. FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/test1234", assetPathFile);
  96. Assert.assertEquals(0, asset.getSize());
  97. Files.write(assetPathFile, new String("abcdef").getBytes("ASCII"));
  98. Assert.assertTrue(asset.getSize()>=6);
  99. }
  100. @Test
  101. public void getData() throws IOException {
  102. FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/test1234", assetPathFile);
  103. Files.write(assetPathFile, "abcdef".getBytes("ASCII"));
  104. try(InputStream is = asset.getReadStream()) {
  105. Assert.assertEquals("abcdef", IOUtils.toString(is, "ASCII"));
  106. }
  107. }
  108. @Test
  109. public void getDataExceptionOnDir() throws IOException {
  110. FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/test1234", assetPathDir);
  111. Files.write(assetPathFile, "abcdef".getBytes("ASCII"));
  112. try {
  113. InputStream is = asset.getReadStream();
  114. Assert.assertFalse("Exception expected for data on dir", true);
  115. } catch (IOException e) {
  116. // fine
  117. }
  118. }
  119. @Test
  120. public void writeData() throws IOException {
  121. FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/test1234", assetPathFile);
  122. Files.write(assetPathFile, "abcdef".getBytes("ASCII"));
  123. try(OutputStream os = asset.getWriteStream(true)) {
  124. IOUtils.write("test12345", os, "ASCII");
  125. }
  126. Assert.assertEquals("test12345", IOUtils.toString(assetPathFile.toUri().toURL(), "ASCII"));
  127. }
  128. @Test
  129. public void writeDataAppend() throws IOException {
  130. FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/test1234", assetPathFile);
  131. Files.write(assetPathFile, "abcdef".getBytes("ASCII"));
  132. try(OutputStream os = asset.getWriteStream(false)) {
  133. IOUtils.write("test12345", os, "ASCII");
  134. }
  135. Assert.assertEquals("abcdeftest12345", IOUtils.toString(assetPathFile.toUri().toURL(), "ASCII"));
  136. }
  137. @Test
  138. public void writeDataExceptionOnDir() throws IOException {
  139. FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/test1234", assetPathDir);
  140. try {
  141. OutputStream os = asset.getWriteStream(true);
  142. Assert.assertTrue("Writing to a directory should throw a IOException", false);
  143. } catch (IOException e) {
  144. // Fine
  145. }
  146. }
  147. @Test
  148. public void storeDataFile() throws IOException {
  149. FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/test1234", assetPathFile);
  150. Path dataFile = Files.createTempFile("testdata", "dat");
  151. try(OutputStream os = Files.newOutputStream(dataFile)) {
  152. IOUtils.write("testkdkdkd", os, "ASCII");
  153. }
  154. asset.replaceDataFromFile(dataFile);
  155. Assert.assertEquals("testkdkdkd", IOUtils.toString(assetPathFile.toUri().toURL(), "ASCII"));
  156. }
  157. @Test
  158. public void exists() {
  159. FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/test1234", assetPathFile);
  160. Assert.assertTrue(asset.exists());
  161. FilesystemAsset asset2 = new FilesystemAsset(filesystemStorage, "/test1234", Paths.get("abcdefgkdkdk"));
  162. Assert.assertFalse(asset2.exists());
  163. }
  164. @Test
  165. public void getFilePath() {
  166. FilesystemAsset asset = new FilesystemAsset(filesystemStorage, "/test1234", assetPathFile);
  167. Assert.assertEquals(assetPathFile, asset.getFilePath());
  168. }
  169. }