-
-
Notifications
You must be signed in to change notification settings - Fork 797
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a new package with an Algorithm that takes a txt file and conve…
…rts it into a list for further use. Also added the test class and some txt files for the testing.
- Loading branch information
1 parent
9ae21a5
commit 7129918
Showing
4 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
src/main/java/com/github/pedrovgs/txtToList/TxtToList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright (C) 2014 Pedro Vicente Gómez Sánchez. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.github.pedrovgs.txtToList; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.FileReader; | ||
import java.util.List; | ||
import java.util.ArrayList; | ||
import java.io.IOException; | ||
import java.nio.file.Paths; | ||
|
||
/** | ||
* This class contains and algorithm that reads txt files and turns them to Lists for further use. | ||
* @author ShoeMaker | ||
* | ||
*/ | ||
|
||
public class TxtToList { | ||
|
||
/** | ||
* Method gets a txt file's path. It opens the file and reads it, | ||
* then tries to get the txt lines and return them as a List. | ||
* @param filepath The file's path. | ||
* @return An List with the file's lines. | ||
* @throws IllegalArgumentException when an IOException occurs or the file is empty. | ||
*/ | ||
|
||
public List<String> readFileToList(String filepath) { | ||
|
||
filepath = Paths.get(".").toAbsolutePath().normalize().toString() + filepath; | ||
List<String> datas = new ArrayList<String>(); | ||
BufferedReader br = null; | ||
String line = null; | ||
try { | ||
br = new BufferedReader(new FileReader(filepath)); | ||
line = br.readLine(); | ||
while (line != null) { | ||
|
||
if (line != null) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
ETsagkaris
Author
|
||
datas.add(line); | ||
} | ||
line = br.readLine(); | ||
} | ||
|
||
br.close(); | ||
} catch (IOException e) { | ||
throw new IllegalArgumentException("Something went wrong while reading the file. Probably wrong path or file doesn't exist."); | ||
} | ||
if (datas.size() == 0) { | ||
throw new IllegalArgumentException("File was empty."); | ||
} | ||
|
||
return datas; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/test/java/com/github/pedrovgs/txtToList/TxtToListTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.github.pedrovgs.txtToList; | ||
|
||
import org.junit.Test; | ||
|
||
import org.junit.Assert; | ||
import org.junit.rules.ExpectedException; | ||
import org.junit.Rule; | ||
import java.util.List; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* This class contains tests for TxtToList.readFileToList method. | ||
* @author ShoeMaker | ||
* | ||
*/ | ||
public class TxtToListTest { | ||
|
||
//Files we are going to use for the test | ||
TxtToList ttl = new TxtToList(); | ||
String path = "\\src\\test\\resources\\grades.txt"; | ||
String empty ="\\src\\test\\resources\\empty.txt"; | ||
|
||
String[] a = {"1", "3", "4", "6", "2", "4", "7", "8", "10", "9", "5"}; //Expected outcome of the readFileToList test | ||
List<String> b = Arrays.asList(a); | ||
/** | ||
* This tests if the readFileToList method works correctly. | ||
*/ | ||
@Test | ||
public void test_readFileToList() { | ||
Assert.assertEquals(b, ttl.readFileToList(path)); | ||
|
||
} | ||
|
||
@Rule | ||
public ExpectedException thrown = ExpectedException.none(); | ||
|
||
/** | ||
* Tests a case of IOException in readFileToList method | ||
*/ | ||
@Test | ||
public void test_readFile_IOException() { | ||
|
||
thrown.expect(IllegalArgumentException.class); | ||
thrown.expectMessage("Something went wrong while reading the file. Probably wrong path or file doesn't exist."); | ||
ttl.readFileToList("asfasdf"); | ||
} | ||
/** | ||
* Tests a case of reading an empty file in readFileToList method | ||
*/ | ||
@Test | ||
public void test_readFile_FileEmpty() { | ||
|
||
thrown.expect(IllegalArgumentException.class); | ||
thrown.expectMessage("File was empty."); | ||
ttl.readFileToList(empty); | ||
} | ||
|
||
|
||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
1 | ||
3 | ||
4 | ||
6 | ||
2 | ||
4 | ||
7 | ||
8 | ||
10 | ||
9 | ||
5 |
Why this condition need to be checked twice?