aboutsummaryrefslogtreecommitdiffstats
path: root/src/integrationtest/org/apache/poi/stress/TestAllFiles.java
blob: 9bce94f0dbc55718aabc300848ac3f6c9e6fa8e1 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* ====================================================================
   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.stress;


import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.apache.commons.collections4.MultiValuedMap;
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.tools.ant.DirectoryScanner;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.opentest4j.AssertionFailedError;

/**
 *  This is an integration test which performs various actions on all stored test-files and tries
 *  to reveal problems which are introduced, but not covered (yet) by unit tests.
 *
 *  This test looks for any file under the test-data directory and tries to do some useful
 *  processing with it based on it's type.
 *
 *  The test is implemented as a junit {@link ParameterizedTest} test, which leads
 *  to one test-method call for each file (currently around 950 files are handled).
 *
 *  There is a a mapping of extension to implementations of the interface
 *  {@link FileHandler} which defines how the file is loaded and which actions are
 *  tried with the file.
 *
 *  The test can be expanded by adding more actions to the FileHandlers, this automatically
 *  applies the action to any such file in our test-data repository.
 *
 *  There is also a list of files that should actually fail.
 *
 *  Note: It is also a test-failure if a file that is expected to fail now actually works,
 *  i.e. if a bug was fixed in POI itself, the file should be removed from the expected-failures
 *  here as well! This is to ensure that files that should not work really do not work, e.g.
 *  that we do not remove expected sanity checks.
 */
// also need to set JVM parameter: -Djunit.jupiter.execution.parallel.enabled=true
@Execution(ExecutionMode.CONCURRENT)
public class TestAllFiles {
    private static final File ROOT_DIR = new File("test-data");


    public static final String[] SCAN_EXCLUDES = {
        "**/.svn/**",
        "lost+found",
        "**/.git/**",
    };

    public static Stream<Arguments> allfiles(String testName) throws IOException {
        MultiValuedMap<String, ExcInfo> exMap;
        Map<String,String> handlerMap;
        try (Workbook wb = WorkbookFactory.create(new File(ROOT_DIR, "spreadsheet/stress.xls"))) {
            exMap = readExMap(wb.getSheet("Exceptions"));
            handlerMap = readHandlerMap(wb.getSheet("Handlers"));
        }

        DirectoryScanner scanner = new DirectoryScanner();
        scanner.setBasedir(ROOT_DIR);
        scanner.setExcludes(SCAN_EXCLUDES);

        scanner.scan();

        final List<Arguments> result = new ArrayList<>(100);
        for (String file : scanner.getIncludedFiles()) {
            // ... failures/handlers lookup doesn't work on windows otherwise
            final String uniFile = file.replace('\\', '/');

            String firstHandler = handlerMap.entrySet().stream()
                .filter(me -> uniFile.endsWith(me.getKey()))
                .map(Map.Entry::getValue).findFirst().orElse("NULL");

            final String[] handlerStr = { firstHandler, secondHandler(firstHandler) };
            for (String hs : handlerStr) {
                if ("NULL".equals(hs)) continue;
                ExcInfo info1 = exMap.get(file).stream()
                    .filter(e ->
                        (e.tests == null || e.tests.contains(testName) || "IGNORE".equals(e.tests)) &&
                        (e.handler == null || e.handler.contains(hs))
                    ).findFirst().orElse(null);

                if (info1 == null || !"IGNORE".equals(info1.tests)) {
                    result.add(Arguments.of(
                        file,
                        hs,
                        (info1 != null) ? info1.password : null,
                        (info1 != null) ? info1.exClazz : null,
                        (info1 != null) ? info1.exMessage : null
                    ));
                }
            }
        }

        return result.stream();
    }

    public static Stream<Arguments> extractFiles() throws IOException {
        return allfiles("extract");
    }

    @ParameterizedTest(name = "#{index} {0} {1}")
    @MethodSource("extractFiles")
    void handleExtracting(String file, String handler, String password, Class<? extends Throwable> exClass, String exMessage) throws IOException {
        System.out.println("Running extractFiles on "+file);
        FileHandler fileHandler = Handler.valueOf(handler).fileHandler.get();
        assertNotNull(fileHandler, "Did not find a handler for file " + file);
        Executable exec = () -> fileHandler.handleExtracting(new File(ROOT_DIR, file));
        verify(exec, exClass, exMessage, password);
    }


    public static Stream<Arguments> handleFiles() throws IOException {
        return allfiles("handle");
    }

    @ParameterizedTest(name = "#{index} {0} {1}")
    @MethodSource("handleFiles")
    void handleFile(String file, String handler, String password, Class<? extends Throwable> exClass, String exMessage) throws IOException {
        System.out.println("Running handleFiles on "+file);
        FileHandler fileHandler = Handler.valueOf(handler).fileHandler.get();
        assertNotNull(fileHandler, "Did not find a handler for file " + file);
        try (InputStream stream = new BufferedInputStream(new FileInputStream(new File(ROOT_DIR, file)), 64 * 1024)) {
            Executable exec = () -> fileHandler.handleFile(stream, file);
            verify(exec, exClass, exMessage, password);
        }
    }

    public static Stream<Arguments> handleAdditionals() throws IOException {
        return allfiles("additional");
    }

    @ParameterizedTest(name = "#{index} {0} {1}")
    @MethodSource("handleAdditionals")
    void handleAdditional(String file, String handler, String password, Class<? extends Throwable> exClass, String exMessage) {
        System.out.println("Running additionals on "+file);
        FileHandler fileHandler = Handler.valueOf(handler).fileHandler.get();
        assertNotNull(fileHandler, "Did not find a handler for file " + file);
        Executable exec = () -> fileHandler.handleAdditional(new File(ROOT_DIR, file));
        verify(exec, exClass, exMessage, password);
    }

    @SuppressWarnings("unchecked")
    private static void verify(Executable exec, Class<? extends Throwable> exClass, String exMessage, String password) {
        // this also removes the password for non encrypted files
        Biff8EncryptionKey.setCurrentUserPassword(password);
        if (exClass != null && AssertionFailedError.class.isAssignableFrom(exClass)) {
            try {
                exec.execute();
                fail("expected failed assertion");
            } catch (AssertionFailedError e) {
                assertEquals(exMessage, e.getMessage());
            } catch (Throwable e) {
                fail("unexpected exception", e);
            }
        } else if (exClass != null) {
            Exception e = assertThrows((Class<? extends Exception>)exClass, exec);
            String actMsg = e.getMessage();
            if (exMessage == null) {
                assertNull(actMsg);
            } else {
                assertNotNull(actMsg);
                assertTrue(actMsg.startsWith(exMessage), "Message: "+actMsg+" - didn't start with "+exMessage);
            }
        } else {
            assertDoesNotThrow(exec);
        }
    }


    private static String secondHandler(String handlerStr) {
        switch (handlerStr) {
            case "XSSF":
            case "XWPF":
            case "XSLF":
            case "XDGF":
                return "OPC";
            case "HSSF":
            case "HWPF":
            case "HSLF":
            case "HDGF":
            case "HSMF":
            case "HBPF":
                return "HPSF";
            default:
                return "NULL";
        }
    }

    private static Map<String,String> readHandlerMap(Sheet sh) {
        Map<String,String> handlerMap = new LinkedHashMap<>();
        boolean IGNORE_SCRATCHPAD = Boolean.getBoolean("scratchpad.ignore");
        boolean isFirst = true;
        for (Row row : sh) {
            if (isFirst) {
                isFirst = false;
                continue;
            }
            Cell cell = row.getCell(2);
            if (IGNORE_SCRATCHPAD || cell == null || cell.getCellType() != CellType.STRING) {
                cell = row.getCell(1);
            }
            handlerMap.put(row.getCell(0).getStringCellValue(), cell.getStringCellValue());
        }
        return handlerMap;
    }


    private static MultiValuedMap<String, ExcInfo> readExMap(Sheet sh) {
        MultiValuedMap<String, ExcInfo> exMap = new ArrayListValuedHashMap<>();

        Iterator<Row> iter = sh.iterator();
        List<BiConsumer<ExcInfo,String>> cols = initCols(iter.next());

        while (iter.hasNext()) {
            ExcInfo info = new ExcInfo();
            for (Cell cell : iter.next()) {
                if (cell.getCellType() == CellType.STRING) {
                    cols.get(cell.getColumnIndex()).accept(info, cell.getStringCellValue());
                }
            }
            exMap.put(info.file, info);
        }
        return exMap;
    }


    private static List<BiConsumer<ExcInfo,String>> initCols(Row row) {
        Map<String,BiConsumer<ExcInfo,String>> m = new HashMap<>();
        m.put("File", (e,s) -> e.file = s);
        m.put("Tests", (e,s) -> e.tests = s);
        m.put("Handler", (e,s) -> e.handler = s);
        m.put("Password", (e,s) -> e.password = s);
        m.put("Exception Class", (e,s) -> {
            try {
                e.exClazz = (Class<? extends Exception>) Class.forName(s);
            } catch (ClassNotFoundException ex) {
                fail(ex);
            }
        });
        m.put("Exception Message", (e,s) -> e.exMessage = s);

        return StreamSupport
            .stream(row.spliterator(), false)
            .map(Cell::getStringCellValue)
            .map(v -> m.getOrDefault(v, (e,s) -> {}))
            .collect(Collectors.toList());
    }

    private static class ExcInfo {
        String file;
        String tests;
        String handler;
        String password;
        Class<? extends Throwable> exClazz;
        String exMessage;


    }

    @SuppressWarnings("unused")
    private enum Handler {
        HDGF(HDGFFileHandler::new),
        HMEF(HMEFFileHandler::new),
        HPBF(HPBFFileHandler::new),
        HPSF(HPSFFileHandler::new),
        HSLF(HSLFFileHandler::new),
        HSMF(HSMFFileHandler::new),
        HSSF(HSSFFileHandler::new),
        HWPF(HWPFFileHandler::new),
        OPC(OPCFileHandler::new),
        POIFS(POIFSFileHandler::new),
        XDGF(XDGFFileHandler::new),
        XSLF(XSLFFileHandler::new),
        XSSFB(XSSFBFileHandler::new),
        XSSF(XSSFFileHandler::new),
        XWPF(XWPFFileHandler::new),
        OWPF(OWPFFileHandler::new),
        NULL(NullFileHandler::new)
        ;

        final Supplier<FileHandler> fileHandler;
        Handler(Supplier<FileHandler> fileHandler) {
            this.fileHandler = fileHandler;
        }
    }

    public static class NullFileHandler implements FileHandler {
        @Override
        public void handleFile(InputStream stream, String path) {
        }

        @Override
        public void handleExtracting(File file) {
        }

        @Override
        public void handleAdditional(File file) {
        }
    }
}