BAEL-2312 - Abstract Classes in Java (#5598)
* Initial Commit * Add files via upload * Update UppercaseFileReaderUnitTest.java
This commit is contained in:
parent
0befbceae3
commit
c7b7d94409
|
@ -0,0 +1,30 @@
|
|||
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;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
|
||||
Application application = new Application();
|
||||
String filePath = application.getFilePathFromResourcesFolder("files/test.txt");
|
||||
|
||||
BaseFileReader lowercaseFileReader = new LowercaseFileReader(filePath);
|
||||
lowercaseFileReader.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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.abstractclasses.filereaders;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class BaseFileReader {
|
||||
|
||||
protected String filePath;
|
||||
|
||||
protected BaseFileReader(String filePath) {
|
||||
this.filePath = filePath;
|
||||
}
|
||||
|
||||
public String getFilePath() {
|
||||
return filePath;
|
||||
}
|
||||
|
||||
public abstract List<String> readFile() throws IOException;
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
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 LowercaseFileReader extends BaseFileReader {
|
||||
|
||||
public LowercaseFileReader(String filePath) {
|
||||
super(filePath);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> readFile() throws IOException {
|
||||
return Files.lines(Paths.get(filePath))
|
||||
.map(String::toLowerCase)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
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());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
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 UppercaseFileReader extends BaseFileReader {
|
||||
|
||||
public UppercaseFileReader(String filePath) {
|
||||
super(filePath);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> readFile() throws IOException {
|
||||
return Files.lines(Paths.get(filePath))
|
||||
.map(String::toUpperCase)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
This is line 1
|
||||
This is line 2
|
||||
This is line 3
|
||||
This is line 4
|
||||
This is line 5
|
||||
This is line 6
|
||||
This is line 7
|
||||
This is line 8
|
||||
This is line 9
|
||||
This is line 10
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.abstractclasses;
|
||||
|
||||
import com.baeldung.abstractclasses.filereaders.BaseFileReader;
|
||||
import com.baeldung.abstractclasses.filereaders.LowercaseFileReader;
|
||||
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 {
|
||||
String filePath = getClass().getClassLoader().getResource("files/test.txt").getPath().substring(1);
|
||||
BaseFileReader lowercaseFileReader = new LowercaseFileReader(filePath);
|
||||
|
||||
assertThat(lowercaseFileReader.readFile()).isInstanceOf(List.class);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.abstractclasses;
|
||||
|
||||
import com.baeldung.abstractclasses.filereaders.BaseFileReader;
|
||||
import com.baeldung.abstractclasses.filereaders.StandardFileReader;
|
||||
import java.util.List;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.Test;
|
||||
|
||||
public class StandardFileReaderUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenStandardFileReaderInstance_whenCalledreadFile_thenCorrect() throws Exception {
|
||||
String filePath = getClass().getClassLoader().getResource("files/test.txt").getPath().substring(1);
|
||||
BaseFileReader standardFileReader = new StandardFileReader(filePath);
|
||||
|
||||
assertThat(standardFileReader.readFile()).isInstanceOf(List.class);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.abstractclasses;
|
||||
|
||||
import com.baeldung.abstractclasses.filereaders.BaseFileReader;
|
||||
import com.baeldung.abstractclasses.filereaders.UppercaseFileReader;
|
||||
import java.util.List;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UppercaseFileReaderUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenUppercaseFileReaderInstance_whenCalledreadFile_thenCorrect() throws Exception {
|
||||
String filePath = getClass().getClassLoader().getResource("files/test.txt").getPath().substring(1);
|
||||
BaseFileReader uppercaseFileReader = new UppercaseFileReader(filePath);
|
||||
|
||||
assertThat(uppercaseFileReader.readFile()).isInstanceOf(List.class);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue