name: Validate Java e2e

on:
  push:
    branches:
      - main
      - releases/*
    paths-ignore:
      - '**.md'
  pull_request:
    paths-ignore:
      - '**.md'
  schedule:
    - cron: '0 */12 * * *'
  workflow_dispatch:

permissions:
  contents: read

jobs:
  setup-java-major-versions:
    name: ${{ matrix.distribution }} ${{ matrix.version }} (jdk-${{ contains(matrix.os, 'macos') && !contains(matrix.os, 'intel') && 'arm64' || 'x64' }}) - ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [macos-15-intel, windows-latest, ubuntu-latest]
        distribution: [
            'temurin',
            'adopt',
            'adopt-openj9',
            'zulu',
            'liberica',
            'microsoft',
            'semeru',
            'corretto',
            'dragonwell',
            'sapmachine',
            'jetbrains',
            'kona',
            'liberica-nik'
          ] # internally 'adopt-hotspot' is the same as 'adopt'
        version: ['21', '11', '17']
        exclude:
          - distribution: microsoft
            version: 8
          - distribution: dragonwell
            os: macos-15-intel
        include:
          - distribution: microsoft
            os: windows-latest
            version: 25
          - distribution: microsoft
            os: ubuntu-latest
            version: 25
          - distribution: microsoft
            os: macos-latest
            version: 25
          - distribution: kona
            os: windows-latest
            version: 25
          - distribution: kona
            os: ubuntu-latest
            version: 25
          - distribution: kona
            os: macos-latest
            version: 25
          - distribution: liberica-nik
            os: windows-latest
            version: 25
          - distribution: liberica-nik
            os: ubuntu-latest
            version: 25
          - distribution: liberica-nik
            os: macos-latest
            version: 25
          - distribution: oracle
            os: macos-15-intel
            version: 17
          - distribution: oracle
            os: windows-latest
            version: 21
          - distribution: oracle
            os: ubuntu-latest
            version: 21
          - distribution: graalvm
            os: macos-latest
            version: 17.0.12
          - distribution: graalvm
            os: windows-latest
            version: 21
          - distribution: graalvm
            os: ubuntu-latest
            version: 21
          - distribution: graalvm
            os: ubuntu-latest
            version: '24-ea'
    steps:
      - &checkout_step
        name: Checkout
        uses: actions/checkout@v7
        with:
          persist-credentials: false
      - name: setup-java
        uses: ./
        id: setup-java
        with:
          java-version: ${{ matrix.version }}
          distribution: ${{ matrix.distribution }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Verify Java
        env:
          JAVA_VERSION: ${{ matrix.version }}
          JAVA_PATH: ${{ steps.setup-java.outputs.path }}
        run: bash __tests__/verify-java.sh "$JAVA_VERSION" "$JAVA_PATH"
        shell: bash

  setup-java-alpine-linux:
    name: ${{ matrix.distribution }} ${{ matrix.version }} (jdk-${{ contains(matrix.os, 'macos') && !contains(matrix.os, 'intel') && 'arm64' || 'x64' }}) - alpine-linux - ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    container:
      image: alpine:3.21
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest]
        distribution: ['temurin', 'sapmachine']
        version: ['21', '17']
    steps:
      - *checkout_step
      - name: Install bash
        run: apk add --no-cache bash
      - name: setup-java
        uses: ./
        id: setup-java
        with:
          java-version: ${{ matrix.version }}
          distribution: ${{ matrix.distribution }}
      - name: Verify Java
        env:
          JAVA_VERSION: ${{ matrix.version }}
          JAVA_PATH: ${{ steps.setup-java.outputs.path }}
        run: bash __tests__/verify-java.sh "$JAVA_VERSION" "$JAVA_PATH"
        shell: bash

  setup-java-major-minor-versions:
    name: ${{ matrix.distribution }} ${{ matrix.version }} (jdk-${{ contains(matrix.os, 'macos') && !contains(matrix.os, 'intel') && 'arm64' || 'x64' }}) - ${{ matrix.os }}
    needs: setup-java-major-versions
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: &default_os [macos-latest, windows-latest, ubuntu-latest]
        distribution: ['temurin', 'zulu', 'liberica']
        version:
          - '11.0'
          - '21.0'
          - '17.0.7+7'
        include:
          - distribution: oracle
            os: ubuntu-latest
            version: '21.0.4'
          - distribution: graalvm
            os: ubuntu-latest
            version: '21.0.4'
          - distribution: dragonwell
            os: ubuntu-latest
            version: '11.0'
          - distribution: dragonwell
            os: ubuntu-latest
            version: '11.0.13+9'
          - distribution: sapmachine
            os: ubuntu-latest
            version: '17.0.7'
          - distribution: jetbrains
            os: ubuntu-latest
            version: '11.0.11'
          - distribution: jetbrains
            os: ubuntu-latest
            version: '17.0.7'
    steps:
      - *checkout_step
      - name: setup-java
        uses: ./
        id: setup-java
        with:
          java-version: ${{ matrix.version }}
          distribution: ${{ matrix.distribution }}
      - name: Verify Java
        env:
          JAVA_VERSION: ${{ matrix.version }}
          JAVA_PATH: ${{ steps.setup-java.outputs.path }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: bash __tests__/verify-java.sh "$JAVA_VERSION" "$JAVA_PATH"
        shell: bash

  setup-java-check-latest:
    name: ${{ matrix.distribution }} ${{ matrix.version }} - check-latest flag - ${{ matrix.os }}
    needs: setup-java-major-versions
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: *default_os
        distribution:
          [
            'temurin',
            'zulu',
            'liberica',
            'dragonwell',
            'sapmachine',
            'jetbrains'
          ]
        exclude:
          - distribution: dragonwell
            os: macos-latest
    steps:
      - *checkout_step
      - name: setup-java
        uses: ./
        id: setup-java
        with:
          distribution: ${{ matrix.distribution }}
          java-version: 11
          check-latest: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Verify Java
        env:
          JAVA_PATH: ${{ steps.setup-java.outputs.path }}
        run: bash __tests__/verify-java.sh "11" "$JAVA_PATH"
        shell: bash

  setup-java-multiple-jdks:
    name: ${{ matrix.distribution }} ${{ matrix.version }} - multiple jdks - ${{ matrix.os }}
    needs: setup-java-major-versions
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: *default_os
        distribution:
          [
            'temurin',
            'zulu',
            'liberica',
            'dragonwell',
            'sapmachine',
            'jetbrains'
          ]
        exclude:
          - distribution: dragonwell
            os: macos-latest
    steps:
      - *checkout_step
      - name: setup-java
        uses: ./
        id: setup-java
        with:
          distribution: ${{ matrix.distribution }}
          java-version: |
            11
            17
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Verify Java env variables
        run: |
          $versionsArr = "11","17"
          foreach ($version in $versionsArr)
          {
            $envName = "JAVA_HOME_${version}_${env:RUNNER_ARCH}"
            $JavaVersionPath = [Environment]::GetEnvironmentVariable($envName)
            if (-not (Test-Path "$JavaVersionPath")) {
              Write-Host "$envName is not found"
              exit 1
            }
          }
        shell: pwsh
      - name: Verify Java
        env:
          JAVA_PATH: ${{ steps.setup-java.outputs.path }}
        run: bash __tests__/verify-java.sh "17" "$JAVA_PATH"
        shell: bash

  setup-java-ea-versions:
    name: ${{ matrix.distribution }} ${{ matrix.version }} (jdk-${{ contains(matrix.os, 'macos') && !contains(matrix.os, 'intel') && 'arm64' || 'x64' }}) - ${{ matrix.os }}
    needs: setup-java-major-minor-versions
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - {os: macos-15-intel, version: '17-ea', distribution: zulu}
          - {os: windows-latest, version: '17-ea', distribution: zulu}
          - {os: ubuntu-latest, version: '17-ea', distribution: zulu}
          - {os: macos-15-intel, version: '15.0.0-ea.14', distribution: zulu}
          - {os: windows-latest, version: '15.0.0-ea.14', distribution: zulu}
          - {os: ubuntu-latest, version: '15.0.0-ea.14', distribution: zulu}
          - {os: macos-latest, version: '17-ea', distribution: temurin}
          - {os: windows-latest, version: '17-ea', distribution: temurin}
          - {os: ubuntu-latest, version: '17-ea', distribution: temurin}
          - {os: macos-latest, version: '17-ea', distribution: sapmachine}
          - {os: windows-latest, version: '17-ea', distribution: sapmachine}
          - {os: ubuntu-latest, version: '17-ea', distribution: sapmachine}
          - {os: macos-latest, version: '21-ea', distribution: sapmachine}
          - {os: windows-latest, version: '21-ea', distribution: sapmachine}
          - {os: ubuntu-latest, version: '21-ea', distribution: sapmachine}
    steps:
      - *checkout_step
      - name: setup-java
        uses: ./
        id: setup-java
        with:
          java-version: ${{ matrix.version }}
          distribution: ${{ matrix.distribution }}
      - name: Verify Java
        env:
          JAVA_VERSION: ${{ matrix.version }}
          JAVA_PATH: ${{ steps.setup-java.outputs.path }}
        run: bash __tests__/verify-java.sh "$JAVA_VERSION" "$JAVA_PATH"
        shell: bash

  setup-java-signature-verification:
    name: ${{ matrix.distribution }} ${{ matrix.version }} signature verification - ${{ matrix.os }}
    needs: setup-java-major-minor-versions
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: *default_os
        version: ['21', '17']
        distribution: [temurin, microsoft]
    steps:
      - *checkout_step
      - name: setup-java with signature verification
        uses: ./
        id: setup-java
        with:
          java-version: ${{ matrix.version }}
          distribution: ${{ matrix.distribution }}
          verify-signature: true
      - name: Verify Java
        env:
          JAVA_VERSION: ${{ matrix.version }}
          JAVA_PATH: ${{ steps.setup-java.outputs.path }}
        run: bash __tests__/verify-java.sh "$JAVA_VERSION" "$JAVA_PATH"
        shell: bash

  setup-java-custom-package-type:
    name: ${{ matrix.distribution }} ${{ matrix.version }} (${{ matrix.java-package }}-x64) - ${{ matrix.os }}
    needs: setup-java-major-minor-versions
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [macos-15-intel, windows-latest, ubuntu-latest]
        distribution:
          ['temurin', 'zulu', 'liberica', 'semeru', 'sapmachine', 'jetbrains']
        java-package: ['jre']
        version: ['17.0']
        include:
          - distribution: 'zulu'
            java-package: jre+fx
            version: '21'
            os: ubuntu-latest
          - distribution: 'zulu'
            java-package: jdk+fx
            version: '21.0'
            os: ubuntu-latest
          - distribution: 'liberica'
            java-package: jdk+fx
            version: '21'
            os: ubuntu-latest
          - distribution: 'liberica'
            java-package: jre+fx
            version: '11'
            os: ubuntu-latest
          - distribution: 'liberica-nik'
            java-package: jdk+fx
            version: '21'
            os: ubuntu-latest
          - distribution: 'corretto'
            java-package: jre
            version: '8'
            os: windows-latest
          - distribution: 'jetbrains'
            java-package: jdk+jcef
            version: '11'
            os: ubuntu-latest
          - distribution: 'jetbrains'
            java-package: jdk+jcef
            version: '17'
            os: ubuntu-latest
          - distribution: 'jetbrains'
            java-package: jdk+jcef
            version: '21'
            os: ubuntu-latest
          - distribution: 'jetbrains'
            java-package: jre+jcef
            version: '11'
            os: ubuntu-latest
          - distribution: 'jetbrains'
            java-package: jre+jcef
            version: '17'
            os: ubuntu-latest
          - distribution: 'jetbrains'
            java-package: jre+jcef
            version: '21'
            os: ubuntu-latest
          - distribution: 'jetbrains'
            java-package: jdk+ft
            version: '17'
            os: ubuntu-latest
          - distribution: 'jetbrains'
            java-package: jdk+ft
            version: '21'
            os: ubuntu-latest
          - distribution: 'jetbrains'
            java-package: jre+ft
            version: '17'
            os: ubuntu-latest
          - distribution: 'jetbrains'
            java-package: jre+ft
            version: '21'
            os: ubuntu-latest

    steps:
      - *checkout_step
      - name: setup-java
        uses: ./
        id: setup-java
        with:
          java-version: ${{ matrix.version }}
          java-package: ${{ matrix.java-package }}
          distribution: ${{ matrix.distribution }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Verify Java
        env:
          JAVA_VERSION: ${{ matrix.version }}
          JAVA_PATH: ${{ steps.setup-java.outputs.path }}
        run: bash __tests__/verify-java.sh "$JAVA_VERSION" "$JAVA_PATH"
        shell: bash

  # Only Liberica and Zulu provide x86
  setup-java-x86:
    name: ${{ matrix.distribution }} ${{ matrix.version }} (jdk-${{ matrix.architecture }}) - ${{ matrix.os }}
    needs: setup-java-major-minor-versions
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        # x86 is not supported on macOS
        os: [windows-latest, ubuntu-22.04]
        distribution: ['liberica', 'zulu', 'corretto']
        version: ['11']
    steps:
      - *checkout_step
      - name: setup-java
        uses: ./
        id: setup-java
        with:
          distribution: ${{ matrix.distribution }}
          java-version: ${{ matrix.version }}
          architecture: 'x86'
      - name: Verify Java
        env:
          JAVA_VERSION: ${{ matrix.version }}
          JAVA_PATH: ${{ steps.setup-java.outputs.path }}
        run: bash __tests__/verify-java.sh "$JAVA_VERSION" "$JAVA_PATH"
        shell: bash

  setup-java-version-both-version-inputs-presents:
    name: ${{ matrix.distribution }} version (should be from input) - ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: *default_os
        distribution: ['temurin', 'microsoft', 'corretto']
        java-version-file: ['.java-version', '.tool-versions']
    steps:
      - *checkout_step
      - name: Create .java-version file
        shell: bash
        run: echo "17" > .java-version
      - name: Create .tool-versions file
        shell: bash
        run: echo "java 17" > .tool-versions
      - name: setup-java
        uses: ./
        id: setup-java
        with:
          distribution: ${{ matrix.distribution }}
          java-version: 11
          java-version-file: ${{matrix.java-version-file }}
      - name: Verify Java
        env:
          JAVA_PATH: ${{ steps.setup-java.outputs.path }}
        run: bash __tests__/verify-java.sh "11" "$JAVA_PATH"
        shell: bash

  setup-java-version-from-file-major-notation:
    name: ${{ matrix.distribution }} version from file X - ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: *default_os
        distribution: ['temurin', 'zulu', 'liberica', 'microsoft', 'corretto']
        java-version-file: ['.java-version', '.tool-versions']
    steps:
      - *checkout_step
      - name: Create .java-version file
        shell: bash
        run: echo "11" > .java-version
      - name: Create .tool-versions file
        shell: bash
        run: echo "java 11" > .tool-versions
      - name: setup-java
        uses: ./
        id: setup-java
        with:
          distribution: ${{ matrix.distribution }}
          java-version-file: ${{matrix.java-version-file }}
      - name: Verify Java
        env:
          JAVA_PATH: ${{ steps.setup-java.outputs.path }}
        run: bash __tests__/verify-java.sh "11" "$JAVA_PATH"
        shell: bash

  setup-java-version-from-file-major-minor-patch-notation:
    name: ${{ matrix.distribution }} version from file X.Y.Z - ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: *default_os
        distribution: ['adopt', 'adopt-openj9', 'zulu']
        java-version-file: ['.java-version', '.tool-versions']
    steps:
      - *checkout_step
      - name: Create .java-version file
        shell: bash
        run: echo "17.0.10" > .java-version
      - name: Create .tool-versions file
        shell: bash
        run: echo "java 17.0.10" > .tool-versions
      - name: setup-java
        uses: ./
        id: setup-java
        with:
          distribution: ${{ matrix.distribution }}
          java-version-file: ${{matrix.java-version-file }}
      - name: Verify Java
        env:
          JAVA_PATH: ${{ steps.setup-java.outputs.path }}
        run: bash __tests__/verify-java.sh "17.0.10" "$JAVA_PATH"
        shell: bash

  setup-java-version-from-file-major-minor-patch-with-dist:
    name: ${{ matrix.distribution }} version from file '${{ matrix.java-version-file }}' - ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: *default_os
        distribution: ['adopt', 'zulu', 'liberica']
        java-version-file: ['.java-version', '.tool-versions', '.sdkmanrc']
    steps:
      - *checkout_step
      - name: Create .java-version file
        shell: bash
        run: echo "openjdk64-17.0.10" > .java-version
      - name: Create .tool-versions file
        shell: bash
        run: echo "java openjdk64-17.0.10" > .tool-versions
      - name: Create .sdkmanrc file
        shell: bash
        run: echo "java=17.0.10-tem" > .sdkmanrc
      - name: setup-java
        uses: ./
        id: setup-java
        with:
          distribution: ${{ matrix.distribution }}
          java-version-file: ${{matrix.java-version-file }}
      - name: Verify Java
        env:
          JAVA_PATH: ${{ steps.setup-java.outputs.path }}
        run: bash __tests__/verify-java.sh "17.0.10" "$JAVA_PATH"
        shell: bash

  setup-java-set-default:
    name: set-default option - ${{ matrix.os }}
    needs: setup-java-major-versions
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: *default_os
    steps:
      - *checkout_step
      - name: Setup Java 17 as default
        uses: ./
        id: setup-java-17
        with:
          distribution: 'temurin'
          java-version: '17'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Setup Java 21 without setting as default
        uses: ./
        id: setup-java-21
        with:
          distribution: 'temurin'
          java-version: '21'
          set-default: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Verify JAVA_HOME still points to Java 17
        env:
          JAVA_17_PATH: ${{ steps.setup-java-17.outputs.path }}
        run: |
          echo "JAVA_HOME=$JAVA_HOME"
          echo "Java 17 path=$JAVA_17_PATH"
          if [ "$JAVA_HOME" != "$JAVA_17_PATH" ]; then
            echo "JAVA_HOME should still point to Java 17"
            exit 1
          fi
        shell: bash
      - name: Verify java -version reports Java 17
        run: |
          JAVA_VERSION=$(java -version 2>&1 | head -1)
          echo "java -version: $JAVA_VERSION"
          if ! echo "$JAVA_VERSION" | grep -q "17"; then
            echo "Default java should still be version 17"
            exit 1
          fi
        shell: bash
      - name: Verify JAVA_HOME_21 env var is set
        run: |
          $envName = "JAVA_HOME_21_${env:RUNNER_ARCH}"
          $JavaVersionPath = [Environment]::GetEnvironmentVariable($envName)
          if (-not $JavaVersionPath) {
            Write-Host "$envName is not set"
            exit 1
          }
          if (-not (Test-Path "$JavaVersionPath")) {
            Write-Host "$envName path does not exist: $JavaVersionPath"
            exit 1
          }
          Write-Host "$envName=$JavaVersionPath"
        shell: pwsh
      - name: Verify Java 21 outputs are set
        env:
          JAVA_21_PATH: ${{ steps.setup-java-21.outputs.path }}
          JAVA_21_VERSION: ${{ steps.setup-java-21.outputs.version }}
        run: |
          echo "Java 21 path=$JAVA_21_PATH"
          echo "Java 21 version=$JAVA_21_VERSION"
          if [ -z "$JAVA_21_PATH" ]; then
            echo "Java 21 path output should be set"
            exit 1
          fi
          if [ -z "$JAVA_21_VERSION" ]; then
            echo "Java 21 version output should be set"
            exit 1
          fi
        shell: bash
