From 55ba7955f9d02fca9ac2f84227e2e6a54a1d4e20 Mon Sep 17 00:00:00 2001 From: geroza Date: Tue, 13 Nov 2018 00:36:08 -0200 Subject: [PATCH] Fixed: * SimpleDateFormatUnitTest test, which relied on the system locale, thus making it platform dependent * UppercaseFileReaderUnitTest, LowercaseFileReaderUnitTest and StandardFileReaderUnitTest, which were using an approach that seems to be efficient only on Windows; on Linux it raises an exception. The new approach should be good for all cases, but we need testing on a Windows environment --- .../LowercaseFileReaderUnitTest.java | 11 ++++++++--- .../StandardFileReaderUnitTest.java | 15 +++++++++++---- .../UppercaseFileReaderUnitTest.java | 7 ++++++- .../SimpleDateFormatUnitTest.java | 2 +- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/core-java/src/test/java/com/baeldung/abstractclasses/LowercaseFileReaderUnitTest.java b/core-java/src/test/java/com/baeldung/abstractclasses/LowercaseFileReaderUnitTest.java index 4058f6f03b..a97a68e0bd 100644 --- a/core-java/src/test/java/com/baeldung/abstractclasses/LowercaseFileReaderUnitTest.java +++ b/core-java/src/test/java/com/baeldung/abstractclasses/LowercaseFileReaderUnitTest.java @@ -2,17 +2,22 @@ package com.baeldung.abstractclasses; import com.baeldung.abstractclasses.filereaders.BaseFileReader; import com.baeldung.abstractclasses.filereaders.LowercaseFileReader; + +import java.net.URL; +import java.nio.file.Paths; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; import org.junit.Test; public class LowercaseFileReaderUnitTest { - + @Test public void givenLowercaseFileReaderInstance_whenCalledreadFile_thenCorrect() throws Exception { - String filePath = getClass().getClassLoader().getResource("files/test.txt").getPath().substring(1); + // We'll transform the resource URL path to URI to load the file correctly in Windows + URL url = getClass().getClassLoader().getResource("files/test.txt"); + String filePath = Paths.get(url.toURI()).toString(); BaseFileReader lowercaseFileReader = new LowercaseFileReader(filePath); - + assertThat(lowercaseFileReader.readFile()).isInstanceOf(List.class); } } diff --git a/core-java/src/test/java/com/baeldung/abstractclasses/StandardFileReaderUnitTest.java b/core-java/src/test/java/com/baeldung/abstractclasses/StandardFileReaderUnitTest.java index e1c1435ef4..348b0f0366 100644 --- a/core-java/src/test/java/com/baeldung/abstractclasses/StandardFileReaderUnitTest.java +++ b/core-java/src/test/java/com/baeldung/abstractclasses/StandardFileReaderUnitTest.java @@ -1,16 +1,23 @@ package com.baeldung.abstractclasses; +import static org.assertj.core.api.Assertions.assertThat; + +import java.net.URL; +import java.nio.file.Paths; +import java.util.List; + +import org.junit.Test; + import com.baeldung.abstractclasses.filereaders.BaseFileReader; import com.baeldung.abstractclasses.filereaders.StandardFileReader; -import java.util.List; -import static org.assertj.core.api.Assertions.assertThat; -import org.junit.Test; public class StandardFileReaderUnitTest { @Test public void givenStandardFileReaderInstance_whenCalledreadFile_thenCorrect() throws Exception { - String filePath = getClass().getClassLoader().getResource("files/test.txt").getPath().substring(1); + // We'll transform the resource URL path to URI to load the file correctly in Windows + URL url = getClass().getClassLoader().getResource("files/test.txt"); + String filePath = Paths.get(url.toURI()).toString(); BaseFileReader standardFileReader = new StandardFileReader(filePath); assertThat(standardFileReader.readFile()).isInstanceOf(List.class); diff --git a/core-java/src/test/java/com/baeldung/abstractclasses/UppercaseFileReaderUnitTest.java b/core-java/src/test/java/com/baeldung/abstractclasses/UppercaseFileReaderUnitTest.java index f9c5fbf94d..d698cfe038 100644 --- a/core-java/src/test/java/com/baeldung/abstractclasses/UppercaseFileReaderUnitTest.java +++ b/core-java/src/test/java/com/baeldung/abstractclasses/UppercaseFileReaderUnitTest.java @@ -2,6 +2,9 @@ package com.baeldung.abstractclasses; import com.baeldung.abstractclasses.filereaders.BaseFileReader; import com.baeldung.abstractclasses.filereaders.UppercaseFileReader; + +import java.net.URL; +import java.nio.file.Paths; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; import org.junit.Test; @@ -10,7 +13,9 @@ public class UppercaseFileReaderUnitTest { @Test public void givenUppercaseFileReaderInstance_whenCalledreadFile_thenCorrect() throws Exception { - String filePath = getClass().getClassLoader().getResource("files/test.txt").getPath().substring(1); + // We'll transform the resource URL path to URI to load the file correctly in Windows + URL url = getClass().getClassLoader().getResource("files/test.txt"); + String filePath = Paths.get(url.toURI()).toString(); BaseFileReader uppercaseFileReader = new UppercaseFileReader(filePath); assertThat(uppercaseFileReader.readFile()).isInstanceOf(List.class); diff --git a/core-java/src/test/java/com/baeldung/simpledateformat/SimpleDateFormatUnitTest.java b/core-java/src/test/java/com/baeldung/simpledateformat/SimpleDateFormatUnitTest.java index 4ae7b77089..7e1fcd9b3d 100644 --- a/core-java/src/test/java/com/baeldung/simpledateformat/SimpleDateFormatUnitTest.java +++ b/core-java/src/test/java/com/baeldung/simpledateformat/SimpleDateFormatUnitTest.java @@ -25,7 +25,7 @@ public class SimpleDateFormatUnitTest { @Test public void givenSpecificDate_whenFormattedUsingDateFormat_thenCheckFormatCorrect() throws Exception { - DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT); + DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US); assertEquals("5/24/77", formatter.format(new Date(233345223232L))); }