#
# Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.  Oracle designates this
# particular file as subject to the "Classpath" exception as provided
# by Oracle in the LICENSE file that accompanied this code.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
#

#----------------------------------------------------------------------

# Test @compile with -compilejdk; use not-a-jdk for -testjdk
$(BUILDTESTDIR)/CompileTest.1.agentvm.ok \
$(BUILDTESTDIR)/CompileTest.1.othervm.ok: \
	    $(JTREG_IMAGEDIR)/lib/jtreg.jar \
	    $(JTREG_IMAGEDIR)/bin/jtreg \
	    $(BUILDDIR)/not-a-jdk/bin/java
	$(RM) $(@:%.ok=%)
	JTREG_SHOWAGENT=true JTREG_SHOWCMD=true \
	JT_JAVA=$(JDKHOME) \
		$(JTREG_IMAGEDIR)/bin/jtreg $(JTREG_OPTS) \
		-w:$(@:%.ok=%)/work -r:$(@:%.ok=%)/report \
		-compilejdk:$(JDK5HOME) \
		-jdk:$(BUILDDIR)/not-a-jdk \
		$(@:$(BUILDTESTDIR)/CompileTest.1.%.ok=-%) \
		$(TESTDIR)/compilejdk/CompileTest.java
	grep $(JDK5HOME) $(@:%.ok=%)/work/CompileTest.jtr
	if grep $(JDKHOME) $(@:%.ok=%)/work/CompileTest.jtr ; then exit 1; fi
	echo "test passed at `date`" > $@

# Precompile test classes, then test @run main with -testjdk, use not-a-jdk for -compilejdk
$(BUILDTESTDIR)/CompileTest.2.agentvm.ok \
$(BUILDTESTDIR)/CompileTest.2.othervm.ok: \
	    $(JTREG_IMAGEDIR)/lib/jtreg.jar \
	    $(JTREG_IMAGEDIR)/bin/jtreg \
	    $(BUILDDIR)/not-a-jdk/bin/java
	$(RM) $(@:%.ok=%)
	JTREG_SHOWAGENT=true JTREG_SHOWCMD=true \
	JT_JAVA=$(JDKHOME) $(JTREG_IMAGEDIR)/bin/jtreg $(JTREG_OPTS) \
		-w:$(@:%.ok=%)/work -r:$(@:%.ok=%)/report \
		-jdk:$(JDK5HOME) \
		$(@:$(BUILDTESTDIR)/CompileTest.2.%.ok=-%) \
		$(TESTDIR)/compilejdk/ExecuteTest.java
	JTREG_SHOWAGENT=true JTREG_SHOWCMD=true \
	JT_JAVA=$(JDKHOME) \
		$(JTREG_IMAGEDIR)/bin/jtreg $(JTREG_OPTS) \
		-w:$(@:%.ok=%)/work -r:$(@:%.ok=%)/report \
		-compilejdk:$(BUILDDIR)/not-a-jdk \
		-jdk:$(JDK5HOME) \
		$(@:$(BUILDTESTDIR)/CompileTest.2.%.ok=-%) \
		$(TESTDIR)/compilejdk/ExecuteTest.java
	grep $(JDK5HOME) $(@:%.ok=%)/work/ExecuteTest.jtr
	if grep $(JDKHOME) $(@:%.ok=%)/work/ExecuteTest.jtr ; then exit 1; fi
	echo "test passed at `date`" > $@

# Test compilation on JDK, execution on just-java (JRE lookalike)
$(BUILDTESTDIR)/CompileTest.3.agentvm.ok \
$(BUILDTESTDIR)/CompileTest.3.othervm.ok: \
	    $(JTREG_IMAGEDIR)/lib/jtreg.jar \
	    $(JTREG_IMAGEDIR)/bin/jtreg \
	    $(BUILDDIR)/just-java/bin/java
	$(RM) $(@:%.ok=%)
	JTREG_SHOWAGENT=true JTREG_SHOWCMD=true \
	JT_JAVA=$(JDKHOME) \
		$(JTREG_IMAGEDIR)/bin/jtreg $(JTREG_OPTS) \
		-w:$(@:%.ok=%)/work -r:$(@:%.ok=%)/report \
		-compilejdk:$(JDK5HOME) \
		-jdk:$(BUILDDIR)/just-java \
		$(@:$(BUILDTESTDIR)/CompileTest.3.%.ok=-%) \
		$(TESTDIR)/compilejdk/ExecuteTest.java
	echo "test passed at `date`" > $@

# Test compilation on one version of JDK, execution on another;
# Verify tools.jar is on classpath for both
$(BUILDTESTDIR)/CompileTest.4.agentvm.ok \
$(BUILDTESTDIR)/CompileTest.4.othervm.ok: \
	    $(JTREG_IMAGEDIR)/lib/jtreg.jar \
	    $(JTREG_IMAGEDIR)/bin/jtreg
	$(RM) $(@:%.ok=%)
	JTREG_SHOWAGENT=true JTREG_SHOWCMD=true \
	JT_JAVA=$(JDKHOME) \
		$(JTREG_IMAGEDIR)/bin/jtreg $(JTREG_OPTS) \
		-w:$(@:%.ok=%)/work -r:$(@:%.ok=%)/report \
		-compilejdk:$(JDK5HOME) \
		-jdk:$(JDK6HOME) \
		$(@:$(BUILDTESTDIR)/CompileTest.4.%.ok=-%) \
		$(TESTDIR)/compilejdk/ToolsJarTest.java
	echo "test passed at `date`" > $@

# Verify shell test gets COMPILEJAVA set as well as TESTJAVA
$(BUILDTESTDIR)/CompileTest.5.ok: \
	    $(JTREG_IMAGEDIR)/lib/jtreg.jar \
	    $(JTREG_IMAGEDIR)/bin/jtreg
	$(RM) $(@:%.ok=%)
	JT_JAVA=$(JDKHOME) \
		$(JTREG_IMAGEDIR)/bin/jtreg $(JTREG_OPTS) \
		-w:$(@:%.ok=%)/work -r:$(@:%.ok=%)/report \
		-compilejdk:$(JDK5HOME) \
		-jdk:$(JDK6HOME) \
		$(TESTDIR)/compilejdk/ShellTest.sh
	fgrep 'COMPILEJAVA=$(JDK5HOME)' $(@:%.ok=%)/work/ShellTest.jtr
	fgrep 'TESTJAVA=$(JDK6HOME)' $(@:%.ok=%)/work/ShellTest.jtr
	echo "test passed at `date`" > $@

# Invalid JDK, to trap unexpected use; suitable for -version, not much else
$(BUILDDIR)/not-a-jdk/bin/java:
	mkdir -p $(@D)
	(   echo '#!/bin/bash' ; \
	    echo 'if [ $${!#} = "java.specification.version" ]; then' ; \
	    echo '    echo java.specification.version=1.2 ;' ; \
	    echo 'else' ; \
	    echo '    echo "Not a JDK: $$0 $$*" ;' ; \
	    echo 'fi' ; \
	) > $@
	chmod +x $@

# Just Java, no javac
$(BUILDDIR)/just-java/bin/java:
	mkdir -p $(@D)
	echo '$(JDK6HOME)/bin/java $$*' > $@
	chmod +x $@

ifneq ($(OS_NAME), windows)
ifdef JDK5HOME
TESTS.jtreg += \
	$(BUILDTESTDIR)/CompileTest.1.agentvm.ok \
	$(BUILDTESTDIR)/CompileTest.1.othervm.ok \
	$(BUILDTESTDIR)/CompileTest.2.agentvm.ok \
	$(BUILDTESTDIR)/CompileTest.2.othervm.ok \
	$(BUILDTESTDIR)/CompileTest.3.agentvm.ok \
	$(BUILDTESTDIR)/CompileTest.3.othervm.ok \
	$(BUILDTESTDIR)/CompileTest.4.agentvm.ok \
	$(BUILDTESTDIR)/CompileTest.4.othervm.ok \
	$(BUILDTESTDIR)/CompileTest.5.ok
endif
endif
