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.

FileUtilsTest.java 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (C) 2012-present the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.pf4j.util;
  17. import org.junit.jupiter.api.Test;
  18. import org.junit.jupiter.api.io.TempDir;
  19. import org.pf4j.test.PluginZip;
  20. import java.io.*;
  21. import java.nio.file.Files;
  22. import java.nio.file.Path;
  23. import java.nio.file.Paths;
  24. import java.util.List;
  25. import static org.junit.jupiter.api.Assertions.assertEquals;
  26. import static org.junit.jupiter.api.Assertions.assertTrue;
  27. public class FileUtilsTest {
  28. @TempDir
  29. Path pluginsPath;
  30. @Test
  31. public void expandIfZipForZipWithOnlyModuleDescriptor() throws Exception {
  32. PluginZip pluginZip = new PluginZip.Builder(pluginsPath.resolve("my-plugin-1.2.3.zip"), "myPlugin")
  33. .pluginVersion("1.2.3")
  34. .build();
  35. Path unzipped = FileUtils.expandIfZip(pluginZip.path());
  36. assertEquals(pluginZip.unzippedPath(), unzipped);
  37. assertTrue(Files.exists(unzipped.resolve("plugin.properties")));
  38. }
  39. @Test
  40. public void expandIfZipForZipWithResourceFile() throws Exception {
  41. PluginZip pluginZip = new PluginZip.Builder(pluginsPath.resolve("my-second-plugin-1.2.3.zip"), "myPlugin")
  42. .pluginVersion("1.2.3")
  43. .addFile(Paths.get("classes/META-INF/plugin-file"), "plugin")
  44. .build();
  45. Path unzipped = FileUtils.expandIfZip(pluginZip.path());
  46. assertEquals(pluginZip.unzippedPath(), unzipped);
  47. assertTrue(Files.exists(unzipped.resolve("classes/META-INF/plugin-file")));
  48. }
  49. @Test
  50. public void expandIfZipNonZipFiles() throws Exception {
  51. // File without .suffix
  52. Path extra = pluginsPath.resolve("extra");
  53. assertEquals(extra, FileUtils.expandIfZip(extra));
  54. // Folder
  55. Path folder = pluginsPath.resolve("folder");
  56. assertEquals(folder, FileUtils.expandIfZip(folder));
  57. }
  58. @Test
  59. public void readLinesIgnoreCommentTest() throws IOException {
  60. File file = createSampleFile("test");
  61. // ignoreComments = true
  62. List<String> ignoreCommentsLines = FileUtils.readLines(file.toPath(), true);
  63. assertEquals("1 content", ignoreCommentsLines.get(0));
  64. assertEquals(2, ignoreCommentsLines.size());
  65. // ignoreComments = false
  66. List<String> lines = FileUtils.readLines(file.toPath(), false);
  67. assertEquals("# 1 comment", lines.get(0));
  68. assertEquals(4, lines.size());
  69. file.deleteOnExit();
  70. }
  71. public File createSampleFile(String fileName) throws IOException {
  72. File file = File.createTempFile(fileName, ".txt");
  73. file.deleteOnExit();
  74. try (Writer writer = new OutputStreamWriter(Files.newOutputStream(file.toPath()))) {
  75. writer.write("# 1 comment\n");
  76. writer.write("1 content\n");
  77. writer.write("2 content\n");
  78. writer.write("# 2 comment\n");
  79. }
  80. return file;
  81. }
  82. }