From 41e1e9ffb88d4076e08ca8de721146287c79eec6 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Fri, 7 Oct 2016 19:33:11 +0000 Subject: [PATCH] Add initial compile steps with a simple multi-project setup to build.gradle Adjust some tests to make them run fine in the Gradle build as well git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1763816 13f79535-47bb-0310-9956-ffa450edef68 --- build.gradle | 219 ++++++++++++++++++ settings.gradle | 10 + .../converter/TestExcelConverterSuite.java | 91 ++++---- .../converter/TestWordToConverterSuite.java | 84 +++---- 4 files changed, 299 insertions(+), 105 deletions(-) create mode 100644 settings.gradle diff --git a/build.gradle b/build.gradle index 20f98e8205..7bf080246e 100644 --- a/build.gradle +++ b/build.gradle @@ -19,3 +19,222 @@ // https://docs.gradle.org/current/userguide/ant.html ant.importBuild 'build.xml' + +/** + +Define properties for all projects, including this one + +*/ +allprojects { + apply plugin: 'eclipse' + + task wrapper(type: Wrapper) { + gradleVersion = '2.14.1' + } + + task adjustWrapperPropertiesFile << { + ant.replaceregexp(match:'^#.*', replace:'', flags:'g', byline:true) { + fileset(dir: project.projectDir, includes: 'gradle/wrapper/gradle-wrapper.properties') + } + new File(project.projectDir, 'gradle/wrapper/gradle-wrapper.properties').with { it.text = it.readLines().findAll { it }.sort().join('\n') } + ant.fixcrlf(file: 'gradle/wrapper/gradle-wrapper.properties', eol: 'lf') + } + wrapper.finalizedBy adjustWrapperPropertiesFile +} + +/** + +Define things that are only necessary in sub-projects, but not in the master-project itself + +*/ +subprojects { + //Put instructions for each sub project, but not the master + apply plugin: 'java' + apply plugin: 'jacoco' + + version = '3.16-beta1' + + tasks.withType(JavaCompile) { + options.encoding = 'UTF-8' + } + + sourceCompatibility = 1.6 + + repositories { + mavenCentral() + } + + jar { + manifest { + attributes 'Implementation-Title': 'Apache POI', 'Implementation-Version': version + } + } + + test { + // Exclude some tests that are not actually tests or do not run cleanly on purpose + exclude '**/BaseTestBorderStyle.class' + exclude '**/BaseTestCellUtil.class' + exclude '**/TestUnfixedBugs.class' + exclude '**/TestOneFile.class' + + systemProperties = System.properties + + // set heap size for the test JVM(s) + minHeapSize = "128m" + maxHeapSize = "768m" + + // show standard out and standard error of the test JVM(s) on the console + //testLogging.showStandardStreams = true + + // http://forums.gradle.org/gradle/topics/jacoco_related_failure_in_multiproject_build + systemProperties['user.dir'] = workingDir + + systemProperties['POI.testdata.path'] = '../../test-data' + //systemProperties['user.language'] = 'en' + //systemProperties['user.country'] = 'US' + } + + test.beforeSuite { TestDescriptor suite -> + System.setProperty('user.language', 'en') + System.setProperty('user.country', 'US') + } + + jacoco { + toolVersion = '0.7.7.201606060606' + } +} + +project('main') { + sourceSets.main.java.srcDirs = ['../../src/java'] + sourceSets.main.resources.srcDirs = ['../../src/resources/main'] + sourceSets.test.java.srcDirs = ['../../src/testcases'] + + dependencies { + compile 'commons-codec:commons-codec:1.10' + compile 'commons-logging:commons-logging:1.2' + + testCompile 'junit:junit:4.12' + } + + // Create a separate jar for test-code to depend on it in other projects + // See http://stackoverflow.com/questions/5144325/gradle-test-dependency + task testJar(type: Jar, dependsOn: testClasses) { + baseName = "test-${project.archivesBaseName}" + from sourceSets.test.output + } + + configurations { + tests + } + + artifacts { + tests testJar + } +} + +project('ooxml') { + sourceSets.main.java.srcDirs = ['../../src/ooxml/java'] + sourceSets.main.resources.srcDirs = ['../../src/ooxml/resources', '../../src/resources/ooxml'] + sourceSets.test.java.srcDirs = ['../../src/ooxml/testcases'] + + // for now import the ant-task for building the jars from build.xml + // we need to rename the tasks as e.g. task "jar" conflicts with :ooxml:jar + ant.importBuild('../../build.xml') { antTargetName -> + 'ant-' + antTargetName + } + compileJava.dependsOn 'ant-compile-ooxml-xsds' + + dependencies { + compile 'org.apache.xmlbeans:xmlbeans:2.6.0' + compile 'org.apache.commons:commons-collections4:4.1' + compile 'org.apache.santuario:xmlsec:2.0.6' + compile 'org.bouncycastle:bcpkix-jdk15on:1.54' + compile 'com.github.virtuald:curvesapi:1.04' + + // for ooxml-lite, should we move this somewhere else? + compile 'junit:junit:4.12' + + compile project(':main') + compile project(':scratchpad') // TODO: get rid of this dependency! + compile files('../../ooxml-lib/ooxml-schemas-1.3.jar') + compile files('../../ooxml-lib/ooxml-security-1.1.jar') + + testCompile 'junit:junit:4.12' + testCompile project(path: ':main', configuration: 'tests') + } +} + +project('examples') { + sourceSets.main.java.srcDirs = ['../../src/examples/src'] + + dependencies { + compile project(':main') + compile project(':ooxml') + } +} + + +project('excelant') { + sourceSets.main.java.srcDirs = ['../../src/excelant/java'] + sourceSets.main.resources.srcDirs = ['../../src/excelant/resources'] + sourceSets.test.java.srcDirs = ['../../src/excelant/testcases'] + + dependencies { + compile 'org.apache.ant:ant:1.9.4' + + compile project(':main') + compile project(':ooxml') + + testCompile project(path: ':main', configuration: 'tests') + } +} + +project('integrationtest') { + sourceSets.test.java.srcDirs = ['../../src/integrationtest'] + + dependencies { + compile 'org.apache.ant:ant:1.9.4' + + compile project(':main') + compile project(':ooxml') + compile project(':scratchpad') + compile project(':examples') + + testCompile 'junit:junit:4.12' + } + + test { + // exclude these from the normal test-run + exclude '**/TestAllFiles.class' + exclude '**/*FileHandler.class' + exclude '**/RecordsStresser.class' + } + + task integrationTest(type: Test) { + // these are just tests used during development of more test-code + exclude '**/*FileHandler.class' + exclude '**/RecordStresser.class' + } +} + +project('scratchpad') { + sourceSets.main.java.srcDirs = ['../../src/scratchpad/src'] + sourceSets.main.resources.srcDirs = ['../../src/resources/scratchpad'] + sourceSets.test.java.srcDirs = ['../../src/scratchpad/testcases'] + + dependencies { + compile project(':main') + // cyclic-dependency here: compile project(':ooxml') + + testCompile 'junit:junit:4.12' + testCompile project(path: ':main', configuration: 'tests') + } +} + +/* + * Notes: + * + * See https://github.com/melix/japicmp-gradle-plugin and + * https://github.com/codehaus/groovy-git/blob/7f940159920d4ea5bc727cfcbef8aba9b48c5e50/gradle/binarycompatibility.gradle for an example of using japicmp + * + **/ diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000000..de9c7e7cb8 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,10 @@ +rootProject.name = 'poi' + +include 'main', 'ooxml', 'excelant', 'examples', 'scratchpad', 'integrationtest' + +project(':main').projectDir = new File(settingsDir, 'build/main') +project(':ooxml').projectDir = new File(settingsDir, 'build/ooxml') +project(':excelant').projectDir = new File(settingsDir, 'build/excelant') +project(':examples').projectDir = new File(settingsDir, 'build/examples') +project(':scratchpad').projectDir = new File(settingsDir, 'build/scratchpad') +project(':integrationtest').projectDir = new File(settingsDir, 'build/integrationtest') diff --git a/src/scratchpad/testcases/org/apache/poi/hssf/converter/TestExcelConverterSuite.java b/src/scratchpad/testcases/org/apache/poi/hssf/converter/TestExcelConverterSuite.java index 895267fd3c..cdd803a713 100644 --- a/src/scratchpad/testcases/org/apache/poi/hssf/converter/TestExcelConverterSuite.java +++ b/src/scratchpad/testcases/org/apache/poi/hssf/converter/TestExcelConverterSuite.java @@ -16,41 +16,42 @@ ==================================================================== */ package org.apache.poi.hssf.converter; -import java.io.File; -import java.io.FilenameFilter; -import java.io.StringWriter; -import java.util.Arrays; -import java.util.List; +import org.apache.poi.POIDataSamples; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.util.XMLHelper; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; +import java.io.File; +import java.io.FilenameFilter; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -import org.apache.poi.POIDataSamples; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; -import org.apache.poi.util.XMLHelper; +import static org.junit.Assert.assertNotNull; +@RunWith(Parameterized.class) public class TestExcelConverterSuite { /** * YK: a quick hack to exclude failing documents from the suite. */ - private static List failingFiles = Arrays.asList( // + @SuppressWarnings("ArraysAsListWithZeroOrOneArgument") + private static List failingFiles = Arrays.asList( /* not failing, but requires more memory */ "ex45698-22488.xls" ); - public static Test suite() - { - TestSuite suite = new TestSuite( - TestExcelConverterSuite.class.getName() ); - - File directory = POIDataSamples.getSpreadSheetInstance().getFile( + @Parameterized.Parameters(name="{index}: {0}") + public static Iterable files() { + List files = new ArrayList(); + File directory = POIDataSamples.getDocumentInstance().getFile( "../spreadsheet" ); for ( final File child : directory.listFiles( new FilenameFilter() { @@ -60,38 +61,24 @@ public class TestExcelConverterSuite } } ) ) { - final String name = child.getName(); - suite.addTest( new TestCase( name + " [FO]" ) - { - @Override - public void runTest() throws Exception - { - testFo( child ); - } - } ); - suite.addTest( new TestCase( name + " [HTML]" ) - { - @Override - public void runTest() throws Exception - { - testHtml( child ); - } - } ); + files.add(new Object[] { child }); } - return suite; + return files; } - protected static void testFo( File child ) throws Exception + + @Parameterized.Parameter + public File child; + + @Test + public void testFo() throws Exception { HSSFWorkbook workbook; - try - { + try { workbook = ExcelToHtmlUtils.loadXls( child ); - } - catch ( Exception exc ) - { - // unable to parse file -- not WordToFoConverter fault + } catch ( Exception exc ) { + // unable to parse file -- not ExcelToFoConverter fault return; } @@ -109,18 +96,18 @@ public class TestExcelConverterSuite transformer.transform( new DOMSource( excelToHtmlConverter.getDocument() ), new StreamResult( stringWriter ) ); + + assertNotNull(stringWriter.toString()); } - protected static void testHtml( File child ) throws Exception + @Test + public void testHtml() throws Exception { HSSFWorkbook workbook; - try - { + try { workbook = ExcelToHtmlUtils.loadXls( child ); - } - catch ( Exception exc ) - { - // unable to parse file -- not WordToFoConverter fault + } catch ( Exception exc ) { + // unable to parse file -- not ExcelToFoConverter fault return; } @@ -138,5 +125,7 @@ public class TestExcelConverterSuite transformer.transform( new DOMSource( excelToHtmlConverter.getDocument() ), new StreamResult( stringWriter ) ); + + assertNotNull(stringWriter.toString()); } } diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToConverterSuite.java b/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToConverterSuite.java index 77a75a1937..f567978a1f 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToConverterSuite.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToConverterSuite.java @@ -19,6 +19,7 @@ package org.apache.poi.hwpf.converter; import java.io.File; import java.io.FilenameFilter; import java.io.StringWriter; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -28,14 +29,16 @@ import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - import org.apache.poi.POIDataSamples; import org.apache.poi.hwpf.HWPFDocumentCore; import org.apache.poi.util.XMLHelper; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import static org.junit.Assert.assertNotNull; + +@RunWith(Parameterized.class) public class TestWordToConverterSuite { /** @@ -44,11 +47,9 @@ public class TestWordToConverterSuite private static List failingFiles = Arrays .asList( "ProblemExtracting.doc" ); - public static Test suite() - { - TestSuite suite = new TestSuite( - TestWordToConverterSuite.class.getName() ); - + @Parameterized.Parameters(name="{index}: {0}") + public static Iterable files() { + List files = new ArrayList(); File directory = POIDataSamples.getDocumentInstance().getFile( "../document" ); for ( final File child : directory.listFiles( new FilenameFilter() @@ -59,44 +60,21 @@ public class TestWordToConverterSuite } } ) ) { - final String name = child.getName(); - - suite.addTest( new TestCase( name + " [FO]" ) - { - public void runTest() throws Exception - { - testFo( child ); - } - } ); - suite.addTest( new TestCase( name + " [HTML]" ) - { - public void runTest() throws Exception - { - testHtml( child ); - } - } ); - suite.addTest( new TestCase( name + " [TEXT]" ) - { - public void runTest() throws Exception - { - testText( child ); - } - } ); - + files.add(new Object[] { child }); } - return suite; + return files; } - protected static void testFo( File child ) throws Exception - { + @Parameterized.Parameter + public File child; + + @Test + public void testFo() throws Exception { HWPFDocumentCore hwpfDocument; - try - { + try { hwpfDocument = AbstractWordUtils.loadDoc( child ); - } - catch ( Exception exc ) - { + } catch ( Exception exc ) { return; } @@ -115,17 +93,16 @@ public class TestWordToConverterSuite new StreamResult( stringWriter ) ); // no exceptions + assertNotNull(stringWriter.toString()); } - protected static void testHtml( File child ) throws Exception + @Test + public void testHtml() throws Exception { HWPFDocumentCore hwpfDocument; - try - { + try { hwpfDocument = AbstractWordUtils.loadDoc( child ); - } - catch ( Exception exc ) - { + } catch ( Exception exc ) { return; } @@ -145,17 +122,16 @@ public class TestWordToConverterSuite new StreamResult( stringWriter ) ); // no exceptions + assertNotNull(stringWriter.toString()); } - protected static void testText( File child ) throws Exception + @Test + public void testText() throws Exception { HWPFDocumentCore wordDocument; - try - { + try { wordDocument = AbstractWordUtils.loadDoc( child ); - } - catch ( Exception exc ) - { + } catch ( Exception exc ) { return; } @@ -174,7 +150,7 @@ public class TestWordToConverterSuite new DOMSource( wordToTextConverter.getDocument() ), new StreamResult( stringWriter ) ); - stringWriter.toString(); // no exceptions + assertNotNull(stringWriter.toString()); } }