BAEL-5656 Constructing a Relative Path in Java from Two Absolute Paths or URLs (#12726)

This commit is contained in:
Mateusz Szablak 2022-09-14 07:06:21 +02:00 committed by GitHub
parent b836dce329
commit 7e63494eb4
7 changed files with 146 additions and 0 deletions

View File

@ -0,0 +1,5 @@
## Core Java IO APIs
This module contains articles about core Java input/output(IO) APIs.
### Relevant Articles:

View File

@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-io-apis-2</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-io-apis-2</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<!-- logging -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-io-apis-2</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>

View File

@ -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");
}
}

View File

@ -67,6 +67,7 @@
<module>core-java-io-3</module>
<module>core-java-io-4</module>
<module>core-java-io-apis</module>
<module>core-java-io-apis-2</module>
<module>core-java-io-conversions</module>
<module>core-java-jar</module>
<module>core-java-jndi</module>