Skip to content

Commit c3a8bb5

Browse files
authored
Merge pull request #42 from ukubuka/feature/UkubukaParser
Updated Parser/Reader
2 parents 057d61e + 2485391 commit c3a8bb5

File tree

3 files changed

+80
-12
lines changed

3 files changed

+80
-12
lines changed

src/main/java/com/ukubuka/core/reader/UkubukaReader.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.File;
44
import java.io.IOException;
5+
import java.net.URISyntaxException;
56
import java.net.URL;
67
import java.util.ArrayList;
78
import java.util.Arrays;
@@ -46,10 +47,10 @@ public List<String> readFile(final SupportedSource source,
4647
LOGGER.info("Reading File - Source: " + source + " | Location: "
4748
+ completeFileName + " | Encoding: " + fileEncoding
4849
+ " | Delimiter: " + endLineDelimiter);
49-
return new ArrayList<>(
50-
Arrays.asList(readFileAsString(source, completeFileName,
51-
fileEncoding)
52-
.split(StringUtils.isEmpty(endLineDelimiter) ? Constants.DEFAULT_FILE_END_LINE_DELIMITER
50+
return new ArrayList<>(Arrays
51+
.asList(readFileAsString(source, completeFileName, fileEncoding)
52+
.split(StringUtils.isEmpty(endLineDelimiter)
53+
? Constants.DEFAULT_FILE_END_LINE_DELIMITER
5354
: endLineDelimiter)));
5455
}
5556

@@ -68,12 +69,15 @@ public String readFileAsString(final SupportedSource source,
6869
try {
6970
return FileUtils
7071
.readFileToString(
71-
source == SupportedSource.URL ? FileUtils
72-
.toFile(new URL(completeFileName))
72+
source == SupportedSource.URL
73+
? new File(
74+
new URL(completeFileName).toURI())
7375
: new File(completeFileName),
74-
StringUtils.isEmpty(fileEncoding) ? Constants.DEFAULT_FILE_ENCODING
76+
StringUtils.isEmpty(fileEncoding)
77+
? Constants.DEFAULT_FILE_ENCODING
7578
: fileEncoding);
76-
} catch (IOException ex) {
79+
} catch (IOException | URISyntaxException
80+
| IllegalArgumentException ex) {
7781
throw new ReaderException(ex);
7882
}
7983
}

src/test/java/com/ukubuka/core/reader/UkubukaReaderTest.java

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.junit.Test;
44

55
import com.ukubuka.core.exception.ReaderException;
6+
import com.ukubuka.core.model.SupportedSource;
67

78
/**
89
* Ukubuka Reader Test
@@ -12,11 +13,71 @@
1213
*/
1314
public class UkubukaReaderTest {
1415

15-
private UkubukaReader ukubukaReader;
16+
private UkubukaReader ukubukaReader = new UkubukaReader();
1617

1718
/******************************** Test(s) ********************************/
18-
@Test(expected = NullPointerException.class)
19-
public void test_readFile_success() throws ReaderException {
20-
ukubukaReader.readFile(null, null, null, null);
19+
@Test
20+
public void test_readFile_withDelim_success() throws ReaderException {
21+
ukubukaReader.readFile(
22+
SupportedSource.FILE, this.getClass().getClassLoader()
23+
.getResource("test-dataset.csv").getFile(),
24+
"UTF-8", "\n");
25+
}
26+
27+
@Test
28+
public void test_readFile_withoutDelim_success() throws ReaderException {
29+
ukubukaReader.readFile(
30+
SupportedSource.FILE, this.getClass().getClassLoader()
31+
.getResource("test-dataset.csv").getFile(),
32+
"UTF-8", null);
33+
}
34+
35+
@Test
36+
public void test_readFileAsString_file_withEncoding_success()
37+
throws ReaderException {
38+
ukubukaReader
39+
.readFileAsString(SupportedSource.FILE,
40+
this.getClass().getClassLoader()
41+
.getResource("test-dataset.csv").getFile(),
42+
"UTF-8");
43+
}
44+
45+
@Test
46+
public void test_readFileAsString_file_withoutEncoding_success()
47+
throws ReaderException {
48+
System.out.println(this.getClass().getClassLoader()
49+
.getResource("test-dataset.csv"));
50+
ukubukaReader
51+
.readFileAsString(SupportedSource.FILE,
52+
this.getClass().getClassLoader()
53+
.getResource("test-dataset.csv").getFile(),
54+
null);
55+
}
56+
57+
@Test
58+
public void test_readFileAsString_url_withEncoding_success()
59+
throws ReaderException {
60+
ukubukaReader.readFileAsString(SupportedSource.URL,
61+
this.getClass().getClassLoader().getResource("test-dataset.csv")
62+
.toString().replace("file:/", "file:///"),
63+
"UTF-8");
64+
}
65+
66+
@Test
67+
public void test_readFileAsString_url_withoutEncoding_success()
68+
throws ReaderException {
69+
ukubukaReader.readFileAsString(SupportedSource.URL,
70+
this.getClass().getClassLoader().getResource("test-dataset.csv")
71+
.toString().replace("file:/", "file:///"),
72+
null);
73+
}
74+
75+
@Test(expected = ReaderException.class)
76+
public void test_readFileAsString_url_withoutEncoding_failure()
77+
throws ReaderException {
78+
ukubukaReader.readFileAsString(SupportedSource.URL,
79+
this.getClass().getClassLoader().getResource("test-dataset.csv")
80+
.toString().replace("file:/", "file://"),
81+
null);
2182
}
2283
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
col_1,col_2,col_3
2+
1,foo,1.01
3+
2,bar,1.02

0 commit comments

Comments
 (0)