package org.apache.poi.ss.unfixedBugs; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.awt.Desktop; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.List; import java.util.Locale; import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.util.AreaReference; import org.apache.poi.ss.util.CellReference; import org.apache.poi.xddf.usermodel.chart.AxisPosition; import org.apache.poi.xddf.usermodel.chart.ChartTypes; import org.apache.poi.xddf.usermodel.chart.XDDFChart; import org.apache.poi.xddf.usermodel.chart.XDDFChartData.Series; import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory; import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource; import org.apache.poi.xddf.usermodel.chart.XDDFScatterChartData; import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFClientAnchor; import org.apache.poi.xssf.usermodel.XSSFDrawing; import org.apache.poi.xssf.usermodel.XSSFName; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFTable; import org.apache.poi.xssf.usermodel.XSSFTableColumn; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.junit.After; import org.junit.Before; import org.junit.Test; /** * Test case for bug 63153 */ public class TestXDDFChartRemoveSeries { final File resultDir=new File("build/custom-reports-test"); String fileName=null; XSSFWorkbook workbook=null; XSSFSheet sheet=null; XDDFScatterChartData m_chartData = null; XDDFChart m_chart=null; final int MAX_NUM_SERIES=1; public TestXDDFChartRemoveSeries() { resultDir.mkdirs(); } /** * This creates a workbook with one worksheet, which contains a single scatter chart. */ @Before public void setup() { final boolean bDebug=false; workbook = new XSSFWorkbook(); sheet = workbook.createSheet(); final XSSFDrawing xssfDrawing = sheet.createDrawingPatriarch(); final XSSFClientAnchor anchor = xssfDrawing.createAnchor(0, 0, 0, 0, 1, 5, 20, 20); if(bDebug) return; m_chart = xssfDrawing.createChart(anchor); final XDDFValueAxis bottomAxis = m_chart.createValueAxis(AxisPosition.BOTTOM); final XDDFValueAxis leftAxis = m_chart.createValueAxis(AxisPosition.LEFT); // Initialize data data sources final Double dX[] = new Double[5]; final Double dY1[] = new Double[5]; final Double dY2[] = new Double[5]; for (int n = 0; n < 5; ++n) { dX[n] = (double) n; dY1[n] = 2.0 * n; dY2[n] = (double) (n * n); } final XDDFNumericalDataSource xData = XDDFDataSourcesFactory.fromArray(dX, null); final XDDFNumericalDataSource yData1 = XDDFDataSourcesFactory.fromArray(dY1, null); final XDDFNumericalDataSource yData2 = XDDFDataSourcesFactory.fromArray(dY2, null); // Create the chartdata m_chartData = (XDDFScatterChartData) m_chart.createData(ChartTypes.SCATTER, bottomAxis, leftAxis); // Add the series m_chartData.addSeries(xData, yData1); m_chartData.addSeries(xData, yData2); } /** * This method writes the workbook to resultDir/fileName. */ @After public void cleanup() { final String procName="TestXSSFTableRemoveColumn.cleanup"; if (workbook == null) { System.out.println(String.format(Locale.ROOT,"%s: workbook==null",procName)); return; } if(fileName==null) { System.out.println(String.format(Locale.ROOT, "%s: fileName==null",procName)); return; } // Finish up m_chart.plot(m_chartData); final int nSheet = workbook.getSheetIndex(sheet); workbook.setSelectedTab(nSheet); workbook.setActiveSheet(nSheet); workbook.setFirstVisibleTab(nSheet); final File file=new File(resultDir,fileName); try (OutputStream fileOut = new FileOutputStream(file)) { workbook.write(fileOut); System.out.println(String.format(Locale.ROOT, "%s: test file written to %s",procName,file.getAbsolutePath())); } catch (Exception e) { System.err.println(e.getMessage()); } finally { try { workbook.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * Remove the first series by calling m_chartData.getSeries().remove(0). *

* This will corrupt the workbook. */ @Test public void testRemoveSeries0() { final String procName="testRemoveSeries0"; fileName=procName+".xlsx"; m_chartData.getSeries().remove(0); } /** * Remove the second series by calling m_chartData.getSeries().remove(1). *

* This will corrupt the workbook. */ @Test public void testRemoveSeries1() { final String procName="testRemoveSeries1"; fileName=procName+".xlsx"; m_chartData.getSeries().remove(1); } /** * Remove the first series by calling m_chartData.removeSeries(0). *

* This will not corrupt the workbook. */ @Test public void testBugFixRemoveSeries0() { final String procName="testBugFixRemoveSeries0"; fileName=procName+".xlsx"; m_chartData.removeSeries(0); } /** * Remove the second series by calling m_chartData.removeSeries(1). *

* This will not corrupt the workbook. */ @Test public void testBugFixRemoveSeries1() { final String procName="testBugFixRemoveSeries1"; fileName=procName+".xlsx"; m_chartData.removeSeries(1); } /** * Do not remove any series from the chart. */ @Test public void testDontRemoveSeries() { final String procName="testDontRemoveSeries"; fileName=procName+".xlsx"; } }