Add files via upload (#13978)
This commit is contained in:
parent
4cc0380249
commit
53d269ca68
|
@ -1,46 +1,115 @@
|
|||
<?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>
|
||||
<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>
|
||||
|
||||
<?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>
|
||||
<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>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>5.7.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
</dependency>
|
||||
<!-- this is all you need to write tests with JUnit Jupiter -->
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- this dependency is needed to create parameterized tests -->
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-params</artifactId>
|
||||
<version>${junit-jupiter-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- contains the engine that actually runs the Jupiter-tests -->
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testng</groupId>
|
||||
<artifactId>testng</artifactId>
|
||||
<version>7.1.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testng</groupId>
|
||||
<artifactId>testng</artifactId>
|
||||
<version>7.5</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<properties>
|
||||
<junit-jupiter-version>5.2.0</junit-jupiter-version>
|
||||
</properties>
|
||||
<build>
|
||||
<finalName>core-java-io-apis-2</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.multinput;
|
||||
|
||||
import java.util.InputMismatchException;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class MultiInputs {
|
||||
public void UsingSpaceDelimiter(){
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
System.out.print("Enter two numbers: ");
|
||||
int num1 = scanner.nextInt();
|
||||
int num2 = scanner.nextInt();
|
||||
System.out.println("You entered " + num1 + " and " + num2);
|
||||
|
||||
}
|
||||
public void UsingREDelimiter(){
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
scanner.useDelimiter("[\\s,]+");
|
||||
System.out.print("Enter two numbers separated by a space or a comma: ");
|
||||
int num1 = scanner.nextInt();
|
||||
int num2 = scanner.nextInt();
|
||||
System.out.println("You entered " + num1 + " and " + num2);
|
||||
|
||||
}
|
||||
public void UsingCustomDelimiter(){
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
scanner.useDelimiter(";");
|
||||
System.out.print("Enter two numbers separated by a semicolon: ");
|
||||
try { int num1 = scanner.nextInt();
|
||||
int num2 = scanner.nextInt();
|
||||
System.out.println("You entered " + num1 + " and " + num2); }
|
||||
catch (InputMismatchException e)
|
||||
{ System.out.println("Invalid input. Please enter two integers separated by a semicolon."); }
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -1,93 +1,92 @@
|
|||
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");
|
||||
}
|
||||
|
||||
}
|
||||
package com.baeldung.absolutetorelative;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
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);
|
||||
|
||||
org.assertj.core.api.Assertions.assertThat(result)
|
||||
.isRelative()
|
||||
.isEqualTo(Paths.get("../two.txt"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAbsolutePaths_whenRelativizePathTwoToPathOne_thenRelativeIsReturned() {
|
||||
Path result = pathTwo.relativize(pathOne);
|
||||
|
||||
org.assertj.core.api.Assertions.assertThat(result)
|
||||
.isRelative()
|
||||
.isEqualTo(Paths.get("../one.txt"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAbsolutePaths_whenRelativizePathOneParentToPathTwo_thenRelativeIsReturned() {
|
||||
Path result = pathOne.getParent().relativize(pathTwo);
|
||||
|
||||
org.assertj.core.api.Assertions.assertThat(result)
|
||||
.isRelative()
|
||||
.isEqualTo(Paths.get("two.txt"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAbsolutePaths_whenRelativizePathOneToPathThree_thenRelativeIsReturned() {
|
||||
Path result = pathOne.relativize(pathThree);
|
||||
|
||||
org.assertj.core.api.Assertions.assertThat(result)
|
||||
.isRelative()
|
||||
.isEqualTo(Paths.get("../../foo/three.txt"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAbsolutePaths_whenRelativizePathThreeToPathOne_thenRelativeIsReturned() {
|
||||
Path result = pathThree.relativize(pathOne);
|
||||
|
||||
org.assertj.core.api.Assertions.assertThat(result)
|
||||
.isRelative()
|
||||
.isEqualTo(Paths.get("../../bar/one.txt"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAbsoluteURIs_whenRelativizeUriOneToUriTwo_thenAbsoluteIsReturned() {
|
||||
URI result = uriOne.relativize(uriTwo);
|
||||
|
||||
org.assertj.core.api.Assertions.assertThat(result)
|
||||
.asString()
|
||||
.contains("/baeldung/bar/two.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAbsoluteURIs_whenRelativizeUriOneParentToUriTwo_thenRelativeIsReturned() {
|
||||
URI result = pathOne.getParent().toUri().relativize(uriTwo);
|
||||
|
||||
org.assertj.core.api.Assertions.assertThat(result)
|
||||
.asString()
|
||||
.contains("two.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAbsoluteURIs_whenRelativizeUriOneParentToUriThree_thenAbsoluteIsReturned() {
|
||||
URI result = pathOne.getParent().toUri().relativize(uriThree);
|
||||
|
||||
org.assertj.core.api.Assertions.assertThat(result)
|
||||
.asString()
|
||||
.contains("/baeldung/foo/three.txt");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,31 +1,31 @@
|
|||
package com.baeldung.bufferedreadervsfilereader;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public class BufferedReaderUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenReadingAFile_thenReadsLineByLine() {
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader("src/test/resources/sampleText1.txt"))) {
|
||||
String line;
|
||||
|
||||
while((line = br.readLine()) != null) {
|
||||
result.append(line);
|
||||
result.append('\n');
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
assertEquals("first line\nsecond line\nthird line\n", result.toString());
|
||||
}
|
||||
|
||||
}
|
||||
package com.baeldung.bufferedreadervsfilereader;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.testng.annotations.Test;
|
||||
public class BufferedReaderUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenReadingAFile_thenReadsLineByLine() {
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader("src/test/resources/sampleText1.txt"))) {
|
||||
String line;
|
||||
|
||||
while((line = br.readLine()) != null) {
|
||||
result.append(line);
|
||||
result.append('\n');
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
assertEquals("first line\nsecond line\nthird line\n", result.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,30 +1,30 @@
|
|||
package com.baeldung.bufferedreadervsfilereader;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class FileReaderUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenReadingAFile_thenReadsCharByChar() {
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
try (FileReader fr = new FileReader("src/test/resources/sampleText2.txt")) {
|
||||
int i = fr.read();
|
||||
|
||||
while(i != -1) {
|
||||
result.append((char)i);
|
||||
|
||||
i = fr.read();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
assertEquals("qwerty", result.toString());
|
||||
}
|
||||
}
|
||||
package com.baeldung.bufferedreadervsfilereader;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class FileReaderUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenReadingAFile_thenReadsCharByChar() {
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
try (FileReader fr = new FileReader("src/test/resources/sampleText2.txt")) {
|
||||
int i = fr.read();
|
||||
|
||||
while(i != -1) {
|
||||
result.append((char)i);
|
||||
|
||||
i = fr.read();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
assertEquals("qwerty", result.toString());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
package com.baeldung.multinput;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.InputMismatchException;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.testng.annotations.Test;
|
||||
import com.baeldung.multinput.MultiInputs;
|
||||
public class TestMultipleInputsUnitTest {
|
||||
@Test
|
||||
public void givenMultipleInputs_whenUsingSpaceDelimiter_thenExpectPrintingOutputs() {
|
||||
String input = "10 20\n";
|
||||
InputStream in = new ByteArrayInputStream(input.getBytes());
|
||||
System.setIn(in);
|
||||
MultiInputs mi = new MultiInputs();
|
||||
mi.UsingSpaceDelimiter();
|
||||
// You can add assertions here to verify the behavior of the method
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultipleInputs_whenUsingREDelimiter_thenExpectPrintingOutputs() {
|
||||
String input = "30, 40\n";
|
||||
InputStream in = new ByteArrayInputStream(input.getBytes());
|
||||
System.setIn(in);
|
||||
MultiInputs mi = new MultiInputs();
|
||||
mi.UsingREDelimiter();
|
||||
// You can add assertions here to verify the behavior of the method
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultipleInputs_whenUsingCustomDelimiter_thenExpectPrintingOutputs() {
|
||||
String input = "50; 60\n";
|
||||
InputStream in = new ByteArrayInputStream(input.getBytes());
|
||||
System.setIn(in);
|
||||
MultiInputs mi = new MultiInputs();
|
||||
mi.UsingCustomDelimiter();
|
||||
// You can add assertions here to verify the behavior of the method
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInput_whenUsingSpaceDelimiter_thenExpectInputMismatchException() {
|
||||
String input = "abc\n";
|
||||
InputStream in = new ByteArrayInputStream(input.getBytes());
|
||||
System.setIn(in);
|
||||
MultiInputs mi = new MultiInputs();
|
||||
Assertions.assertThrows(InputMismatchException.class, mi::UsingSpaceDelimiter);
|
||||
}
|
||||
}
|
|
@ -1,29 +1,27 @@
|
|||
package com.baeldung.path;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import javax.swing.filechooser.FileSystemView;
|
||||
|
||||
public class DesktopPathUnitTest {
|
||||
// Adapt DESKTOP_PATH variable to your own system path
|
||||
// private static final String DESKTOP_PATH = "C:\\Users\\HRAF\\Desktop";
|
||||
|
||||
@Test
|
||||
public void whenUsingGetUserHomeProperty_thenShouldEqualDesktopPath() {
|
||||
String desktopPath = System.getProperty("user.home") + File.separator + "Desktop";
|
||||
// assertEquals(DESKTOP_PATH, desktopPath);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingFileSystemViewGetHomeDirectory_thenShouldEqualDesktopPath() {
|
||||
FileSystemView view = FileSystemView.getFileSystemView();
|
||||
File file = view.getHomeDirectory();
|
||||
String path = file.getPath();
|
||||
// assertEquals(DESKTOP_PATH, path);
|
||||
}
|
||||
|
||||
}
|
||||
package com.baeldung.path;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.swing.filechooser.FileSystemView;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class DesktopPathUnitTest {
|
||||
// Adapt DESKTOP_PATH variable to your own system path
|
||||
// private static final String DESKTOP_PATH = "C:\\Users\\HRAF\\Desktop";
|
||||
|
||||
@Test
|
||||
public void whenUsingGetUserHomeProperty_thenShouldEqualDesktopPath() {
|
||||
String desktopPath = System.getProperty("user.home") + File.separator + "Desktop";
|
||||
// assertEquals(DESKTOP_PATH, desktopPath);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingFileSystemViewGetHomeDirectory_thenShouldEqualDesktopPath() {
|
||||
FileSystemView view = FileSystemView.getFileSystemView();
|
||||
File file = view.getHomeDirectory();
|
||||
String path = file.getPath();
|
||||
// assertEquals(DESKTOP_PATH, path);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,85 +1,85 @@
|
|||
package com.baeldung.scanner;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.InputMismatchException;
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class NextLineVsNextIntUnitTest {
|
||||
|
||||
@Test
|
||||
void whenInputLineIsNumber_thenNextLineAndNextIntBothWork() {
|
||||
String input = "42\n";
|
||||
|
||||
//nextLine()
|
||||
Scanner sc1 = new Scanner(input);
|
||||
int num1 = Integer.parseInt(sc1.nextLine());
|
||||
assertEquals(42, num1);
|
||||
|
||||
//nextInt()
|
||||
Scanner sc2 = new Scanner(input);
|
||||
int num2 = sc2.nextInt();
|
||||
assertEquals(42, num2);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenInputIsNotValidNumber_thenNextLineAndNextIntThrowDifferentException() {
|
||||
String input = "Nan\n";
|
||||
|
||||
//nextLine() -> NumberFormatException
|
||||
Scanner sc1 = new Scanner(input);
|
||||
assertThrows(NumberFormatException.class, () -> Integer.parseInt(sc1.nextLine()));
|
||||
|
||||
//nextInt() -> InputMismatchException
|
||||
Scanner sc2 = new Scanner(input);
|
||||
assertThrows(InputMismatchException.class, sc2::nextInt);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingNextInt_thenTheNextTokenAfterItFailsToParseIsNotConsumed() {
|
||||
String input = "42 is a magic number\n";
|
||||
|
||||
//nextInt() to read '42'
|
||||
Scanner sc2 = new Scanner(input);
|
||||
int num2 = sc2.nextInt();
|
||||
assertEquals(42, num2);
|
||||
|
||||
// call nextInt() again on "is"
|
||||
assertThrows(InputMismatchException.class, sc2::nextInt);
|
||||
|
||||
String theNextToken = sc2.next();
|
||||
assertEquals("is", theNextToken);
|
||||
|
||||
theNextToken = sc2.next();
|
||||
assertEquals("a", theNextToken);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenReadingTwoInputLines_thenNextLineAndNextIntBehaveDifferently() {
|
||||
|
||||
String input = new StringBuilder().append("42\n")
|
||||
.append("It is a magic number.\n")
|
||||
.toString();
|
||||
|
||||
//nextLine()
|
||||
Scanner sc1 = new Scanner(input);
|
||||
int num1 = Integer.parseInt(sc1.nextLine());
|
||||
String nextLineText1 = sc1.nextLine();
|
||||
assertEquals(42, num1);
|
||||
assertEquals("It is a magic number.", nextLineText1);
|
||||
|
||||
//nextInt()
|
||||
Scanner sc2 = new Scanner(input);
|
||||
int num2 = sc2.nextInt();
|
||||
assertEquals(42, num2);
|
||||
|
||||
// nextInt() leaves the newline character (\n) behind
|
||||
String nextLineText2 = sc2.nextLine();
|
||||
assertEquals("", nextLineText2);
|
||||
}
|
||||
|
||||
package com.baeldung.scanner;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.InputMismatchException;
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class NextLineVsNextIntUnitTest {
|
||||
|
||||
@Test
|
||||
void whenInputLineIsNumber_thenNextLineAndNextIntBothWork() {
|
||||
String input = "42\n";
|
||||
|
||||
//nextLine()
|
||||
Scanner sc1 = new Scanner(input);
|
||||
int num1 = Integer.parseInt(sc1.nextLine());
|
||||
assertEquals(42, num1);
|
||||
|
||||
//nextInt()
|
||||
Scanner sc2 = new Scanner(input);
|
||||
int num2 = sc2.nextInt();
|
||||
assertEquals(42, num2);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenInputIsNotValidNumber_thenNextLineAndNextIntThrowDifferentException() {
|
||||
String input = "Nan\n";
|
||||
|
||||
//nextLine() -> NumberFormatException
|
||||
Scanner sc1 = new Scanner(input);
|
||||
assertThrows(NumberFormatException.class, () -> Integer.parseInt(sc1.nextLine()));
|
||||
|
||||
//nextInt() -> InputMismatchException
|
||||
Scanner sc2 = new Scanner(input);
|
||||
assertThrows(InputMismatchException.class, sc2::nextInt);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingNextInt_thenTheNextTokenAfterItFailsToParseIsNotConsumed() {
|
||||
String input = "42 is a magic number\n";
|
||||
|
||||
//nextInt() to read '42'
|
||||
Scanner sc2 = new Scanner(input);
|
||||
int num2 = sc2.nextInt();
|
||||
assertEquals(42, num2);
|
||||
|
||||
// call nextInt() again on "is"
|
||||
assertThrows(InputMismatchException.class, sc2::nextInt);
|
||||
|
||||
String theNextToken = sc2.next();
|
||||
assertEquals("is", theNextToken);
|
||||
|
||||
theNextToken = sc2.next();
|
||||
assertEquals("a", theNextToken);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenReadingTwoInputLines_thenNextLineAndNextIntBehaveDifferently() {
|
||||
|
||||
String input = new StringBuilder().append("42\n")
|
||||
.append("It is a magic number.\n")
|
||||
.toString();
|
||||
|
||||
//nextLine()
|
||||
Scanner sc1 = new Scanner(input);
|
||||
int num1 = Integer.parseInt(sc1.nextLine());
|
||||
String nextLineText1 = sc1.nextLine();
|
||||
assertEquals(42, num1);
|
||||
assertEquals("It is a magic number.", nextLineText1);
|
||||
|
||||
//nextInt()
|
||||
Scanner sc2 = new Scanner(input);
|
||||
int num2 = sc2.nextInt();
|
||||
assertEquals(42, num2);
|
||||
|
||||
// nextInt() leaves the newline character (\n) behind
|
||||
String nextLineText2 = sc2.nextLine();
|
||||
assertEquals("", nextLineText2);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,38 +1,38 @@
|
|||
package com.baeldung.scanner;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ScanACharacterUnitTest {
|
||||
|
||||
// given - input scanner source, no need to scan from console
|
||||
String input = new StringBuilder().append("abc\n")
|
||||
.append("mno\n")
|
||||
.append("xyz\n")
|
||||
.toString();
|
||||
|
||||
@Test
|
||||
public void givenInputSource_whenScanCharUsingNext_thenOneCharIsRead() {
|
||||
Scanner sc = new Scanner(input);
|
||||
char c = sc.next().charAt(0);
|
||||
assertEquals('a', c);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInputSource_whenScanCharUsingFindInLine_thenOneCharIsRead() {
|
||||
Scanner sc = new Scanner(input);
|
||||
char c = sc.findInLine(".").charAt(0);
|
||||
assertEquals('a', c);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInputSource_whenScanCharUsingUseDelimiter_thenOneCharIsRead() {
|
||||
Scanner sc = new Scanner(input);
|
||||
char c = sc.useDelimiter("").next().charAt(0);
|
||||
assertEquals('a', c);
|
||||
}
|
||||
|
||||
}
|
||||
package com.baeldung.scanner;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ScanACharacterUnitTest {
|
||||
|
||||
// given - input scanner source, no need to scan from console
|
||||
String input = new StringBuilder().append("abc\n")
|
||||
.append("mno\n")
|
||||
.append("xyz\n")
|
||||
.toString();
|
||||
|
||||
@Test
|
||||
public void givenInputSource_whenScanCharUsingNext_thenOneCharIsRead() {
|
||||
Scanner sc = new Scanner(input);
|
||||
char c = sc.next().charAt(0);
|
||||
assertEquals('a', c);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInputSource_whenScanCharUsingFindInLine_thenOneCharIsRead() {
|
||||
Scanner sc = new Scanner(input);
|
||||
char c = sc.findInLine(".").charAt(0);
|
||||
assertEquals('a', c);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInputSource_whenScanCharUsingUseDelimiter_thenOneCharIsRead() {
|
||||
Scanner sc = new Scanner(input);
|
||||
char c = sc.useDelimiter("").next().charAt(0);
|
||||
assertEquals('a', c);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
first line
|
||||
second line
|
||||
third line
|
||||
first line
|
||||
second line
|
||||
third line
|
||||
|
|
Loading…
Reference in New Issue