diff --git a/.github/scripts/create_api_user.sh b/.github/scripts/create_api_user.sh new file mode 100755 index 0000000..b9452e2 --- /dev/null +++ b/.github/scripts/create_api_user.sh @@ -0,0 +1,12 @@ +#!/bin/bash +set -ev + +echo Running command: sqlplus -S -L sys/oracle@${DB_URL} AS SYSDBA +sqlplus -S -L sys/oracle@${DB_URL} AS SYSDBA < demo_project.sh.tmp <<- EOF +sqlplus -S -L sys/oracle@${DB_URL} AS SYSDBA <<- SQL + PROMPT Connected to ${DB_URL} + PROMPT Creating Database User ${DB_USER} + PROMPT create user ${DB_USER} identified by ${DB_PASS} quota unlimited on USERS default tablespace USERS; + PROMPT grant create session, create procedure, create type, create table, create sequence, create view to ${DB_USER}; + PROMPT grant connect to ${DB_USER}; + PROMPT grant select any dictionary to ${DB_USER}; + create user ${DB_USER} identified by ${DB_PASS} quota unlimited on USERS default tablespace USERS; + grant create session, create procedure, create type, create table, create sequence, create view to ${DB_USER}; + grant connect to ${DB_USER}; + grant select any dictionary to ${DB_USER}; + exit +SQL + +cd /${PROJECT_FILE} +sqlplus -S -L ${DB_USER}/${DB_PASS}@${DB_URL} <<- SQL + whenever sqlerror exit failure rollback + whenever oserror exit failure rollback + PROMPT Connected to ${DB_URL} + PROMPT Installing sources of demo project into schema ${DB_USER} + @source/install.sql + exit +SQL + +sqlplus -S -L ${DB_USER}/${DB_PASS}@${DB_URL} <<- SQL + whenever sqlerror exit failure rollback + whenever oserror exit failure rollback + PROMPT Connected to ${DB_URL} + PROMPT Installing tests for demo project into schema ${DB_USER} + @test/install.sql +exit +SQL +EOF + +docker cp ./${PROJECT_FILE} oracle:/${PROJECT_FILE} +docker cp ./demo_project.sh.tmp oracle:/demo_project.sh +docker exec oracle bash /demo_project.sh diff --git a/.github/scripts/install_utplsql.sh b/.github/scripts/install_utplsql.sh new file mode 100755 index 0000000..8aee546 --- /dev/null +++ b/.github/scripts/install_utplsql.sh @@ -0,0 +1,33 @@ +#!/bin/bash +set -ev +cd $(dirname $(readlink -f $0)) + +# Download the specified version of utPLSQL. +if [ "$UTPLSQL_VERSION" == "develop" ] +then + git clone -b develop --single-branch https://github.com/utPLSQL/utPLSQL.git +else + curl -L -O "https://github.com/utPLSQL/utPLSQL/releases/download/$UTPLSQL_VERSION/$UTPLSQL_FILE.tar.gz" + tar -xzf ${UTPLSQL_FILE}.tar.gz && rm ${UTPLSQL_FILE}.tar.gz +fi + +chmod -R go+w ./${UTPLSQL_FILE}/{source,examples} +# Create a temporary install script. +cat > install.sh.tmp <- + --health-cmd healthcheck.sh + --health-interval 10s + --health-timeout 5s + --health-retries 10 + --name oracle + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up JDK + uses: actions/setup-java@v5 + with: + distribution: 'temurin' + java-version: '21' + + - name: Install utplsql + run: .github/scripts/install_utplsql.sh + + - name: Install demo project + run: .github/scripts/install_demo_project.sh + + - name: Build and Test + run: | + API_VERSION="${{ github.event.client_payload.api_version || inputs.api_version }}" + mvn verify appassembler:assemble -Dutplsql-java-api.version="$API_VERSION" diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..f5c64e2 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,67 @@ +name: Build and test + +on: + push: + branches: [ develop ] + + pull_request: + branches: [ develop ] + + workflow_dispatch: + + +defaults: + run: + shell: bash + +jobs: + build: + name: Test on JDK ${{ matrix.jdk }} with utPLSQL ${{ matrix.utplsql_version }} + runs-on: ubuntu-latest + env: + UTPLSQL_VERSION: ${{matrix.utplsql_version}} + UTPLSQL_FILE: ${{matrix.utplsql_file}} + DB_URL: "//localhost:1521/FREEPDB1" + DB_USER: APP + DB_PASS: pass + + strategy: + fail-fast: false + matrix: + utplsql_version: ["v3.2.01","develop"] + utplsql_file: ["utPLSQL"] + jdk: ['17', '21', '25'] + services: + oracle: + image: gvenzl/oracle-free:23-slim-faststart + env: + ORACLE_PASSWORD: oracle + DB_URL: "//localhost:1521/FREEPDB1" + ports: + - 1521:1521 + options: >- + --health-cmd healthcheck.sh + --health-interval 10s + --health-timeout 5s + --health-retries 10 + --name oracle + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up JDK + uses: actions/setup-java@v5 + with: + distribution: 'temurin' + java-version: ${{matrix.jdk}} + + - name: Install utplsql + run: .github/scripts/install_utplsql.sh + + - name: Install demo project + run: .github/scripts/install_demo_project.sh + + - name: Build and Test + run: mvn verify appassembler:assemble diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..145b920 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,97 @@ +name: Release + +on: + workflow_dispatch: + inputs: + release_version: + description: 'Release version (e.g. 3.2.3)' + required: true + next_snapshot: + description: 'Next development version (default: auto-increment patch, e.g. 3.2.4) without the "-SNAPSHOT" word' + required: false + +permissions: + contents: write + id-token: write + attestations: write + +defaults: + run: + shell: bash + +jobs: + release: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up JDK + uses: actions/setup-java@v5 + with: + distribution: 'temurin' + java-version: '21' + cache: 'maven' + + - name: Configure Git + run: | + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + + - name: Set release version + run: mvn versions:set -DnewVersion=${{ inputs.release_version }} -DgenerateBackupPoms=false + + - name: Build Release + run: mvn package appassembler:assemble assembly:single checksum:files -DskipTests + + - name: Attest build provenance + uses: actions/attest-build-provenance@v2 + with: + subject-path: | + target/utPLSQL-cli.zip + target/utPLSQL-cli.zip.sha256 + + - name: Commit and tag release version + run: | + git add pom.xml + git commit -m "Release v${{ inputs.release_version }}" + git tag -a "v${{ inputs.release_version }}" -m "Release ${{ inputs.release_version }}" + + - name: Calculate and set next development version + run: | + if [[ -n "${{ inputs.next_snapshot }}" ]]; then + NEXT="${{ inputs.next_snapshot }}-SNAPSHOT" + else + IFS='.' read -r major minor patch <<< "${{ inputs.release_version }}" + NEXT="${major}.${minor}.$((patch + 1))-SNAPSHOT" + fi + mvn -B versions:set -DnewVersion="$NEXT" -DgenerateBackupPoms=false + git add pom.xml + git commit -m "Prepare next development version $NEXT [skip ci]" + + - name: Push commits and tag + run: | + git push origin HEAD:${{ github.ref_name }} + git push origin "v${{ inputs.release_version }}" + + - name: Create GitHub Release + uses: softprops/action-gh-release@v2 + with: + tag_name: v${{ inputs.release_version }} + name: "utPLSQL-cli v${{ inputs.release_version }}" + generate_release_notes: true + fail_on_unmatched_files: true + files: | + target/utPLSQL-cli.zip + target/utPLSQL-cli.zip.sha256 + + - name: Announce release in org discussions and on utplsql.github.io + uses: utPLSQL/.github/actions/announce-release@v1 + with: + project-name: utPLSQL-cli + tag: v${{ inputs.release_version }} + discussion-token: ${{ secrets.API_TOKEN_GITHUB }} + site-token: ${{ secrets.API_TOKEN_GITHUB }} + site-category: utplsql-cli diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 76b64a0..0000000 --- a/.travis.yml +++ /dev/null @@ -1,87 +0,0 @@ -sudo: required -language: java - -services: - - docker - -jdk: - - openjdk8 - -env: - global: - - DOCKER_CFG=$HOME/.docker - - DOCKER_REPO="utplsqlv3/oracledb" - - CACHE_DIR=$HOME/.cache - - MAVEN_HOME=/usr/local/maven - - MAVEN_CFG=$HOME/.m2 - - DB_URL="127.0.0.1:1521:XE" - - DB_USER=app - - DB_PASS=app - - ORACLE_VERSION="11g-r2-xe" - - DOCKER_OPTIONS="--shm-size=1g" - - UTPLSQL_FILE="utPLSQL" - matrix: - - UTPLSQL_VERSION="v3.0.0" - UTPLSQL_FILE="utPLSQLv3.0.0" - - UTPLSQL_VERSION="v3.0.1" - - UTPLSQL_VERSION="v3.0.2" - - UTPLSQL_VERSION="v3.0.3" - - UTPLSQL_VERSION="v3.0.4" - - UTPLSQL_VERSION="v3.1.1" - - UTPLSQL_VERSION="v3.1.2" - - UTPLSQL_VERSION="v3.1.3" - - UTPLSQL_VERSION="v3.1.6" - - UTPLSQL_VERSION="v3.1.7" - - UTPLSQL_VERSION="v3.1.8" - - UTPLSQL_VERSION="develop" - UTPLSQL_FILE="utPLSQL" - -cache: - directories: - - $DOCKER_CFG - - $CACHE_DIR - - $MAVEN_CFG - -install: - - bash .travis/start_db.sh - - bash .travis/install_utplsql.sh - - bash .travis/install_demo_project.sh - -script: - - mvn package -DskipTests - - mvn package verify jar:jar appassembler:assemble - -before_deploy: - - bash .travis/create_release.sh - - if [ ! -z "$TRAVIS_TAG" ]; then VERSION=$(tr -d "/v/" <<<$TRAVIS_TAG); mvn org.codehaus.mojo:versions-maven-plugin:2.1:set -DnewVersion=${VERSION}; fi - -deploy: - - provider: releases - api_key: $GITHUB_API_TOKEN - file: - - utPLSQL-cli.zip - - utPLSQL-cli.zip.md5 - skip_cleanup: true - on: - repository: utPLSQL/utPLSQL-cli - tags: true - # Use only first job "#xxx.1" to publish artifacts - condition: "${TRAVIS_JOB_NUMBER} =~ \\.1$" - - provider: bintray - file: bintray.json - user: ${BINTRAY_USER} - key: ${BINTRAY_API_KEY} - dry-run: false - skip_cleanup: true - on: - repository: utPLSQL/utPLSQL-cli - branch: develop - # Use only first job "#xxx.1" to publish artifacts - condition: "${TRAVIS_JOB_NUMBER} =~ \\.1$" - -notifications: - slack: - rooms: - - secure: "jTPq4AcWQdWp1rB175c05ei/1lXZF1DBoqiKl9PPe7f8vqXS2QLPXMwGTEkZ1YnqL2MPZMB+50Gw5RhLKQ+t+9pN7ejLO0D8ULi1e96PDU5ZL5pNvRglH6U/lR5iT1CjELUuraDXDJ98Vu4gluLMiwTtFYQhNrOoA2V84L+8fF7rGjbGl4/zOXA9RQ4YAcOomJesb4vE7BCjhDjUuMW43xjNWg2s0WAiF+fC1HY/tsBMb1CGfpVULnO5ES5bKtUv/aGtySzH0vXilGDUvhZ/8mdaRn6uxpzcqrFdAyW8elSD28CypcYoxy6Myudw3SFiRPs0/Z02VXvm8DQtU/lcFEnaO7dMG+FpFhsWbrEjcRS6R7ve8uc7e/WuaQhtNVzSTOzRe+JFPGP9FOcYN+AcW2NJFILV9yT7+X/MPIB0OMxuwaPmVgtoyx0oec2nw05azmDr76P2e1XLnKtxa8ouwvPx8EMgApnXSR6VmLGu/w8nmtvIWjEAJ88cdGhwBagtyAsn2frzHq8aKpMziDRYvw4ivwGAeHJFsDtdRXzSbxhxmjl6+is3P0g0iAfojQ/Pzd4GugSYwqRQLLE7dUE0FAKXhKAAQAkjRZxik2+w6jgG8PuLuHdR4pm7C9srL+WAX8aFq/RQXE7BoIdqiZ5TMekw8d6eCRTjcRHsa3pPAd0=" - on_success: change - on_failure: always diff --git a/.travis/create_api_user.sh b/.travis/create_api_user.sh deleted file mode 100644 index c898f15..0000000 --- a/.travis/create_api_user.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash -set -ev - -sqlplus -S -L sys/oracle@//127.0.0.1:1521/xe AS SYSDBA < demo_project.sh.tmp < install.sh.tmp < +#### ConnectionURL This is used in all commands as first parameter (though it's optional for `info`). @@ -92,16 +98,49 @@ Accepted formats: - `/@//[:]/` - `/@::` - `/@` +- `/@` - credentials are taken from an Oracle Wallet (Secure External Password Store), see [Oracle Wallet](#oracle-wallet-secure-external-password-store) -To connect using TNS, you need to have the ORACLE_HOME environment variable set. -The file tnsnames.ora must exist in path %ORACLE_HOME%/network/admin -The file tnsnames.ora must contain valid TNS entries. +To connect using a TNS name, the file `tnsnames.ora` with a valid entry for that name must be found. +The directory holding `tnsnames.ora` (and `ojdbc.properties`, if used) is taken from the first of these that is set: + +1. `TNS_ADMIN` parameter in the connect string, e.g. `app/pass@MYDATABASE?TNS_ADMIN=/path/to/network/admin` +2. Java system property `oracle.net.tns_admin`, e.g. `export JAVA_OPTS="-Doracle.net.tns_admin=/path/to/network/admin"` +3. `TNS_ADMIN` environment variable +4. `$ORACLE_HOME/network/admin`, when the `ORACLE_HOME` environment variable is set -In case you use a username containing `/` or a password containing `@` you should encapsulate it with double quotes `"`: +Options 1-3 are handled by the Oracle JDBC driver. Option 4 is a fallback provided by utPLSQL-cli, used only when none of the others is set. + +A password may contain `@`: everything up to the last `@` is taken as the password, e.g. `utplsql run myUser/myP@ssword@connectstring`. +A username containing `/` must be enclosed in double quotes `"`, and so may the password: ``` utplsql run "my/Username"/"myP@ssword"@connectstring ``` +#### Oracle Wallet (Secure External Password Store) + +To avoid passing the password on the command line, store the credentials in an Oracle Wallet and connect with `/@`: +``` +utplsql run /@MYDATABASE +``` + +Setup example: +``` +# create an auto-login wallet with credentials for TNS alias MYDATABASE +orapki wallet create -wallet $HOME/oracle/wallet -auto_login_local +mkstore -wrl $HOME/oracle/wallet -createCredential MYDATABASE someusername + +# point the JDBC driver to the wallet +echo "oracle.net.wallet_location=(SOURCE=(METHOD=FILE)(METHOD_DATA=(DIRECTORY=$HOME/oracle/wallet)))" \ + > $HOME/oracle/network/admin/ojdbc.properties + +# tnsnames.ora with the MYDATABASE entry must be in the same directory as ojdbc.properties +export TNS_ADMIN=$HOME/oracle/network/admin +``` + +`ojdbc.properties` is read from the same directory as `tnsnames.ora`, so any of the options listed under [ConnectionURL](#connectionurl) can be used instead of `TNS_ADMIN`, for example `utplsql run "/@MYDATABASE?TNS_ADMIN=/path/to/network/admin"`. + +The TNS alias used in the connect string must match the alias of the credential stored in the wallet. + ### run `utplsql run []` @@ -116,9 +155,11 @@ utplsql run "my/Username"/"myP@ssword"@connectstring If only schema is provided, then all suites owner by that schema are executed. If -p is omitted, the current schema is used. ---tags=tags - A comma separated list of tags to run. - Format: --tags=tag1[,tag2[,tag3]] - +--tags=tags - A comma separated list of tags to include or exclude in the run. + The excluded tags must be preceeded by a `-` (minus) sign and the entire expression must be surrounded by escaped doubleqotes in command line. + Format: --tags=tag1[,tag2,...,tagN] + or + Format: --tags=\"-tag1\"[,\"-tag2\",...,tagN] -f=format - A reporter to be used for reporting. (--format) If no -f option is provided, the default ut_documentation_reporter is used. See reporters command for possible values @@ -126,7 +167,7 @@ utplsql run "my/Username"/"myP@ssword"@connectstring If defined, the output is not displayed on screen by default. This can be changed with the -s parameter. If not defined, then output will be displayed on screen, even if the parameter -s is not specified. If more than one -o parameter is specified for one -f parameter, the last one is taken into consideration. - -s - Forces putting output to to screen for a given -f parameter. + -s - Forces putting output to screen for a given -f parameter. -source_path=source - path to project source files, use the following options to enable custom type mappings: -owner="app" @@ -145,10 +186,9 @@ utplsql run "my/Username"/"myP@ssword"@connectstring -name_subexpression=subexpression_number -c - If specified, enables printing of test results in colors as defined by ANSICONSOLE standards. -(--color) Works only on reporeters that support colors (ut_documentation_reporter). +(--color) Works only on reporters that support colors (ut_documentation_reporter). --fcode=code - Override the exit code on failure, defaults to 1. You can set it to 0 to always exit with a success status. -(--failure-exit-code) +--failure-exit-code=code - Override the exit code on failure, defaults to 1. You can set it to 0 to always exit with a success status. -scc - If specified, skips the compatibility-check with the version of the database framework. (--skip- If you skip compatibility-check, CLI will expect the most actual framework version @@ -161,6 +201,18 @@ utplsql run "my/Username"/"myP@ssword"@connectstring -exclude=pckg_list - Comma-separated object list to exclude from the coverage report. Format: [schema.]package[,[schema.]package ...]. See coverage reporting options in framework documentation. + +-include-schema-expr=regex - Regular expression matching the names of schemas to include in the coverage report. +-exclude-schema-expr=regex - Regular expression matching the names of schemas to exclude from the coverage report. +-include-object-expr=regex - Regular expression matching the names of objects to include in the coverage report. +-exclude-object-expr=regex - Regular expression matching the names of objects to exclude from the coverage report. + Case-insensitive. Object expressions are matched against the object name without the schema. + Anchor them with `^` and `$`, otherwise any partial match counts. + If an include expression is given, `-include` and `--coverage-schemes` are ignored. + Schema and object exclude expressions are both applied. + If any exclude expression is given, the `-exclude` list is ignored. + Ignored when `-source_path` is used (file mapping defines the coverage scope). + See https://www.utplsql.org/utPLSQL/latest/userguide/coverage.html#filtering-objects-in-coverage-reports -q - Does not output the informational messages normally printed to console. (--quiet) Default: false @@ -177,13 +229,18 @@ utplsql run "my/Username"/"myP@ssword"@connectstring -r - Enables random order of test executions (--random-test-order) Default: false --seed - Sets the seed to use for random test execution order. If set, it sets -random to true +-seed=seed - Sets the seed to use for random test execution order. If set, it sets -r (--random-test-order) to true (--random-test-order-seed) + +--coverage-schemes - A comma separated list of schemas on which coverage should be gathered + Format: --coverage-schemes=schema1[,schema2[,schema3]] + +--ora-stuck-timeout - Sets a timeout around Reporter creation and retries when not ready after a while. 0 = no timeout. ``` Parameters -f, -o, -s are correlated. That is parameters -o and -s are controlling outputs for reporter specified by the preceding -f parameter. -Sonar and Coveralls reporter will only provide valid reports, when source_path and/or test_path are provided, and ut_run is executed from your project's root path. +Sonar reporter will only provide valid reports, when source_path and/or test_path are provided, and ut_run is executed from your project's root path. #### Examples @@ -202,6 +259,12 @@ Invokes all Unit tests from schema/package "hr_test" with two reporters: Invokes all unit test suites from schema "hr". Results are displayed to screen using default ut_documentation_reporter. +``` +> utplsql run hr/hr@xe -p=hr_test -f=ut_coverage_html_reporter -o=coverage.html -include-schema-expr=^HR$ -exclude-object-expr=_TMP$ +``` + +Runs the tests from "hr_test" and writes an HTML coverage report that covers all objects in schema "HR" except those whose name ends with "_TMP". + ### info `utplsql info []` @@ -246,11 +309,6 @@ UT_COVERAGE_SONAR_REPORTER: Designed for [SonarQube](https://about.sonarqube.com/) to report coverage. JSON format returned conforms with the Sonar specification: https://docs.sonarqube.org/display/SONAR/Generic+Test+Data -UT_COVERALLS_REPORTER: - Generates a JSON coverage report providing information on code coverage with line numbers. - Designed for [Coveralls](https://coveralls.io/). - JSON format conforms with specification: https://docs.coveralls.io/api-introduction - UT_DEBUG_REPORTER: No description available diff --git a/pom.xml b/pom.xml index 3b38f34..3d6a68b 100644 --- a/pom.xml +++ b/pom.xml @@ -1,171 +1,212 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - org.utplsql - cli - 3.1.8-SNAPSHOT - jar + org.utplsql + cli + 3.2.3-SNAPSHOT - cli - http://maven.apache.org + utPLSQL CLI + CLI for running Unit Tests with utPLSQL v3+. + https://github.com/utPLSQL/utPLSQL-cli UTF-8 - 1.8 - 1.8 - 5.5.2 - 19.3.0.0 + 17 + + 3.2.5 + + 5.12.2 + 4.7.7 + 1.5.32 + 2.2 local + + utplsql + https://sonarcloud.io - - - org.utplsql - java-api - 3.1.8 - compile - - - com.oracle.ojdbc - ucp - - - com.oracle.ojdbc - ojdbc8 - - - com.oracle.ojdbc - orai18n - - - - - javax.xml.bind - jaxb-api - 2.3.0 - - - ch.qos.logback - logback-classic - 1.2.3 - - - info.picocli - picocli - 4.0.0-beta-1b - - - com.oracle.ojdbc - ojdbc8 - ${oracle.jdbc.version} - compile - - - com.oracle.ojdbc - ucp - - - - - com.oracle.ojdbc - orai18n - ${oracle.jdbc.version} - compile - + + + Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + + GitHub Issues + https://github.com/utPLSQL/utPLSQL-java-api/issues + - - - org.junit.jupiter - junit-jupiter - ${junit.jupiter.version} - test - - - org.hamcrest - hamcrest - 2.1 - test - + + + Vinicius Avellar Moreira + utPLSQL.org + https://utplsql.org + amvinicius@gmail.com + + + Samuel Nitsche + utPLSQL.org + https://utplsql.org + derpesse@gmail.com + + + Jacek Gebal + utPLSQL.org + https://utplsql.org + jgebal@gmail.com + + + Simon Martinelli + utPLSQL.org + https://utplsql.org + simon@martineli.ch + https://martinelli.ch + + - + + + org.utplsql + utplsql-java-api + ${utplsql-java-api.version} + + + javax.xml.bind + jaxb-api + 2.3.1 + + + ch.qos.logback + logback-classic + ${logback.version} + + + info.picocli + picocli + ${picocli.version} + + + + org.junit.jupiter + junit-jupiter + ${junit.jupiter.version} + test + + + org.hamcrest + hamcrest + ${hamcrest.version} + test + + - - - - org.codehaus.mojo - appassembler-maven-plugin - 1.10 - - true - true - etc - lib - flat - - - org.utplsql.cli.Cli - utplsql - - - - - - org.apache.maven.plugins - maven-surefire-plugin - 2.22.0 - + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.13.0 + + + org.codehaus.mojo + appassembler-maven-plugin + 2.1.0 + + true + true + etc + lib + flat + + + org.utplsql.cli.Cli + utplsql + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.5.5 + + + **/*IT.java + + + + + org.apache.maven.plugins + maven-failsafe-plugin + 3.5.5 + + + + integration-test + verify + + + + + + org.apache.maven.plugins + maven-assembly-plugin + 3.8.0 + + utPLSQL-cli + false + + src/main/assembly/zip.xml + + + + + net.nicoulaj.maven.plugins + checksum-maven-plugin + 1.11 + + + + ${project.build.directory} + + utPLSQL-cli.zip + + + + + SHA-256 + + + + + + + src/main/resources + true + + **/utplsql-cli.version + + + + src/main/resources + false - **/*IT.java + **/utplsql-cli.version - - - - org.apache.maven.plugins - maven-failsafe-plugin - 2.22.0 - - - - integration-test - verify - - - - - - - - src/main/resources - true - - **/utplsql-cli.version - - - - src/main/resources - false - - **/utplsql-cli.version - - - - + + + - - - utplsql-java-api - - https://packagecloud.io/utplsql/utplsql-java-api/maven2 - - - true - - - true - - - + + + central-snapshots + https://central.sonatype.com/repository/maven-snapshots/ + true + + @@ -176,13 +217,6 @@ org.apache.maven.plugins maven-failsafe-plugin - - - ${dbUrl} - ${dbUser} - ${dbPass} - - diff --git a/src/main/assembly/zip.xml b/src/main/assembly/zip.xml new file mode 100644 index 0000000..6d16b57 --- /dev/null +++ b/src/main/assembly/zip.xml @@ -0,0 +1,19 @@ + + + zip + + false + + + zip + + + + + ${project.build.directory}/appassembler + /utPLSQL-cli + + + diff --git a/src/main/java/org/utplsql/cli/Cli.java b/src/main/java/org/utplsql/cli/Cli.java index e330818..4a980a4 100644 --- a/src/main/java/org/utplsql/cli/Cli.java +++ b/src/main/java/org/utplsql/cli/Cli.java @@ -1,11 +1,17 @@ package org.utplsql.cli; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import picocli.CommandLine; +import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; public class Cli { + private static final Logger logger = LoggerFactory.getLogger(Cli.class); + static final int DEFAULT_ERROR_CODE = 1; public static void main(String[] args) { @@ -15,8 +21,20 @@ public static void main(String[] args) { System.exit(exitCode); } + /** + * @return the arguments separated by ", ", with the credentials of the connect string masked + */ + static String maskedArgs(String... args) { + return Arrays.stream(args) + .map(ConnectionConfig::maskCredentials) + .collect(Collectors.joining(", ")); + } + static int runPicocliWithExitCode(String[] args) { + + logger.debug("Args: {}", maskedArgs(args)); + CommandLine commandLine = new CommandLine(UtplsqlPicocliCommand.class); commandLine.setTrimQuotes(true); diff --git a/src/main/java/org/utplsql/cli/ConnectionConfig.java b/src/main/java/org/utplsql/cli/ConnectionConfig.java index 05e2de7..180a268 100644 --- a/src/main/java/org/utplsql/cli/ConnectionConfig.java +++ b/src/main/java/org/utplsql/cli/ConnectionConfig.java @@ -5,21 +5,41 @@ public class ConnectionConfig { + /** + * Either {@code /@} or {@code /@}. + * An unquoted password extends to the last {@code @}, so it may contain {@code @} itself. + */ + private static final Pattern CONNECT_STRING_PATTERN = + Pattern.compile("^(?:(\".+\"|[^/]+)/(\".+\"|.+)|/)@(.*)$"); + private final String user; private final String password; private final String connect; public ConnectionConfig(String connectString) { - Matcher m = Pattern.compile("^(\".+\"|[^/]+)/(\".+\"|[^@]+)@(.*)$").matcher(connectString); + Matcher m = CONNECT_STRING_PATTERN.matcher(connectString); if (m.find()) { - user = stripEnclosingQuotes(m.group(1)); - password = stripEnclosingQuotes(m.group(2)); + user = m.group(1) == null ? null : stripEnclosingQuotes(m.group(1)); + password = m.group(2) == null ? null : stripEnclosingQuotes(m.group(2)); connect = m.group(3); } else { throw new IllegalArgumentException("Not a valid connectString: '" + connectString + "'"); } } + /** + * Masks the credentials of a connect string, e.g. for logging. + * + * @param value any string, e.g. a command line argument + * @return the value as returned by {@link #getMaskedConnectString()} for a connect string, otherwise the unchanged value + */ + public static String maskCredentials(String value) { + if (value == null || !CONNECT_STRING_PATTERN.matcher(value).matches()) { + return value; + } + return new ConnectionConfig(value).getMaskedConnectString(); + } + private String stripEnclosingQuotes(String value) { if (value.length() > 1 && value.startsWith("\"") @@ -42,10 +62,32 @@ public String getPassword() { return password; } + /** + * @return true when no user/password was given (connect string {@code /@}), + * meaning credentials are provided externally, e.g. by an Oracle Wallet + */ + public boolean isExternalAuthentication() { + return user == null; + } + public String getConnectString() { + if (isExternalAuthentication()) { + return "/@" + connect; + } return user + "/" + password + "@" + connect; } + /** + * @return the connect string with user and password replaced by asterisks, + * or {@code /@} for external authentication + */ + public String getMaskedConnectString() { + if (isExternalAuthentication()) { + return "/@" + connect; + } + return "****/****@" + connect; + } + public boolean isSysDba() { return user != null && (user.toLowerCase().endsWith(" as sysdba") diff --git a/src/main/java/org/utplsql/cli/DataSourceProvider.java b/src/main/java/org/utplsql/cli/DataSourceProvider.java index 4eee258..d3dbfcc 100644 --- a/src/main/java/org/utplsql/cli/DataSourceProvider.java +++ b/src/main/java/org/utplsql/cli/DataSourceProvider.java @@ -13,12 +13,32 @@ */ public class DataSourceProvider { + private static final String TNS_ADMIN_PROPERTY = "oracle.net.tns_admin"; + static { - String oracleHome = System.getenv("ORACLE_HOME"); - if (oracleHome != null && System.getProperty("oracle.net.tns_admin") == null) { - System.setProperty("oracle.net.tns_admin", - String.join(File.separator, oracleHome, "NETWORK", "ADMIN")); + String tnsAdminFallback = getTnsAdminFallback( + System.getProperty(TNS_ADMIN_PROPERTY), System.getenv("TNS_ADMIN"), System.getenv("ORACLE_HOME")); + if (tnsAdminFallback != null) { + System.setProperty(TNS_ADMIN_PROPERTY, tnsAdminFallback); + } + } + + /** + * The JDBC driver resolves tnsnames.ora / ojdbc.properties from the {@value TNS_ADMIN_PROPERTY} property + * or the TNS_ADMIN environment variable on its own, but it doesn't look into ORACLE_HOME. + * The property takes precedence over the environment variable, so it must not be set when TNS_ADMIN is. + * + * @return ORACLE_HOME/network/admin (lowercase, as in Oracle installations; paths are case-sensitive on Linux) when neither {@value TNS_ADMIN_PROPERTY} nor TNS_ADMIN is set, otherwise null + */ + static String getTnsAdminFallback(String tnsAdminProperty, String tnsAdminEnv, String oracleHome) { + if (isEmpty(tnsAdminProperty) && isEmpty(tnsAdminEnv) && !isEmpty(oracleHome)) { + return String.join(File.separator, oracleHome, "network", "admin"); } + return null; + } + + private static boolean isEmpty(String value) { + return value == null || value.isEmpty(); } public static DataSource getDataSource(String connectString, int maxConnections) throws SQLException { diff --git a/src/main/java/org/utplsql/cli/RunAction.java b/src/main/java/org/utplsql/cli/RunAction.java index 29d248b..7433d9e 100644 --- a/src/main/java/org/utplsql/cli/RunAction.java +++ b/src/main/java/org/utplsql/cli/RunAction.java @@ -165,7 +165,13 @@ TestRunner newTestRunner(List reporterList) { .excludeObjects(Arrays.asList(config.getExcludePackages())) .randomTestOrder(config.isRandomTestOrder()) .randomTestOrderSeed(config.getRandomTestOrderSeed()) - .addTags(Arrays.asList(config.getTags())); + .addTags(Arrays.asList(config.getTags())) + .addCoverageSchemes(Arrays.asList(config.getCoverageSchemes())) + .includeSchemaExpr(config.getIncludeSchemaExpr()) + .excludeSchemaExpr(config.getExcludeSchemaExpr()) + .includeObjectExpr(config.getIncludeObjectExpr()) + .excludeObjectExpr(config.getExcludeObjectExpr()) + .oraStuckTimeout(config.getOraStuckTimeout()); } private void outputMainInformation() { @@ -190,8 +196,8 @@ private void initDatabase(DataSource dataSource) throws SQLException { compatibilityProxy = checkFrameworkCompatibility(conn); - logger.info("Successfully connected to database. UtPLSQL core: {}", compatibilityProxy.getVersionDescription()); - logger.info("Oracle-Version: {}", new DefaultDatabaseInformation().getOracleVersion(conn)); + logger.info("Connected to Oracle database: {}", new DefaultDatabaseInformation().getOracleVersion(conn)); + logger.info("utPLSQL core: {}", compatibilityProxy.getVersionDescription()); } catch (SQLException e) { if (e.getErrorCode() == 1017 || e.getErrorCode() == 12514) { throw new DatabaseConnectionFailed(e); diff --git a/src/main/java/org/utplsql/cli/RunPicocliCommand.java b/src/main/java/org/utplsql/cli/RunPicocliCommand.java index da0d871..4285549 100644 --- a/src/main/java/org/utplsql/cli/RunPicocliCommand.java +++ b/src/main/java/org/utplsql/cli/RunPicocliCommand.java @@ -28,6 +28,11 @@ public class RunPicocliCommand implements IRunCommand { split = ",") private List tags = new ArrayList<>(); + @Option(names = {"--coverage-schemes"}, + description = "comma-separated list of schemas on which coverage should be gathered", + split = ",") + private List coverageSchemes = new ArrayList<>(); + @Option( names = {"-c", "--color"}, @@ -76,6 +81,34 @@ public class RunPicocliCommand implements IRunCommand { private String excludeObjects = null; + @Option( + names = {"-include-schema-expr"}, + description = "Regular expression to include in the coverage report schemas whose name matches the expression. " + + "See coverage reporting options in framework documentation" + ) + private String includeSchemaExpr; + + @Option( + names = {"-exclude-schema-expr"}, + description = "Regular expression to exclude from the coverage report schemas whose name matches the expression. " + + "See coverage reporting options in framework documentation" + ) + private String excludeSchemaExpr; + + @Option( + names = {"-include-object-expr"}, + description = "Regular expression to include in the coverage report objects whose name matches the expression. " + + "See coverage reporting options in framework documentation" + ) + private String includeObjectExpr; + + @Option( + names = {"-exclude-object-expr"}, + description = "Regular expression to exclude from the coverage report objects whose name matches the expression. " + + "See coverage reporting options in framework documentation" + ) + private String excludeObjectExpr; + @Option( names = {"-D", "--dbms_output"}, description = "Enables DBMS_OUTPUT for the TestRunner (default: DISABLED)" @@ -99,7 +132,7 @@ public class RunPicocliCommand implements IRunCommand { private List reporters = new ArrayList<>(); static class Format { - @Option(names = {"-f", "--format"}, required = true, description = "Enables specified format reporting") + @Option(names = {"-f", "--format"}, description = "Enables specified format reporting") String format; @Option(names = {"-o"}, description = "Outputs format to file") String outputFile; @@ -178,6 +211,9 @@ FileMapperConfig toFileMapperConfig() { @Option(names = "-h", usageHelp = true, description = "display this help and exit") boolean help; + @Option(names = "--ora-stuck-timeout", description = "Sets a timeout around Reporter creation and retries when not ready after a while. 0 = no timeout.") + Integer oraStuckTimeout = 0; + private RunAction runAction; private String[] splitOrEmpty(String value) { @@ -222,23 +258,30 @@ public RunCommandConfig getRunCommandConfig() { } } - return new RunCommandConfig( - connectionString, - suitePaths.toArray(new String[0]), - reporterConfigs.toArray(new ReporterConfig[0]), - colorConsole, - failureExitCode, - skipCompatibilityCheck, - splitOrEmpty(includeObjects), - splitOrEmpty(excludeObjects), - sourceFileMapping, - testFileMapping, - loggerConfigLevel, - timeoutInMinutes, - enableDbmsOutput, - randomTestOrder, - randomTestOrderSeed, - tags.toArray(new String[0])); + return new RunCommandConfig.Builder() + .connectString(connectionString) + .suitePaths(suitePaths.toArray(new String[0])) + .reporters(reporterConfigs.toArray(new ReporterConfig[0])) + .outputAnsiColor(colorConsole) + .failureExitCode(failureExitCode) + .skipCompatibilityCheck(skipCompatibilityCheck) + .includePackages(splitOrEmpty(includeObjects)) + .excludePackages(splitOrEmpty(excludeObjects)) + .sourceMapping(sourceFileMapping) + .testMapping(testFileMapping) + .logConfigLevel(loggerConfigLevel) + .timeoutInMinutes(timeoutInMinutes) + .dbmsOutput(enableDbmsOutput) + .randomTestOrder(randomTestOrder) + .randomTestOrderSeed(randomTestOrderSeed) + .tags(tags.toArray(new String[0])) + .coverageSchemes(coverageSchemes.toArray(new String[0])) + .oraStuckTimeout(oraStuckTimeout) + .includeSchemaExpr(includeSchemaExpr) + .excludeSchemaExpr(excludeSchemaExpr) + .includeObjectExpr(includeObjectExpr) + .excludeObjectExpr(excludeObjectExpr) + .create(); } private RunAction getRunAction() { diff --git a/src/main/java/org/utplsql/cli/config/RunCommandConfig.java b/src/main/java/org/utplsql/cli/config/RunCommandConfig.java index 4418a45..1bc1632 100644 --- a/src/main/java/org/utplsql/cli/config/RunCommandConfig.java +++ b/src/main/java/org/utplsql/cli/config/RunCommandConfig.java @@ -22,9 +22,15 @@ public class RunCommandConfig extends ConnectionConfig { private boolean randomTestOrder = false; private final Integer randomTestOrderSeed; private final String[] tags; + private final String[] coverageSchemes; + private final Integer oraStuckTimeout; + private final String includeSchemaExpr; + private final String excludeSchemaExpr; + private final String includeObjectExpr; + private final String excludeObjectExpr; - @ConstructorProperties({"connectString", "suitePaths", "reporters", "outputAnsiColor", "failureExitCode", "skipCompatibilityCheck", "includePackages", "excludePackages", "sourceMapping", "testMapping", "logConfigLevel", "timeoutInMinutes", "dbmsOutput", "randomTestOrder", "randomTestOrderSeed", "tags"}) - public RunCommandConfig(String connectString, String[] suitePaths, ReporterConfig[] reporters, boolean outputAnsiColor, Integer failureExitCode, boolean skipCompatibilityCheck, String[] includePackages, String[] excludePackages, FileMapperConfig sourceMapping, FileMapperConfig testMapping, ConfigLevel logConfigLevel, Integer timeoutInMinutes, boolean dbmsOutput, boolean randomTestOrder, Integer randomTestOrderSeed, String[] tags) { + @ConstructorProperties({"connectString", "suitePaths", "reporters", "outputAnsiColor", "failureExitCode", "skipCompatibilityCheck", "includePackages", "excludePackages", "sourceMapping", "testMapping", "logConfigLevel", "timeoutInMinutes", "dbmsOutput", "randomTestOrder", "randomTestOrderSeed", "tags", "coverageSchemes", "oraStuckTimeout", "includeSchemaExpr", "excludeSchemaExpr", "includeObjectExpr", "excludeObjectExpr"}) + public RunCommandConfig(String connectString, String[] suitePaths, ReporterConfig[] reporters, boolean outputAnsiColor, Integer failureExitCode, boolean skipCompatibilityCheck, String[] includePackages, String[] excludePackages, FileMapperConfig sourceMapping, FileMapperConfig testMapping, ConfigLevel logConfigLevel, Integer timeoutInMinutes, boolean dbmsOutput, boolean randomTestOrder, Integer randomTestOrderSeed, String[] tags, String[] coverageSchemes, Integer oraStuckTimeout, String includeSchemaExpr, String excludeSchemaExpr, String includeObjectExpr, String excludeObjectExpr) { super(connectString); this.suitePaths = suitePaths; this.reporters = reporters; @@ -41,6 +47,12 @@ public RunCommandConfig(String connectString, String[] suitePaths, ReporterConfi this.randomTestOrder = randomTestOrder; this.randomTestOrderSeed = randomTestOrderSeed; this.tags = tags; + this.coverageSchemes = coverageSchemes; + this.oraStuckTimeout = oraStuckTimeout; + this.includeSchemaExpr = includeSchemaExpr; + this.excludeSchemaExpr = excludeSchemaExpr; + this.includeObjectExpr = includeObjectExpr; + this.excludeObjectExpr = excludeObjectExpr; } public String[] getSuitePaths() { @@ -102,4 +114,158 @@ public boolean isRandomTestOrder() { public Integer getRandomTestOrderSeed() { return randomTestOrderSeed; } + + public String[] getCoverageSchemes() { + return coverageSchemes; + } + + public Integer getOraStuckTimeout() { return oraStuckTimeout; } + + public String getIncludeSchemaExpr() { return includeSchemaExpr; } + + public String getExcludeSchemaExpr() { return excludeSchemaExpr; } + + public String getIncludeObjectExpr() { return includeObjectExpr; } + + public String getExcludeObjectExpr() { return excludeObjectExpr; } + + public static class Builder { + + private String connectString; + private String[] suitePaths = new String[0]; + private ReporterConfig[] reporters; + private boolean outputAnsiColor; + private Integer failureExitCode; + private boolean skipCompatibilityCheck; + private String[] includePackages = new String[0]; + private String[] excludePackages = new String[0]; + private FileMapperConfig sourceMapping; + private FileMapperConfig testMapping; + private ConfigLevel logConfigLevel; + private Integer timeoutInMinutes; + private boolean dbmsOutput; + private boolean randomTestOrder; + private Integer randomTestOrderSeed; + private String[] tags = new String[0]; + private String[] coverageSchemes = new String[0]; + private Integer oraStuckTimeout; + private String includeSchemaExpr; + private String excludeSchemaExpr; + private String includeObjectExpr; + private String excludeObjectExpr; + + public Builder connectString(String connectString) { + this.connectString = connectString; + return this; + } + + public Builder suitePaths(String[] suitePaths) { + this.suitePaths = suitePaths; + return this; + } + + public Builder reporters(ReporterConfig[] reporters) { + this.reporters = reporters; + return this; + } + + public Builder outputAnsiColor(boolean outputAnsiColor) { + this.outputAnsiColor = outputAnsiColor; + return this; + } + + public Builder failureExitCode(Integer failureExitCode) { + this.failureExitCode = failureExitCode; + return this; + } + + public Builder skipCompatibilityCheck(boolean skipCompatibilityCheck) { + this.skipCompatibilityCheck = skipCompatibilityCheck; + return this; + } + + public Builder includePackages(String[] includePackages) { + this.includePackages = includePackages; + return this; + } + + public Builder excludePackages(String[] excludePackages) { + this.excludePackages = excludePackages; + return this; + } + + public Builder sourceMapping(FileMapperConfig sourceMapping) { + this.sourceMapping = sourceMapping; + return this; + } + + public Builder testMapping(FileMapperConfig testMapping) { + this.testMapping = testMapping; + return this; + } + + public Builder logConfigLevel(ConfigLevel logConfigLevel) { + this.logConfigLevel = logConfigLevel; + return this; + } + + public Builder timeoutInMinutes(Integer timeoutInMinutes) { + this.timeoutInMinutes = timeoutInMinutes; + return this; + } + + public Builder dbmsOutput(boolean dbmsOutput) { + this.dbmsOutput = dbmsOutput; + return this; + } + + public Builder randomTestOrder(boolean randomTestOrder) { + this.randomTestOrder = randomTestOrder; + return this; + } + + public Builder randomTestOrderSeed(Integer randomTestOrderSeed) { + this.randomTestOrderSeed = randomTestOrderSeed; + return this; + } + + public Builder tags(String[] tags) { + this.tags = tags; + return this; + } + + public Builder coverageSchemes(String[] coverageSchemes) { + this.coverageSchemes = coverageSchemes; + return this; + } + + public Builder oraStuckTimeout(Integer oraStuckTimeout) { + this.oraStuckTimeout = oraStuckTimeout; + return this; + } + + public Builder includeSchemaExpr(String includeSchemaExpr) { + this.includeSchemaExpr = includeSchemaExpr; + return this; + } + + public Builder excludeSchemaExpr(String excludeSchemaExpr) { + this.excludeSchemaExpr = excludeSchemaExpr; + return this; + } + + public Builder includeObjectExpr(String includeObjectExpr) { + this.includeObjectExpr = includeObjectExpr; + return this; + } + + public Builder excludeObjectExpr(String excludeObjectExpr) { + this.excludeObjectExpr = excludeObjectExpr; + return this; + } + + public RunCommandConfig create() { + return new RunCommandConfig(connectString, suitePaths, reporters, outputAnsiColor, failureExitCode, skipCompatibilityCheck, includePackages, excludePackages, sourceMapping, testMapping, logConfigLevel, timeoutInMinutes, dbmsOutput, randomTestOrder, randomTestOrderSeed, tags, coverageSchemes, oraStuckTimeout, includeSchemaExpr, excludeSchemaExpr, includeObjectExpr, excludeObjectExpr); + } + } } diff --git a/src/main/java/org/utplsql/cli/datasource/InitializableOracleDataSource.java b/src/main/java/org/utplsql/cli/datasource/InitializableOracleDataSource.java index db32b1a..0aa44fa 100644 --- a/src/main/java/org/utplsql/cli/datasource/InitializableOracleDataSource.java +++ b/src/main/java/org/utplsql/cli/datasource/InitializableOracleDataSource.java @@ -2,31 +2,102 @@ import oracle.jdbc.pool.OracleDataSource; +import javax.sql.DataSource; +import java.io.PrintWriter; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; +import java.util.logging.Logger; -public class InitializableOracleDataSource extends OracleDataSource { +public class InitializableOracleDataSource implements DataSource { + private final OracleDataSource delegate; private String initSql; public InitializableOracleDataSource() throws SQLException { + this.delegate = new OracleDataSource(); } + // --- URL / credentials passthrough (mirrors what callers used before) --- + + public void setURL(String url) throws SQLException { + delegate.setURL(url); + } + + public void setUser(String user) { + delegate.setUser(user); + } + + public void setPassword(String password) { + delegate.setPassword(password); + } + + // --- Core DataSource methods --- + @Override public Connection getConnection() throws SQLException { - Connection con = super.getConnection(); - - if ( initSql != null && !initSql.isEmpty() ) { - try (CallableStatement stmt = con.prepareCall(initSql)) { - stmt.execute(); - } - } + Connection con = delegate.getConnection(); + runInitSql(con); + return con; + } + @Override + public Connection getConnection(String username, String password) throws SQLException { + Connection con = delegate.getConnection(username, password); + runInitSql(con); return con; } - public void setConnectionInitSql( String sql ) { + public void setConnectionInitSql(String sql) { this.initSql = sql; } -} + + // --- DataSource boilerplate --- + + @Override + public PrintWriter getLogWriter() throws SQLException { + return delegate.getLogWriter(); + } + + @Override + public void setLogWriter(PrintWriter out) throws SQLException { + delegate.setLogWriter(out); + } + + @Override + public void setLoginTimeout(int seconds) throws SQLException { + delegate.setLoginTimeout(seconds); + } + + @Override + public int getLoginTimeout() throws SQLException { + return delegate.getLoginTimeout(); + } + + @Override + public Logger getParentLogger() throws SQLFeatureNotSupportedException { + return delegate.getParentLogger(); + } + + @Override + public T unwrap(Class iface) throws SQLException { + if (iface.isInstance(delegate)) return iface.cast(delegate); + return delegate.unwrap(iface); + } + + @Override + public boolean isWrapperFor(Class iface) throws SQLException { + return iface.isInstance(delegate) || delegate.isWrapperFor(iface); + } + + // --- Private helpers --- + + private void runInitSql(Connection con) throws SQLException { + if (initSql != null && !initSql.isEmpty()) { + try (CallableStatement stmt = con.prepareCall(initSql)) { + stmt.execute(); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/org/utplsql/cli/datasource/TestedDataSourceProvider.java b/src/main/java/org/utplsql/cli/datasource/TestedDataSourceProvider.java index 8ecfeca..7034144 100644 --- a/src/main/java/org/utplsql/cli/datasource/TestedDataSourceProvider.java +++ b/src/main/java/org/utplsql/cli/datasource/TestedDataSourceProvider.java @@ -1,6 +1,5 @@ package org.utplsql.cli.datasource; -import oracle.jdbc.pool.OracleDataSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.utplsql.api.EnvironmentVariableUtil; @@ -17,23 +16,18 @@ public class TestedDataSourceProvider { - interface ConnectStringPossibility { - String getConnectString(ConnectionConfig config); - - String getMaskedConnectString(ConnectionConfig config); - } - private static final Logger logger = LoggerFactory.getLogger(TestedDataSourceProvider.class); + /** + * JDBC URL prefixes tried in this order: thick (OCI) driver first, then thin driver + */ + private static final List JDBC_URL_PREFIXES = List.of("jdbc:oracle:oci8:", "jdbc:oracle:thin:"); + private final ConnectionConfig config; - private final List possibilities = new ArrayList<>(); private final int maxConnections; public TestedDataSourceProvider(ConnectionConfig config, int maxConnections) { this.config = config; this.maxConnections = maxConnections; - - possibilities.add(new ThickConnectStringPossibility()); - possibilities.add(new ThinConnectStringPossibility()); } public DataSource getDataSource() throws SQLException { @@ -46,20 +40,25 @@ public DataSource getDataSource() throws SQLException { return ds; } - private void setThickOrThinJdbcUrl(OracleDataSource ds) throws SQLException { + private void setThickOrThinJdbcUrl(InitializableOracleDataSource ds) throws SQLException { List errors = new ArrayList<>(); Throwable lastException = null; - ds.setUser(config.getUser()); - ds.setPassword(config.getPassword()); + // With external authentication (Oracle Wallet) the driver looks up the credentials itself + if (!config.isExternalAuthentication()) { + ds.setUser(config.getUser()); + ds.setPassword(config.getPassword()); + } - for (ConnectStringPossibility possibility : possibilities) { - ds.setURL(possibility.getConnectString(config)); + for (String jdbcUrlPrefix : JDBC_URL_PREFIXES) { + String maskedUrl = jdbcUrlPrefix + config.getMaskedConnectString(); + logger.debug("Try connecting {}", maskedUrl); + ds.setURL(jdbcUrlPrefix + "@" + config.getConnect()); try (Connection ignored = ds.getConnection()) { - logger.info("Use connection string {}", possibility.getMaskedConnectString(config)); + logger.info("Use connection string {}", maskedUrl); return; - } catch (UnsatisfiedLinkError | Exception e) { - errors.add(possibility.getMaskedConnectString(config) + ": " + e.getMessage()); + } catch (Error | Exception e) { + errors.add(maskedUrl + ": " + e.getMessage()); lastException = e; } } @@ -84,7 +83,7 @@ private void setInitSqlFrom_NLS_LANG(InitializableOracleDataSource ds) { sqlCommands.add(String.format("ALTER SESSION SET NLS_TERRITORY='%s'", matcher.group(2))); } - if (sqlCommands.size() > 0) { + if (!sqlCommands.isEmpty()) { StringBuilder sb = new StringBuilder(); sb.append("BEGIN\n"); for (String command : sqlCommands) { @@ -92,34 +91,10 @@ private void setInitSqlFrom_NLS_LANG(InitializableOracleDataSource ds) { } sb.append("END;"); - logger.debug("NLS settings: {}", sb.toString()); + logger.debug("NLS settings: {}", sb); ds.setConnectionInitSql(sb.toString()); } } } } - - private static class ThickConnectStringPossibility implements ConnectStringPossibility { - @Override - public String getConnectString(ConnectionConfig config) { - return "jdbc:oracle:oci8:@" + config.getConnect(); - } - - @Override - public String getMaskedConnectString(ConnectionConfig config) { - return "jdbc:oracle:oci8:****/****@" + config.getConnect(); - } - } - - private static class ThinConnectStringPossibility implements ConnectStringPossibility { - @Override - public String getConnectString(ConnectionConfig config) { - return "jdbc:oracle:thin:@" + config.getConnect(); - } - - @Override - public String getMaskedConnectString(ConnectionConfig config) { - return "jdbc:oracle:thin:****/****@" + config.getConnect(); - } - } } diff --git a/src/main/java/org/utplsql/cli/reporters/LocalAssetsCoverageHTMLReporter.java b/src/main/java/org/utplsql/cli/reporters/LocalAssetsCoverageHTMLReporter.java index 602ee7c..20483c1 100644 --- a/src/main/java/org/utplsql/cli/reporters/LocalAssetsCoverageHTMLReporter.java +++ b/src/main/java/org/utplsql/cli/reporters/LocalAssetsCoverageHTMLReporter.java @@ -29,13 +29,17 @@ public LocalAssetsCoverageHTMLReporter(String selfType, Object[] attributes) { public Reporter init(Connection con, CompatibilityProxy compatibilityProxy, ReporterFactory reporterFactory) throws SQLException { super.init(con, compatibilityProxy, reporterFactory); - if (hasOutputToFile()) { + if (hasOutputToFile() && assetsAreBundled()) { writeReportAssetsTo(getPhysicalAssetPath()); } return this; } + private static boolean assetsAreBundled() { + return CoverageHTMLReporter.class.getResource("/CoverageHTMLReporter") != null; + } + private String getNameOfOutputFile() { Path outputPath = Paths.get(options.getOutputFileName()); return outputPath.getName(outputPath.getNameCount() - 1).toString(); @@ -51,7 +55,7 @@ private Path getPhysicalAssetPath() { } private void setAssetsPathFromOptions() { - if (hasOutputToFile()) { + if (hasOutputToFile() && assetsAreBundled()) { setAssetsPath(getNameOfOutputFile() + "_assets/"); } } diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml new file mode 100644 index 0000000..488718d --- /dev/null +++ b/src/main/resources/logback.xml @@ -0,0 +1,17 @@ + + + + + %msg%n + + + + + + + + + diff --git a/src/test/java/org/utplsql/cli/CliArgsLoggingTest.java b/src/test/java/org/utplsql/cli/CliArgsLoggingTest.java new file mode 100644 index 0000000..8ccab49 --- /dev/null +++ b/src/test/java/org/utplsql/cli/CliArgsLoggingTest.java @@ -0,0 +1,60 @@ +package org.utplsql.cli; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * The command line arguments are logged at debug level; the credentials of the connect string must not be (issue #172). + */ +class CliArgsLoggingTest { + + private final Logger cliLogger = (Logger) LoggerFactory.getLogger(Cli.class); + private final ListAppender appender = new ListAppender<>(); + private Level originalLevel; + + @BeforeEach + void captureCliLog() { + originalLevel = cliLogger.getLevel(); + cliLogger.setLevel(Level.DEBUG); + appender.start(); + cliLogger.addAppender(appender); + } + + @AfterEach + void restoreCliLog() { + cliLogger.detachAppender(appender); + cliLogger.setLevel(originalLevel); + } + + @Test + void maskedArgs() { + assertEquals("run, ****/****@//localhost:1521/FREEPDB1, --debug", + Cli.maskedArgs("run", "app/Sup3rSecretPw@//localhost:1521/FREEPDB1", "--debug")); + } + + @Test + void passwordIsNotLogged() { + // "run -h" only prints the usage, so no database is needed + Cli.runPicocliWithExitCode(new String[]{"run", "app/Sup3rSecretPw@//localhost:1521/FREEPDB1", "-h"}); + + List messages = appender.list.stream() + .map(ILoggingEvent::getFormattedMessage) + .toList(); + + assertTrue(messages.contains("Args: run, ****/****@//localhost:1521/FREEPDB1, -h"), () -> "Logged: " + messages); + assertFalse(messages.stream().anyMatch(message -> message.contains("Sup3rSecretPw")), () -> "Logged: " + messages); + } +} diff --git a/src/test/java/org/utplsql/cli/CliVersionInfoTest.java b/src/test/java/org/utplsql/cli/CliVersionInfoTest.java index 28db14c..4a89f27 100644 --- a/src/test/java/org/utplsql/cli/CliVersionInfoTest.java +++ b/src/test/java/org/utplsql/cli/CliVersionInfoTest.java @@ -8,6 +8,6 @@ class CliVersionInfoTest { @Test void getCliVersionInfo() { - assertTrue(CliVersionInfo.getVersion().startsWith("3.1")); + assertTrue(CliVersionInfo.getVersion().startsWith("3.2")); } } diff --git a/src/test/java/org/utplsql/cli/ConnectionConfigTest.java b/src/test/java/org/utplsql/cli/ConnectionConfigTest.java index ee1e0b9..79c3553 100644 --- a/src/test/java/org/utplsql/cli/ConnectionConfigTest.java +++ b/src/test/java/org/utplsql/cli/ConnectionConfigTest.java @@ -1,6 +1,9 @@ package org.utplsql.cli; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; import static org.junit.jupiter.api.Assertions.*; @@ -54,4 +57,95 @@ void parseSpecialCharsUser() { assertEquals("my.local.host/service", info.getConnect()); assertFalse(info.isSysDba()); } + + @Test + void parseCredentialsIsNotExternalAuthentication() { + ConnectionConfig info = new ConnectionConfig("test/pw@MY_TNS_ALIAS"); + + assertFalse(info.isExternalAuthentication()); + assertEquals("test/pw@MY_TNS_ALIAS", info.getConnectString()); + } + + @Test + void parseExternalAuthentication() { + ConnectionConfig info = new ConnectionConfig("/@MY_TNS_ALIAS"); + + assertNull(info.getUser()); + assertNull(info.getPassword()); + assertEquals("MY_TNS_ALIAS", info.getConnect()); + assertTrue(info.isExternalAuthentication()); + assertFalse(info.isSysDba()); + assertEquals("/@MY_TNS_ALIAS", info.getConnectString()); + } + + @Test + void parseExternalAuthenticationWithTnsAdminInUrl() { + ConnectionConfig info = new ConnectionConfig("/@MY_TNS_ALIAS?TNS_ADMIN=/home/me/oracle/network/admin"); + + assertNull(info.getUser()); + assertNull(info.getPassword()); + assertEquals("MY_TNS_ALIAS?TNS_ADMIN=/home/me/oracle/network/admin", info.getConnect()); + assertTrue(info.isExternalAuthentication()); + } + + @Test + void parseExternalAuthenticationWithEzConnect() { + ConnectionConfig info = new ConnectionConfig("/@//my.local.host:1521/service"); + + assertEquals("//my.local.host:1521/service", info.getConnect()); + assertTrue(info.isExternalAuthentication()); + } + + @ParameterizedTest + @ValueSource(strings = { + "/pw@MY_TNS_ALIAS", // password without user + "test/@MY_TNS_ALIAS", // user without password + "@MY_TNS_ALIAS", + "test@MY_TNS_ALIAS", + "MY_TNS_ALIAS" + }) + void rejectInvalidConnectString(String connectString) { + assertThrows(IllegalArgumentException.class, () -> new ConnectionConfig(connectString)); + } + + @Test + void parseUnquotedPasswordWithAt() { + ConnectionConfig info = new ConnectionConfig("test/p@ss@w0rd@MY_TNS_ALIAS"); + + assertEquals("test", info.getUser()); + assertEquals("p@ss@w0rd", info.getPassword()); + assertEquals("MY_TNS_ALIAS", info.getConnect()); + } + + @ParameterizedTest + @CsvSource(delimiter = '|', value = { + "test/pw@my.local.host/service | ****/****@my.local.host/service", + "test/pw@//my.local.host:1521/service | ****/****@//my.local.host:1521/service", + "sys as sysdba/pw@MY_TNS_ALIAS | ****/****@MY_TNS_ALIAS", + "test/\"p@ssw0rd=\"@MY_TNS_ALIAS | ****/****@MY_TNS_ALIAS", + "\"User/Mine@=\"/pw@MY_TNS_ALIAS | ****/****@MY_TNS_ALIAS", + "test/p@ss@MY_TNS_ALIAS | ****/****@MY_TNS_ALIAS", + "/@MY_TNS_ALIAS | /@MY_TNS_ALIAS" + }) + void maskCredentials(String connectString, String expected) { + assertEquals(expected, ConnectionConfig.maskCredentials(connectString)); + assertEquals(expected, new ConnectionConfig(connectString).getMaskedConnectString()); + } + + @ParameterizedTest + @ValueSource(strings = { + "run", + "--debug", + "-p=app.test_pkg", + "-f=ut_documentation_reporter", + "MY_TNS_ALIAS" + }) + void maskCredentialsLeavesOtherValuesUnchanged(String value) { + assertEquals(value, ConnectionConfig.maskCredentials(value)); + } + + @Test + void maskCredentialsOfNull() { + assertNull(ConnectionConfig.maskCredentials(null)); + } } diff --git a/src/test/java/org/utplsql/cli/DataSourceProviderTest.java b/src/test/java/org/utplsql/cli/DataSourceProviderTest.java new file mode 100644 index 0000000..8e274f6 --- /dev/null +++ b/src/test/java/org/utplsql/cli/DataSourceProviderTest.java @@ -0,0 +1,36 @@ +package org.utplsql.cli; + +import org.junit.jupiter.api.Test; + +import java.io.File; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +class DataSourceProviderTest { + + private static final String ORACLE_HOME = "/opt/oracle"; + + @Test + void tnsAdminFallsBackToOracleHome() { + assertEquals(String.join(File.separator, ORACLE_HOME, "network", "admin"), + DataSourceProvider.getTnsAdminFallback(null, null, ORACLE_HOME)); + } + + @Test + void tnsAdminEnvVariableIsLeftToJdbcDriver() { + // Setting the property would override the TNS_ADMIN environment variable in the JDBC driver + assertNull(DataSourceProvider.getTnsAdminFallback(null, "/my/tns_admin", ORACLE_HOME)); + } + + @Test + void tnsAdminPropertyIsNotOverridden() { + assertNull(DataSourceProvider.getTnsAdminFallback("/my/tns_admin", null, ORACLE_HOME)); + } + + @Test + void noFallbackWithoutOracleHome() { + assertNull(DataSourceProvider.getTnsAdminFallback(null, null, null)); + assertNull(DataSourceProvider.getTnsAdminFallback("", "", "")); + } +} diff --git a/src/test/java/org/utplsql/cli/PicocliRunCommandTest.java b/src/test/java/org/utplsql/cli/PicocliRunCommandTest.java index 809912d..b81e46e 100644 --- a/src/test/java/org/utplsql/cli/PicocliRunCommandTest.java +++ b/src/test/java/org/utplsql/cli/PicocliRunCommandTest.java @@ -48,11 +48,20 @@ void runCommandAllArguments() throws Exception { "-type_mapping=\"tsql=PACKAGE BODY\"", "-owner_subexpression=1", "-type_subexpression=2", - "-name_subexpression=3"); + "-name_subexpression=3", + "--coverage-schemes=schema1,other_schema", + "-include-schema-expr=^APP$", + "-exclude-schema-expr=^APP_TEST$", + "-include-object-expr=^PKG_", + "-exclude-object-expr=_TMP$"); assertNotNull(config.getConnectString()); assertThat( config.getSuitePaths(), is(new String[]{"app.betwnstr", "app.basic"})); assertThat( config.getTags(), is(new String[]{"tag1", "tag.2"})); + assertEquals( "^APP$", config.getIncludeSchemaExpr() ); + assertEquals( "^APP_TEST$", config.getExcludeSchemaExpr() ); + assertEquals( "^PKG_", config.getIncludeObjectExpr() ); + assertEquals( "_TMP$", config.getExcludeObjectExpr() ); assertTrue( config.isOutputAnsiColor() ); assertEquals( LoggerConfiguration.ConfigLevel.NONE, config.getLogConfigLevel()); assertEquals( 10, config.getFailureExitCode()); @@ -65,6 +74,7 @@ void runCommandAllArguments() throws Exception { assertEquals( 123, config.getRandomTestOrderSeed() ); assertNotNull( config.getReporters() ); assertEquals( 1, config.getReporters().length ); + assertThat( config.getCoverageSchemes(), is(new String[]{"schema1", "other_schema"}) ); // Source FileMapping assertNotNull(config.getSourceMapping()); @@ -238,4 +248,13 @@ void testFileMappingWithoutDetails() throws Exception { assertNull( testMapperConfig.getTypeSubexpression()); assertNull( testMapperConfig.getNameSubexpression()); } + + @Test + void negatedTag() throws Exception { + RunCommandConfig config = parseForConfig("run", + TestHelper.getConnectionString(), + "--tags=\"-dontwantit\""); + + assertThat(config.getTags(), hasItemInArray("-dontwantit") ); + } } diff --git a/src/test/java/org/utplsql/cli/RunCommandArgumentsTest.java b/src/test/java/org/utplsql/cli/RunCommandArgumentsTest.java index a2f0a9a..77c455c 100644 --- a/src/test/java/org/utplsql/cli/RunCommandArgumentsTest.java +++ b/src/test/java/org/utplsql/cli/RunCommandArgumentsTest.java @@ -1,5 +1,6 @@ package org.utplsql.cli; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.utplsql.api.TestRunner; @@ -7,10 +8,12 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.equalTo; public class RunCommandArgumentsTest { @Test + @DisplayName("All arguments are recognized") public void allArgumentsAreRecognized() { IRunCommand runCmd = TestHelper.createRunCommand(TestHelper.getConnectionString(), "-p=app", @@ -18,6 +21,7 @@ public void allArgumentsAreRecognized() { "-o=sonar_result.xml", "-s", "--tags=tag1,tag2", + "--coverage-schemes=schema1,some_other_schema", "-d", "-c", "--failure-exit-code=10", @@ -38,7 +42,8 @@ public void allArgumentsAreRecognized() { "-type_mapping=\"sql=PACKAGE BODY\"", "-owner_subexpression=0", "-type_subexpression=0", - "-name_subexpression=0" + "-name_subexpression=0", + "--ora-stuck-timeout=2" ); TestRunner testRunner = runCmd.newTestRunner(new ArrayList<>()); @@ -64,4 +69,30 @@ void provideTags() { TestRunner testRunner = runCmd.newTestRunner(new ArrayList<>()); assertThat( testRunner.getOptions().tags, contains("tag1", "tag.2") ); } + + @Test + void provideExpressionFilters() { + IRunCommand runCmd = TestHelper.createRunCommand(TestHelper.getConnectionString(), + "-include-schema-expr=^APP$", + "-exclude-schema-expr=^APP_TEST$", + "-include-object-expr=^PKG_", + "-exclude-object-expr=_TMP$" + ); + + TestRunner testRunner = runCmd.newTestRunner(new ArrayList<>()); + assertThat( testRunner.getOptions().includeSchemaExpr, equalTo("^APP$") ); + assertThat( testRunner.getOptions().excludeSchemaExpr, equalTo("^APP_TEST$") ); + assertThat( testRunner.getOptions().includeObjectExpr, equalTo("^PKG_") ); + assertThat( testRunner.getOptions().excludeObjectExpr, equalTo("_TMP$") ); + } + + @Test + void provideCoverageSchemes() { + IRunCommand runCmd = TestHelper.createRunCommand(TestHelper.getConnectionString(), + "--coverage-schemes=schema-1,some_other_schema" + ); + + TestRunner testRunner = runCmd.newTestRunner(new ArrayList<>()); + assertThat( testRunner.getOptions().coverageSchemes, contains("schema-1", "some_other_schema") ); + } } diff --git a/src/test/java/org/utplsql/cli/RunCommandConfigParamsArePassedToTestRunnerTest.java b/src/test/java/org/utplsql/cli/RunCommandConfigParamsArePassedToTestRunnerTest.java new file mode 100644 index 0000000..d3b5194 --- /dev/null +++ b/src/test/java/org/utplsql/cli/RunCommandConfigParamsArePassedToTestRunnerTest.java @@ -0,0 +1,57 @@ +package org.utplsql.cli; + +import org.junit.jupiter.api.Test; +import org.utplsql.api.TestRunner; +import org.utplsql.cli.config.RunCommandConfig; + +import java.util.ArrayList; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class RunCommandConfigParamsArePassedToTestRunnerTest { + + @Test + void tags() { + RunCommandConfig config = new RunCommandConfig.Builder() + .tags(new String[]{"tag1", "tag2"}) + .create(); + TestRunner testRunner = new RunAction(config).newTestRunner(new ArrayList<>()); + assertThat( testRunner.getOptions().tags, contains("tag1", "tag2") ); + } + + @Test + void coverageSchemes() { + RunCommandConfig config = new RunCommandConfig.Builder() + .coverageSchemes(new String[]{"schema1", "another_schema", "and-another-one"}) + .create(); + TestRunner testRunner = new RunAction(config).newTestRunner(new ArrayList<>()); + assertThat( testRunner.getOptions().coverageSchemes, contains("schema1", "another_schema", "and-another-one") ); + } + + @Test + void oraStuckTimeout() { + RunCommandConfig config = new RunCommandConfig.Builder() + .oraStuckTimeout(2) + .create(); + TestRunner testRunner = new RunAction(config).newTestRunner(new ArrayList<>()); + assertThat( testRunner.getOptions().oraStuckTimeout, equalTo(2) ); + } + + @Test + void expressionFilters() { + RunCommandConfig config = new RunCommandConfig.Builder() + .includeSchemaExpr("^APP$") + .excludeSchemaExpr("^APP_TEST$") + .includeObjectExpr("^PKG_") + .excludeObjectExpr("_TMP$") + .create(); + TestRunner testRunner = new RunAction(config).newTestRunner(new ArrayList<>()); + assertThat( testRunner.getOptions().includeSchemaExpr, equalTo("^APP$") ); + assertThat( testRunner.getOptions().excludeSchemaExpr, equalTo("^APP_TEST$") ); + assertThat( testRunner.getOptions().includeObjectExpr, equalTo("^PKG_") ); + assertThat( testRunner.getOptions().excludeObjectExpr, equalTo("_TMP$") ); + } +} diff --git a/src/test/java/org/utplsql/cli/RunCommandCoverageReporterIT.java b/src/test/java/org/utplsql/cli/RunCommandCoverageReporterIT.java index bf67f7f..e95dce2 100644 --- a/src/test/java/org/utplsql/cli/RunCommandCoverageReporterIT.java +++ b/src/test/java/org/utplsql/cli/RunCommandCoverageReporterIT.java @@ -19,7 +19,7 @@ */ class RunCommandCoverageReporterIT extends AbstractFileOutputTest { - private static final Pattern REGEX_COVERAGE_TITLE = Pattern.compile("([a-zA-Z0-9\\._]+)<\\/a>"); + private static final Pattern REGEX_COVERAGE_TITLE = Pattern.compile("([a-zA-Z ]+ )?([a-zA-Z0-9\\._]+)<\\/a>"); private String getTempCoverageFileName(int counter) { @@ -60,7 +60,7 @@ private boolean hasCoverageListed(String content, String packageName) { Matcher m = REGEX_COVERAGE_TITLE.matcher(content); while (m.find()) { - if (packageName.equals(m.group(1))) + if (packageName.equals(m.group(2))) return true; } diff --git a/src/test/java/org/utplsql/cli/RunCommandIT.java b/src/test/java/org/utplsql/cli/RunCommandIT.java index e6ac671..95effad 100644 --- a/src/test/java/org/utplsql/cli/RunCommandIT.java +++ b/src/test/java/org/utplsql/cli/RunCommandIT.java @@ -89,4 +89,14 @@ void run_withOutputButNoReporterDefined() throws Exception { assertValidReturnCode(result); } + + @Test + void run_withOraStuckTimeout() throws Exception { + int result = TestHelper.runApp("run", + TestHelper.getConnectionString(), + "--ora-stuck-timeout=2", + "--failure-exit-code=2"); + + assertValidReturnCode(result); + } } diff --git a/src/test/java/org/utplsql/cli/RunCommandIssue20IT.java b/src/test/java/org/utplsql/cli/RunCommandIssue20IT.java index 0976706..8162fc9 100644 --- a/src/test/java/org/utplsql/cli/RunCommandIssue20IT.java +++ b/src/test/java/org/utplsql/cli/RunCommandIssue20IT.java @@ -24,7 +24,8 @@ void runLoop() { IRunCommand runCmd = TestHelper.createRunCommand( TestHelper.getConnectionString(), "-p=TEST_BETWNSTR.normal_case", - "-f=ut_documentation_reporter"); + "-f=ut_documentation_reporter", + "--ora-stuck-timeout=3"); List reporterOptionsList = runCmd.getReporterOptionsList(); ReporterOptions reporterOptions1 = reporterOptionsList.get(0); assertEquals(CoreReporters.UT_DOCUMENTATION_REPORTER.name(), reporterOptions1.getReporterName()); diff --git a/src/test/java/org/utplsql/cli/TestHelper.java b/src/test/java/org/utplsql/cli/TestHelper.java index 488654e..dddca1a 100644 --- a/src/test/java/org/utplsql/cli/TestHelper.java +++ b/src/test/java/org/utplsql/cli/TestHelper.java @@ -19,9 +19,9 @@ class TestHelper { private static String sPass; static { - sUrl = EnvironmentVariableUtil.getEnvValue("DB_URL", "192.168.99.100:1521:XE"); - sUser = EnvironmentVariableUtil.getEnvValue("DB_USER", "app"); - sPass = EnvironmentVariableUtil.getEnvValue("DB_PASS", "app"); + sUrl = EnvironmentVariableUtil.getEnvValue("DB_URL", "//localhost:1521/FREEPDB1"); + sUser = EnvironmentVariableUtil.getEnvValue("DB_USER", "APP"); + sPass = EnvironmentVariableUtil.getEnvValue("DB_PASS", "pass"); } static RunPicocliCommand createPicocliRunCommand(String... args ) { diff --git a/src/test/java/org/utplsql/cli/VersionInfoCommandIT.java b/src/test/java/org/utplsql/cli/VersionInfoCommandIT.java index 073d50a..0838511 100644 --- a/src/test/java/org/utplsql/cli/VersionInfoCommandIT.java +++ b/src/test/java/org/utplsql/cli/VersionInfoCommandIT.java @@ -18,15 +18,23 @@ void setupCaptureSystemOut() { capturer = new SystemCapturer.SystemOutCapturer(); } - private int getNonEmptyLines(String content) { - return (int) Arrays.stream(content.split("[\n|\r]")) + private String[] getNonEmptyLineArray(String content) { + return Arrays.stream(content.split("[\r\n]+")) .filter(line -> !line.isEmpty()) - .count(); + .toArray(String[]::new); } private void assertNumberOfLines( int expected, String content ) { - int numOfLines = getNonEmptyLines(content); - assertEquals(expected, numOfLines, String.format("Expected output to have %n lines, but got %n", expected, numOfLines)); + String[] lines = getNonEmptyLineArray(content); + assertEquals(expected, lines.length, () -> { + StringBuilder sb = new StringBuilder(); + sb.append(String.format("Expected output to have %d lines, but got %d.%n", expected, lines.length)); + sb.append("Actual lines were:").append(System.lineSeparator()); + for (int i = 0; i < lines.length; i++) { + sb.append(String.format(" [%d] %s%n", i, lines[i])); + } + return sb.toString(); + }); } @Test void infoCommandRunsWithoutConnection() { diff --git a/src/test/java/org/utplsql/cli/WalletConnectionIT.java b/src/test/java/org/utplsql/cli/WalletConnectionIT.java new file mode 100644 index 0000000..4b1d5eb --- /dev/null +++ b/src/test/java/org/utplsql/cli/WalletConnectionIT.java @@ -0,0 +1,172 @@ +package org.utplsql.cli; + +import oracle.security.pki.OracleSecretStore; +import oracle.security.pki.OracleWallet; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.utplsql.cli.datasource.TestedDataSourceProvider; + +import javax.sql.DataSource; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +/** + * Connecting with {@code /@} using credentials stored in an Oracle Wallet + * (Secure External Password Store), see issue #225. + *

+ * The wallet, tnsnames.ora and ojdbc.properties are created on the fly from DB_URL / DB_USER / DB_PASS, + * so no Oracle client tooling (mkstore/orapki) is needed. + *

+ * TNS_ADMIN and ORACLE_HOME are read once per JVM and environment variables can't be changed at runtime, + * so the scenarios depending on them run the CLI in a separate JVM with a controlled environment. + */ +class WalletConnectionIT { + + private static final String TNS_ALIAS = "UTPLSQL_CLI_WALLET"; + private static final Pattern EZ_CONNECT = Pattern.compile("^//([^:/]+)(?::(\\d+))?/(.+)$"); + private static final long SUBPROCESS_TIMEOUT_MINUTES = 3; + + @TempDir + static Path tempDir; + + private static Path tnsAdmin; + private static Path oracleHome; + private static Path emptyOracleHome; + private static String walletConnectString; + + @BeforeAll + static void createWallet() throws Exception { + Matcher m = EZ_CONNECT.matcher(TestHelper.getUrl()); + assumeTrue(m.matches(), "DB_URL must be in //host[:port]/service format to generate tnsnames.ora"); + String host = m.group(1); + String port = m.group(2) == null ? "1521" : m.group(2); + String service = m.group(3); + + Path walletDir = Files.createDirectories(tempDir.resolve("wallet")); + char[] walletPassword = "Wallet_Pwd_123".toCharArray(); + + OracleWallet wallet = new OracleWallet(); + wallet.create(walletPassword); + OracleSecretStore secretStore = wallet.getSecretStore(); + secretStore.createCredential(TNS_ALIAS.toCharArray(), + TestHelper.getUser().toCharArray(), + TestHelper.getPass().toCharArray()); + wallet.setSecretStore(secretStore); + wallet.saveAs(walletDir.toString()); + wallet.createSSO(); + wallet.saveSSO(); + + assertTrue(Files.exists(walletDir.resolve("cwallet.sso")), "auto-login wallet was not created"); + + String tnsnamesOra = TNS_ALIAS + " = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = " + host + ")(PORT = " + port + "))" + + "(CONNECT_DATA = (SERVICE_NAME = " + service + ")))\n"; + String ojdbcProperties = "oracle.net.wallet_location=(SOURCE=(METHOD=FILE)(METHOD_DATA=(DIRECTORY=" + + forwardSlashes(walletDir) + ")))\n"; + + tnsAdmin = writeNetworkConfig(tempDir.resolve("tns_admin"), tnsnamesOra, ojdbcProperties); + oracleHome = tempDir.resolve("oracle_home"); + writeNetworkConfig(oracleHome.resolve("network").resolve("admin"), tnsnamesOra, ojdbcProperties); + emptyOracleHome = Files.createDirectories(tempDir.resolve("empty_oracle_home")); + + walletConnectString = "/@" + TNS_ALIAS + "?TNS_ADMIN=" + forwardSlashes(tnsAdmin); + } + + private static Path writeNetworkConfig(Path dir, String tnsnamesOra, String ojdbcProperties) throws IOException { + Files.createDirectories(dir); + Files.writeString(dir.resolve("tnsnames.ora"), tnsnamesOra); + Files.writeString(dir.resolve("ojdbc.properties"), ojdbcProperties); + return dir; + } + + private static String forwardSlashes(Path path) { + return path.toAbsolutePath().toString().replace('\\', '/'); + } + + @Test + void connectsAsWalletUser() throws Exception { + ConnectionConfig config = new ConnectionConfig(walletConnectString); + DataSource dataSource = new TestedDataSourceProvider(config, 1).getDataSource(); + + try (Connection con = dataSource.getConnection(); + Statement stmt = con.createStatement(); + ResultSet rs = stmt.executeQuery("select user from dual")) { + assertTrue(rs.next()); + assertEquals(TestHelper.getUser().toUpperCase(), rs.getString(1)); + } + } + + @Test + void runCommandWithWallet() { + int result = TestHelper.runApp("run", + walletConnectString, + "-f=ut_documentation_reporter", + "-s", + "--failure-exit-code=0"); + + assertEquals(0, result); + } + + @Test + void runCommandWithTnsAdminEnvironmentVariable() throws Exception { + assertCliConnectsWithWallet(Map.of("TNS_ADMIN", tnsAdmin.toString())); + } + + @Test + void runCommandWithOracleHomeFallback() throws Exception { + assertCliConnectsWithWallet(Map.of("ORACLE_HOME", oracleHome.toString())); + } + + @Test + void tnsAdminEnvironmentVariableTakesPrecedenceOverOracleHome() throws Exception { + // Fails if ORACLE_HOME/network/admin (without tnsnames.ora) is used instead of TNS_ADMIN + assertCliConnectsWithWallet(Map.of( + "TNS_ADMIN", tnsAdmin.toString(), + "ORACLE_HOME", emptyOracleHome.toString())); + } + + /** + * Runs {@code utplsql run /@} in a separate JVM with TNS_ADMIN and ORACLE_HOME + * replaced by the given environment + */ + private void assertCliConnectsWithWallet(Map environment) throws Exception { + Path java = Paths.get(System.getProperty("java.home"), "bin", "java"); + ProcessBuilder processBuilder = new ProcessBuilder( + java.toString(), "-cp", System.getProperty("java.class.path"), + Cli.class.getName(), "run", "/@" + TNS_ALIAS, + "-f=ut_documentation_reporter", "-s", "--failure-exit-code=0"); + + processBuilder.environment().remove("TNS_ADMIN"); + processBuilder.environment().remove("ORACLE_HOME"); + processBuilder.environment().putAll(environment); + + Path output = Files.createTempFile(tempDir, "cli-output", ".log"); + processBuilder.redirectErrorStream(true); + processBuilder.redirectOutput(output.toFile()); + + Process process = processBuilder.start(); + if (!process.waitFor(SUBPROCESS_TIMEOUT_MINUTES, TimeUnit.MINUTES)) { + process.destroyForcibly(); + fail("CLI did not finish within " + SUBPROCESS_TIMEOUT_MINUTES + " minutes. Output:\n" + Files.readString(output)); + } + + String cliOutput = Files.readString(output); + assertEquals(0, process.exitValue(), () -> "CLI failed with environment " + environment + ". Output:\n" + cliOutput); + assertTrue(cliOutput.contains("Use connection string jdbc:oracle:thin:/@" + TNS_ALIAS), + () -> "Expected wallet connection with environment " + environment + ". Output:\n" + cliOutput); + } +}