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

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