!******************************************************************************!
! Test the Generate command to generate a JNLP file, its associated jars, and
! test to ensure that everything was generated properly to execute a Web Start
! test locally via "file:///" to generated JNLP file. Also test running Java Web Start
! against a local web server (HTTPSERV) (i.e. http://...).
!
! Note: this test requires that 'javaws' (Java Web Start) under $JAVAHOME or in OS path.
!
! Author: Neon Ngo
!******************************************************************************!
startmacro
  switch INTERACTIVE interAct   GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch BASELINE    baseline   GET DEF=FALSE SDEF=TRUE  ! for defects or more obscure test case(s)
  switch ALTTESTENV  alttestenv GET DEF=FALSE SDEF=TRUE  ! alternate test environment

  res altinstr ""
  if alttestenv then
    res altinstr "/ALTTESTENV"
  endif

  ! set destination directory to write AUX
  fname destdir ^{aux.write} OPT=AUX
  if destdir eqs "ram:" then
    warning "Cannot run this test case with WRITE AUX set to RAM."
    stop
  endif

  ! The following are the actual commands to generate the webstart application.
  set cmdname   "demo"
  set opts      "DSP|SYS"
  set codebase  "file:///^{env.nmroot}jars"
  file name jnlplocalfile "^{destdir}" cmdname "jnlp"

  set autotestsrc  "nxm.sys.test.test_webstart_autotest.jnlp"
  fname/parse autotestsrc name=fn ext=fext
  file name autotestjnlp ,,      fn fext
  file name jnlpdest     destdir fn fext

  ! ==== GENERATE JNLP FILE
  generate jnlp cmdname "^opts" destdir codebase jnlplocalfile

  ! Now we check to ensure that all of the files were written out, and for
  ! some of the files, make sure the contents look correct.
  assert/stat=test/text="^reg.main.name: GENERATE created JNLP file ^jnlplocalfile" jnlplocalfile fexist
  if test isFalse then stop ! Abort if unable to generate file

  ! ==== COPY AUTOTEST JNLP FILE
  ! This is used to test Bug 1512 (HTTPSERV: Problems serving up JNLP and JAR files).
  !say "running: file copy/b autotestsrc jnlpdest"
  file copy/b autotestsrc jnlpdest
  assert/stat=test/text="^reg.main.name: JNLP file copied and ready for use" jnlpdest fexist
  if test isFalse then stop ! Abort if unable to copy file

  if interAct then
    ! determine where javaws is located
    import "JAVAHOME" javahome
    if javahome ndexists then import "JAVA_HOME" javahome
    ! location of javaws for Oracle Java 8
    file name javaws_cmd "(^{javahome},bin)" javaws

    if javaws_cmd NFEXISTS then
      ! Check if exists under Windows platform
      if ^{javaws_cmd}.exe FEXISTS then
        set javaws_cmd "^{javaws_cmd}.exe"
      else
        ! try 'javaws' just in case it is on OS path
        warning "Unable to find '^{javaws_cmd}' for running Java Web Start, will try 'javaws'"
        set javaws_cmd "javaws"
      endif
    endif
    ! since 3.5.4 to test OpenJDK's IcedTea-Web's javaws (Linux only?) for BUG 2897
    file name javaws_itweb_cmd "(usr,bin)" javaws.itweb

    pipe on
      panel/msgid=MAIN/setup/controls=gc
      ! port 0 to listen on any free port
      httpserv/AUXLIST=^{aux.write} PORT=0 DIR="jars" NAME="jars"
      ! URL when using HTTPSERV/AUXLIST=1 -> http://hostname:port/Files/1/test_webstart_autotest.jnlp
      set jnlpurl  "http://^{reg.httpserv.host}:^{reg.httpserv.port}/Files/^{aux.write}/^{autotestjnlp}"
      textdisplay/id=tdisp text=""

      gcontrol label macro "MACRO"
      gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
      set gc.main.itemColors {PASS=GREEN,FAIL=RED}
      gcontrol menu tests "Java Web Start tests" "1. StartDemo   (file),&
                                                  2. AutoTest    (http),&
                                                  3. IcedTea-Web (http) Linux,&
                                                  4. Check #3 no workaround" /AlwaysOpen

      message send tdisp ,, "APPENDL" ,, "If running with Oracle Java, SKIP this test if running JDK 11+"
      message send tdisp ,, "APPENDL" ,, "If running with IcedTea-Web 1.7.x or earlier, SKIP this test if running JDK 11+"
      message send tdisp ,, "APPENDL" ,, "If running with IcedTea-Web 1.8.x or later, SKIP this test if running JDK 17+"
      message send tdisp ,, "APPENDL" ,, ""
      message send tdisp ,, "APPENDL" ,, " 1. Click on [StartDemo] to launch the 'DEMO' macro from local file."
      message send tdisp ,, "APPENDL" ,, ""
      message send tdisp ,, "APPENDL" ,, " -> The 'DEMO' macro should have executed in a separate frame via Java Web Start."
      message send tdisp ,, "APPENDL" ,, ""
      message send tdisp ,, "APPENDL" ,, " -> In the terminal of the 'DEMO' macro:"
      message send tdisp ,, "APPENDL" ,, ""
      message send tdisp ,, "APPENDL" ,, " ->   1) Type 'option'."
      message send tdisp ,, "APPENDL" ,, " ->      Verify that it prints the option trees. Look for classpath:... in the output terminal."
      message send tdisp ,, "APPENDL" ,, ""
      ! 2010-01-22 NTN: Instructions for testing Bug 2054 to run UNITTEST within Web Start
      message send tdisp ,, "APPENDL" ,, " ->   2) Run 'UNITTEST SYS'."
      message send tdisp ,, "APPENDL" ,, "         It should run all SYS unit tests with no failures. "
      message send tdisp ,, "APPENDL" ,, ""
      ! 2017-08-04 JTM: Instructions for testing NXM-3044 NeXtMidas Interfering with Java Web Start caching
      message send tdisp ,, "APPENDL" ,, " ->   3) Set HOMEPATH to test area 'HOME classpath:nxm/sys/test'."
      message send tdisp ,, "APPENDL" ,, " ->   4) Run '%test_webstart_caching^{altinstr}'.  It should PASS."
      message send tdisp ,, "APPENDL" ,, ""
      message send tdisp ,, "APPENDL" ,, " -> Press [Stop] in the MACRO section of the 'DEMO' macro to exit the demo."
      message send tdisp ,, "APPENDL" ,, ""
      message send tdisp ,, "APPENDL" ,, " 2. Click on [AutoTest] to run the automatic tests from local web server."
      message send tdisp ,, "APPENDL" ,, ""
      message send tdisp ,, "APPENDL" ,, " -> The test macro should have executed in a separate frame via Java Web Start."
      message send tdisp ,, "APPENDL" ,, ""
      message send tdisp ,, "APPENDL" ,, " -> When the 'Tests Passed' popup comes up, press [OK]."
      message send tdisp ,, "APPENDL" ,, ""
    if javaws_itweb_cmd NFEXISTS AND env.osname eqs "Linux" then
      message send tdisp ,, "APPENDL" ,, " Prerequisite: Unable to find '^{javaws_itweb_cmd}', run 'sudo yum install icedtea-web'"
    endif
      message send tdisp ,, "APPENDL" ,, " 3. (Linux only) click on [IcedTea-Web] to run the automatic tests from local web server."
      message send tdisp ,, "APPENDL" ,, ""
      message send tdisp ,, "APPENDL" ,, " -> The test macro should have executed in a separate frame via Java Web Start."
      message send tdisp ,, "APPENDL" ,, ""
      message send tdisp ,, "APPENDL" ,, " -> When the 'Tests Passed' popup comes up, press [OK]."
      message send tdisp ,, "APPENDL" ,, ""
      message send tdisp ,, "APPENDL" ,, " 4. (Linux only) click on #4, this is same as #3 IcedTea-Web with no workaround."
      message send tdisp ,, "APPENDL" ,, "    Unknown issue: if it works, we can look into adjusting workaround in JarResource.exists()"
      message send tdisp ,, "APPENDL" ,, ""
      message send tdisp ,, "APPENDL" ,, " 5. NOTE: If you built NeXtMidas with JDK 8 then you must tell IcedTea-Web to use OpenJDK8 because it"
      message send tdisp ,, "APPENDL" ,, "   defaults to OpenJDK7. You can accomplish this by executing itweb-settings.itweb"
      message send tdisp ,, "APPENDL" ,, "   in your terminal. Then, go to JVM Settings and Set your JVM for IcedTea-Web to OpenJDK8."
      message send tdisp ,, "APPENDL" ,, ""
      message send tdisp ,, "APPENDL" ,, "Webstart Security Warnings"
      message send tdisp ,, "APPENDL" ,, "-----------------------------------"
      message send tdisp ,, "APPENDL" ,, "    If prompted with a 'Security Warning' dialog, select 'Start', 'Yes' or 'Run'."
      message send tdisp ,, "APPENDL" ,, "    See NeXtMidas User's Guide > 'Java WebStart' > 'WebStart' >"
      message send tdisp ,, "APPENDL" ,, "    'Building Web Start Applications' for details."
      message send tdisp ,, "APPENDL" ,, "    Note: Instructions start with 'In order to suppress the security warning'"

    pipe off
  endif

  ! Now go through and delete all of the files that were created.
  erase jnlplocalfile
  erase jnlpdest

endmacro

!******************************************************************************!
procedure processMessage m:msg
  !say "^{reg.main.name}: msg.name ^msg.name, data ^msg.data"

  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: Tests for WEBSTART" msg.data eqss "PASS"
    pipe stop

  elseif msg.name eqs "Tests" then
    if msg.data eqss "1. StartDemo"   then set cmd "^javaws_cmd ^jnlplocalfile"
    if msg.data eqss "2. AutoTest"    then set cmd "^javaws_cmd ^jnlpurl"
    if msg.data eqss "3. IcedTea-Web" then set cmd "^javaws_itweb_cmd -strict ^jnlpurl"
    if msg.data eqss "4. Check #3"    then set cmd "^javaws_itweb_cmd -property BUGFIX.JARRESOURCE_ICEDTEA_WEB_WORKAROUND=false ^jnlpurl"
    INFO "Running: ^cmd"
    os ^cmd

  else
    say "^reg.main.name: Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed
!******************************************************************************!
procedure processException m:emsg
  res emsg.data.message
  if expectedException REXISTS then
    assert/text="^reg.main.name: Expected ERROR starts with: ^{expectedException}" expectedException EQSS emsg.data.message
    remove expectedException
  else
    assert/text="^reg.main.name: Caught unexpected exception ^{emsg.data}" "FALSE" isTrue
    if "Trace" SUBS ENV.DEBUG then invoke ,, emsg.data.printStackTrace()
  endif
return
