PlainFileLineWriter extends AbstractFileLineWriter<String> { @Override public void printDataLine(String t) { checkWriteTrailer(); try { getWriter().write(t); getWriter().write(getLineFeedChar()); } catch (IOException e) { throw new FileException("writer control operation was failed.", e, getFileName()); } setWriteData(true); } @SuppressWarnings("unchecked")  PlainFileLineWriter(String fileName, @SuppressWarnings("rawtypes") Class clazz,
            Map<String, ColumnFormatter> columnFormatterMap); @Override void printDataLine(String t); @Override char getDelimiter(); @Override char getEncloseChar();  }
@Test public void testPrintDataLine01() throws Exception { String fileName = TEMP_FILE_NAME; Map<String, ColumnFormatter> columnFormatterMap = new HashMap<String, ColumnFormatter>(); columnFormatterMap.put("java.lang.String", new NullColumnFormatter()); PlainFileLineWriter plainFileLineWriter = Mockito.spy(new PlainFileLineWriter( fileName, PlainFileLineWriter_Stub01.class, columnFormatterMap)); String t = "データ"; plainFileLineWriter.printDataLine(t); Mockito.verify(plainFileLineWriter).checkWriteTrailer(); Mockito.verify(plainFileLineWriter).getLineFeedChar(); Mockito.verify(plainFileLineWriter, Mockito.times(2)).getWriter(); Mockito.verify(plainFileLineWriter).setWriteData(Mockito.anyBoolean()); plainFileLineWriter.closeFile(); }
@Test public void testPrintDataLine02() throws Exception { String fileName = TEMP_FILE_NAME; Class<PlainFileLineWriter_Stub01> clazz = PlainFileLineWriter_Stub01.class; Map<String, ColumnFormatter> columnFormatterMap = new HashMap<String, ColumnFormatter>(); columnFormatterMap.put("java.lang.String", new NullColumnFormatter()); PlainFileLineWriter plainFileLineWriter = Mockito.spy(new PlainFileLineWriter( fileName, clazz, columnFormatterMap)); String t = "データ"; IOException exception = new IOException(); Writer writer = PowerMockito.mock(Writer.class); Mockito.doThrow(exception).when(writer).write(Mockito.anyString()); Mockito.doReturn(writer).when(plainFileLineWriter).getWriter(); try { plainFileLineWriter.printDataLine(t); fail("FileExceptionがスローされませんでした"); } catch (FileException e) { Mockito.verify(plainFileLineWriter).checkWriteTrailer(); Mockito.verify(plainFileLineWriter, Mockito.never()).getLineFeedChar(); Mockito.verify(plainFileLineWriter).getWriter(); Mockito.verify(plainFileLineWriter, Mockito.never()).setWriteData(Mockito.anyBoolean()); assertSame(FileException.class, e.getClass()); assertEquals("writer control operation was failed.", e.getMessage()); assertSame(exception, e.getCause()); assertEquals(fileName, e.getFileName()); } finally { plainFileLineWriter.closeFile(); } }
@Test public void testPrintDataLine03() throws Exception { String fileName = TEMP_FILE_NAME; Class<PlainFileLineWriter_Stub01> clazz = PlainFileLineWriter_Stub01.class; Map<String, ColumnFormatter> columnFormatterMap = new HashMap<String, ColumnFormatter>(); columnFormatterMap.put("java.lang.String", new NullColumnFormatter()); PlainFileLineWriter plainFileLineWriter = Mockito.spy(new PlainFileLineWriter( fileName, clazz, columnFormatterMap)); String t = "データ"; FileException exception = new FileException("checkWriteTrailerからのエラーです"); Mockito.doThrow(exception).when(plainFileLineWriter).checkWriteTrailer(); Writer writer = PowerMockito.mock(Writer.class); Mockito.doReturn(writer).when(plainFileLineWriter).getWriter(); try { plainFileLineWriter.printDataLine(t); fail("FileExceptionがスローされませんでした"); } catch (FileException e) { Mockito.verify(plainFileLineWriter).checkWriteTrailer(); Mockito.verify(writer, Mockito.never()).write(Mockito.anyString()); Mockito.verify(plainFileLineWriter, Mockito.never()).getLineFeedChar(); Mockito.verify(plainFileLineWriter, Mockito.never()).getWriter(); Mockito.verify(plainFileLineWriter, Mockito.never()).setWriteData(Mockito.anyBoolean()); assertSame(exception, e); } finally { plainFileLineWriter.closeFile(); } }
@Test public void testPrintDataLine04() throws Exception { String fileName = TEMP_FILE_NAME; Class<PlainFileLineWriter_Stub01> clazz = PlainFileLineWriter_Stub01.class; Map<String, ColumnFormatter> columnFormatterMap = new HashMap<String, ColumnFormatter>(); columnFormatterMap.put("java.lang.String", new NullColumnFormatter()); PlainFileLineWriter plainFileLineWriter = new PlainFileLineWriter( fileName, clazz, columnFormatterMap); String t = null; try { plainFileLineWriter.printDataLine(t); fail("NullPointerExceptionがスローされませんでした"); } catch (NullPointerException e) { assertSame(NullPointerException.class, e.getClass()); } finally { plainFileLineWriter.closeFile(); } }
@Test public void testPrintDataLine05() throws Exception { String fileName = TEMP_FILE_NAME; Map<String, ColumnFormatter> columnFormatterMap = new HashMap<String, ColumnFormatter>(); columnFormatterMap.put("java.lang.String", new NullColumnFormatter()); PlainFileLineWriter fileLineWriter = new PlainFileLineWriter(fileName, PlainFileLineIterator_Stub02.class, columnFormatterMap); fileLineWriter.printDataLine("\"1\",'22',\"333\",|4444|"); fileLineWriter.printDataLine("\"5\",'66',\"777\",|8888|"); fileLineWriter.printDataLine("\"9\",'AA',\"BBB\",|CCCC|"); fileLineWriter.closeFile(); BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader( new FileInputStream(fileName), System .getProperty("file.encoding"))); assertEquals("\"1\",'22',\"333\",|4444|", reader.readLine()); assertEquals("\"5\",'66',\"777\",|8888|", reader.readLine()); assertEquals("\"9\",'AA',\"BBB\",|CCCC|", reader.readLine()); } finally { reader.close(); } }