AbstractFileLineWriter implements FileLineWriter<T> { protected void init() { if (!calledInit) { FileFormat fileFormat = clazz.getAnnotation(FileFormat.class); buildFields(); if (isCheckEncloseChar()) { if (enclosed) { throw new FileException("columnEncloseChar can not change.", new IllegalStateException(), fileName); } } if (isCheckColumnAnnotationCount()) { if (fields.length == 0) { throw new FileException("OutputFileColumn is not found.", new IllegalStateException(), fileName); } } buildStringConverters(); buildMethods(); try { boolean append = !fileFormat.overWriteFlg(); writer = new BufferedWriter(new OutputStreamWriter((new FileOutputStream(fileName, append)), fileEncoding)); } catch (UnsupportedEncodingException e) { throw new FileException("Failed in generation of writer.", e, fileName); } catch (FileNotFoundException e) { throw new FileException("Failed in generation of writer.", e, fileName); } calledInit = true; } } AbstractFileLineWriter(String fileName, Class<T> clazz,
            Map<String, ColumnFormatter> columnFormatterMap); @Override void printHeaderLine(List<String> headerLine); @Override void printDataLine(T t); @Override void printTrailerLine(List<String> trailerLine); @Override void closeFile(); String getFileName(); void setColumnFormatterMap(
            Map<String, ColumnFormatter> columnFormatterMap); abstract char getDelimiter(); abstract char getEncloseChar();  }
@Test public void testInit01() throws Exception { String fileName = TEMP_FILE_NAME; Class<AbstractFileLineWriter_Stub01> clazz = AbstractFileLineWriter_Stub01.class; Map<String, ColumnFormatter> columnFormatterMap = new HashMap<String, ColumnFormatter>(); columnFormatterMap.put("int", new IntColumnFormatter()); columnFormatterMap.put("java.lang.String", new NullColumnFormatter()); AbstractFileLineWriter<AbstractFileLineWriter_Stub01> fileLineWriter = PowerMockito .spy(new AbstractFileLineWriterImpl01<AbstractFileLineWriter_Stub01>(fileName, clazz, columnFormatterMap)); BufferedWriter writer = null; BufferedReader postReader = null; try { ReflectionTestUtils.setField(fileLineWriter, "calledInit", Boolean.TRUE); writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName, true), "UTF-8")); ReflectionTestUtils.setField(fileLineWriter, "writer", writer); fileLineWriter.init(); assertSame(writer, ReflectionTestUtils.getField(fileLineWriter, "writer")); PowerMockito.verifyPrivate(fileLineWriter, Mockito.never()).invoke( "buildFields"); PowerMockito.verifyPrivate(fileLineWriter, Mockito.never()).invoke( "buildStringConverters"); PowerMockito.verifyPrivate(fileLineWriter, Mockito.never()).invoke( "buildMethods"); assertTrue(new File(fileName).exists()); postReader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8")); } finally { if (writer != null) { writer.close(); } if (postReader != null) { postReader.close(); } } }
@Test public void testInit02() throws Exception { String fileName = TEMP_FILE_NAME; Class<AbstractFileLineWriter_Stub01> clazz = AbstractFileLineWriter_Stub01.class; Map<String, ColumnFormatter> columnFormatterMap = new HashMap<String, ColumnFormatter>(); columnFormatterMap.put("int", new IntColumnFormatter()); columnFormatterMap.put("java.lang.String", new NullColumnFormatter()); AbstractFileLineWriter<AbstractFileLineWriter_Stub01> fileLineWriter = PowerMockito .spy(new AbstractFileLineWriterImpl01<AbstractFileLineWriter_Stub01>(fileName, clazz, columnFormatterMap)); ReflectionTestUtils.setField(fileLineWriter, "calledInit", Boolean.FALSE); ReflectionTestUtils.setField(fileLineWriter, "writer", null); BufferedReader postReader = null; try { fileLineWriter.init(); assertNotNull(ReflectionTestUtils.getField(fileLineWriter, "writer")); PowerMockito.verifyPrivate(fileLineWriter).invoke("buildFields"); PowerMockito.verifyPrivate(fileLineWriter).invoke( "buildStringConverters"); PowerMockito.verifyPrivate(fileLineWriter).invoke("buildMethods"); assertTrue(new File(fileName).exists()); postReader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8")); assertFalse(postReader.ready()); } finally { Writer writer = (Writer) ReflectionTestUtils.getField( fileLineWriter, "writer"); if (writer != null) { writer.close(); } if (postReader != null) { postReader.close(); } } }
@Test public void testInit03() throws Exception { String fileName = TEMP_FILE_NAME; Class<AbstractFileLineWriter_Stub06> clazz = AbstractFileLineWriter_Stub06.class; Map<String, ColumnFormatter> columnFormatterMap = new HashMap<String, ColumnFormatter>(); columnFormatterMap.put("int", new IntColumnFormatter()); columnFormatterMap.put("java.lang.String", new NullColumnFormatter()); AbstractFileLineWriter<AbstractFileLineWriter_Stub06> fileLineWriter = PowerMockito .spy(new AbstractFileLineWriterImpl01<AbstractFileLineWriter_Stub06>(fileName, clazz, columnFormatterMap)); ReflectionTestUtils.setField(fileLineWriter, "calledInit", Boolean.FALSE); ReflectionTestUtils.setField(fileLineWriter, "writer", null); BufferedReader postReader = null; try { fileLineWriter.init(); assertNotNull(ReflectionTestUtils.getField(fileLineWriter, "writer")); PowerMockito.verifyPrivate(fileLineWriter).invoke("buildFields"); PowerMockito.verifyPrivate(fileLineWriter).invoke( "buildStringConverters"); PowerMockito.verifyPrivate(fileLineWriter).invoke("buildMethods"); assertTrue(new File(fileName).exists()); postReader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8")); } finally { Writer writer = (Writer) ReflectionTestUtils.getField( fileLineWriter, "writer"); if (writer != null) { writer.close(); } if (postReader != null) { postReader.close(); } } }
@SuppressWarnings("rawtypes") @Test public void testPrintList02() throws Exception { String fileName = TEMP_FILE_NAME; Class<AbstractFileLineWriter_Stub10> clazz = AbstractFileLineWriter_Stub10.class; Map<String, ColumnFormatter> columnFormatterMap = new HashMap<String, ColumnFormatter>(); columnFormatterMap.put("int", new IntColumnFormatter()); columnFormatterMap.put("java.lang.String", new NullColumnFormatter()); AbstractFileLineWriter<AbstractFileLineWriter_Stub10> fileLineWriter = new AbstractFileLineWriterImpl01<AbstractFileLineWriter_Stub10>(fileName, clazz, columnFormatterMap); Reader postReader = null; Writer writer = null; try { fileLineWriter.init(); List<String> stringList = new ArrayList<String>(); ReflectionTestUtils.setField(fileLineWriter, "lineFeedChar", System .getProperty("line.separator")); ReflectionTestUtils.setField(fileLineWriter, "currentLineCount", 0); Method method = AbstractFileLineWriter.class.getDeclaredMethod( "printList", new Class[] { List.class }); method.setAccessible(true); method.invoke(fileLineWriter, new Object[] { stringList }); writer = (Writer) ReflectionTestUtils.getField(fileLineWriter, "writer"); writer.flush(); postReader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), System .getProperty("file.encoding"))); assertFalse(postReader.ready()); } finally { if (writer != null) { writer.close(); } if (postReader != null) { postReader.close(); } Field field = AbstractFileLineWriter.class.getDeclaredField( "stringConverterCacheMap"); field.setAccessible(true); field.set(AbstractFileLineWriter.class, new HashMap<Class, StringConverter>()); } }
@SuppressWarnings("rawtypes") @Test public void testPrintList03() throws Exception { String fileName = TEMP_FILE_NAME; Class<AbstractFileLineWriter_Stub10> clazz = AbstractFileLineWriter_Stub10.class; Map<String, ColumnFormatter> columnFormatterMap = new HashMap<String, ColumnFormatter>(); columnFormatterMap.put("int", new IntColumnFormatter()); columnFormatterMap.put("java.lang.String", new NullColumnFormatter()); AbstractFileLineWriter<AbstractFileLineWriter_Stub10> fileLineWriter = new AbstractFileLineWriterImpl01<AbstractFileLineWriter_Stub10>(fileName, clazz, columnFormatterMap); Reader postReader = null; Writer writer = null; try { fileLineWriter.init(); List<String> stringList = new ArrayList<String>(); stringList.add("testPrintList03_data1"); ReflectionTestUtils.setField(fileLineWriter, "lineFeedChar", System .getProperty("line.separator")); ReflectionTestUtils.setField(fileLineWriter, "currentLineCount", 0); Method method = AbstractFileLineWriter.class.getDeclaredMethod( "printList", new Class[] { List.class }); method.setAccessible(true); method.invoke(fileLineWriter, new Object[] { stringList }); writer = (Writer) ReflectionTestUtils.getField(fileLineWriter, "writer"); writer.flush(); postReader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), System .getProperty("file.encoding"))); assertTrue(postReader.ready()); } finally { if (writer != null) { writer.close(); } if (postReader != null) { postReader.close(); } Field field = AbstractFileLineWriter.class.getDeclaredField( "stringConverterCacheMap"); field.setAccessible(true); field.set(AbstractFileLineWriter.class, new HashMap<Class, StringConverter>()); } }
@SuppressWarnings("rawtypes") @Test public void testPrintList04() throws Exception { String fileName = TEMP_FILE_NAME; Class<AbstractFileLineWriter_Stub10> clazz = AbstractFileLineWriter_Stub10.class; Map<String, ColumnFormatter> columnFormatterMap = new HashMap<String, ColumnFormatter>(); columnFormatterMap.put("int", new IntColumnFormatter()); columnFormatterMap.put("java.lang.String", new NullColumnFormatter()); AbstractFileLineWriter<AbstractFileLineWriter_Stub10> fileLineWriter = new AbstractFileLineWriterImpl01<AbstractFileLineWriter_Stub10>(fileName, clazz, columnFormatterMap); Reader postReader = null; Writer writer = null; try { fileLineWriter.init(); List<String> stringList = new ArrayList<String>(); stringList.add("testPrintList04_data1"); stringList.add("testPrintList04_data2"); stringList.add("testPrintList04_data3"); ReflectionTestUtils.setField(fileLineWriter, "lineFeedChar", System .getProperty("line.separator")); ReflectionTestUtils.setField(fileLineWriter, "currentLineCount", 0); Method method = AbstractFileLineWriter.class.getDeclaredMethod( "printList", new Class[] { List.class }); method.setAccessible(true); method.invoke(fileLineWriter, new Object[] { stringList }); writer = (Writer) ReflectionTestUtils.getField(fileLineWriter, "writer"); writer.flush(); postReader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), System .getProperty("file.encoding"))); assertTrue(postReader.ready()); } finally { if (writer != null) { writer.close(); } if (postReader != null) { postReader.close(); } Field field = AbstractFileLineWriter.class.getDeclaredField( "stringConverterCacheMap"); field.setAccessible(true); field.set(AbstractFileLineWriter.class, new HashMap<Class, StringConverter>()); } }
@SuppressWarnings("rawtypes") @Test public void testPrintList05() throws Exception { String fileName = TEMP_FILE_NAME; Class<AbstractFileLineWriter_Stub10> clazz = AbstractFileLineWriter_Stub10.class; Map<String, ColumnFormatter> columnFormatterMap = new HashMap<String, ColumnFormatter>(); columnFormatterMap.put("int", new IntColumnFormatter()); columnFormatterMap.put("java.lang.String", new NullColumnFormatter()); AbstractFileLineWriter<AbstractFileLineWriter_Stub10> fileLineWriter = new AbstractFileLineWriterImpl01<AbstractFileLineWriter_Stub10>(fileName, clazz, columnFormatterMap); Reader postReader = null; Writer writer = null; try { fileLineWriter.init(); List<String> stringList = null; ReflectionTestUtils.setField(fileLineWriter, "lineFeedChar", System .getProperty("line.separator")); ReflectionTestUtils.setField(fileLineWriter, "currentLineCount", 0); Method method = AbstractFileLineWriter.class.getDeclaredMethod( "printList", new Class[] { List.class }); method.setAccessible(true); method.invoke(fileLineWriter, new Object[] { stringList }); fail("NullPointerExceptionが発生しませんでした。"); } catch (InvocationTargetException e) { assertTrue(e.getTargetException() instanceof NullPointerException); assertTrue(NullPointerException.class.isAssignableFrom(e .getTargetException().getClass())); writer = (Writer) ReflectionTestUtils.getField(fileLineWriter, "writer"); writer.flush(); postReader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), System .getProperty("file.encoding"))); assertFalse(postReader.ready()); } finally { if (writer != null) { writer.close(); } if (postReader != null) { postReader.close(); } Field field = AbstractFileLineWriter.class.getDeclaredField( "stringConverterCacheMap"); field.setAccessible(true); field.set(AbstractFileLineWriter.class, new HashMap<Class, StringConverter>()); } }