JeffGrigg

BestWayToWriteMethodTest

Mar 25th, 2017
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.38 KB | None | 0 0
  1. import static org.junit.Assert.*;
  2.  
  3. import com.google.common.jimfs.Configuration;
  4. import com.google.common.jimfs.Jimfs;
  5.  
  6. import java.io.IOException;
  7. import java.nio.charset.StandardCharsets;
  8. import java.nio.file.*;
  9. import java.util.Arrays;
  10.  
  11. import junit.framework.AssertionFailedError;
  12. import org.junit.Before;
  13. import org.junit.Test;
  14.  
  15. // uses https://github.com/google/jimfs
  16.  
  17. public class BestWayToWriteMethodTest {
  18.  
  19.     private static final String TEST_FILE_NAME = "/testFileName.txt";
  20.  
  21.     private Path _rootPath;
  22.     private BestWayToWriteMethod _bestWay;
  23.  
  24.     @Before
  25.     public void before() {
  26.         final FileSystem mockFileSystem = Jimfs.newFileSystem(Configuration.unix());
  27.         _rootPath = mockFileSystem.getPath("/");
  28.  
  29.         _bestWay = new BestWayToWriteMethod(mockFileSystem);
  30.     }
  31.  
  32.     @Test
  33.     public void zeroDataLines() throws IOException {
  34.         writeTestFileContents(TEST_FILE_NAME,
  35.                 "01 header",
  36.                 "99 trailer  000000000");
  37.  
  38.         _bestWay.processFile(TEST_FILE_NAME);
  39.     }
  40.  
  41.     @Test
  42.     public void simpleOneLineValidFile() throws IOException {
  43.         writeTestFileContents(TEST_FILE_NAME,
  44.                 "01 header",
  45.                 "02 data",
  46.                 "99 trailer  000000001");
  47.  
  48.         _bestWay.processFile(TEST_FILE_NAME);
  49.     }
  50.  
  51.     @Test
  52.     public void simpleTwoLineValidFile() throws IOException {
  53.         writeTestFileContents(TEST_FILE_NAME,
  54.                 "01 header",
  55.                 "02 line 1",
  56.                 "02 line 2",
  57.                 "99 trailer  000000002");
  58.  
  59.         _bestWay.processFile(TEST_FILE_NAME);
  60.     }
  61.  
  62.     @Test
  63.     public void firstLineNotHeader() throws IOException {
  64.         writeTestFileContents(TEST_FILE_NAME,
  65.                 "NO Header");
  66.  
  67.         processFileExpectingAssertionFailedError(TEST_FILE_NAME,
  68.                 "First record must be a header record. Found record type 'NO' instead. Line #1 = NO Header");
  69.     }
  70.  
  71.     @Test
  72.     public void trailerCountWrong() throws IOException {
  73.         writeTestFileContents(TEST_FILE_NAME,
  74.                 "01 header",
  75.                 "99 trailer  000123456");
  76.  
  77.         processFileExpectingAssertionFailedError(TEST_FILE_NAME,
  78.                 "Trailer record has incorrect record count. Trailer record has 123456. Actual detail record count 0.");
  79.     }
  80.  
  81.     @Test
  82.     public void trailerCountWrongWithDetails() throws IOException {
  83.         writeTestFileContents(TEST_FILE_NAME,
  84.                 "01 header",
  85.                 "02 detail 1",
  86.                 "02 detail 2",
  87.                 "02 detail 3",
  88.                 "99 trailer  123456789");
  89.  
  90.         processFileExpectingAssertionFailedError(TEST_FILE_NAME,
  91.                 "Trailer record has incorrect record count. Trailer record has 123456789. Actual detail record count 3.");
  92.     }
  93.  
  94.     @Test
  95.     public void dataAfterTrailer() throws IOException {
  96.         writeTestFileContents(TEST_FILE_NAME,
  97.                 "01 header",
  98.                 "02 detail 1",
  99.                 "99 trailer  000000001",
  100.                 "02 out of place detail record");
  101.  
  102.         processFileExpectingAssertionFailedError(TEST_FILE_NAME,
  103.                 "Invalid record after trailer on line 4: 02 out of place detail record");
  104.     }
  105.  
  106.     @Test
  107.     public void dataAfterTrailerWithDifferentCounts() throws IOException {
  108.         writeTestFileContents(TEST_FILE_NAME,
  109.                 "01 header",
  110.                 "02 detail 1",
  111.                 "02 detail 2",
  112.                 "02 detail 3",
  113.                 "02 detail 4",
  114.                 "02 detail 5",
  115.                 "99 trailer  000000005",
  116.                 "02 another out of place record");
  117.  
  118.         processFileExpectingAssertionFailedError(TEST_FILE_NAME,
  119.                 "Invalid record after trailer on line 8: 02 another out of place record");
  120.     }
  121.  
  122.     @Test
  123.     public void outOfPlaceHeaderRecord() throws IOException {
  124.         writeTestFileContents(TEST_FILE_NAME,
  125.                 "01 header",
  126.                 "02 detail 1",
  127.                 "01 out of place",
  128.                 "02 detail 2",
  129.                 "99 trailer  000000002");
  130.  
  131.         processFileExpectingAssertionFailedError(TEST_FILE_NAME,
  132.                 "Duplicate header record found at line 3: 01 out of place");
  133.     }
  134.  
  135.     @Test
  136.     public void invalidRecordType() throws IOException {
  137.         writeTestFileContents(TEST_FILE_NAME,
  138.                 "01 header",
  139.                 "02 detail 1",
  140.                 "86 bad record type",
  141.                 "02 detail 2",
  142.                 "99 trailer  000000002");
  143.  
  144.         processFileExpectingAssertionFailedError(TEST_FILE_NAME,
  145.                 "Unrecognized record type '86' found at line 3: 86 bad record type");
  146.     }
  147.  
  148.     private void writeTestFileContents(final String fileName, final String... lines) throws IOException {
  149.         Files.write(_rootPath.resolve(fileName), Arrays.asList(lines), StandardCharsets.UTF_8);
  150.     }
  151.  
  152.     private void processFileExpectingAssertionFailedError(final String fileName, final String expectedExceptionMessage) throws IOException {
  153.         try {
  154.             _bestWay.processFile(fileName);
  155.             throw new IllegalStateException("Expected AssertionFailedError, with message '" + expectedExceptionMessage + "'.");
  156.         } catch (final AssertionFailedError afe) {
  157.             assertEquals(expectedExceptionMessage, afe.getMessage());
  158.         }
  159.     }
  160.  
  161. }
Add Comment
Please, Sign In to add comment