diff --git a/src/main/java/pega/command/executor/SortCommandExecutor.java b/src/main/java/pega/command/executor/SortCommandExecutor.java index a9907ff..8af856f 100644 --- a/src/main/java/pega/command/executor/SortCommandExecutor.java +++ b/src/main/java/pega/command/executor/SortCommandExecutor.java @@ -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); diff --git a/src/main/java/pega/io/FileInputProvider.java b/src/main/java/pega/io/FileInputProvider.java new file mode 100644 index 0000000..3d68cf9 --- /dev/null +++ b/src/main/java/pega/io/FileInputProvider.java @@ -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; + } +} diff --git a/src/main/java/pega/io/InputProvider.java b/src/main/java/pega/io/InputProvider.java index 169d39c..3d8c744 100644 --- a/src/main/java/pega/io/InputProvider.java +++ b/src/main/java/pega/io/InputProvider.java @@ -1,5 +1,8 @@ package pega.io; +import java.io.FileNotFoundException; +import java.io.IOException; + public interface InputProvider { - int[] getInput(); + int[] getInput() throws IOException; } diff --git a/src/test/java/pega/command/executor/SortCommandExecutorTest.java b/src/test/java/pega/command/executor/SortCommandExecutorTest.java index 3032aed..038a919 100644 --- a/src/test/java/pega/command/executor/SortCommandExecutorTest.java +++ b/src/test/java/pega/command/executor/SortCommandExecutorTest.java @@ -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.*; @@ -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); diff --git a/src/test/java/pega/io/FileInputProviderTest.java b/src/test/java/pega/io/FileInputProviderTest.java new file mode 100644 index 0000000..48ad9bf --- /dev/null +++ b/src/test/java/pega/io/FileInputProviderTest.java @@ -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()); + } +} \ No newline at end of file