#
# Makefile for running Cobertura with TestNG.
#

# Path to TestNG jar
TESTNG_JAR =	/home/gfisher/classes/309/lib/testng/testng-6.8.jar

# Path to Cobertura directory, from the unpacked Cobertura jar
COBERTURA_DIR =	/home/gfisher/classes/309/lib/cobertura-1.9.3

# Path to Cobertura jar
COBERTURA_JAR =	$(COBERTURA_DIR)/cobertura.jar

# Directory for executable testing files.  For a full-project makefile, this
# corresponds to the project directory testing/implementation/executables/JVM,
# or the equivalent for your project.
EXECUTABLES =	classes

# Source code directory.  For a full-project makefile, this corresponds to the
# project directory implementation/source, or the equivalent for your project.
#
IMPLE_SRC_DIR = .

# All of the files to be compiled for testing, which includes both the files to
# be tested and the files doing the testing.
TEST_FILES = \
	CoverageExampleTest.java \
	$(IMPLE_SRC_DIR)/CoverageExample.java


#
# Tell make to ignore the fact that some tests may fail when TestNG is run.
# This means that a coverage report will be generated whether or not the TestNG
# tests all pass.
#
.IGNORE:	run run_coverage



#
# Compile, run the tests, and run the coverage report.
#
test:		compile run cover

#
# Compile the implementation and testing files.  In a full project Makefile,
# the -sourcepath argument should include both the testing and implementation
# directories, e.g., "-sourcepath .:$(IMPLE_SRC_DIR)"
#
compile:
	@echo "Compiling tests ..."
	javac -g \
	  -classpath $(TESTNG_JAR) \
	  -sourcepath . \
	  -d $(EXECUTABLES) \
		$(TEST_FILES)

#
# Run the TestNG tests alone, without a coverage report.
#
run:
	@echo "Running tests ..."
	java -classpath $(TESTNG_JAR):$(EXECUTABLES) \
	    org.testng.TestNG testng.xml

#
# Run the TestNG tests under Cobertura and generate a coverage report.
#
# This rule is broken up into three parts for explanatory purposes.  You can do
# all three parts in one make rule if you like.  The Cobertura command-line
# interface is further documented in
#     http://cobertura.sourceforge.net/commandlinereference.html
#
cover:	instrument run_coverage gen_report

#
# Instrument the files to be checked for coverage.  This *excludes* the testing
# files, since we don't want to run coverage on those.
#
instrument:
	@echo "Instrumenting ..."
	rm -f instrumented/* cobertura.ser
	$(COBERTURA_DIR)/cobertura-instrument.sh --destination instrumented \
	    $(EXECUTABLES)/CoverageExample.class

#
# Run the TestNG tests under Cobertura.  The coverage results go into a file
# named "cobertura.ser".  You don't need to worry about the contents of this
# file; it's used by the Cobertura report generator in the gen_report rule.
#
run_coverage:
	@echo "Running code coverage ..."
	java -cp $(COBERTURA_JAR):$(TESTNG_JAR):instrumented:$(EXECUTABLES):. \
	    org.testng.TestNG testng.xml

#
# Generate the coverage report from the cobertura.ser file.  Note the inclusion
# of IMPLE_SRC_DIR as an argument.  Cobertura needs the source files to
# generate the nice-looking line-numbered coverage display in the HTMl report.
#
gen_report:
	$(COBERTURA_DIR)/cobertura-report.sh --destination coverage-report \
	    $(IMPLE_SRC_DIR)

#
# Open the TestNG output and the Cobertura coverage reports.  The rules use the
# Mac OS "open" command, which you can substitute with the appropriate
# alternate command for your platform.  The point of the rules is to show where
# the TestNG output and Cobertura coverage reports are located.
#
open_reports:	test_output coverage_report
test_output:
	@csh -q -c 'open test-output/index.html'
coverage_report:
	@csh -q -c 'open coverage-report/index.html'

#
# Remove all .class files from the classes dir, all the test output files, and
# all the coverage report files.  Also remove cobertura.ser.
#
clean:
	rm -rf classes/*.class
	rm -rf test-output/*
	rm -rf coverage-report/*
	rm -f cobertura.ser