|
| 1 | +package com.nordstrom.common.file; |
| 2 | + |
| 3 | +import static org.testng.Assert.assertEquals; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.io.IOException; |
| 7 | +import java.lang.reflect.Constructor; |
| 8 | +import java.lang.reflect.InvocationTargetException; |
| 9 | +import java.lang.reflect.Modifier; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.Path; |
| 12 | +import java.nio.file.Paths; |
| 13 | + |
| 14 | +import org.testng.ITestContext; |
| 15 | +import org.testng.ITestResult; |
| 16 | +import org.testng.Reporter; |
| 17 | +import org.testng.annotations.Test; |
| 18 | + |
| 19 | +public class PathUtilsTest { |
| 20 | + |
| 21 | + @Test |
| 22 | + public void testNextPath() throws IOException { |
| 23 | + Path outputDir = getOutputPath(); |
| 24 | + Path targetPath = outputDir.resolve("targetPath"); |
| 25 | + if (targetPath.toFile().exists()) { |
| 26 | + for (File file : targetPath.toFile().listFiles()) { |
| 27 | + file.delete(); |
| 28 | + } |
| 29 | + } else { |
| 30 | + Files.createDirectories(targetPath); |
| 31 | + } |
| 32 | + |
| 33 | + Path path1 = PathUtils.getNextPath(targetPath, "test", "txt"); |
| 34 | + assertEquals(path1.getFileName().toString(), "test.txt"); |
| 35 | + |
| 36 | + path1.toFile().createNewFile(); |
| 37 | + Path path2 = PathUtils.getNextPath(targetPath, "test", "txt"); |
| 38 | + assertEquals(path2.getFileName().toString(), "test-2.txt"); |
| 39 | + } |
| 40 | + |
| 41 | + private Path getOutputPath() { |
| 42 | + ITestResult testResult = Reporter.getCurrentTestResult(); |
| 43 | + ITestContext testContext = testResult.getTestContext(); |
| 44 | + String outputDirectory = testContext.getOutputDirectory(); |
| 45 | + Path outputDir = Paths.get(outputDirectory); |
| 46 | + return outputDir; |
| 47 | + } |
| 48 | + |
| 49 | + @Test(expectedExceptions = {AssertionError.class}, |
| 50 | + expectedExceptionsMessageRegExp = "PathUtils is a static utility class that cannot be instantiated") |
| 51 | + public void testPrivateConstructor() throws Throwable { |
| 52 | + |
| 53 | + Constructor<?>[] ctors; |
| 54 | + ctors = PathUtils.class.getDeclaredConstructors(); |
| 55 | + assertEquals(ctors.length, 1, "PathUtils must have exactly one constructor"); |
| 56 | + assertEquals(ctors[0].getModifiers() & Modifier.PRIVATE, Modifier.PRIVATE, |
| 57 | + "PathUtils constructor must be private"); |
| 58 | + assertEquals(ctors[0].getParameterTypes().length, 0, "PathUtils constructor must have no arguments"); |
| 59 | + |
| 60 | + try { |
| 61 | + ctors[0].setAccessible(true); |
| 62 | + ctors[0].newInstance(); |
| 63 | + } catch (InvocationTargetException e) { |
| 64 | + throw e.getCause(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Test(expectedExceptions = {NullPointerException.class}) |
| 69 | + public void testNullPath() throws IOException { |
| 70 | + PathUtils.getNextPath(null, "test", "txt"); |
| 71 | + } |
| 72 | + |
| 73 | + @Test(expectedExceptions = {IllegalArgumentException.class}) |
| 74 | + public void testNonExistentPath() throws IOException { |
| 75 | + PathUtils.getNextPath(Paths.get("foobar"), "test", "txt"); |
| 76 | + } |
| 77 | + |
| 78 | + @Test(expectedExceptions = {NullPointerException.class}) |
| 79 | + public void testNullBaseName() throws IOException { |
| 80 | + PathUtils.getNextPath(getOutputPath(), null, "txt"); |
| 81 | + } |
| 82 | + |
| 83 | + @Test(expectedExceptions = {IllegalArgumentException.class}) |
| 84 | + public void testEmptyBaseName() throws IOException { |
| 85 | + PathUtils.getNextPath(getOutputPath(), "", "txt"); |
| 86 | + } |
| 87 | + |
| 88 | + @Test(expectedExceptions = {NullPointerException.class}) |
| 89 | + public void testNullExtenstion() throws IOException { |
| 90 | + PathUtils.getNextPath(getOutputPath(), "test", null); |
| 91 | + } |
| 92 | + |
| 93 | + @Test(expectedExceptions = {IllegalArgumentException.class}) |
| 94 | + public void testEmptyExtension() throws IOException { |
| 95 | + PathUtils.getNextPath(getOutputPath(), "test", ""); |
| 96 | + } |
| 97 | +} |
0 commit comments