View | Details | Raw Unified | Return to bug 55752
Collapse All | Expand All

(-)src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestUnfixedBugs.java (-58 / +45 lines)
Lines 18-27 Link Here
18
package org.apache.poi.xssf.usermodel;
18
package org.apache.poi.xssf.usermodel;
19
19
20
import java.io.ByteArrayOutputStream;
20
import java.io.ByteArrayOutputStream;
21
import java.io.File;
22
import java.io.FileOutputStream;
23
import java.io.IOException;
21
import java.io.IOException;
24
import java.io.OutputStream;
25
import java.nio.charset.Charset;
22
import java.nio.charset.Charset;
26
import java.util.Calendar;
23
import java.util.Calendar;
27
import java.util.Date;
24
import java.util.Date;
Lines 220-283 Link Here
220
217
221
   @Test
218
   @Test
222
   public void testBug55752() throws IOException {
219
   public void testBug55752() throws IOException {
223
       Workbook wb = new XSSFWorkbook();
220
       final Workbook wb = new XSSFWorkbook();
224
       try {
221
       Sheet sheet = wb.createSheet("test");
225
           Sheet sheet = wb.createSheet("test");
222
226
    
223
       for (int i = 0; i < 4; i++) {
227
           for (int i = 0; i < 4; i++) {
224
           Row row = sheet.createRow(i);
228
               Row row = sheet.createRow(i);
225
           for (int j = 0; j < 2; j++) {
229
               for (int j = 0; j < 2; j++) {
226
               Cell cell = row.createCell(j);
230
                   Cell cell = row.createCell(j);
227
               cell.setCellStyle(wb.createCellStyle());
231
                   cell.setCellStyle(wb.createCellStyle());
232
               }
233
           }
228
           }
234
    
229
       }
235
           // set content
236
           Row row1 = sheet.getRow(0);
237
           row1.getCell(0).setCellValue("AAA");
238
           Row row2 = sheet.getRow(1);
239
           row2.getCell(0).setCellValue("BBB");
240
           Row row3 = sheet.getRow(2);
241
           row3.getCell(0).setCellValue("CCC");
242
           Row row4 = sheet.getRow(3);
243
           row4.getCell(0).setCellValue("DDD");
244
    
245
           // merge cells
246
           CellRangeAddress range1 = new CellRangeAddress(0, 0, 0, 1);
247
           sheet.addMergedRegion(range1);
248
           CellRangeAddress range2 = new CellRangeAddress(1, 1, 0, 1);
249
           sheet.addMergedRegion(range2);
250
           CellRangeAddress range3 = new CellRangeAddress(2, 2, 0, 1);
251
           sheet.addMergedRegion(range3);
252
           assertEquals(0, range3.getFirstColumn());
253
           assertEquals(1, range3.getLastColumn());
254
           assertEquals(2, range3.getLastRow());
255
           CellRangeAddress range4 = new CellRangeAddress(3, 3, 0, 1);
256
           sheet.addMergedRegion(range4);
257
           
258
           // set border
259
           RegionUtil.setBorderBottom(CellStyle.BORDER_THIN, range1, sheet, wb);
260
           
261
           row2.getCell(0).getCellStyle().setBorderBottom(CellStyle.BORDER_THIN);
262
           row2.getCell(1).getCellStyle().setBorderBottom(CellStyle.BORDER_THIN);
263
230
264
           Cell cell0 = CellUtil.getCell(row3, 0);
231
       // set content
265
           CellUtil.setCellStyleProperty(cell0, wb, CellUtil.BORDER_BOTTOM, CellStyle.BORDER_THIN);
232
       Row row1 = sheet.getRow(0);
266
           Cell cell1 = CellUtil.getCell(row3, 1);
233
       row1.getCell(0).setCellValue("AAA");
267
           CellUtil.setCellStyleProperty(cell1, wb, CellUtil.BORDER_BOTTOM, CellStyle.BORDER_THIN);
234
       Row row2 = sheet.getRow(1);
235
       row2.getCell(0).setCellValue("BBB");
236
       Row row3 = sheet.getRow(2);
237
       row3.getCell(0).setCellValue("CCC");
238
       Row row4 = sheet.getRow(3);
239
       row4.getCell(0).setCellValue("DDD");
268
240
269
           RegionUtil.setBorderBottom(CellStyle.BORDER_THIN, range4, sheet, wb);
241
       // merge cells
270
    
242
       CellRangeAddress range1 = new CellRangeAddress(0, 0, 0, 1);
271
           // write to file
243
       sheet.addMergedRegion(range1);
272
           OutputStream stream = new FileOutputStream(new File("C:/temp/55752.xlsx"));
244
       CellRangeAddress range2 = new CellRangeAddress(1, 1, 0, 1);
273
           try {
245
       sheet.addMergedRegion(range2);
274
               wb.write(stream);
246
       CellRangeAddress range3 = new CellRangeAddress(2, 2, 0, 1);
275
           } finally {
247
       sheet.addMergedRegion(range3);
276
               stream.close();
248
       assertEquals(0, range3.getFirstColumn());
277
           }
249
       assertEquals(1, range3.getLastColumn());
278
       } finally {
250
       assertEquals(2, range3.getLastRow());
279
           wb.close();
251
       CellRangeAddress range4 = new CellRangeAddress(3, 3, 0, 1);
280
       }
252
       sheet.addMergedRegion(range4);
253
       
254
       // set border
255
       RegionUtil.setBorderBottom(CellStyle.BORDER_THIN, range1, sheet, wb);
256
       
257
       row2.getCell(0).getCellStyle().setBorderBottom(CellStyle.BORDER_THIN);
258
       row2.getCell(1).getCellStyle().setBorderBottom(CellStyle.BORDER_THIN);
259
260
       Cell cell0 = CellUtil.getCell(row3, 0);
261
       CellUtil.setCellStyleProperty(cell0, wb, CellUtil.BORDER_BOTTOM, CellStyle.BORDER_THIN);
262
       Cell cell1 = CellUtil.getCell(row3, 1);
263
       CellUtil.setCellStyleProperty(cell1, wb, CellUtil.BORDER_BOTTOM, CellStyle.BORDER_THIN);
264
265
       RegionUtil.setBorderBottom(CellStyle.BORDER_THIN, range4, sheet, wb);
266
267
       XSSFTestDataSamples.writeOutToFile(wb, "bug55752");
281
   }
268
   }
282
269
283
   @Test
270
   @Test
(-)src/ooxml/testcases/org/apache/poi/xssf/XSSFTestDataSamples.java (-18 / +54 lines)
Lines 56-65 Link Here
56
	public static XSSFWorkbook openSampleWorkbook(String sampleName) {
56
	public static XSSFWorkbook openSampleWorkbook(String sampleName) {
57
		InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleName);
57
		InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleName);
58
		try {
58
		try {
59
			return new XSSFWorkbook(is);
59
			final XSSFWorkbook wb = new XSSFWorkbook(is);
60
			is.close();
61
			return wb;
60
		} catch (IOException e) {
62
		} catch (IOException e) {
61
			throw new RuntimeException(e);
63
			throw new RuntimeException(e);
62
		}
64
		}
65
		
63
	}
66
	}
64
    public static <R extends Workbook> R writeOutAndReadBack(R wb) {
67
    public static <R extends Workbook> R writeOutAndReadBack(R wb) {
65
    	Workbook result;
68
    	Workbook result;
Lines 84-89 Link Here
84
    }
87
    }
85
    
88
    
86
    /**
89
    /**
90
     * Writes the Workbook into a file using the system property {@value #TEST_OUTPUT_DIR}/testName.xlsx
91
     * @param wb workbook to write
92
     * @param testName file name to be used to save the workbook. The old file with the same name will be overridden.
93
     */
94
    public static <R extends Workbook> void writeOutToFile(R wb, String testName) throws IOException {
95
        final String test_output_dir = System.getProperty(TEST_OUTPUT_DIR);
96
        if (test_output_dir != null) {
97
            File file = new File(test_output_dir, testName + ".xlsx");
98
            writeOutToFile(wb, file);
99
        }
100
        else {
101
            throw new RuntimeException("System property " + TEST_OUTPUT_DIR + " must be set");
102
        }
103
    }
104
    
105
    
106
    /**
107
     * Writes the Workbook into the specified file
108
     * @param wb workbook to write
109
     * @param file location to save the workbook. An old file with the same name will be overridden.
110
     */
111
    public static <R extends Workbook> void writeOutToFile(R wb, File file) throws IOException {
112
        if (file.exists()) {
113
            file.delete();
114
        }
115
        FileOutputStream out = new FileOutputStream(file);
116
        try {
117
            wb.write(out);
118
        } finally {
119
            out.close();
120
        }
121
    }
122
    
123
    /**
124
     * Reads the Workbook from the specified file
125
     * @param file location to read the workbook.
126
     * @return workbook read in from file
127
     */
128
    public static XSSFWorkbook readInFromFile(File file) throws IOException {
129
        final InputStream is = new FileInputStream(file);
130
        XSSFWorkbook result = new XSSFWorkbook(is);
131
        return result;
132
    }
133
    
134
    /**
87
     * Writes the Workbook either into a file or into a byte array, depending on presence of 
135
     * Writes the Workbook either into a file or into a byte array, depending on presence of 
88
     * the system property {@value #TEST_OUTPUT_DIR}, and reads it in a new instance of the Workbook back.
136
     * the system property {@value #TEST_OUTPUT_DIR}, and reads it in a new instance of the Workbook back.
89
     * @param wb workbook to write
137
     * @param wb workbook to write
Lines 92-115 Link Here
92
     */
140
     */
93
    public static <R extends Workbook> R writeOutAndReadBack(R wb, String testName) {
141
    public static <R extends Workbook> R writeOutAndReadBack(R wb, String testName) {
94
        XSSFWorkbook result = null;
142
        XSSFWorkbook result = null;
95
        if (System.getProperty(TEST_OUTPUT_DIR) != null) {
143
        final String test_output_dir = System.getProperty(TEST_OUTPUT_DIR);
144
        if (test_output_dir != null) {
96
            try {
145
            try {
97
                File file = new File(System.getProperty(TEST_OUTPUT_DIR), testName + ".xlsx");
146
                File file = new File(test_output_dir, testName + ".xlsx");
98
                if (file.exists()) {
147
                writeOutToFile(wb, file);
99
                    file.delete();
148
                result = readInFromFile(file);
100
                }
101
                FileOutputStream out = new FileOutputStream(file);
102
                try {
103
                    wb.write(out);
104
                } finally {
105
                    out.close();
106
                }
107
                FileInputStream in = new FileInputStream(file);
108
                try {
109
                    result = new XSSFWorkbook(in);
110
                } finally {
111
                    in.close();
112
                }
113
            } catch (IOException e) {
149
            } catch (IOException e) {
114
                throw new RuntimeException(e);
150
                throw new RuntimeException(e);
115
            }
151
            }

Return to bug 55752