#!/bin/bash
set -e

# autopkgtest check: run the unit tests.
# (C) 2022 Pierre Gruet.
# Author: Pierre Gruet <pgt@debian.org>


pkg=gkl

export LC_ALL=C.UTF-8
if [ "${AUTOPKGTEST_TMP}" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  trap "rm -rf ${AUTOPKGTEST_TMP}" 0 INT QUIT ABRT PIPE TERM
fi

# Copying the whole src dir as we will need src/test/resources for the test.
cp -a src/ "${AUTOPKGTEST_TMP}"
cp debian/tests/testng.xml "${AUTOPKGTEST_TMP}"

cd "${AUTOPKGTEST_TMP}"
# Ensuring we won't use the sources of the classes from the lib itself.
rm -rf src/main/

# Putting the way to the installed jar in the classpath.
export CLASSPATH=$CLASSPATH:src/test/java/:/usr/share/java/gkl.jar:/usr/share/java/jcommander.jar:/usr/share/java/testng.jar


# Building test classes.
javac $(find src/test/java/ -name "*.java")

# Running the tests
echo "Errors will show up during the test, this is perfectly normal: the"
echo "concerned tests are expected to raise exceptions."
java org.testng.TestNG testng.xml
