Skip to content

Instantly share code, notes, and snippets.

@l-ray
Created May 2, 2014 11:25
Show Gist options
  • Save l-ray/11472684 to your computer and use it in GitHub Desktop.
Save l-ray/11472684 to your computer and use it in GitHub Desktop.
package de.lray.intercom.highscore;
import de.lray.intercom.pattern.Strategy;
import org.junit.Assert;
import org.junit.Test;
import java.io.*;
import static org.mockito.Mockito.*;
/**
* This worker traverses through a given text file and executes the given
* strategy on every read line.
*/
public class FileTraverser {
private static final String ENTRY_IS_NAN_MESSAGE = "Given line in file is not a number and gets ignored: ";
private static final String CURRENT_STATUS_MESSAGE = " numbers analyzed.";
private static final String STARTING_MESSAGE = "Starting... ";
public static final int SHOW_STATUS_EVERY = 1000000;
Strategy strategy;
String fileName = "";
PrintStream out;
/**
* Static constructor.
*
* @param fileName - source text file
* @param strategy - strategy to be used
* @return instance of FileTraverser
*/
public static FileTraverser createInstance(String fileName, Strategy strategy) {
FileTraverser traverser = new FileTraverser();
traverser.strategy = strategy;
traverser.fileName = fileName;
traverser.out = System.out;
return traverser;
}
/**
* Static constructor.
*
* @param fileName - source text file
* @param strategy - strategy to be used
* @param out - print stream for messages
* @return instance of FileTraverser
*/
public static FileTraverser createInstance(String fileName, Strategy strategy, PrintStream out) {
FileTraverser traverser = createInstance(fileName, strategy);
traverser.out = out;
return traverser;
}
/**
* Should be private, but as the tests are in the same file, and those need an
* public constructor, here it is.
*/
public FileTraverser() { }
/**
* Opens a reader for the given text file and traverses the text lines with
* the given strategy.
*
* @return current result of strategy
* @throws FileNotFoundException
*/
public Integer[] traverseWithStrategy() throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(this.fileName));
out.println(STARTING_MESSAGE +fileName);
return traverseWithStrategy(reader);
}
/**
* Traverses the readers the text lines with the given strategy.
*
* @param reader - input data
* @return current result of strategy
*/
public Integer[] traverseWithStrategy(BufferedReader reader) throws IOException {
String sourceValue;
long currentIndex = 0;
while((sourceValue = reader.readLine()) != null) {
try {
strategy.execute(Integer.valueOf(sourceValue));
} catch (NumberFormatException e) {
out.println(ENTRY_IS_NAN_MESSAGE + sourceValue);
}
currentIndex++;
if (currentIndex % SHOW_STATUS_EVERY == 0) {
out.println("" + currentIndex + CURRENT_STATUS_MESSAGE);
}
}
return strategy.getResult();
}
@Test
public void testRetrieveTopN() throws IOException {
BufferedReader bufferedReader = mock(BufferedReader.class);
when(bufferedReader.readLine())
.thenReturn("5")
.thenReturn("2")
.thenReturn("3")
.thenReturn("8")
.thenReturn("1")
.thenReturn(null);
Strategy mockStrategy = mock(Strategy.class);
when(mockStrategy.getResult()).thenReturn(new Integer[]{8, 5, 3});
this.strategy = mockStrategy;
Integer[] highScore = traverseWithStrategy(bufferedReader);
verify(mockStrategy, times(5)).execute(anyInt());
verify(mockStrategy, times(1)).execute(1);
verify(mockStrategy, times(1)).execute(2);
verify(mockStrategy, times(1)).execute(3);
verify(mockStrategy, times(1)).execute(5);
verify(mockStrategy, times(1)).execute(8);
Assert.assertArrayEquals(new Integer[]{8, 5, 3}, highScore);
}
@Test
public void testRetrieveTopNWithNullSource() throws IOException {
try {
traverseWithStrategy(null);
Assert.fail("Expected Exception being thrown.");
} catch (NullPointerException e) {
Assert.assertTrue(true);
}
}
@Test
public void testRetrieveTopNWithNullFileName() throws IOException {
try {
traverseWithStrategy();
Assert.fail("Expected Exception being thrown.");
} catch (FileNotFoundException e) {
Assert.assertTrue(true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment