|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+package com.ruoyi.web.modules.industryservice.support;
|
|
|
2
|
+
|
|
|
3
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
4
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
5
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
6
|
+
|
|
|
7
|
+import java.io.ByteArrayInputStream;
|
|
|
8
|
+import java.math.BigDecimal;
|
|
|
9
|
+import java.util.Arrays;
|
|
|
10
|
+import java.util.Collections;
|
|
|
11
|
+import org.apache.poi.ss.usermodel.Row;
|
|
|
12
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
|
13
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
14
|
+import org.apache.poi.ss.usermodel.WorkbookFactory;
|
|
|
15
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
16
|
+import org.junit.jupiter.api.Test;
|
|
|
17
|
+import com.ruoyi.web.modules.industryservice.domain.BizCooperativeDevelopment;
|
|
|
18
|
+
|
|
|
19
|
+@DisplayName("CooperativeDevelopmentExcelExporter")
|
|
|
20
|
+class CooperativeDevelopmentExcelExporterTest
|
|
|
21
|
+{
|
|
|
22
|
+ private final CooperativeDevelopmentExcelExporter exporter = new CooperativeDevelopmentExcelExporter();
|
|
|
23
|
+
|
|
|
24
|
+ @Test
|
|
|
25
|
+ @DisplayName("导出仅数据行、不含合计")
|
|
|
26
|
+ void exportWithoutTotalRow() throws Exception
|
|
|
27
|
+ {
|
|
|
28
|
+ BizCooperativeDevelopment a = sample("雅安镇", "雅安镇荣嘎村", "合作社A", 10, 20);
|
|
|
29
|
+ BizCooperativeDevelopment b = sample("雅安镇", "雅安镇公随村", "合作社B", 5, 15);
|
|
|
30
|
+ byte[] bytes = exporter.export(2026, 7, Arrays.asList(a, b), "admin");
|
|
|
31
|
+ assertNotNull(bytes);
|
|
|
32
|
+ assertTrue(bytes.length > 1000);
|
|
|
33
|
+
|
|
|
34
|
+ try (Workbook wb = WorkbookFactory.create(new ByteArrayInputStream(bytes)))
|
|
|
35
|
+ {
|
|
|
36
|
+ Sheet sheet = CooperativeDevelopmentExcelParser.resolveSheet(wb);
|
|
|
37
|
+ int header = CooperativeDevelopmentExcelParser.detectHeaderRow(sheet);
|
|
|
38
|
+ int dataStart = header + CooperativeDevelopmentExcelParser.HEADER_SPAN;
|
|
|
39
|
+ assertEquals(dataStart + 1, sheet.getLastRowNum());
|
|
|
40
|
+ Row last = sheet.getRow(sheet.getLastRowNum());
|
|
|
41
|
+ assertNotNull(last);
|
|
|
42
|
+ assertEquals("合作社B", CooperativeDevelopmentExcelParser.cellString(last,
|
|
|
43
|
+ CooperativeDevelopmentExcelParser.COL_COOP_NAME));
|
|
|
44
|
+ for (int r = dataStart; r <= sheet.getLastRowNum(); r++)
|
|
|
45
|
+ {
|
|
|
46
|
+ String seq = CooperativeDevelopmentExcelParser.cellString(sheet.getRow(r), 0);
|
|
|
47
|
+ assertTrue(seq == null || !seq.contains("合计"), "不应出现合计行");
|
|
|
48
|
+ }
|
|
|
49
|
+ }
|
|
|
50
|
+ }
|
|
|
51
|
+
|
|
|
52
|
+ @Test
|
|
|
53
|
+ @DisplayName("第3行及以后复用模板数据行边框样式")
|
|
|
54
|
+ void exportThirdRowKeepsBorderStyle() throws Exception
|
|
|
55
|
+ {
|
|
|
56
|
+ BizCooperativeDevelopment a = sample("雅安镇", "雅安镇荣嘎村", "合作社A", 10, 20);
|
|
|
57
|
+ BizCooperativeDevelopment b = sample("雅安镇", "雅安镇公随村", "合作社B", 5, 15);
|
|
|
58
|
+ BizCooperativeDevelopment c = sample("雅安镇", "雅安镇荣嘎村", "合作社C", 8, 12);
|
|
|
59
|
+ byte[] bytes = exporter.export(2026, 7, Arrays.asList(a, b, c), "admin");
|
|
|
60
|
+ try (Workbook wb = WorkbookFactory.create(new ByteArrayInputStream(bytes)))
|
|
|
61
|
+ {
|
|
|
62
|
+ Sheet sheet = CooperativeDevelopmentExcelParser.resolveSheet(wb);
|
|
|
63
|
+ int header = CooperativeDevelopmentExcelParser.detectHeaderRow(sheet);
|
|
|
64
|
+ int dataStart = header + CooperativeDevelopmentExcelParser.HEADER_SPAN;
|
|
|
65
|
+ Row first = sheet.getRow(dataStart);
|
|
|
66
|
+ Row third = sheet.getRow(dataStart + 2);
|
|
|
67
|
+ assertNotNull(first);
|
|
|
68
|
+ assertNotNull(third);
|
|
|
69
|
+ assertEquals("合作社C", CooperativeDevelopmentExcelParser.cellString(third,
|
|
|
70
|
+ CooperativeDevelopmentExcelParser.COL_COOP_NAME));
|
|
|
71
|
+ org.apache.poi.ss.usermodel.BorderStyle expected = first.getCell(0).getCellStyle().getBorderBottom();
|
|
|
72
|
+ assertTrue(expected != org.apache.poi.ss.usermodel.BorderStyle.NONE);
|
|
|
73
|
+ assertEquals(expected, third.getCell(0).getCellStyle().getBorderBottom());
|
|
|
74
|
+ assertEquals(expected, third.getCell(CooperativeDevelopmentExcelParser.COL_LAST).getCellStyle()
|
|
|
75
|
+ .getBorderBottom());
|
|
|
76
|
+ }
|
|
|
77
|
+ }
|
|
|
78
|
+
|
|
|
79
|
+ @Test
|
|
|
80
|
+ @DisplayName("无数据时不写合计行")
|
|
|
81
|
+ void exportEmptyNoTotal() throws Exception
|
|
|
82
|
+ {
|
|
|
83
|
+ byte[] bytes = exporter.export(2026, 7, Collections.emptyList(), "admin");
|
|
|
84
|
+ try (Workbook wb = WorkbookFactory.create(new ByteArrayInputStream(bytes)))
|
|
|
85
|
+ {
|
|
|
86
|
+ Sheet sheet = CooperativeDevelopmentExcelParser.resolveSheet(wb);
|
|
|
87
|
+ int header = CooperativeDevelopmentExcelParser.detectHeaderRow(sheet);
|
|
|
88
|
+ int dataStart = header + CooperativeDevelopmentExcelParser.HEADER_SPAN;
|
|
|
89
|
+ assertTrue(sheet.getLastRowNum() < dataStart);
|
|
|
90
|
+ for (int r = 0; r <= sheet.getLastRowNum(); r++)
|
|
|
91
|
+ {
|
|
|
92
|
+ Row row = sheet.getRow(r);
|
|
|
93
|
+ if (row == null)
|
|
|
94
|
+ {
|
|
|
95
|
+ continue;
|
|
|
96
|
+ }
|
|
|
97
|
+ String seq = CooperativeDevelopmentExcelParser.cellString(row, 0);
|
|
|
98
|
+ assertTrue(seq == null || !seq.contains("合计"));
|
|
|
99
|
+ }
|
|
|
100
|
+ }
|
|
|
101
|
+ }
|
|
|
102
|
+
|
|
|
103
|
+ private static BizCooperativeDevelopment sample(String town, String village, String coop, int hh, int pop)
|
|
|
104
|
+ {
|
|
|
105
|
+ BizCooperativeDevelopment row = new BizCooperativeDevelopment();
|
|
|
106
|
+ row.setTownName(town);
|
|
|
107
|
+ row.setVillageDeptId((long) village.hashCode());
|
|
|
108
|
+ row.setVillageName(village);
|
|
|
109
|
+ row.setCooperativeName(coop);
|
|
|
110
|
+ row.setMemberHouseholdCount(hh);
|
|
|
111
|
+ row.setMemberPopulationCount(pop);
|
|
|
112
|
+ row.setRegisteredCapital(new BigDecimal("100"));
|
|
|
113
|
+ row.setYearEndTotalAssets(new BigDecimal("200"));
|
|
|
114
|
+ row.setTotalIncome(new BigDecimal("50"));
|
|
|
115
|
+ return row;
|
|
|
116
|
+ }
|
|
|
117
|
+}
|