Merge pull request #8125 from eugenp/revert-8119-BAEL-3275-2

Revert "BAEL-3275: Using blocking queue for pub-sub"
This commit is contained in:
Eric Martin
2019-10-31 20:43:47 -05:00
committed by GitHub
parent db85c8f275
commit 3225470df5
20543 changed files with 1642750 additions and 0 deletions
@@ -0,0 +1,45 @@
package com.baeldung.creationdate;
import org.junit.Test;
import java.io.File;
import java.nio.file.Path;
import java.time.Instant;
import java.util.Optional;
import static org.junit.Assert.assertTrue;
public class CreationDateResolverUnitTest {
private final CreationDateResolver creationDateResolver = new CreationDateResolver();
@Test
public void givenFile_whenGettingCreationDateTimeFromBasicAttributes_thenReturnDate() throws Exception {
final File file = File.createTempFile("createdFile", ".txt");
final Path path = file.toPath();
final Instant response = creationDateResolver.resolveCreationTimeWithBasicAttributes(path);
assertTrue(Instant
.now()
.isAfter(response));
}
@Test
public void givenFile_whenGettingCreationDateTimeFromAttribute_thenReturnDate() throws Exception {
final File file = File.createTempFile("createdFile", ".txt");
final Path path = file.toPath();
final Optional<Instant> response = creationDateResolver.resolveCreationTimeWithAttribute(path);
response.ifPresent((value) -> {
assertTrue(Instant
.now()
.isAfter(value));
});
}
}
@@ -0,0 +1,67 @@
package com.baeldung.file;
import static com.baeldung.file.NumberOfLineFinder.getTotalNumberOfLinesUsingApacheCommonsIO;
import static com.baeldung.file.NumberOfLineFinder.getTotalNumberOfLinesUsingBufferedReader;
import static com.baeldung.file.NumberOfLineFinder.getTotalNumberOfLinesUsingGoogleGuava;
import static com.baeldung.file.NumberOfLineFinder.getTotalNumberOfLinesUsingLineNumberReader;
import static com.baeldung.file.NumberOfLineFinder.getTotalNumberOfLinesUsingNIOFileChannel;
import static com.baeldung.file.NumberOfLineFinder.getTotalNumberOfLinesUsingNIOFiles;
import static com.baeldung.file.NumberOfLineFinder.getTotalNumberOfLinesUsingNIOFilesReadAllLines;
import static com.baeldung.file.NumberOfLineFinder.getTotalNumberOfLinesUsingScanner;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class NumberOfLineFinderUnitTest {
private static final String INPUT_FILE_NAME = "src/main/resources/input.txt";
private static final int ACTUAL_LINE_COUNT = 45;
@Test
public void whenUsingBufferedReader_thenReturnTotalNumberOfLines() {
int lines = getTotalNumberOfLinesUsingBufferedReader(INPUT_FILE_NAME);
assertEquals(ACTUAL_LINE_COUNT, lines);
}
@Test
public void whenUsingLineNumberReader_thenReturnTotalNumberOfLines() {
int lines = getTotalNumberOfLinesUsingLineNumberReader(INPUT_FILE_NAME);
assertEquals(ACTUAL_LINE_COUNT, lines);
}
@Test
public void whenUsingScanner_thenReturnTotalNumberOfLines() {
int lines = getTotalNumberOfLinesUsingScanner(INPUT_FILE_NAME);
assertEquals(ACTUAL_LINE_COUNT, lines);
}
@Test
public void whenUsingNIOFiles_thenReturnTotalNumberOfLines() {
int lines = getTotalNumberOfLinesUsingNIOFiles(INPUT_FILE_NAME);
assertEquals(ACTUAL_LINE_COUNT, lines);
}
@Test
public void whenUsingNIOFilesReadAllLines_thenReturnTotalNumberOfLines() {
int lines = getTotalNumberOfLinesUsingNIOFilesReadAllLines(INPUT_FILE_NAME);
assertEquals(ACTUAL_LINE_COUNT, lines);
}
@Test
public void whenUsingNIOFileChannel_thenReturnTotalNumberOfLines() {
int lines = getTotalNumberOfLinesUsingNIOFileChannel(INPUT_FILE_NAME);
assertEquals(ACTUAL_LINE_COUNT, lines);
}
@Test
public void whenUsingApacheCommonsIO_thenReturnTotalNumberOfLines() {
int lines = getTotalNumberOfLinesUsingApacheCommonsIO(INPUT_FILE_NAME);
assertEquals(ACTUAL_LINE_COUNT, lines);
}
@Test
public void whenUsingGoogleGuava_thenReturnTotalNumberOfLines() {
int lines = getTotalNumberOfLinesUsingGoogleGuava(INPUT_FILE_NAME);
assertEquals(ACTUAL_LINE_COUNT, lines);
}
}
@@ -0,0 +1,165 @@
package com.baeldung.filechannel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.charset.StandardCharsets;
import org.junit.Test;
public class FileChannelUnitTest {
@Test
public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException {
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
FileChannel channel = reader.getChannel();
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
int bufferSize = 1024;
if (bufferSize > channel.size()) {
bufferSize = (int) channel.size();
}
ByteBuffer buff = ByteBuffer.allocate(bufferSize);
while (channel.read(buff) > 0) {
out.write(buff.array(), 0, buff.position());
buff.clear();
}
String fileContent = new String(out.toByteArray(), StandardCharsets.UTF_8);
assertEquals("Hello world", fileContent);
}
}
@Test
public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException {
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
FileInputStream fin = new FileInputStream("src/test/resources/test_read.in");
FileChannel channel = fin.getChannel()) {
int bufferSize = 1024;
if (bufferSize > channel.size()) {
bufferSize = (int) channel.size();
}
ByteBuffer buff = ByteBuffer.allocate(bufferSize);
while (channel.read(buff) > 0) {
out.write(buff.array(), 0, buff.position());
buff.clear();
}
String fileContent = new String(out.toByteArray(), StandardCharsets.UTF_8);
assertEquals("Hello world", fileContent);
}
}
@Test
public void givenFile_whenReadAFileSectionIntoMemoryWithFileChannel_thenCorrect() throws IOException {
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
FileChannel channel = reader.getChannel();
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
MappedByteBuffer buff = channel.map(FileChannel.MapMode.READ_ONLY, 6, 5);
if (buff.hasRemaining()) {
byte[] data = new byte[buff.remaining()];
buff.get(data);
assertEquals("world", new String(data, StandardCharsets.UTF_8));
}
}
}
@Test
public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException {
String file = "src/test/resources/test_write_using_filechannel.txt";
try (RandomAccessFile writer = new RandomAccessFile(file, "rw");
FileChannel channel = writer.getChannel()) {
ByteBuffer buff = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.UTF_8));
channel.write(buff);
// now we verify whether the file was written correctly
RandomAccessFile reader = new RandomAccessFile(file, "r");
assertEquals("Hello world", reader.readLine());
reader.close();
}
}
@Test
public void givenFile_whenWriteAFileUsingLockAFileSectionWithFileChannel_thenCorrect() throws IOException {
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "rw");
FileChannel channel = reader.getChannel();
FileLock fileLock = channel.tryLock(6, 5, Boolean.FALSE);) {
assertNotNull(fileLock);
}
}
@Test
public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException {
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
FileChannel channel = reader.getChannel()) {
int bufferSize = 1024;
if (bufferSize > channel.size()) {
bufferSize = (int) channel.size();
}
ByteBuffer buff = ByteBuffer.allocate(bufferSize);
while (channel.read(buff) > 0) {
out.write(buff.array(), 0, buff.position());
buff.clear();
}
// the original file is 11 bytes long, so that's where the position pointer should be
assertEquals(11, channel.position());
channel.position(4);
assertEquals(4, channel.position());
}
}
@Test
public void whenGetFileSize_thenCorrect() throws IOException {
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
FileChannel channel = reader.getChannel()) {
// the original file is 11 bytes long, so that's where the position pointer should be
assertEquals(11, channel.size());
}
}
@Test
public void whenTruncateFile_thenCorrect() throws IOException {
String input = "this is a test input";
FileOutputStream fout = new FileOutputStream("src/test/resources/test_truncate.txt");
FileChannel channel = fout.getChannel();
ByteBuffer buff = ByteBuffer.wrap(input.getBytes());
channel.write(buff);
buff.flip();
channel = channel.truncate(5);
assertEquals(5, channel.size());
fout.close();
channel.close();
}
}
@@ -0,0 +1,13 @@
*.class
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear
@@ -0,0 +1 @@
Hello world
@@ -0,0 +1 @@
this