BAEL-2312 - Abstract Classes in Java (#5704)

* Initial Commit

* Fixed project files

* Update UppercaseFileReaderUnitTest.java

* Update UppercaseFileReaderUnitTest.java

* Refactored File Reader SubClasses

* Update UppercaseFileReaderUnitTest.java
This commit is contained in:
Alejandro Gervasio 2018-11-18 12:54:57 -03:00 committed by KevinGilmore
parent b4a830475c
commit 86184f8270
8 changed files with 48 additions and 103 deletions

View File

@ -2,29 +2,28 @@ package com.baeldung.abstractclasses.application;
import com.baeldung.abstractclasses.filereaders.BaseFileReader;
import com.baeldung.abstractclasses.filereaders.LowercaseFileReader;
import com.baeldung.abstractclasses.filereaders.StandardFileReader;
import com.baeldung.abstractclasses.filereaders.UppercaseFileReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Application {
public static void main(String[] args) throws IOException {
public static void main(String[] args) throws IOException, URISyntaxException {
Application application = new Application();
String filePath = application.getFilePathFromResourcesFolder("files/test.txt");
BaseFileReader lowercaseFileReader = new LowercaseFileReader(filePath);
Path path = application.getPathFromResourcesFile("files/test.txt");
BaseFileReader lowercaseFileReader = new LowercaseFileReader(path);
lowercaseFileReader.readFile().forEach(line -> System.out.println(line));
BaseFileReader uppercaseFileReader = new UppercaseFileReader(path);
uppercaseFileReader.readFile().forEach(line -> System.out.println(line));
BaseFileReader upperCaseFileReader = new UppercaseFileReader(filePath);
upperCaseFileReader.readFile().forEach(line -> System.out.println(line));
BaseFileReader standardFileReader = new StandardFileReader(filePath);
standardFileReader.readFile().forEach(line -> System.out.println(line));
}
private String getFilePathFromResourcesFolder(String fileName) {
return getClass().getClassLoader().getResource(fileName).getPath().substring(1);
private Path getPathFromResourcesFile(String filePath) throws URISyntaxException {
return Paths.get(getClass().getClassLoader().getResource(filePath).toURI());
}
}

View File

@ -1,19 +1,27 @@
package com.baeldung.abstractclasses.filereaders;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;
public abstract class BaseFileReader {
protected String filePath;
protected Path filePath;
protected BaseFileReader(String filePath) {
protected BaseFileReader(Path filePath) {
this.filePath = filePath;
}
public String getFilePath() {
public Path getFilePath() {
return filePath;
}
public abstract List<String> readFile() throws IOException;
public List<String> readFile() throws IOException {
return Files.lines(filePath)
.map(this::mapFileLine).collect(Collectors.toList());
}
protected abstract String mapFileLine(String line);
}

View File

@ -1,21 +1,15 @@
package com.baeldung.abstractclasses.filereaders;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.nio.file.Path;
public class LowercaseFileReader extends BaseFileReader {
public LowercaseFileReader(String filePath) {
public LowercaseFileReader(Path filePath) {
super(filePath);
}
@Override
public List<String> readFile() throws IOException {
return Files.lines(Paths.get(filePath))
.map(String::toLowerCase)
.collect(Collectors.toList());
}
public String mapFileLine(String line) {
return line.toLowerCase();
}
}

View File

@ -1,19 +0,0 @@
package com.baeldung.abstractclasses.filereaders;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
public class StandardFileReader extends BaseFileReader {
public StandardFileReader(String filePath) {
super(filePath);
}
@Override
public List<String> readFile() throws IOException {
return Files.lines(Paths.get(filePath)).collect(Collectors.toList());
}
}

View File

@ -1,21 +1,15 @@
package com.baeldung.abstractclasses.filereaders;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.nio.file.Path;
public class UppercaseFileReader extends BaseFileReader {
public UppercaseFileReader(String filePath) {
public UppercaseFileReader(Path filePath) {
super(filePath);
}
@Override
public List<String> readFile() throws IOException {
return Files.lines(Paths.get(filePath))
.map(String::toUpperCase)
.collect(Collectors.toList());
public String mapFileLine(String line) {
return line.toUpperCase();
}
}

View File

@ -1,25 +0,0 @@
package com.baeldung.abstractclasses;
import static org.assertj.core.api.Assertions.assertThat;
import java.net.URL;
import java.nio.file.Paths;
import java.util.List;
import org.junit.Test;
import com.baeldung.abstractclasses.filereaders.BaseFileReader;
import com.baeldung.abstractclasses.filereaders.StandardFileReader;
public class StandardFileReaderUnitTest {
@Test
public void givenStandardFileReaderInstance_whenCalledreadFile_thenCorrect() throws Exception {
// We'll transform the resource URL path to URI to load the file correctly in Windows
URL url = getClass().getClassLoader().getResource("files/test.txt");
String filePath = Paths.get(url.toURI()).toString();
BaseFileReader standardFileReader = new StandardFileReader(filePath);
assertThat(standardFileReader.readFile()).isInstanceOf(List.class);
}
}

View File

@ -1,23 +1,20 @@
package com.baeldung.abstractclasses;
package com.baeldung.abstractclasses.test;
import com.baeldung.abstractclasses.filereaders.BaseFileReader;
import com.baeldung.abstractclasses.filereaders.LowercaseFileReader;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class LowercaseFileReaderUnitTest {
@Test
public void givenLowercaseFileReaderInstance_whenCalledreadFile_thenCorrect() throws Exception {
// We'll transform the resource URL path to URI to load the file correctly in Windows
URL url = getClass().getClassLoader().getResource("files/test.txt");
String filePath = Paths.get(url.toURI()).toString();
BaseFileReader lowercaseFileReader = new LowercaseFileReader(filePath);
Path path = Paths.get(getClass().getClassLoader().getResource("files/test.txt").toURI());
BaseFileReader lowercaseFileReader = new LowercaseFileReader(path);
assertThat(lowercaseFileReader.readFile()).isInstanceOf(List.class);
}
}

View File

@ -1,9 +1,8 @@
package com.baeldung.abstractclasses;
package com.baeldung.abstractclasses.test;
import com.baeldung.abstractclasses.filereaders.BaseFileReader;
import com.baeldung.abstractclasses.filereaders.UppercaseFileReader;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@ -13,11 +12,9 @@ public class UppercaseFileReaderUnitTest {
@Test
public void givenUppercaseFileReaderInstance_whenCalledreadFile_thenCorrect() throws Exception {
// We'll transform the resource URL path to URI to load the file correctly in Windows
URL url = getClass().getClassLoader().getResource("files/test.txt");
String filePath = Paths.get(url.toURI()).toString();
BaseFileReader uppercaseFileReader = new UppercaseFileReader(filePath);
Path path = Paths.get(getClass().getClassLoader().getResource("files/test.txt").toURI());
BaseFileReader uppercaseFileReader = new UppercaseFileReader(path);
assertThat(uppercaseFileReader.readFile()).isInstanceOf(List.class);
}
}
}