Skip to content

Commit

Permalink
FileInputProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
mkordus committed Sep 23, 2017
1 parent cc4f0ee commit 5ad7d91
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/main/java/pega/command/executor/SortCommandExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import pega.command.SortCommand;

import java.io.IOException;
import java.util.Arrays;

public class SortCommandExecutor {

public void execute(SortCommand command) {
public void execute(SortCommand command) throws IOException {
int[] input = command.getInputProvider().getInput();

Arrays.parallelSort(input);
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/pega/io/FileInputProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package pega.io;

import java.io.*;

public class FileInputProvider implements InputProvider {

private final File file;
private final int startPosition;
private final int numberOfElementsToRead;
private final int intSize = 4;

public FileInputProvider(File file, Integer startPosition, Integer numberOfElementsToRead) {

this.file = file;
this.startPosition = startPosition;
this.numberOfElementsToRead = numberOfElementsToRead;
}

@Override
public int[] getInput() throws IOException {
int[] buffer = new int[numberOfElementsToRead];

try (RandomAccessFile input = new RandomAccessFile(file, "r")) {
input.seek(startPosition * intSize);

for (int index = 0; index < numberOfElementsToRead; index++) {
buffer[index] = input.readInt();
}
}

return buffer;
}
}
5 changes: 4 additions & 1 deletion src/main/java/pega/io/InputProvider.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package pega.io;

import java.io.FileNotFoundException;
import java.io.IOException;

public interface InputProvider {
int[] getInput();
int[] getInput() throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import pega.io.InputProvider;
import pega.io.OutputProvider;

import java.io.FileNotFoundException;
import java.io.IOException;

import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.*;

Expand All @@ -30,7 +33,7 @@ public void setUp() {

@Test
@UseDataProvider("dataProvider")
public void executeTest(int[] input, int[] output) {
public void executeTest(int[] input, int[] output) throws IOException {
given(inputProvider.getInput())
.willReturn(input);

Expand Down
72 changes: 72 additions & 0 deletions src/test/java/pega/io/FileInputProviderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package pega.io;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

import static org.junit.Assert.*;

@RunWith(MockitoJUnitRunner.class)
public class FileInputProviderTest {

@Rule
public TemporaryFolder folder = new TemporaryFolder();
private File file;

@Before
public void setUp() throws IOException {
file = folder.newFile("test");

try (RandomAccessFile accessFile = new RandomAccessFile(
file,
"rw"
)) {
for (int i = 0; i < 10; i++) {
accessFile.writeInt(i + 1);
}
}
}

@Test
public void testGetAllData() throws IOException {
FileInputProvider provider = new FileInputProvider(
file,
0,
10
);

int[] expectedResult = {1, 2 ,3 ,4 ,5, 6, 7, 8, 9, 10};
assertArrayEquals(expectedResult, provider.getInput());
}

@Test
public void testGetDataToMiddle() throws IOException {
FileInputProvider provider = new FileInputProvider(
file,
0,
5
);

int[] expectedResult = {1, 2 ,3 ,4 ,5};
assertArrayEquals(expectedResult, provider.getInput());
}

@Test
public void testGetDataFromMiddle() throws IOException {
FileInputProvider provider = new FileInputProvider(
file,
5,
5
);

int[] expectedResult = {6, 7, 8, 9, 10};
assertArrayEquals(expectedResult, provider.getInput());
}
}

0 comments on commit 5ad7d91

Please sign in to comment.