|
| 1 | +package org.hjug.refactorfirst; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
| 6 | + |
| 7 | +import java.lang.reflect.Field; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Path; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.junit.jupiter.api.io.TempDir; |
| 12 | + |
| 13 | +class ReportCommandTest { |
| 14 | + |
| 15 | + @Test |
| 16 | + void call_returnsFailureWhenOutputPathContainsSymlink(@TempDir Path tempDir) throws Exception { |
| 17 | + Path target = Files.createDirectory(tempDir.resolve("target")); |
| 18 | + Path link = tempDir.resolve("link"); |
| 19 | + try { |
| 20 | + Files.createSymbolicLink(link, target); |
| 21 | + } catch (UnsupportedOperationException e) { |
| 22 | + assumeTrue(false, "Symbolic links not supported on this platform"); |
| 23 | + } |
| 24 | + |
| 25 | + ReportCommand command = new ReportCommand(); |
| 26 | + setField(command, "baseDir", tempDir.toFile()); |
| 27 | + setField(command, "outputDirectory", "link/reports"); |
| 28 | + setField(command, "reportType", ReportType.CSV); |
| 29 | + |
| 30 | + assertEquals(1, command.call()); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void call_returnsFailureWhenOutputPathTraversesOutsideBase(@TempDir Path tempDir) throws Exception { |
| 35 | + Path baseDirectory = Files.createDirectory(tempDir.resolve("project")); |
| 36 | + ReportCommand command = new ReportCommand(); |
| 37 | + setField(command, "baseDir", baseDirectory.toFile()); |
| 38 | + setField(command, "outputDirectory", "../escape"); |
| 39 | + setField(command, "reportType", ReportType.CSV); |
| 40 | + |
| 41 | + assertEquals(1, command.call()); |
| 42 | + assertFalse(Files.exists(tempDir.resolve("escape"))); |
| 43 | + } |
| 44 | + |
| 45 | + private static void setField(ReportCommand command, String name, Object value) throws ReflectiveOperationException { |
| 46 | + Field field = ReportCommand.class.getDeclaredField(name); |
| 47 | + field.setAccessible(true); |
| 48 | + field.set(command, value); |
| 49 | + } |
| 50 | +} |
0 commit comments