gbuild for Java tests – LibreOffice build system part 3
In this blog post, I discuss gbuild for Java tests. The goal is to write a Makefile to compile and run a JUnit test for LibreOffice. You can also refer to part 1 and part 2 for a brif overiew on gbuild, the LibreOffice build system.
Macro Examples from gbuild for Java Tests
In the first post on gbuild, I have mentioned some macro examples including gb_Output_announce
which was used to print nice messages like the ones including “[CXX]”. Now let’s explain some more macros related to Java tests.
Consider that you want to compile and run a JUnitTest. To do that, you need to write the test in a Java file, and create a Makefile to run that.
This is an example for running a test defined in Java file sw/qa/complex/indeterminateState/CheckIndeterminateState.java.
$(eval $(call gb_JunitTest_JunitTest,sw_complex)) $(eval $(call gb_JunitTest_add_sourcefiles,sw_complex,\ sw/qa/complex/indeterminateState/CheckIndeterminateState \ )) $(eval $(call gb_JunitTest_use_unoapi_jars,sw_complex)) $(eval $(call gb_JunitTest_add_classes,sw_complex,\ complex.indeterminateState.CheckIndeterminateState \ ))
The make file for running this Java test consists of calling multiple macros. It starts with gb_JunitTest_JunitTest
macro, which defines the test by its name, sw_complex
. This macro is defined in solenv/gbuild/JunitTest.mk
. If you grep
for define in the same file, you will see this result:
$ grep -w define solenv/gbuild/JunitTest.mk define gb_JunitTest_JunitTest define gb_JunitTest_set_defs define gb_JunitTest_add_classes define gb_JunitTest_add_class define gb_JunitTest_add_sourcefile define gb_JunitTest_add_sourcefiles define gb_JunitTest_use_jar define gb_JunitTest_use_jars define gb_JunitTest_use_jar_classset define gb_JunitTest_add_classpath define gb_JunitTest_use_system_jar define gb_JunitTest_use_system_jars define gb_JunitTest_use_external define gb_JunitTest_use_externals define gb_JunitTest_use_customtarget define gb_JunitTest_use_customtargets define gb_JunitTest_use_unoapi_jars define gb_JunitTest_use_unoapi_test_class define gb_JunitTest_set_unoapi_test_defaults define gb_JunitTest_JunitTest
To stick to the macros used in the above example, I describe these macros:
gb_JunitTest_add_sourcefiles
: This macro adds a Java source file to the test. It defines the code that adds the sw/qa/complex/indeterminateState/CheckIndeterminateState.java
to the test. But please note that you should drop the .java
extension:
$(eval $(call gb_JunitTest_add_sourcefiles,sw_complex,\ sw/qa/complex/indeterminateState/CheckIndeterminateState \ ))
The other macro gb_JunitTest_use_unoapi_jars
, adds the UNO API JAR files to be used with the test.
And in the end, you need to add the test class name using gb_JunitTest_add_classes
macro. The class name is visible in the end.
The result can be quite complex, but it works. 🙂
java.exe -Xmx64M -classpath "$W/JavaClassSet/JunitTest/sw_complex;C:/cygwin64/home/user/lode/opt/share/java/junit.jar;$I/program;$W/Jar/OOoRunner.jar;$I/program/classes/libreoffice.jar;$W/Jar/test.jar" -Dorg.openoffice.test.arg.soffice="path:$I/program/soffice" -Dorg.openoffice.test.arg.env=PATH="$PATH" -Dorg.openoffice.test.arg.user=file:///$W/JunitTest/sw_complex/user org.junit.runner.JUnitCore complex.indeterminateState.CheckIndeterminateState
The above is the actual command that runs the test. Please note that if you forget the gb_JunitTest_add_classes
macro to define the class name, the test may compile, but it will not run.
As an example, you can see the below patch. This patch fixes the problem of the JUnit test not running:
Final Words
Many macros are available in gbuild, making easier to create Makefiles to compile and run tests, build libraries and executable applications and many other relevant tasks. The best way to find and understand these macros is to look at the Makefiles written by others to the same task. Look for .mk
files, and if you want to see the implementation of the macros, look into solenv/gbuild/
.
I will write more about gbuild macros in the next series of blog posts on gbuild.