From 9cf024a40a6fce4d5e341b1f5b6fea05c23e8114 Mon Sep 17 00:00:00 2001 From: Duarte Carvalho Date: Wed, 14 Apr 2021 23:45:19 +0100 Subject: [PATCH] Created Utils, UtilsTest and test file (#29) Closes #26 --- .gitignore | 2 ++ .idea/libraries/jade2.xml | 9 ++++++++ .idea/misc.xml | 2 +- Car Routing.iml | 21 +++++++++++++++++- src/utils/Utils.java | 40 ++++++++++++++++++++++++++++++++++ test/java/utils/UtilsTest.java | 26 ++++++++++++++++++++++ test/resources/fileTest.txt | 2 ++ 7 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 .idea/libraries/jade2.xml create mode 100644 src/utils/Utils.java create mode 100644 test/java/utils/UtilsTest.java create mode 100644 test/resources/fileTest.txt diff --git a/.gitignore b/.gitignore index 3c26a7c..fac7bef 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,5 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + +out/* diff --git a/.idea/libraries/jade2.xml b/.idea/libraries/jade2.xml new file mode 100644 index 0000000..248a6a0 --- /dev/null +++ b/.idea/libraries/jade2.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 2c90057..c6c1f2f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/Car Routing.iml b/Car Routing.iml index c0d9a17..3fe5417 100644 --- a/Car Routing.iml +++ b/Car Routing.iml @@ -9,7 +9,26 @@ - + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/utils/Utils.java b/src/utils/Utils.java new file mode 100644 index 0000000..8864169 --- /dev/null +++ b/src/utils/Utils.java @@ -0,0 +1,40 @@ +package utils; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Scanner; + +public class Utils { + + public static Scanner getFileReference(final String filePath) { + + File f; + Scanner fileReference; + + try { + f = new File(filePath); + + if(f.exists()) { + System.out.println(filePath + " file opened."); + } + else { + System.out.println(filePath + " doesn't exist."); + return null; + } + } catch(NullPointerException | SecurityException e) { + System.out.println(filePath + "couldn't be opened."); + System.out.println(e.getMessage()); + return null; + } + + try { + fileReference= new Scanner(f); + } catch(FileNotFoundException e) { + System.out.println(filePath + "couldn't be found."); + System.out.println(e.getMessage()); + return null; + } + + return fileReference; + } +} diff --git a/test/java/utils/UtilsTest.java b/test/java/utils/UtilsTest.java new file mode 100644 index 0000000..112a1ab --- /dev/null +++ b/test/java/utils/UtilsTest.java @@ -0,0 +1,26 @@ +package utils; + +import org.junit.jupiter.api.Test; + +import java.util.Scanner; + +import static org.junit.jupiter.api.Assertions.*; + +class UtilsTest { + + @Test + void getFileReference() { + Scanner file = Utils.getFileReference("test/resources/fileTest.txt"); + + assertNotNull(file); + + int nLines = 0; + + while(file.hasNextLine()) { + nLines++; + System.out.println(file.nextLine()); + } + + assertEquals(2, nLines, "The number of lines should be 2"); + } +} \ No newline at end of file diff --git a/test/resources/fileTest.txt b/test/resources/fileTest.txt new file mode 100644 index 0000000..f27b913 --- /dev/null +++ b/test/resources/fileTest.txt @@ -0,0 +1,2 @@ +hello world! +I'm in. \ No newline at end of file