diff --git a/core-java-modules/core-java-io-apis-2/README.md b/core-java-modules/core-java-io-apis-2/README.md new file mode 100644 index 000000000000..c08c112d7773 --- /dev/null +++ b/core-java-modules/core-java-io-apis-2/README.md @@ -0,0 +1,5 @@ +## Core Java IO APIs + +This module contains articles about core Java input/output(IO) APIs. + +### Relevant Articles: \ No newline at end of file diff --git a/core-java-modules/core-java-io-apis-2/pom.xml b/core-java-modules/core-java-io-apis-2/pom.xml new file mode 100644 index 000000000000..70c3a87da296 --- /dev/null +++ b/core-java-modules/core-java-io-apis-2/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + core-java-io-apis-2 + 0.1.0-SNAPSHOT + core-java-io-apis-2 + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + + + + + log4j + log4j + ${log4j.version} + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + + + org.projectlombok + lombok + ${lombok.version} + provided + + + + + core-java-io-apis-2 + + + src/main/resources + true + + + + + \ No newline at end of file diff --git a/core-java-modules/core-java-io-apis-2/src/main/java/.gitkeep b/core-java-modules/core-java-io-apis-2/src/main/java/.gitkeep new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/core-java-modules/core-java-io-apis-2/src/main/resources/.gitkeep b/core-java-modules/core-java-io-apis-2/src/main/resources/.gitkeep new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/absolutetorelative/AbsoluteToRelativeUnitTest.java b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/absolutetorelative/AbsoluteToRelativeUnitTest.java new file mode 100644 index 000000000000..0830b0808a82 --- /dev/null +++ b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/absolutetorelative/AbsoluteToRelativeUnitTest.java @@ -0,0 +1,93 @@ +package com.baeldung.absolutetorelative; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.net.URI; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class AbsoluteToRelativeUnitTest { + + // given - until using Paths, no need to create physical files + private final Path pathOne = Paths.get("/baeldung/bar/one.txt"); + private final Path pathTwo = Paths.get("/baeldung/bar/two.txt"); + private final Path pathThree = Paths.get("/baeldung/foo/three.txt"); + + private final URI uriOne = pathOne.toUri(); + private final URI uriTwo = pathTwo.toUri(); + private final URI uriThree = pathThree.toUri(); + + @Test + public void givenAbsolutePaths_whenRelativizePathOneToPathTwo_thenRelativeIsReturned() { + Path result = pathOne.relativize(pathTwo); + + Assertions.assertThat(result) + .isRelative() + .isEqualTo(Paths.get("../two.txt")); + } + + @Test + public void givenAbsolutePaths_whenRelativizePathTwoToPathOne_thenRelativeIsReturned() { + Path result = pathTwo.relativize(pathOne); + + Assertions.assertThat(result) + .isRelative() + .isEqualTo(Paths.get("../one.txt")); + } + + @Test + public void givenAbsolutePaths_whenRelativizePathOneParentToPathTwo_thenRelativeIsReturned() { + Path result = pathOne.getParent().relativize(pathTwo); + + Assertions.assertThat(result) + .isRelative() + .isEqualTo(Paths.get("two.txt")); + } + + @Test + public void givenAbsolutePaths_whenRelativizePathOneToPathThree_thenRelativeIsReturned() { + Path result = pathOne.relativize(pathThree); + + Assertions.assertThat(result) + .isRelative() + .isEqualTo(Paths.get("../../foo/three.txt")); + } + + @Test + public void givenAbsolutePaths_whenRelativizePathThreeToPathOne_thenRelativeIsReturned() { + Path result = pathThree.relativize(pathOne); + + Assertions.assertThat(result) + .isRelative() + .isEqualTo(Paths.get("../../bar/one.txt")); + } + + @Test + public void givenAbsoluteURIs_whenRelativizeUriOneToUriTwo_thenAbsoluteIsReturned() { + URI result = uriOne.relativize(uriTwo); + + Assertions.assertThat(result) + .asString() + .contains("/baeldung/bar/two.txt"); + } + + @Test + public void givenAbsoluteURIs_whenRelativizeUriOneParentToUriTwo_thenRelativeIsReturned() { + URI result = pathOne.getParent().toUri().relativize(uriTwo); + + Assertions.assertThat(result) + .asString() + .contains("two.txt"); + } + + @Test + public void givenAbsoluteURIs_whenRelativizeUriOneParentToUriThree_thenAbsoluteIsReturned() { + URI result = pathOne.getParent().toUri().relativize(uriThree); + + Assertions.assertThat(result) + .asString() + .contains("/baeldung/foo/three.txt"); + } + +} diff --git a/core-java-modules/core-java-io-apis-2/src/test/resources/.gitkeep b/core-java-modules/core-java-io-apis-2/src/test/resources/.gitkeep new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index 22a7fdec1e4d..fdd502095d7a 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -67,6 +67,7 @@ core-java-io-3 core-java-io-4 core-java-io-apis + core-java-io-apis-2 core-java-io-conversions core-java-jar core-java-jndi