PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/uk/ac/manchester/cs/owl/semspreadsheets/model/xssf/impl/WorkbookXSSFImplTest.java

https://github.com/semantalytics/RightField
Java | 147 lines | 108 code | 24 blank | 15 comment | 0 complexity | 3cbe71b1d3dc9e93a2b8a17b703afc4f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. package uk.ac.manchester.cs.owl.semspreadsheets.model.xssf.impl;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertFalse;
  4. import static org.junit.Assert.assertNotNull;
  5. import static org.junit.Assert.assertTrue;
  6. import static org.junit.Assert.fail;
  7. import java.io.BufferedOutputStream;
  8. import java.io.File;
  9. import java.io.FileOutputStream;
  10. import java.util.UUID;
  11. import org.apache.poi.xssf.usermodel.XSSFCell;
  12. import org.apache.poi.xssf.usermodel.XSSFRow;
  13. import org.apache.poi.xssf.usermodel.XSSFSheet;
  14. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  15. import org.apache.xmlbeans.impl.values.XmlValueDisconnectedException;
  16. import org.junit.Ignore;
  17. import org.junit.Test;
  18. import uk.ac.manchester.cs.owl.semspreadsheets.DocumentsCatalogue;
  19. import uk.ac.manchester.cs.owl.semspreadsheets.SpreadsheetTestHelper;
  20. import uk.ac.manchester.cs.owl.semspreadsheets.model.Cell;
  21. import uk.ac.manchester.cs.owl.semspreadsheets.model.InvalidWorkbookFormatException;
  22. import uk.ac.manchester.cs.owl.semspreadsheets.model.Sheet;
  23. import uk.ac.manchester.cs.owl.semspreadsheets.model.Workbook;
  24. import uk.ac.manchester.cs.owl.semspreadsheets.model.WorkbookFactory;
  25. import uk.ac.manchester.cs.owl.semspreadsheets.model.impl.GeneralWorkbookTests;
  26. public class WorkbookXSSFImplTest extends GeneralWorkbookTests {
  27. @Test
  28. @Ignore("No longer needed as we found a workaround, but kept test to check if its fixed in POI in the future")
  29. public void testColumnWidthPOI() throws Exception {
  30. XSSFWorkbook workbook = new XSSFWorkbook();
  31. XSSFSheet sheet = workbook.createSheet();
  32. XSSFRow row = sheet.createRow(0);
  33. XSSFCell cell = row.createCell(0);
  34. cell.setCellValue("hello world");
  35. workbook.getSheetAt(0).getColumnHelper().getColumn(0, false);
  36. assertEquals("hello world",workbook.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
  37. assertEquals(2048,workbook.getSheetAt(0).getColumnWidth(0));
  38. //gets a UUID based temporary file
  39. File tmpDir = new File(System.getProperty("java.io.tmpdir"));
  40. String uuid = UUID.randomUUID().toString();
  41. File f = new File(tmpDir,uuid+".xlsx");
  42. BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(f));
  43. workbook.write(stream);
  44. stream.close();
  45. assertTrue(f.exists());
  46. assertEquals("hello world",workbook.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
  47. workbook.getSheetAt(0).getColumnHelper().getColumn(0, false);
  48. assertEquals(2048,workbook.getSheetAt(0).getColumnWidth(0));
  49. }
  50. @Test
  51. public void testSaveWorkbookTwice() throws Exception {
  52. //there was a particular problem with XSSF where after the 2nd save the workbook became corrupted due to bug https://issues.apache.org/bugzilla/show_bug.cgi?id=52233
  53. //this test was originally to test a workaround, but the workaround has changed and is now here to spot flag if/when Apache POI is fixed.
  54. Workbook wb = getTestWorkbook();
  55. assertEquals(1,wb.getSheet(0).getValidations().size());
  56. File f = SpreadsheetTestHelper.getTempFile(getExtension());
  57. assertFalse(f.exists());
  58. wb.saveAs(f.toURI());
  59. assertTrue(f.exists());
  60. try {
  61. wb.saveAs(f.toURI());
  62. fail("GOOD News - its possible that this issue has been fixed by Apache POI");
  63. }
  64. catch(XmlValueDisconnectedException e) {
  65. //expected due to bug
  66. }
  67. try {
  68. wb = WorkbookFactory.createWorkbook(f.toURI());
  69. fail("GOOD News - its possible that this issue has been fixed by Apache POI");
  70. }
  71. catch(InvalidWorkbookFormatException e) {
  72. //expected due to bug
  73. }
  74. assertNotNull(wb);
  75. //this is to test a weird problem with the sheet column widths becoming corrupt after a save
  76. Sheet sheet = wb.getSheet(0);
  77. Cell cell = sheet.getCellAt(3, 11);
  78. assertEquals("Experimental Design",cell.getValue());
  79. assertEquals(1,wb.getSheet(0).getValidations().size());
  80. }
  81. @Test
  82. public void testSaveWorkbookTwice2() throws Exception {
  83. // related to testSaveWorkbookTwice, but a slightly different error due
  84. // to the content (but probably the same root cause)
  85. // this test was originally to test a workaround, but the workaround has
  86. // changed and is now here to spot flag if/when Apache POI is fixed.
  87. Workbook wb = getTestWorkbook();
  88. Sheet sheet = wb.getSheet(0);
  89. Cell cell = sheet.addCellAt(0, 0);
  90. cell.setValue("Fred");
  91. File f = SpreadsheetTestHelper.getTempFile(getExtension());
  92. assertFalse(f.exists());
  93. wb.saveAs(f.toURI());
  94. assertTrue(f.exists());
  95. try {
  96. wb.saveAs(f.toURI());
  97. fail("GOOD News - its possible that this issue has been fixed by Apache POI");
  98. } catch (XmlValueDisconnectedException e) {
  99. //expected due to bug
  100. }
  101. try {
  102. assertNotNull(WorkbookFactory.createWorkbook(f.toURI()));
  103. fail("GOOD News - its possible that this issue has been fixed by Apache POI");
  104. } catch (InvalidWorkbookFormatException e) {
  105. // expected due to bug
  106. }
  107. // this is to test a weird problem with the sheet column widths becoming
  108. // corrupt after a save
  109. sheet = wb.getSheet(0);
  110. cell = sheet.getCellAt(0, 0);
  111. assertEquals("Fred", cell.getValue());
  112. }
  113. protected Workbook getEmptyWorkbook() throws Exception {
  114. return SpreadsheetTestHelper.getBlankXSSFWorkbook();
  115. }
  116. //opens the workbook src/test/resources/simple_annotated_book.xls
  117. protected Workbook getTestWorkbook() throws Exception {
  118. return SpreadsheetTestHelper.openWorkbookXSSF(DocumentsCatalogue.simpleAnnotatedXLSXWorkbookURI());
  119. }
  120. protected String getExtension() {
  121. return "xlsx";
  122. }
  123. }