33import java .io .BufferedWriter ;
44import java .io .File ;
55import java .io .FileWriter ;
6+ import java .io .IOException ;
67import java .util .List ;
78
89import org .apache .commons .lang3 .StringUtils ;
910import org .junit .jupiter .api .BeforeAll ;
1011import org .junit .jupiter .api .Test ;
1112
13+ import root .core .domain .MonitoringResult ;
1214import root .core .domain .TableSpaceUsage ;
1315import root .utils .CsvUtils ;
14- import root .utils .DateUtils ;
1516
1617public class ReportRepositoryImplTest {
1718
@@ -26,16 +27,19 @@ public void writeReportFile_TableSpaceObj() {
2627 String filePath = "TableSpaceUsage" ;
2728 String fileName = "DB1" ;
2829 String fileExtension = ".csv" ;
29- List <TableSpaceUsage > monitoringResult = List .of (
30- new TableSpaceUsage ("GGS_DATA" , 17.67 , 16.83 , 95 , .84 ),
30+ List <TableSpaceUsage > list = List .of (new TableSpaceUsage ("GGS_DATA" , 17.67 , 16.83 , 95 , .84 ),
3131 new TableSpaceUsage ("SYSTEM" , 3.2 , 2.9 , 91 , .3 ),
3232 new TableSpaceUsage ("DAISO_INDX" , 1080 , 960.09 , 89 , 119.91 ),
33- new TableSpaceUsage ("DAISO_TBS" , 2130 , 1719.55 , 81 , 410.45 )
34- );
33+ new TableSpaceUsage ("DAISO_TBS" , 2130 , 1719.55 , 81 , 410.45 ));
34+
35+ MonitoringResult <TableSpaceUsage > monitoringResult = new MonitoringResult <>(list );
36+
37+ File file = new File (rootDirectory + "/" + filePath + "/" + fileName + fileExtension );
38+ File parentDir = file .getParentFile ();
39+
40+ String content = null ;
3541
3642 try {
37- File file = new File (rootDirectory + "/" + filePath + "/" + fileName + fileExtension );
38- File parentDir = file .getParentFile ();
3943
4044 boolean isNewFile = false ;
4145 if (!file .exists ()) {
@@ -44,26 +48,33 @@ public void writeReportFile_TableSpaceObj() {
4448 isNewFile = true ;
4549 }
4650
47- String content = null ;
4851 if (isNewFile ) { // 첫 파일작성인 경우 헤더 추가
4952 content = StringUtils .joinWith ("," , "MONITORING_DATE" , "MONITORING_TIME" ,
5053 CsvUtils .createCsvHeader (TableSpaceUsage .class ));
5154 }
52-
53- String now = DateUtils .getToday ("yyyyMMddHHmmss" );
54- for (TableSpaceUsage t : monitoringResult ) {
55- String row = StringUtils .joinWith ("," , now .substring (0 , 8 ), now .substring (8 ),
56- CsvUtils .createCsvRow (t , TableSpaceUsage .class ));
55+
56+ String monitoringDay = monitoringResult .getMonitoringDay ();
57+ String monitoringTime = monitoringResult .getMonitoringTime ();
58+ for (Object t : monitoringResult .getMonitoringResults ()) {
59+ String row = StringUtils .joinWith ("," , monitoringDay , monitoringTime ,
60+ CsvUtils .createCsvRow (t , t .getClass ()));
5761 content = StringUtils .joinWith (System .lineSeparator (), content , row );
58- }
59-
60- BufferedWriter bw = new BufferedWriter (new FileWriter (file , true ));
62+ }
63+ } catch (Exception e ) {
64+ e .printStackTrace ();
65+ }
66+
67+ if (content == null ) {
68+ System .out .println (String .format ("파일에 작성할 내용이 없습니다. 파일경로: %s" , file .getPath ()));
69+ return ;
70+ }
71+
72+ try (BufferedWriter bw = new BufferedWriter (new FileWriter (file , true ))) {
6173 bw .append (content );
6274 bw .flush ();
6375 bw .close ();
64-
65- } catch (Exception e ) {
76+ } catch (IOException e ) {
6677 e .printStackTrace ();
67- }
78+ }
6879 }
6980}
0 commit comments