Skip to content

Commit 1f91ec9

Browse files
committed
Add JUnit5 dependency and Write test code that write .csv report file
1 parent 1b655fa commit 1f91ec9

File tree

3 files changed

+115
-3
lines changed

3 files changed

+115
-3
lines changed

pom.xml

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,20 @@
33
<groupId>DBMonitoring</groupId>
44
<artifactId>DBMonitoring</artifactId>
55
<version>0.0.1-SNAPSHOT</version>
6+
<build>
7+
<plugins>
8+
<plugin>
9+
<groupId>org.apache.maven.plugins</groupId>
10+
<artifactId>maven-compiler-plugin</artifactId>
11+
<configuration>
12+
<source>9</source>
13+
<target>9</target>
14+
</configuration>
15+
</plugin>
16+
</plugins>
17+
</build>
618

7-
<dependencies>
19+
<dependencies>
820
<!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz -->
921
<dependency>
1022
<groupId>org.quartz-scheduler</groupId>
@@ -173,5 +185,14 @@
173185
<artifactId>commons-lang3</artifactId>
174186
<version>3.11</version>
175187
</dependency>
188+
189+
<!-- Junit-jupiter-api -->
190+
<dependency>
191+
<groupId>org.junit.jupiter</groupId>
192+
<artifactId>junit-jupiter-api</artifactId>
193+
<version>5.7.0</version>
194+
<scope>test</scope>
195+
</dependency>
196+
176197
</dependencies>
177198
</project>

src/main/java/root/utils/CsvUtils.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,27 @@ public static String createCsvHeader(List<String> fieldNames) {
5252
return sb.toString();
5353
}
5454

55+
/**
56+
*
57+
* @param fieldNames
58+
* @return
59+
*/
60+
public static String createCsvHeader(Class<?> clazz) {
61+
StringBuffer sb = new StringBuffer();
62+
63+
try {
64+
Field[] fields = clazz.getDeclaredFields();
65+
List<String> fieldName = Arrays.asList(fields).stream().map(f -> (f.getName()))
66+
.collect(Collectors.toList());
67+
68+
sb.append(createCsvHeader(fieldName));
69+
} catch (IllegalArgumentException e) {
70+
e.printStackTrace();
71+
}
72+
73+
return sb.toString();
74+
}
75+
5576
/**
5677
*
5778
* @param object
@@ -66,7 +87,9 @@ public static String createCsvRow(Object object, Class<?> clazz)
6687

6788
if (StringUtils.equals(object.getClass().getName(), clazz.getName())) {
6889
for (Field f : clazz.getDeclaredFields()) {
69-
boolean accessible = f.canAccess(object);
90+
@SuppressWarnings("deprecation")
91+
boolean accessible = f.isAccessible();
92+
7093
f.setAccessible(true);
7194

7295
String appender = sb.isEmpty() ? StringUtils.getIfEmpty(f.get(object).toString(), () -> "-")
@@ -79,5 +102,4 @@ public static String createCsvRow(Object object, Class<?> clazz)
79102

80103
return sb.toString();
81104
}
82-
83105
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package root.core.repository.implement;
2+
3+
import java.io.BufferedWriter;
4+
import java.io.File;
5+
import java.io.FileWriter;
6+
import java.util.List;
7+
8+
import org.apache.commons.lang3.StringUtils;
9+
import org.junit.jupiter.api.BeforeAll;
10+
import org.junit.jupiter.api.Test;
11+
12+
import root.core.domain.TableSpaceUsage;
13+
import root.utils.CsvUtils;
14+
import root.utils.DateUtils;
15+
16+
public class ReportRepositoryImplTest {
17+
18+
public static String rootDirectory = "./report";
19+
20+
@BeforeAll
21+
public static void setUp() {
22+
}
23+
24+
@Test
25+
public void writeReportFile_TableSpaceObj() {
26+
String filePath = "TableSpaceUsage";
27+
String fileName = "DB1";
28+
String fileExtension = ".csv";
29+
List<TableSpaceUsage> monitoringResult = List.of(
30+
new TableSpaceUsage("GGS_DATA", 17.67, 16.83, 95, .84),
31+
new TableSpaceUsage("SYSTEM", 3.2, 2.9, 91, .3),
32+
new TableSpaceUsage("DAISO_INDX", 1080, 960.09, 89, 119.91),
33+
new TableSpaceUsage("DAISO_TBS", 2130, 1719.55, 81, 410.45)
34+
);
35+
36+
try {
37+
File file = new File(rootDirectory + "/" + filePath + "/" + fileName + fileExtension);
38+
File parentDir = file.getParentFile();
39+
40+
boolean isNewFile = false;
41+
if (!file.exists()) {
42+
parentDir.mkdirs();
43+
file.createNewFile();
44+
isNewFile = true;
45+
}
46+
47+
String content = null;
48+
if (isNewFile) { // 첫 파일작성인 경우 헤더 추가
49+
content = StringUtils.joinWith(",", "MONITORING_DATE", "MONITORING_TIME",
50+
CsvUtils.createCsvHeader(TableSpaceUsage.class));
51+
}
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));
57+
content = StringUtils.joinWith(System.lineSeparator(), content, row);
58+
}
59+
60+
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
61+
bw.append(content);
62+
bw.flush();
63+
bw.close();
64+
65+
} catch (Exception e) {
66+
e.printStackTrace();
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)