Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import static org.junit.Assert.*;
- import com.google.common.jimfs.Configuration;
- import com.google.common.jimfs.Jimfs;
- import java.io.IOException;
- import java.nio.charset.StandardCharsets;
- import java.nio.file.*;
- import java.util.Arrays;
- import junit.framework.AssertionFailedError;
- import org.junit.Before;
- import org.junit.Test;
- // uses https://github.com/google/jimfs
- public class BestWayToWriteMethodTest {
- private static final String TEST_FILE_NAME = "/testFileName.txt";
- private Path _rootPath;
- private BestWayToWriteMethod _bestWay;
- @Before
- public void before() {
- final FileSystem mockFileSystem = Jimfs.newFileSystem(Configuration.unix());
- _rootPath = mockFileSystem.getPath("/");
- _bestWay = new BestWayToWriteMethod(mockFileSystem);
- }
- @Test
- public void zeroDataLines() throws IOException {
- writeTestFileContents(TEST_FILE_NAME,
- "01 header",
- "99 trailer 000000000");
- _bestWay.processFile(TEST_FILE_NAME);
- }
- @Test
- public void simpleOneLineValidFile() throws IOException {
- writeTestFileContents(TEST_FILE_NAME,
- "01 header",
- "02 data",
- "99 trailer 000000001");
- _bestWay.processFile(TEST_FILE_NAME);
- }
- @Test
- public void simpleTwoLineValidFile() throws IOException {
- writeTestFileContents(TEST_FILE_NAME,
- "01 header",
- "02 line 1",
- "02 line 2",
- "99 trailer 000000002");
- _bestWay.processFile(TEST_FILE_NAME);
- }
- @Test
- public void firstLineNotHeader() throws IOException {
- writeTestFileContents(TEST_FILE_NAME,
- "NO Header");
- processFileExpectingAssertionFailedError(TEST_FILE_NAME,
- "First record must be a header record. Found record type 'NO' instead. Line #1 = NO Header");
- }
- @Test
- public void trailerCountWrong() throws IOException {
- writeTestFileContents(TEST_FILE_NAME,
- "01 header",
- "99 trailer 000123456");
- processFileExpectingAssertionFailedError(TEST_FILE_NAME,
- "Trailer record has incorrect record count. Trailer record has 123456. Actual detail record count 0.");
- }
- @Test
- public void trailerCountWrongWithDetails() throws IOException {
- writeTestFileContents(TEST_FILE_NAME,
- "01 header",
- "02 detail 1",
- "02 detail 2",
- "02 detail 3",
- "99 trailer 123456789");
- processFileExpectingAssertionFailedError(TEST_FILE_NAME,
- "Trailer record has incorrect record count. Trailer record has 123456789. Actual detail record count 3.");
- }
- @Test
- public void dataAfterTrailer() throws IOException {
- writeTestFileContents(TEST_FILE_NAME,
- "01 header",
- "02 detail 1",
- "99 trailer 000000001",
- "02 out of place detail record");
- processFileExpectingAssertionFailedError(TEST_FILE_NAME,
- "Invalid record after trailer on line 4: 02 out of place detail record");
- }
- @Test
- public void dataAfterTrailerWithDifferentCounts() throws IOException {
- writeTestFileContents(TEST_FILE_NAME,
- "01 header",
- "02 detail 1",
- "02 detail 2",
- "02 detail 3",
- "02 detail 4",
- "02 detail 5",
- "99 trailer 000000005",
- "02 another out of place record");
- processFileExpectingAssertionFailedError(TEST_FILE_NAME,
- "Invalid record after trailer on line 8: 02 another out of place record");
- }
- @Test
- public void outOfPlaceHeaderRecord() throws IOException {
- writeTestFileContents(TEST_FILE_NAME,
- "01 header",
- "02 detail 1",
- "01 out of place",
- "02 detail 2",
- "99 trailer 000000002");
- processFileExpectingAssertionFailedError(TEST_FILE_NAME,
- "Duplicate header record found at line 3: 01 out of place");
- }
- @Test
- public void invalidRecordType() throws IOException {
- writeTestFileContents(TEST_FILE_NAME,
- "01 header",
- "02 detail 1",
- "86 bad record type",
- "02 detail 2",
- "99 trailer 000000002");
- processFileExpectingAssertionFailedError(TEST_FILE_NAME,
- "Unrecognized record type '86' found at line 3: 86 bad record type");
- }
- private void writeTestFileContents(final String fileName, final String... lines) throws IOException {
- Files.write(_rootPath.resolve(fileName), Arrays.asList(lines), StandardCharsets.UTF_8);
- }
- private void processFileExpectingAssertionFailedError(final String fileName, final String expectedExceptionMessage) throws IOException {
- try {
- _bestWay.processFile(fileName);
- throw new IllegalStateException("Expected AssertionFailedError, with message '" + expectedExceptionMessage + "'.");
- } catch (final AssertionFailedError afe) {
- assertEquals(expectedExceptionMessage, afe.getMessage());
- }
- }
- }
Add Comment
Please, Sign In to add comment