Skip to content

Commit 7129918

Browse files
committed
Added a new package with an Algorithm that takes a txt file and converts it into a list for further use. Also added the test class and some txt files for the testing.
1 parent 9ae21a5 commit 7129918

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (C) 2014 Pedro Vicente Gómez Sánchez.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.github.pedrovgs.txtToList;
18+
19+
import java.io.BufferedReader;
20+
import java.io.FileReader;
21+
import java.util.List;
22+
import java.util.ArrayList;
23+
import java.io.IOException;
24+
import java.nio.file.Paths;
25+
26+
/**
27+
* This class contains and algorithm that reads txt files and turns them to Lists for further use.
28+
* @author ShoeMaker
29+
*
30+
*/
31+
32+
public class TxtToList {
33+
34+
/**
35+
* Method gets a txt file's path. It opens the file and reads it,
36+
* then tries to get the txt lines and return them as a List.
37+
* @param filepath The file's path.
38+
* @return An List with the file's lines.
39+
* @throws IllegalArgumentException when an IOException occurs or the file is empty.
40+
*/
41+
42+
public List<String> readFileToList(String filepath) {
43+
44+
filepath = Paths.get(".").toAbsolutePath().normalize().toString() + filepath;
45+
List<String> datas = new ArrayList<String>();
46+
BufferedReader br = null;
47+
String line = null;
48+
try {
49+
br = new BufferedReader(new FileReader(filepath));
50+
line = br.readLine();
51+
while (line != null) {
52+
53+
if (line != null) {
54+
datas.add(line);
55+
}
56+
line = br.readLine();
57+
}
58+
59+
br.close();
60+
} catch (IOException e) {
61+
throw new IllegalArgumentException("Something went wrong while reading the file. Probably wrong path or file doesn't exist.");
62+
}
63+
if (datas.size() == 0) {
64+
throw new IllegalArgumentException("File was empty.");
65+
}
66+
67+
return datas;
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.github.pedrovgs.txtToList;
2+
3+
import org.junit.Test;
4+
5+
import org.junit.Assert;
6+
import org.junit.rules.ExpectedException;
7+
import org.junit.Rule;
8+
import java.util.List;
9+
import java.util.Arrays;
10+
11+
/**
12+
* This class contains tests for TxtToList.readFileToList method.
13+
* @author ShoeMaker
14+
*
15+
*/
16+
public class TxtToListTest {
17+
18+
//Files we are going to use for the test
19+
TxtToList ttl = new TxtToList();
20+
String path = "\\src\\test\\resources\\grades.txt";
21+
String empty ="\\src\\test\\resources\\empty.txt";
22+
23+
String[] a = {"1", "3", "4", "6", "2", "4", "7", "8", "10", "9", "5"}; //Expected outcome of the readFileToList test
24+
List<String> b = Arrays.asList(a);
25+
/**
26+
* This tests if the readFileToList method works correctly.
27+
*/
28+
@Test
29+
public void test_readFileToList() {
30+
Assert.assertEquals(b, ttl.readFileToList(path));
31+
32+
}
33+
34+
@Rule
35+
public ExpectedException thrown = ExpectedException.none();
36+
37+
/**
38+
* Tests a case of IOException in readFileToList method
39+
*/
40+
@Test
41+
public void test_readFile_IOException() {
42+
43+
thrown.expect(IllegalArgumentException.class);
44+
thrown.expectMessage("Something went wrong while reading the file. Probably wrong path or file doesn't exist.");
45+
ttl.readFileToList("asfasdf");
46+
}
47+
/**
48+
* Tests a case of reading an empty file in readFileToList method
49+
*/
50+
@Test
51+
public void test_readFile_FileEmpty() {
52+
53+
thrown.expect(IllegalArgumentException.class);
54+
thrown.expectMessage("File was empty.");
55+
ttl.readFileToList(empty);
56+
}
57+
58+
59+
}

src/test/resources/empty.txt

Whitespace-only changes.

src/test/resources/grades.txt

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
1
2+
3
3+
4
4+
6
5+
2
6+
4
7+
7
8+
8
9+
10
10+
9
11+
5

0 commit comments

Comments
 (0)