这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core-java-modules/core-java-io-apis-2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Core Java IO APIs

This module contains articles about core Java input/output(IO) APIs.

### Relevant Articles:
47 changes: 47 additions & 0 deletions core-java-modules/core-java-io-apis-2/pom.xml
Original file line number Diff line number Diff line change
@@ -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>
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -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");
}

}
Empty file.
1 change: 1 addition & 0 deletions core-java-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down