aboutsummaryrefslogtreecommitdiffstats
path: root/poi-ooxml/src/test/java/org/apache/poi/xddf/usermodel/chart/TestXDDFChartRemoveSeries.java
blob: 8bea979e33dcf4e7e7478415de2641fdcba28ec8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */
package org.apache.poi.xddf.usermodel.chart;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
 * Test case for bug 63153
 */
class TestXDDFChartRemoveSeries {
    final File resultDir = new File("build/custom-reports-test");
    String procName = null;
    String fileName = null;
    XSSFWorkbook workbook = null;
    XSSFSheet sheet = null;
    XDDFScatterChartData chartData = null;
    XDDFChart chart = null;

    public TestXDDFChartRemoveSeries() {
        resultDir.mkdirs();
    }

    /**
     * This creates a workbook with one worksheet, which contains a single
     * scatter chart.
     */
    @BeforeEach
    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;
        }
        chart = xssfDrawing.createChart(anchor);
        final XDDFValueAxis bottomAxis = chart.createValueAxis(AxisPosition.BOTTOM);
        final XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);

        // Initialize 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<Double> xData = XDDFDataSourcesFactory.fromArray(dX, null);
        final XDDFNumericalDataSource<Double> yData1 = XDDFDataSourcesFactory.fromArray(dY1, null);
        final XDDFNumericalDataSource<Double> yData2 = XDDFDataSourcesFactory.fromArray(dY2, null);

        // Create the chartdata

        chartData = (XDDFScatterChartData) chart.createData(ChartTypes.SCATTER, bottomAxis, leftAxis);

        // Add the series

        chartData.addSeries(xData, yData1);
        chartData.addSeries(xData, yData2);
    }

    /**
     * This method writes the workbook to resultDir/fileName.
     */
    @AfterEach
    void cleanup() throws IOException {
        // Finish up
        chart.plot(chartData);
        final int index = workbook.getSheetIndex(sheet);
        workbook.setSelectedTab(index);
        workbook.setActiveSheet(index);
        workbook.setFirstVisibleTab(index);

        final File file = new File(resultDir, fileName);
        try (OutputStream fileOut = new FileOutputStream(file)) {
            workbook.write(fileOut);
        }
        workbook.close();
    }

    /**
     * Attempt to remove the first series by calling chartData.getSeries().remove(0).
     * <p>
     * This used to corrupt the workbook but the returned <code>List</code> is unmodifiable.
     */
    @Test
    void testRemoveSeries0() {
        procName = "testRemoveSeries0";
        fileName = procName + ".xlsx";

        assertThrows(UnsupportedOperationException.class, () -> chartData.getSeries().remove(0));
        assertEquals(2, chartData.getSeriesCount());
    }

    /**
     * Remove the first series by calling chartData.removeSeries(0).
     * <p>
     * This will not corrupt the workbook.
     */
    @Test
    void testBugFixRemoveSeries0() {
        procName = "testBugFixRemoveSeries0";
        fileName = procName + ".xlsx";

        chartData.removeSeries(0);
        assertEquals(1, chartData.getSeriesCount());
    }

    /**
     * Remove the second series by calling chartData.removeSeries(1).
     * <p>
     * This will not corrupt the workbook.
     */
    @Test
    void testBugFixRemoveSeries1() {
        procName = "testBugFixRemoveSeries1";
        fileName = procName + ".xlsx";

        chartData.removeSeries(1);
        assertEquals(1, chartData.getSeriesCount());
    }

    /**
     * Do not remove any series from the chart.
     */
    @Test
    void testDontRemoveSeries() {
        procName = "testDontRemoveSeries";
        fileName = procName + ".xlsx";

        IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> chartData.removeSeries(2));
        assertEquals("removeSeries(2): illegal index", e.getMessage());
        assertEquals(2, chartData.getSeriesCount());
    }

}