From 25917125c727f0cb8938d0c9e07bcb05dabc482f Mon Sep 17 00:00:00 2001 From: tienvn4 Date: Tue, 13 Jun 2023 17:02:25 +0700 Subject: [PATCH 1/3] BAEL-6541-convert-relative-path --- .../convertpaths/RelativePathConverter.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 core-java-modules/core-java-20/src/main/java/com/baeldung/convertpaths/RelativePathConverter.java diff --git a/core-java-modules/core-java-20/src/main/java/com/baeldung/convertpaths/RelativePathConverter.java b/core-java-modules/core-java-20/src/main/java/com/baeldung/convertpaths/RelativePathConverter.java new file mode 100644 index 0000000000..8e166aa441 --- /dev/null +++ b/core-java-modules/core-java-20/src/main/java/com/baeldung/convertpaths/RelativePathConverter.java @@ -0,0 +1,30 @@ +package com.baeldung.convertpaths; + +import java.io.File; +import java.nio.file.FileSystems; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class RelativePathConverter { + + public static String convertToAbsoluteUsePathsClass(String relativePath) { + Path absolutePath = Paths.get(relativePath).toAbsolutePath(); + return absolutePath.toString(); + } + + public static String convertToAbsoluteUseFileClass(String relativePath) { + File file = new File(relativePath); + return file.getAbsolutePath(); + } + + public static String convertToAbsoluteUseFileSystemsClass(String relativePath) { + Path absolutePath = FileSystems.getDefault().getPath(relativePath).toAbsolutePath(); + return absolutePath.toString(); + } + + public static void main(String[] args) { + String relativePath = "myFolder/myFile.txt"; + String absolutePath = convertToAbsoluteUseFileSystemsClass(relativePath); + System.out.println("Absolute Path: " + absolutePath); + } +} From f9e48c05a29ce9f8f55f6624313f76a0f34fc7f9 Mon Sep 17 00:00:00 2001 From: tienvn Date: Sat, 8 Jul 2023 00:39:27 +0700 Subject: [PATCH 2/3] add unit test --- .../RelativePathConverterUnitTest.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 core-java-modules/core-java-20/src/test/java/com/baeldung/convertpaths/RelativePathConverterUnitTest.java diff --git a/core-java-modules/core-java-20/src/test/java/com/baeldung/convertpaths/RelativePathConverterUnitTest.java b/core-java-modules/core-java-20/src/test/java/com/baeldung/convertpaths/RelativePathConverterUnitTest.java new file mode 100644 index 0000000000..1573611697 --- /dev/null +++ b/core-java-modules/core-java-20/src/test/java/com/baeldung/convertpaths/RelativePathConverterUnitTest.java @@ -0,0 +1,46 @@ +package com.baeldung.convertpaths; + +import org.junit.Test; + +public class RelativePathConverterUnitTest { + + @Test + public void testConvertToAbsolutePath() { + String relativePath = "data/sample.txt"; + System.out.println(RelativePathConverter.convertToAbsoluteUsePathsClass(relativePath)); + System.out.println(RelativePathConverter.convertToAbsoluteUseFileClass(relativePath)); + System.out.println(RelativePathConverter.convertToAbsoluteUseFileSystemsClass(relativePath)); + } + + @Test + public void testConvertToAbsolutePath_withAbsolutePath() { + String absolutePath = "/var/www/index.html"; + System.out.println(RelativePathConverter.convertToAbsoluteUsePathsClass(absolutePath)); + System.out.println(RelativePathConverter.convertToAbsoluteUseFileClass(absolutePath)); + System.out.println(RelativePathConverter.convertToAbsoluteUseFileSystemsClass(absolutePath)); + } + + @Test + public void testConvertToAbsolutePath_withEmptyPath() { + String emptyPath = ""; + System.out.println(RelativePathConverter.convertToAbsoluteUsePathsClass(emptyPath)); + System.out.println(RelativePathConverter.convertToAbsoluteUseFileClass(emptyPath)); + System.out.println(RelativePathConverter.convertToAbsoluteUseFileSystemsClass(emptyPath)); + } + + @Test + public void testConvertToAbsolutePath_withParentDirectory() { + String relativePath = "../data/sample.txt"; + System.out.println(RelativePathConverter.convertToAbsoluteUsePathsClass(relativePath)); + System.out.println(RelativePathConverter.convertToAbsoluteUseFileClass(relativePath)); + System.out.println(RelativePathConverter.convertToAbsoluteUseFileSystemsClass(relativePath)); + } + + @Test + public void testConvertToAbsolutePath_withRelativePathContainingDots() { + String relativePath = "././data/sample.txt"; + System.out.println(RelativePathConverter.convertToAbsoluteUsePathsClass(relativePath)); + System.out.println(RelativePathConverter.convertToAbsoluteUseFileClass(relativePath)); + System.out.println(RelativePathConverter.convertToAbsoluteUseFileSystemsClass(relativePath)); + } +} From 201746172f7dca393e080f74de47f854e68a9457 Mon Sep 17 00:00:00 2001 From: tienvn Date: Mon, 10 Jul 2023 22:42:12 +0700 Subject: [PATCH 3/3] edit unit test --- .../RelativePathConverterUnitTest.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/core-java-modules/core-java-20/src/test/java/com/baeldung/convertpaths/RelativePathConverterUnitTest.java b/core-java-modules/core-java-20/src/test/java/com/baeldung/convertpaths/RelativePathConverterUnitTest.java index 1573611697..80f61d45c9 100644 --- a/core-java-modules/core-java-20/src/test/java/com/baeldung/convertpaths/RelativePathConverterUnitTest.java +++ b/core-java-modules/core-java-20/src/test/java/com/baeldung/convertpaths/RelativePathConverterUnitTest.java @@ -5,40 +5,45 @@ import org.junit.Test; public class RelativePathConverterUnitTest { @Test - public void testConvertToAbsolutePath() { + public void givenRelativePath_whenConvertingToAbsolutePath_thenPrintOutput() { String relativePath = "data/sample.txt"; + System.out.println(RelativePathConverter.convertToAbsoluteUsePathsClass(relativePath)); System.out.println(RelativePathConverter.convertToAbsoluteUseFileClass(relativePath)); System.out.println(RelativePathConverter.convertToAbsoluteUseFileSystemsClass(relativePath)); } @Test - public void testConvertToAbsolutePath_withAbsolutePath() { + public void givenAbsolutePath_whenConvertingToAbsolutePath_thenPrintOutput() { String absolutePath = "/var/www/index.html"; + System.out.println(RelativePathConverter.convertToAbsoluteUsePathsClass(absolutePath)); System.out.println(RelativePathConverter.convertToAbsoluteUseFileClass(absolutePath)); System.out.println(RelativePathConverter.convertToAbsoluteUseFileSystemsClass(absolutePath)); } @Test - public void testConvertToAbsolutePath_withEmptyPath() { + public void givenEmptyPath_whenConvertingToAbsolutePath_thenPrintOutput() { String emptyPath = ""; + System.out.println(RelativePathConverter.convertToAbsoluteUsePathsClass(emptyPath)); System.out.println(RelativePathConverter.convertToAbsoluteUseFileClass(emptyPath)); System.out.println(RelativePathConverter.convertToAbsoluteUseFileSystemsClass(emptyPath)); } @Test - public void testConvertToAbsolutePath_withParentDirectory() { + public void givenParentDirectoryPath_whenConvertingToAbsolutePath_thenPrintOutput() { String relativePath = "../data/sample.txt"; + System.out.println(RelativePathConverter.convertToAbsoluteUsePathsClass(relativePath)); System.out.println(RelativePathConverter.convertToAbsoluteUseFileClass(relativePath)); System.out.println(RelativePathConverter.convertToAbsoluteUseFileSystemsClass(relativePath)); } @Test - public void testConvertToAbsolutePath_withRelativePathContainingDots() { + public void givenRelativePathContainingDots_whenConvertingToAbsolutePath_thenPrintOutput() { String relativePath = "././data/sample.txt"; + System.out.println(RelativePathConverter.convertToAbsoluteUsePathsClass(relativePath)); System.out.println(RelativePathConverter.convertToAbsoluteUseFileClass(relativePath)); System.out.println(RelativePathConverter.convertToAbsoluteUseFileSystemsClass(relativePath));