Skip to content

Commit

Permalink
Added a new package with an Algorithm that takes a txt file and conve…
Browse files Browse the repository at this point in the history
…rts it into a list for further use. Also added the test class and some txt files for the testing.
  • Loading branch information
ETsagkaris committed May 11, 2018
1 parent 9ae21a5 commit 7129918
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/main/java/com/github/pedrovgs/txtToList/TxtToList.java
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.

Copy link
@bluecloudes

bluecloudes May 12, 2018

Why this condition need to be checked twice?

This comment has been minimized.

Copy link
@ETsagkaris

ETsagkaris May 12, 2018

Author

You are right, there is no reason to check it twice

This comment has been minimized.

Copy link
@ETsagkaris

ETsagkaris May 12, 2018

Author

Do you know why travis finds errors in the testing part ? I tried to build the project in 3 different systems and it worked fine.

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 src/test/java/com/github/pedrovgs/txtToList/TxtToListTest.java
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 added src/test/resources/empty.txt
Empty file.
11 changes: 11 additions & 0 deletions src/test/resources/grades.txt
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

0 comments on commit 7129918

Please sign in to comment.