Skip to content

Commit

Permalink
Created Utils, UtilsTest and test file (#29)
Browse files Browse the repository at this point in the history
Closes #26
  • Loading branch information
duartencar committed Apr 14, 2021
1 parent 028c072 commit 9cf024a
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

out/*
9 changes: 9 additions & 0 deletions .idea/libraries/jade2.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion Car Routing.iml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,26 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="jade" level="project" />
<orderEntry type="library" name="org.junit.jupiter:junit-jupiter:5.4.2" level="project" />
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/jade.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="library" name="jade" level="project" />
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="file://$MODULE_DIR$/lib" />
</CLASSES>
<JAVADOC />
<SOURCES />
<jarDirectory url="file://$MODULE_DIR$/lib" recursive="false" />
</library>
</orderEntry>
</component>
</module>
40 changes: 40 additions & 0 deletions src/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
26 changes: 26 additions & 0 deletions test/java/utils/UtilsTest.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
2 changes: 2 additions & 0 deletions test/resources/fileTest.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hello world!
I'm in.

0 comments on commit 9cf024a

Please sign in to comment.