123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- /* ====================================================================
- 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.ooxml.util;
-
- import java.io.File;
- import java.io.IOException;
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
- import java.net.URL;
- import java.security.AccessController;
- import java.security.CodeSource;
- import java.security.PrivilegedAction;
- import java.security.ProtectionDomain;
- import java.util.ArrayList;
- import java.util.Enumeration;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- import java.util.Vector;
- import java.util.jar.JarEntry;
- import java.util.jar.JarFile;
- import java.util.regex.Pattern;
-
- import org.apache.poi.util.IOUtils;
- import org.apache.poi.util.StringUtil;
- import org.apache.poi.util.SuppressForbidden;
- import org.apache.xmlbeans.StringEnumAbstractBase;
- import org.junit.Test;
- import org.junit.internal.TextListener;
- import org.junit.runner.Description;
- import org.junit.runner.JUnitCore;
- import org.junit.runner.Result;
- import org.reflections.Reflections;
-
- import junit.framework.TestCase;
-
- /**
- * Build a 'lite' version of the ooxml-schemas.jar
- *
- * @author Yegor Kozlov
- */
- public final class OOXMLLite {
- private static final Pattern SCHEMA_PATTERN = Pattern.compile("schemaorg_apache_xmlbeans/(system|element)/.*\\.xsb");
-
- /**
- * Destination directory to copy filtered classes
- */
- private File _destDest;
-
- /**
- * Directory with the compiled ooxml tests
- */
- private File _testDir;
-
- /**
- * Reference to the ooxml-schemas.jar
- */
- private File _ooxmlJar;
-
-
- OOXMLLite(String dest, String test, String ooxmlJar) {
- _destDest = new File(dest);
- _testDir = new File(test);
- _ooxmlJar = new File(ooxmlJar);
- }
-
- public static void main(String[] args) throws IOException {
- System.out.println("Free memory (bytes): " +
- Runtime.getRuntime().freeMemory());
- long maxMemory = Runtime.getRuntime().maxMemory();
- System.out.println("Maximum memory (bytes): " +
- (maxMemory == Long.MAX_VALUE ? "no limit" : maxMemory));
- System.out.println("Total memory (bytes): " +
- Runtime.getRuntime().totalMemory());
-
- String dest = null, test = null, ooxml = null;
-
- for (int i = 0; i < args.length; i++) {
- switch (args[i]) {
- case "-dest":
- dest = args[++i]; // lgtm[java/index-out-of-bounds]
- break;
- case "-test":
- test = args[++i]; // lgtm[java/index-out-of-bounds]
- break;
- case "-ooxml":
- ooxml = args[++i]; // lgtm[java/index-out-of-bounds]
- break;
- }
- }
- OOXMLLite builder = new OOXMLLite(dest, test, ooxml);
- builder.build();
- }
-
- void build() throws IOException {
- List<Class<?>> lst = new ArrayList<>();
- //collect unit tests
- String exclude = StringUtil.join("|",
- "BaseTestXWorkbook",
- "BaseTestXSheet",
- "BaseTestXRow",
- "BaseTestXCell",
- "BaseTestXSSFPivotTable",
- "TestSXSSFWorkbook\\$\\d",
- "TestUnfixedBugs",
- "MemoryUsage",
- "TestDataProvider",
- "TestDataSamples",
- "All.+Tests",
- "ZipFileAssert",
- "AesZipFileZipEntrySource",
- "TempFileRecordingSXSSFWorkbookWithCustomZipEntrySource",
- "PkiTestUtils",
- "TestCellFormatPart\\$\\d",
- "TestSignatureInfo\\$\\d",
- "TestCertificateEncryption\\$CertData",
- "TestPOIXMLDocument\\$OPCParser",
- "TestPOIXMLDocument\\$TestFactory",
- "TestXSLFTextParagraph\\$DrawTextParagraphProxy",
- "TestXSSFExportToXML\\$\\d",
- "TestXSSFExportToXML\\$DummyEntityResolver",
- "TestFormulaEvaluatorOnXSSF\\$Result",
- "TestFormulaEvaluatorOnXSSF\\$SS",
- "TestMultiSheetFormulaEvaluatorOnXSSF\\$Result",
- "TestMultiSheetFormulaEvaluatorOnXSSF\\$SS",
- "TestXSSFBugs\\$\\d",
- "AddImageBench",
- "AddImageBench_jmhType_B\\d",
- "AddImageBench_benchCreatePicture_jmhTest",
- "TestEvilUnclosedBRFixingInputStream\\$EvilUnclosedBRFixingInputStream",
- "TempFileRecordingSXSSFWorkbookWithCustomZipEntrySource\\$TempFileRecordingSheetDataWriterWithDecorator",
- "TestXSSFBReader\\$1",
- "TestXSSFBReader\\$TestSheetHandler",
- "TestFormulaEvaluatorOnXSSF\\$1",
- "TestMultiSheetFormulaEvaluatorOnXSSF\\$1",
- "TestZipPackagePropertiesMarshaller\\$1",
- "SLCommonUtils",
- "TestPPTX2PNG\\$1",
- "TestMatrixFormulasFromXMLSpreadsheet\\$1",
- "TestMatrixFormulasFromXMLSpreadsheet\\$Navigator",
- "TestPOIXMLDocument\\$UncaughtHandler",
- "TestOleShape\\$Api",
- "TestOleShape\\$1",
- "TestPOIXMLDocument\\$1",
- "TestXMLSlideShow\\$1",
- "TestXMLSlideShow\\$BufAccessBAOS",
- "TestXDDFChart\\$1",
- "TestOOXMLLister\\$1",
- "TestOOXMLPrettyPrint\\$1"
- );
- System.out.println("Collecting unit tests from " + _testDir);
- collectTests(_testDir, _testDir, lst, ".+.class$", ".+(" + exclude + ").class");
- System.out.println("Found " + lst.size() + " classes");
-
- //run tests
- JUnitCore jUnitCore = new JUnitCore();
- jUnitCore.addListener(new TextListener(System.out) {
- private final Set<String> classes = new HashSet<>();
- private int count;
-
- @Override
- public void testStarted(Description description) {
- // count how many test-classes we already saw
- classes.add(description.getClassName());
- count++;
- if(count % 100 == 0) {
- System.out.println();
- System.out.println(classes.size() + "/" + lst.size() + ": " + description.getDisplayName());
- }
-
- super.testStarted(description);
- }
- });
- Result result = jUnitCore.run(lst.toArray(new Class<?>[0]));
- if (!result.wasSuccessful()) {
- throw new RuntimeException("Tests did not succeed, cannot build ooxml-lite jar");
- }
-
- //see what classes from the ooxml-schemas.jar are loaded
- System.out.println("Copying classes to " + _destDest);
- Set<Class<?>> classes = getLoadedClasses(_ooxmlJar.getName());
- Set<String> packages = new HashSet<>();
- for (Class<?> cls : classes) {
- copyFile(cls);
- packages.add(cls.getPackage().getName());
-
- if (cls.isInterface()) {
- /// Copy classes and interfaces declared as members of this class
- for (Class<?> fc : cls.getDeclaredClasses()) {
- copyFile(fc);
- }
- }
- }
- for (String pkg : packages) {
- Reflections reflections = new Reflections(pkg);
- Set<Class<? extends List>> listClasses = reflections.getSubTypesOf(List.class);
- listClasses.removeAll(classes);
- for (Class listClass : listClasses) {
- for (Class<?> compare : classes) {
- if (listClass.getName().startsWith(compare.getName())) {
- copyFile(listClass);
- }
- }
- }
- Set<Class<? extends StringEnumAbstractBase>> enumClasses = reflections.getSubTypesOf(StringEnumAbstractBase.class);
- listClasses.removeAll(classes);
- for (Class enumClass : enumClasses) {
- for (Class<?> compare : classes) {
- if (enumClass.getName().startsWith(compare.getName())) {
- copyFile(enumClass);
- }
- }
- }
- }
-
- //finally copy the compiled .xsb files
- System.out.println("Copying .xsb resources");
- try (JarFile jar = new JarFile(_ooxmlJar)) {
- for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements(); ) {
- JarEntry je = e.nextElement();
- if (SCHEMA_PATTERN.matcher(je.getName()).matches()) {
- File destFile = new File(_destDest, je.getName());
- IOUtils.copy(jar.getInputStream(je), destFile);
- }
- }
- }
- }
-
- private void copyFile(Class<?> cls) throws IOException {
- String className = cls.getName();
- String classRef = className.replace('.', '/') + ".class";
- File destFile = new File(_destDest, classRef);
- IOUtils.copy(cls.getResourceAsStream('/' + classRef), destFile);
- }
-
- private static boolean checkForTestAnnotation(Class<?> testclass) {
- for (Method m : testclass.getDeclaredMethods()) {
- if(m.isAnnotationPresent(Test.class)) {
- return true;
- }
- }
-
- // also check super classes
- if(testclass.getSuperclass() != null) {
- for (Method m : testclass.getSuperclass().getDeclaredMethods()) {
- if(m.isAnnotationPresent(Test.class)) {
- return true;
- }
- }
- }
-
- System.out.println("Class " + testclass.getName() + " does not derive from TestCase and does not have a @Test annotation");
-
- // Should we also look at superclasses to find cases
- // where we have abstract base classes with derived tests?
- // if(checkForTestAnnotation(testclass.getSuperclass())) return true;
-
- return false;
- }
-
- /**
- * Recursively collect classes from the supplied directory
- *
- * @param arg the directory to search in
- * @param out output
- * @param ptrn the pattern (regexp) to filter found files
- */
- private static void collectTests(File root, File arg, List<Class<?>> out, String ptrn, String exclude) {
- if (arg.isDirectory()) {
- File files[] = arg.listFiles();
- if (files != null) {
- for (File f : files) {
- collectTests(root, f, out, ptrn, exclude);
- }
- }
- } else {
- String path = arg.getAbsolutePath();
- String prefix = root.getAbsolutePath();
- String cls = path.substring(prefix.length() + 1).replace(File.separator, ".");
- if(!cls.matches(ptrn)) {
- return;
- }
- if (cls.matches(exclude)) {
- return;
- }
- //ignore inner classes defined in tests
- if (cls.indexOf('$') != -1) {
- System.out.println("Inner class " + cls + " not included");
- return;
- }
-
- cls = cls.replace(".class", "");
-
- try {
- Class<?> testclass = Class.forName(cls);
- if (TestCase.class.isAssignableFrom(testclass)
- || checkForTestAnnotation(testclass)) {
- out.add(testclass);
- }
- } catch (Throwable e) { // NOSONAR
- System.out.println("Class " + cls + " is not in classpath");
- }
- }
- }
-
- /**
- *
- * @param ptrn the pattern to filter output
- * @return the classes loaded by the system class loader
- */
- @SuppressWarnings("unchecked")
- private static Set<Class<?>> getLoadedClasses(String ptrn) {
- // make the field accessible, we defer this from static initialization to here to
- // allow JDKs which do not have this field (e.g. IBM JDK) to at least load the class
- // without failing, see https://issues.apache.org/bugzilla/show_bug.cgi?id=56550
- final Field _classes = AccessController.doPrivileged(new PrivilegedAction<Field>() {
- @Override
- @SuppressForbidden("TODO: Reflection works until Java 8 on Oracle/Sun JDKs, but breaks afterwards (different classloader types, access checks)")
- public Field run() {
- try {
- Field fld = ClassLoader.class.getDeclaredField("classes");
- fld.setAccessible(true);
- return fld;
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
-
- }
- });
-
- ClassLoader appLoader = ClassLoader.getSystemClassLoader();
- try {
- Vector<Class<?>> classes = (Vector<Class<?>>) _classes.get(appLoader);
- Set<Class<?>> set = new HashSet<>();
- for (Class<?> cls : classes) {
- // e.g. proxy-classes, ...
- ProtectionDomain pd = cls.getProtectionDomain();
- if (pd == null) {
- continue;
- }
- CodeSource cs = pd.getCodeSource();
- if (cs == null) {
- continue;
- }
- URL loc = cs.getLocation();
- if (loc == null) {
- continue;
- }
-
- String jar = loc.toString();
- if (jar.contains(ptrn)) {
- set.add(cls);
- }
- }
- return set;
- } catch (IllegalAccessException e) {
- throw new RuntimeException(e);
- }
- }
- }
|