diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9fd2262..6f239d4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -55,6 +55,7 @@ jobs: demo: - spring-boot-3.4-maven - spring-boot-3.5-maven + - spring-boot-4.0-maven # - spring-boot-3.5-maven-failsafe-parallel # - spring-boot-3.5-maven-junit-parallel diff --git a/.gitignore b/.gitignore index 4f70dc3..2109b75 100644 --- a/.gitignore +++ b/.gitignore @@ -122,7 +122,7 @@ $RECYCLE.BIN/ PROMPTS.md VIDEO.md SCRIPT.md - +MARKETING.md ### JS ### coverage/ diff --git a/demo/spring-boot-3.5-maven/pom.xml b/demo/spring-boot-3.5-maven/pom.xml index 834398d..2e0e053 100644 --- a/demo/spring-boot-3.5-maven/pom.xml +++ b/demo/spring-boot-3.5-maven/pom.xml @@ -35,6 +35,11 @@ spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-actuator + + org.springframework.boot spring-boot-starter-data-jpa diff --git a/demo/spring-boot-3.5-maven/src/main/resources/application.properties b/demo/spring-boot-3.5-maven/src/main/resources/application.properties index 5afd1e6..2fc3dba 100644 --- a/demo/spring-boot-3.5-maven/src/main/resources/application.properties +++ b/demo/spring-boot-3.5-maven/src/main/resources/application.properties @@ -9,3 +9,5 @@ spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.show-sql=false # H2 Console (for development) spring.h2.console.enabled=true + +management.server.port=9090 diff --git a/demo/spring-boot-4.0-maven/.mvn/wrapper/maven-wrapper.properties b/demo/spring-boot-4.0-maven/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 0000000..2f94e61 --- /dev/null +++ b/demo/spring-boot-4.0-maven/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,19 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +wrapperVersion=3.3.2 +distributionType=only-script +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.10/apache-maven-3.9.10-bin.zip diff --git a/demo/spring-boot-4.0-maven/README.md b/demo/spring-boot-4.0-maven/README.md new file mode 100644 index 0000000..f99a53a --- /dev/null +++ b/demo/spring-boot-4.0-maven/README.md @@ -0,0 +1,68 @@ +# Spring Test Insight Demo + +This demo project showcases the Spring Test Insight Extension in action. + +## Overview + +This is a simple Spring Boot application with: + +- REST API for user management +- JPA/Hibernate with H2 in-memory database +- Comprehensive test suite demonstrating Spring Test context caching + +## Running the Tests + +First, install the Spring Test Insight Extension to your local Maven repository: + +```bash +cd ../spring-test-profiler-extension +./mvnw clean install +``` + +Then run the demo tests: + +```bash +cd ../demo +mvn clean test +``` + +## Viewing the Test Report + +After running the tests, the Spring Test Insight report will be generated at: + +- `target/spring-test-profiler/latest.html` + +Open this file in a web browser to see: + +- Test execution summary +- Spring context caching statistics +- Cache hit/miss rates +- Individual test results with execution times +- Failed test details with stack traces + +## Test Structure + +The demo includes various types of tests: + +- `UserRepositoryTest` - @DataJpaTest for repository layer +- `UserServiceTest` - Unit tests with mocks +- `UserControllerTest` - @WebMvcTest for REST controllers +- `UserIntegrationTest` - Full @SpringBootTest integration tests + +Each test class uses different Spring configurations, demonstrating how the extension tracks context caching across +different test types. + +## Key Features Demonstrated + +1. **Context Caching Visualization**: See which test classes share Spring contexts +2. **Performance Metrics**: Track context load times and test execution durations +3. **Cache Efficiency**: Monitor cache hit rates to optimize test suite performance +4. **Test Results**: Comprehensive view of passed, failed, and skipped tests + +## Tips for Optimization + +Based on the report, you can: + +- Identify tests that create new contexts unnecessarily +- Group tests with similar configurations to improve cache reuse +- Find slow context initialization that impacts test performance diff --git a/demo/spring-boot-4.0-maven/mvnw b/demo/spring-boot-4.0-maven/mvnw new file mode 100755 index 0000000..19529dd --- /dev/null +++ b/demo/spring-boot-4.0-maven/mvnw @@ -0,0 +1,259 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Apache Maven Wrapper startup batch script, version 3.3.2 +# +# Optional ENV vars +# ----------------- +# JAVA_HOME - location of a JDK home dir, required when download maven via java source +# MVNW_REPOURL - repo url base for downloading maven distribution +# MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven +# MVNW_VERBOSE - true: enable verbose log; debug: trace the mvnw script; others: silence the output +# ---------------------------------------------------------------------------- + +set -euf +[ "${MVNW_VERBOSE-}" != debug ] || set -x + +# OS specific support. +native_path() { printf %s\\n "$1"; } +case "$(uname)" in +CYGWIN* | MINGW*) + [ -z "${JAVA_HOME-}" ] || JAVA_HOME="$(cygpath --unix "$JAVA_HOME")" + native_path() { cygpath --path --windows "$1"; } + ;; +esac + +# set JAVACMD and JAVACCMD +set_java_home() { + # For Cygwin and MinGW, ensure paths are in Unix format before anything is touched + if [ -n "${JAVA_HOME-}" ]; then + if [ -x "$JAVA_HOME/jre/sh/java" ]; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + JAVACCMD="$JAVA_HOME/jre/sh/javac" + else + JAVACMD="$JAVA_HOME/bin/java" + JAVACCMD="$JAVA_HOME/bin/javac" + + if [ ! -x "$JAVACMD" ] || [ ! -x "$JAVACCMD" ]; then + echo "The JAVA_HOME environment variable is not defined correctly, so mvnw cannot run." >&2 + echo "JAVA_HOME is set to \"$JAVA_HOME\", but \"\$JAVA_HOME/bin/java\" or \"\$JAVA_HOME/bin/javac\" does not exist." >&2 + return 1 + fi + fi + else + JAVACMD="$( + 'set' +e + 'unset' -f command 2>/dev/null + 'command' -v java + )" || : + JAVACCMD="$( + 'set' +e + 'unset' -f command 2>/dev/null + 'command' -v javac + )" || : + + if [ ! -x "${JAVACMD-}" ] || [ ! -x "${JAVACCMD-}" ]; then + echo "The java/javac command does not exist in PATH nor is JAVA_HOME set, so mvnw cannot run." >&2 + return 1 + fi + fi +} + +# hash string like Java String::hashCode +hash_string() { + str="${1:-}" h=0 + while [ -n "$str" ]; do + char="${str%"${str#?}"}" + h=$(((h * 31 + $(LC_CTYPE=C printf %d "'$char")) % 4294967296)) + str="${str#?}" + done + printf %x\\n $h +} + +verbose() { :; } +[ "${MVNW_VERBOSE-}" != true ] || verbose() { printf %s\\n "${1-}"; } + +die() { + printf %s\\n "$1" >&2 + exit 1 +} + +trim() { + # MWRAPPER-139: + # Trims trailing and leading whitespace, carriage returns, tabs, and linefeeds. + # Needed for removing poorly interpreted newline sequences when running in more + # exotic environments such as mingw bash on Windows. + printf "%s" "${1}" | tr -d '[:space:]' +} + +# parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties +while IFS="=" read -r key value; do + case "${key-}" in + distributionUrl) distributionUrl=$(trim "${value-}") ;; + distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;; + esac +done <"${0%/*}/.mvn/wrapper/maven-wrapper.properties" +[ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in ${0%/*}/.mvn/wrapper/maven-wrapper.properties" + +case "${distributionUrl##*/}" in +maven-mvnd-*bin.*) + MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ + case "${PROCESSOR_ARCHITECTURE-}${PROCESSOR_ARCHITEW6432-}:$(uname -a)" in + *AMD64:CYGWIN* | *AMD64:MINGW*) distributionPlatform=windows-amd64 ;; + :Darwin*x86_64) distributionPlatform=darwin-amd64 ;; + :Darwin*arm64) distributionPlatform=darwin-aarch64 ;; + :Linux*x86_64*) distributionPlatform=linux-amd64 ;; + *) + echo "Cannot detect native platform for mvnd on $(uname)-$(uname -m), use pure java version" >&2 + distributionPlatform=linux-amd64 + ;; + esac + distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip" + ;; +maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;; +*) MVN_CMD="mvn${0##*/mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;; +esac + +# apply MVNW_REPOURL and calculate MAVEN_HOME +# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ +[ -z "${MVNW_REPOURL-}" ] || distributionUrl="$MVNW_REPOURL$_MVNW_REPO_PATTERN${distributionUrl#*"$_MVNW_REPO_PATTERN"}" +distributionUrlName="${distributionUrl##*/}" +distributionUrlNameMain="${distributionUrlName%.*}" +distributionUrlNameMain="${distributionUrlNameMain%-bin}" +MAVEN_USER_HOME="${MAVEN_USER_HOME:-${HOME}/.m2}" +MAVEN_HOME="${MAVEN_USER_HOME}/wrapper/dists/${distributionUrlNameMain-}/$(hash_string "$distributionUrl")" + +exec_maven() { + unset MVNW_VERBOSE MVNW_USERNAME MVNW_PASSWORD MVNW_REPOURL || : + exec "$MAVEN_HOME/bin/$MVN_CMD" "$@" || die "cannot exec $MAVEN_HOME/bin/$MVN_CMD" +} + +if [ -d "$MAVEN_HOME" ]; then + verbose "found existing MAVEN_HOME at $MAVEN_HOME" + exec_maven "$@" +fi + +case "${distributionUrl-}" in +*?-bin.zip | *?maven-mvnd-?*-?*.zip) ;; +*) die "distributionUrl is not valid, must match *-bin.zip or maven-mvnd-*.zip, but found '${distributionUrl-}'" ;; +esac + +# prepare tmp dir +if TMP_DOWNLOAD_DIR="$(mktemp -d)" && [ -d "$TMP_DOWNLOAD_DIR" ]; then + clean() { rm -rf -- "$TMP_DOWNLOAD_DIR"; } + trap clean HUP INT TERM EXIT +else + die "cannot create temp dir" +fi + +mkdir -p -- "${MAVEN_HOME%/*}" + +# Download and Install Apache Maven +verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." +verbose "Downloading from: $distributionUrl" +verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" + +# select .zip or .tar.gz +if ! command -v unzip >/dev/null; then + distributionUrl="${distributionUrl%.zip}.tar.gz" + distributionUrlName="${distributionUrl##*/}" +fi + +# verbose opt +__MVNW_QUIET_WGET=--quiet __MVNW_QUIET_CURL=--silent __MVNW_QUIET_UNZIP=-q __MVNW_QUIET_TAR='' +[ "${MVNW_VERBOSE-}" != true ] || __MVNW_QUIET_WGET='' __MVNW_QUIET_CURL='' __MVNW_QUIET_UNZIP='' __MVNW_QUIET_TAR=v + +# normalize http auth +case "${MVNW_PASSWORD:+has-password}" in +'') MVNW_USERNAME='' MVNW_PASSWORD='' ;; +has-password) [ -n "${MVNW_USERNAME-}" ] || MVNW_USERNAME='' MVNW_PASSWORD='' ;; +esac + +if [ -z "${MVNW_USERNAME-}" ] && command -v wget >/dev/null; then + verbose "Found wget ... using wget" + wget ${__MVNW_QUIET_WGET:+"$__MVNW_QUIET_WGET"} "$distributionUrl" -O "$TMP_DOWNLOAD_DIR/$distributionUrlName" || die "wget: Failed to fetch $distributionUrl" +elif [ -z "${MVNW_USERNAME-}" ] && command -v curl >/dev/null; then + verbose "Found curl ... using curl" + curl ${__MVNW_QUIET_CURL:+"$__MVNW_QUIET_CURL"} -f -L -o "$TMP_DOWNLOAD_DIR/$distributionUrlName" "$distributionUrl" || die "curl: Failed to fetch $distributionUrl" +elif set_java_home; then + verbose "Falling back to use Java to download" + javaSource="$TMP_DOWNLOAD_DIR/Downloader.java" + targetZip="$TMP_DOWNLOAD_DIR/$distributionUrlName" + cat >"$javaSource" <<-END + public class Downloader extends java.net.Authenticator + { + protected java.net.PasswordAuthentication getPasswordAuthentication() + { + return new java.net.PasswordAuthentication( System.getenv( "MVNW_USERNAME" ), System.getenv( "MVNW_PASSWORD" ).toCharArray() ); + } + public static void main( String[] args ) throws Exception + { + setDefault( new Downloader() ); + java.nio.file.Files.copy( java.net.URI.create( args[0] ).toURL().openStream(), java.nio.file.Paths.get( args[1] ).toAbsolutePath().normalize() ); + } + } + END + # For Cygwin/MinGW, switch paths to Windows format before running javac and java + verbose " - Compiling Downloader.java ..." + "$(native_path "$JAVACCMD")" "$(native_path "$javaSource")" || die "Failed to compile Downloader.java" + verbose " - Running Downloader.java ..." + "$(native_path "$JAVACMD")" -cp "$(native_path "$TMP_DOWNLOAD_DIR")" Downloader "$distributionUrl" "$(native_path "$targetZip")" +fi + +# If specified, validate the SHA-256 sum of the Maven distribution zip file +if [ -n "${distributionSha256Sum-}" ]; then + distributionSha256Result=false + if [ "$MVN_CMD" = mvnd.sh ]; then + echo "Checksum validation is not supported for maven-mvnd." >&2 + echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 + exit 1 + elif command -v sha256sum >/dev/null; then + if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c >/dev/null 2>&1; then + distributionSha256Result=true + fi + elif command -v shasum >/dev/null; then + if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | shasum -a 256 -c >/dev/null 2>&1; then + distributionSha256Result=true + fi + else + echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." >&2 + echo "Please install either command, or disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 + exit 1 + fi + if [ $distributionSha256Result = false ]; then + echo "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised." >&2 + echo "If you updated your Maven version, you need to update the specified distributionSha256Sum property." >&2 + exit 1 + fi +fi + +# unzip and move +if command -v unzip >/dev/null; then + unzip ${__MVNW_QUIET_UNZIP:+"$__MVNW_QUIET_UNZIP"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -d "$TMP_DOWNLOAD_DIR" || die "failed to unzip" +else + tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR" || die "failed to untar" +fi +printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/mvnw.url" +mv -- "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME" + +clean || : +exec_maven "$@" diff --git a/demo/spring-boot-4.0-maven/mvnw.cmd b/demo/spring-boot-4.0-maven/mvnw.cmd new file mode 100644 index 0000000..249bdf3 --- /dev/null +++ b/demo/spring-boot-4.0-maven/mvnw.cmd @@ -0,0 +1,149 @@ +<# : batch portion +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM http://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Apache Maven Wrapper startup batch script, version 3.3.2 +@REM +@REM Optional ENV vars +@REM MVNW_REPOURL - repo url base for downloading maven distribution +@REM MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven +@REM MVNW_VERBOSE - true: enable verbose log; others: silence the output +@REM ---------------------------------------------------------------------------- + +@IF "%__MVNW_ARG0_NAME__%"=="" (SET __MVNW_ARG0_NAME__=%~nx0) +@SET __MVNW_CMD__= +@SET __MVNW_ERROR__= +@SET __MVNW_PSMODULEP_SAVE=%PSModulePath% +@SET PSModulePath= +@FOR /F "usebackq tokens=1* delims==" %%A IN (`powershell -noprofile "& {$scriptDir='%~dp0'; $script='%__MVNW_ARG0_NAME__%'; icm -ScriptBlock ([Scriptblock]::Create((Get-Content -Raw '%~f0'))) -NoNewScope}"`) DO @( + IF "%%A"=="MVN_CMD" (set __MVNW_CMD__=%%B) ELSE IF "%%B"=="" (echo %%A) ELSE (echo %%A=%%B) +) +@SET PSModulePath=%__MVNW_PSMODULEP_SAVE% +@SET __MVNW_PSMODULEP_SAVE= +@SET __MVNW_ARG0_NAME__= +@SET MVNW_USERNAME= +@SET MVNW_PASSWORD= +@IF NOT "%__MVNW_CMD__%"=="" (%__MVNW_CMD__% %*) +@echo Cannot start maven from wrapper >&2 && exit /b 1 +@GOTO :EOF +: end batch / begin powershell #> + +$ErrorActionPreference = "Stop" +if ($env:MVNW_VERBOSE -eq "true") { + $VerbosePreference = "Continue" +} + +# calculate distributionUrl, requires .mvn/wrapper/maven-wrapper.properties +$distributionUrl = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionUrl +if (!$distributionUrl) { + Write-Error "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties" +} + +switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) { + "maven-mvnd-*" { + $USE_MVND = $true + $distributionUrl = $distributionUrl -replace '-bin\.[^.]*$',"-windows-amd64.zip" + $MVN_CMD = "mvnd.cmd" + break + } + default { + $USE_MVND = $false + $MVN_CMD = $script -replace '^mvnw','mvn' + break + } +} + +# apply MVNW_REPOURL and calculate MAVEN_HOME +# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ +if ($env:MVNW_REPOURL) { + $MVNW_REPO_PATTERN = if ($USE_MVND) { "/org/apache/maven/" } else { "/maven/mvnd/" } + $distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace '^.*'+$MVNW_REPO_PATTERN,'')" +} +$distributionUrlName = $distributionUrl -replace '^.*/','' +$distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$','' +$MAVEN_HOME_PARENT = "$HOME/.m2/wrapper/dists/$distributionUrlNameMain" +if ($env:MAVEN_USER_HOME) { + $MAVEN_HOME_PARENT = "$env:MAVEN_USER_HOME/wrapper/dists/$distributionUrlNameMain" +} +$MAVEN_HOME_NAME = ([System.Security.Cryptography.MD5]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join '' +$MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME" + +if (Test-Path -Path "$MAVEN_HOME" -PathType Container) { + Write-Verbose "found existing MAVEN_HOME at $MAVEN_HOME" + Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" + exit $? +} + +if (! $distributionUrlNameMain -or ($distributionUrlName -eq $distributionUrlNameMain)) { + Write-Error "distributionUrl is not valid, must end with *-bin.zip, but found $distributionUrl" +} + +# prepare tmp dir +$TMP_DOWNLOAD_DIR_HOLDER = New-TemporaryFile +$TMP_DOWNLOAD_DIR = New-Item -Itemtype Directory -Path "$TMP_DOWNLOAD_DIR_HOLDER.dir" +$TMP_DOWNLOAD_DIR_HOLDER.Delete() | Out-Null +trap { + if ($TMP_DOWNLOAD_DIR.Exists) { + try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } + catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } + } +} + +New-Item -Itemtype Directory -Path "$MAVEN_HOME_PARENT" -Force | Out-Null + +# Download and Install Apache Maven +Write-Verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." +Write-Verbose "Downloading from: $distributionUrl" +Write-Verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" + +$webclient = New-Object System.Net.WebClient +if ($env:MVNW_USERNAME -and $env:MVNW_PASSWORD) { + $webclient.Credentials = New-Object System.Net.NetworkCredential($env:MVNW_USERNAME, $env:MVNW_PASSWORD) +} +[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 +$webclient.DownloadFile($distributionUrl, "$TMP_DOWNLOAD_DIR/$distributionUrlName") | Out-Null + +# If specified, validate the SHA-256 sum of the Maven distribution zip file +$distributionSha256Sum = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionSha256Sum +if ($distributionSha256Sum) { + if ($USE_MVND) { + Write-Error "Checksum validation is not supported for maven-mvnd. `nPlease disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." + } + Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash + if ((Get-FileHash "$TMP_DOWNLOAD_DIR/$distributionUrlName" -Algorithm SHA256).Hash.ToLower() -ne $distributionSha256Sum) { + Write-Error "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised. If you updated your Maven version, you need to update the specified distributionSha256Sum property." + } +} + +# unzip and move +Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null +Rename-Item -Path "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" -NewName $MAVEN_HOME_NAME | Out-Null +try { + Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null +} catch { + if (! (Test-Path -Path "$MAVEN_HOME" -PathType Container)) { + Write-Error "fail to move MAVEN_HOME" + } +} finally { + try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } + catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } +} + +Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" diff --git a/demo/spring-boot-4.0-maven/pom.xml b/demo/spring-boot-4.0-maven/pom.xml new file mode 100644 index 0000000..8b60975 --- /dev/null +++ b/demo/spring-boot-4.0-maven/pom.xml @@ -0,0 +1,111 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 4.0.0-M2 + + + + digital.pragmatech + spring-test-profiler-demo-spring-boot-4.0-maven + 0.0.1 + jar + + Spring Test Insight Demo - Spring Boot 4.0 + Demo project for Spring Profiler Insight Extension + + + 21 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-webmvc + + + + org.springframework.boot + spring-boot-starter-webclient + + + + org.springframework.boot + spring-boot-starter-restclient + + + + org.springframework.boot + spring-boot-starter-actuator + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + com.h2database + h2 + runtime + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + digital.pragmatech.testing + spring-test-profiler + 0.0.12-SNAPSHOT + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-compiler-plugin + 3.13.0 + + + org.apache.maven.plugins + maven-surefire-plugin + 3.5.3 + + + org.apache.maven.plugins + maven-failsafe-plugin + 3.5.3 + + + + integration-test + verify + + + + + + + diff --git a/demo/spring-boot-4.0-maven/src/main/java/digital/pragmatech/demo/DemoApplication.java b/demo/spring-boot-4.0-maven/src/main/java/digital/pragmatech/demo/DemoApplication.java new file mode 100644 index 0000000..b04e05f --- /dev/null +++ b/demo/spring-boot-4.0-maven/src/main/java/digital/pragmatech/demo/DemoApplication.java @@ -0,0 +1,32 @@ +package digital.pragmatech.demo; + +import java.util.Arrays; +import java.util.Map; + +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; + +@SpringBootApplication +public class DemoApplication implements CommandLineRunner { + + private final ApplicationContext context; + + public DemoApplication(ApplicationContext context) { + this.context = context; + } + + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class, args); + } + + @Override + public void run(String... args) throws Exception { + String[] beanNames = context.getBeanDefinitionNames(); + System.out.println(Map.of( + "count", beanNames.length, + "beans", Arrays.asList(beanNames) + )); + } +} diff --git a/demo/spring-boot-4.0-maven/src/main/java/digital/pragmatech/demo/controller/BookController.java b/demo/spring-boot-4.0-maven/src/main/java/digital/pragmatech/demo/controller/BookController.java new file mode 100644 index 0000000..bd70a16 --- /dev/null +++ b/demo/spring-boot-4.0-maven/src/main/java/digital/pragmatech/demo/controller/BookController.java @@ -0,0 +1,36 @@ +package digital.pragmatech.demo.controller; + +import digital.pragmatech.demo.entity.Book; +import digital.pragmatech.demo.service.BookService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/api/books") +public class BookController { + + @Autowired + private BookService bookService; + + @PostMapping + public ResponseEntity createBook(@RequestBody Book book) { + Book createdBook = bookService.createBook(book); + return new ResponseEntity<>(createdBook, HttpStatus.CREATED); + } + + @GetMapping + public ResponseEntity getAllBooks() { + List books = bookService.findAll(); + return ResponseEntity.ok(books.toArray(new Book[0])); + } + + @GetMapping("/count") + public ResponseEntity getBookCount() { + long count = bookService.count(); + return ResponseEntity.ok(count); + } +} diff --git a/demo/spring-boot-4.0-maven/src/main/java/digital/pragmatech/demo/entity/Book.java b/demo/spring-boot-4.0-maven/src/main/java/digital/pragmatech/demo/entity/Book.java new file mode 100644 index 0000000..2ff8274 --- /dev/null +++ b/demo/spring-boot-4.0-maven/src/main/java/digital/pragmatech/demo/entity/Book.java @@ -0,0 +1,120 @@ +package digital.pragmatech.demo.entity; + +import java.math.BigDecimal; +import java.time.LocalDate; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.Table; + +@Entity +@Table(name = "books") +public class Book { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(nullable = false) + private String title; + + @Column(nullable = false) + private String author; + + @Column(unique = true, nullable = false) + private String isbn; + + @Column(precision = 10, scale = 2) + private BigDecimal price; + + @Column(name = "publication_date") + private LocalDate publicationDate; + + @Enumerated(EnumType.STRING) + private BookCategory category; + + @Column(columnDefinition = "TEXT") + private String description; + + // Constructors + public Book() { + } + + public Book(String title, String author, String isbn, BigDecimal price, BookCategory category) { + this.title = title; + this.author = author; + this.isbn = isbn; + this.price = price; + this.category = category; + } + + // Getters and Setters + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public String getIsbn() { + return isbn; + } + + public void setIsbn(String isbn) { + this.isbn = isbn; + } + + public BigDecimal getPrice() { + return price; + } + + public void setPrice(BigDecimal price) { + this.price = price; + } + + public LocalDate getPublicationDate() { + return publicationDate; + } + + public void setPublicationDate(LocalDate publicationDate) { + this.publicationDate = publicationDate; + } + + public BookCategory getCategory() { + return category; + } + + public void setCategory(BookCategory category) { + this.category = category; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } +} diff --git a/demo/spring-boot-4.0-maven/src/main/java/digital/pragmatech/demo/entity/BookCategory.java b/demo/spring-boot-4.0-maven/src/main/java/digital/pragmatech/demo/entity/BookCategory.java new file mode 100644 index 0000000..488ee98 --- /dev/null +++ b/demo/spring-boot-4.0-maven/src/main/java/digital/pragmatech/demo/entity/BookCategory.java @@ -0,0 +1,14 @@ +package digital.pragmatech.demo.entity; + +public enum BookCategory { + FICTION, + NON_FICTION, + SCIENCE, + TECHNOLOGY, + BIOGRAPHY, + HISTORY, + FANTASY, + MYSTERY, + ROMANCE, + HORROR +} diff --git a/demo/spring-boot-4.0-maven/src/main/java/digital/pragmatech/demo/repository/BookRepository.java b/demo/spring-boot-4.0-maven/src/main/java/digital/pragmatech/demo/repository/BookRepository.java new file mode 100644 index 0000000..bf638ba --- /dev/null +++ b/demo/spring-boot-4.0-maven/src/main/java/digital/pragmatech/demo/repository/BookRepository.java @@ -0,0 +1,36 @@ +package digital.pragmatech.demo.repository; + +import java.math.BigDecimal; +import java.util.List; +import java.util.Optional; + +import digital.pragmatech.demo.entity.Book; +import digital.pragmatech.demo.entity.BookCategory; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +@Repository +public interface BookRepository extends JpaRepository { + + Optional findByIsbn(String isbn); + + List findByAuthorContainingIgnoreCase(String author); + + List findByTitleContainingIgnoreCase(String title); + + List findByCategory(BookCategory category); + + List findByPriceBetween(BigDecimal minPrice, BigDecimal maxPrice); + + @Query("SELECT b FROM Book b WHERE b.author = :author AND b.category = :category") + List findByAuthorAndCategory(@Param("author") String author, @Param("category") BookCategory category); + + @Query("SELECT COUNT(b) FROM Book b WHERE b.category = :category") + long countByCategory(@Param("category") BookCategory category); + + boolean existsByIsbn(String isbn); + + void deleteByIsbn(String isbn); +} diff --git a/demo/spring-boot-4.0-maven/src/main/java/digital/pragmatech/demo/service/BookService.java b/demo/spring-boot-4.0-maven/src/main/java/digital/pragmatech/demo/service/BookService.java new file mode 100644 index 0000000..d4da349 --- /dev/null +++ b/demo/spring-boot-4.0-maven/src/main/java/digital/pragmatech/demo/service/BookService.java @@ -0,0 +1,114 @@ +package digital.pragmatech.demo.service; + +import java.math.BigDecimal; +import java.util.List; +import java.util.Optional; + +import digital.pragmatech.demo.entity.Book; +import digital.pragmatech.demo.entity.BookCategory; +import digital.pragmatech.demo.repository.BookRepository; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional +public class BookService { + + private final BookRepository bookRepository; + + public BookService(BookRepository bookRepository) { + this.bookRepository = bookRepository; + } + + public Book createBook(Book book) { + if (bookRepository.existsByIsbn(book.getIsbn())) { + throw new IllegalArgumentException("Book with ISBN " + book.getIsbn() + " already exists"); + } + return bookRepository.save(book); + } + + @Transactional(readOnly = true) + public Optional findById(Long id) { + return bookRepository.findById(id); + } + + @Transactional(readOnly = true) + public Optional findByIsbn(String isbn) { + return bookRepository.findByIsbn(isbn); + } + + @Transactional(readOnly = true) + public List findAll() { + return bookRepository.findAll(); + } + + @Transactional(readOnly = true) + public List findByAuthor(String author) { + return bookRepository.findByAuthorContainingIgnoreCase(author); + } + + @Transactional(readOnly = true) + public List findByTitle(String title) { + return bookRepository.findByTitleContainingIgnoreCase(title); + } + + @Transactional(readOnly = true) + public List findByCategory(BookCategory category) { + return bookRepository.findByCategory(category); + } + + @Transactional(readOnly = true) + public List findByPriceRange(BigDecimal minPrice, BigDecimal maxPrice) { + return bookRepository.findByPriceBetween(minPrice, maxPrice); + } + + public Book updateBook(Long id, Book updatedBook) { + Book existingBook = bookRepository.findById(id) + .orElseThrow(() -> new IllegalArgumentException("Book not found with id: " + id)); + + // Check if ISBN is being changed and if new ISBN already exists + if (!existingBook.getIsbn().equals(updatedBook.getIsbn()) && + bookRepository.existsByIsbn(updatedBook.getIsbn())) { + throw new IllegalArgumentException("Book with ISBN " + updatedBook.getIsbn() + " already exists"); + } + + existingBook.setTitle(updatedBook.getTitle()); + existingBook.setAuthor(updatedBook.getAuthor()); + existingBook.setIsbn(updatedBook.getIsbn()); + existingBook.setPrice(updatedBook.getPrice()); + existingBook.setPublicationDate(updatedBook.getPublicationDate()); + existingBook.setCategory(updatedBook.getCategory()); + existingBook.setDescription(updatedBook.getDescription()); + + return bookRepository.save(existingBook); + } + + public void deleteBook(Long id) { + if (!bookRepository.existsById(id)) { + throw new IllegalArgumentException("Book not found with id: " + id); + } + bookRepository.deleteById(id); + } + + public void deleteByIsbn(String isbn) { + if (!bookRepository.existsByIsbn(isbn)) { + throw new IllegalArgumentException("Book not found with ISBN: " + isbn); + } + bookRepository.deleteByIsbn(isbn); + } + + @Transactional(readOnly = true) + public long countBooks() { + return bookRepository.count(); + } + + @Transactional(readOnly = true) + public long countByCategory(BookCategory category) { + return bookRepository.countByCategory(category); + } + + @Transactional(readOnly = true) + public long count() { + return bookRepository.count(); + } +} diff --git a/demo/spring-boot-4.0-maven/src/main/resources/application.properties b/demo/spring-boot-4.0-maven/src/main/resources/application.properties new file mode 100644 index 0000000..5afd1e6 --- /dev/null +++ b/demo/spring-boot-4.0-maven/src/main/resources/application.properties @@ -0,0 +1,11 @@ +# H2 Database Configuration +spring.datasource.url=jdbc:h2:mem:testdb +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.username=sa +spring.datasource.password= +# JPA Configuration +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect +spring.jpa.hibernate.ddl-auto=create-drop +spring.jpa.show-sql=false +# H2 Console (for development) +spring.h2.console.enabled=true diff --git a/demo/spring-boot-4.0-maven/src/test/java/digital/pragmatech/demo/BadOneIT.java b/demo/spring-boot-4.0-maven/src/test/java/digital/pragmatech/demo/BadOneIT.java new file mode 100644 index 0000000..84deb02 --- /dev/null +++ b/demo/spring-boot-4.0-maven/src/test/java/digital/pragmatech/demo/BadOneIT.java @@ -0,0 +1,73 @@ +package digital.pragmatech.demo; + +import java.math.BigDecimal; + +import digital.pragmatech.demo.entity.Book; +import digital.pragmatech.demo.entity.BookCategory; +import digital.pragmatech.demo.repository.BookRepository; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.TestPropertySource; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +/** + * BAD EXAMPLE: This test uses @DirtiesContext unnecessarily, + * causing the Spring context to be recreated for every test method. + * This is a cache MISS every time! + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ActiveProfiles("test") +@TestPropertySource(properties = { + "spring.datasource.url=jdbc:h2:mem:badtest1;DB_CLOSE_DELAY=-1", + "spring.jpa.hibernate.ddl-auto=create-drop", + "logging.level.org.springframework.web=DEBUG" // Different logging config +}) +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) // BAD: Forces context reload +class BadOneIT { + + private final BookRepository bookRepository; + + public BadOneIT(@Autowired BookRepository bookRepository) { + this.bookRepository = bookRepository; + } + + @Test + void testCreateBook() { + Book book = new Book("Spring in Action", "Craig Walls", "978-1617294945", + new BigDecimal("39.99"), BookCategory.TECHNOLOGY); + + Book saved = bookRepository.save(book); + assertNotNull(saved.getId()); + assertEquals("Spring in Action", saved.getTitle()); + } + + @Test + void testFindByCategory() { + Book book1 = new Book("Clean Code", "Robert Martin", "978-0132350884", + new BigDecimal("45.99"), BookCategory.TECHNOLOGY); + Book book2 = new Book("The Hobbit", "J.R.R. Tolkien", "978-0547928227", + new BigDecimal("12.99"), BookCategory.FANTASY); + + bookRepository.save(book1); + bookRepository.save(book2); + + var techBooks = bookRepository.findByCategory(BookCategory.TECHNOLOGY); + assertEquals(1, techBooks.size()); + assertEquals("Clean Code", techBooks.get(0).getTitle()); + } + + @Test + void testCountBooks() { + Book book = new Book("Effective Java", "Joshua Bloch", "978-0134685991", + new BigDecimal("52.99"), BookCategory.TECHNOLOGY); + bookRepository.save(book); + + long count = bookRepository.count(); + assertEquals(1, count); + } +} diff --git a/demo/spring-boot-4.0-maven/src/test/java/digital/pragmatech/demo/BadThreeIT.java b/demo/spring-boot-4.0-maven/src/test/java/digital/pragmatech/demo/BadThreeIT.java new file mode 100644 index 0000000..6da4c6b --- /dev/null +++ b/demo/spring-boot-4.0-maven/src/test/java/digital/pragmatech/demo/BadThreeIT.java @@ -0,0 +1,74 @@ +package digital.pragmatech.demo; + +import java.math.BigDecimal; + +import digital.pragmatech.demo.entity.Book; +import digital.pragmatech.demo.entity.BookCategory; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.web.server.test.client.TestRestTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.TestPropertySource; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * BAD EXAMPLE: This test uses @AutoConfigureWebMvc annotation unnecessarily, + * and has yet another different configuration, causing another context cache MISS. + * Also uses webEnvironment.RANDOM_PORT but different from other tests. + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@AutoConfigureWebMvc // BAD: Unnecessary annotation that changes context +@ActiveProfiles("test") // Same as first test, but other config differs +@TestPropertySource(properties = { + "spring.datasource.url=jdbc:h2:mem:badtest3;DB_CLOSE_DELAY=-1", // Different DB name again + "spring.jpa.hibernate.ddl-auto=create-drop", + "server.servlet.context-path=/api/v1", // BAD: Different context path + "spring.jackson.property-naming-strategy=SNAKE_CASE" // BAD: Different Jackson config +}) +class BadThreeIT { + + @Autowired + private TestRestTemplate testRestTemplate; + + @Test + void testCreateBookViaRestApi() { + Book book = new Book("Java: The Complete Reference", "Herbert Schildt", + "978-1260440232", new BigDecimal("59.99"), BookCategory.TECHNOLOGY); + + ResponseEntity response = testRestTemplate.postForEntity("/api/books", book, Book.class); + + assertEquals(HttpStatus.CREATED, response.getStatusCode()); + assertNotNull(response.getBody()); + assertEquals("Java: The Complete Reference", response.getBody().getTitle()); + } + + @Test + void testGetAllBooksViaRestApi() { + // First create a book + Book book = new Book("Spring Boot in Action", "Craig Walls", "978-1617292545", + new BigDecimal("44.99"), BookCategory.TECHNOLOGY); + testRestTemplate.postForEntity("/api/books", book, Book.class); + + ResponseEntity response = testRestTemplate.getForEntity("/api/books", Book[].class); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertNotNull(response.getBody()); + assertTrue(response.getBody().length > 0); + } + + @Test + void testGetBookCountViaRestApi() { + ResponseEntity response = testRestTemplate.getForEntity("/api/books/count", Long.class); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertNotNull(response.getBody()); + assertTrue(response.getBody() >= 0); + } +} diff --git a/demo/spring-boot-4.0-maven/src/test/java/digital/pragmatech/demo/BadTwoIT.java b/demo/spring-boot-4.0-maven/src/test/java/digital/pragmatech/demo/BadTwoIT.java new file mode 100644 index 0000000..709dd28 --- /dev/null +++ b/demo/spring-boot-4.0-maven/src/test/java/digital/pragmatech/demo/BadTwoIT.java @@ -0,0 +1,69 @@ +package digital.pragmatech.demo; + +import java.math.BigDecimal; + +import digital.pragmatech.demo.entity.Book; +import digital.pragmatech.demo.entity.BookCategory; +import digital.pragmatech.demo.service.BookService; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.TestPropertySource; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +/** + * BAD EXAMPLE: This test uses different TestPropertySource values, + * causing a context cache MISS because the configuration is different + * from other tests, even though the difference is minimal. + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, + properties = "server.port=8081") // BAD: Different port +@ActiveProfiles({"test", "integration"}) // BAD: Different profiles +@TestPropertySource(properties = { + "spring.datasource.url=jdbc:h2:mem:badtest2;DB_CLOSE_DELAY=-1", // Different DB name + "spring.jpa.hibernate.ddl-auto=create-drop", + "spring.jpa.show-sql=true", // Different JPA setting + "management.endpoints.web.exposure.include=health,info" // Additional property +}) +class BadTwoIT { + + @Autowired + private BookService bookService; + + @Test + void testServiceCreateBook() { + Book book = new Book("Design Patterns", "Gang of Four", "978-0201633612", + new BigDecimal("54.99"), BookCategory.TECHNOLOGY); + + Book created = bookService.createBook(book); + assertNotNull(created.getId()); + assertEquals("Design Patterns", created.getTitle()); + } + + @Test + void testServiceFindByAuthor() { + Book book1 = new Book("Refactoring", "Martin Fowler", "978-0134757599", + new BigDecimal("47.99"), BookCategory.TECHNOLOGY); + Book book2 = new Book("UML Distilled", "Martin Fowler", "978-0321193681", + new BigDecimal("39.99"), BookCategory.TECHNOLOGY); + + bookService.createBook(book1); + bookService.createBook(book2); + + var fowlerBooks = bookService.findByAuthor("Martin Fowler"); + assertEquals(2, fowlerBooks.size()); + } + + @Test + void testServiceCountByCategory() { + Book book = new Book("Dune", "Frank Herbert", "978-0441172719", + new BigDecimal("16.99"), BookCategory.FICTION); + bookService.createBook(book); + + long fictionCount = bookService.countByCategory(BookCategory.FICTION); + assertEquals(1, fictionCount); + } +} diff --git a/demo/spring-boot-4.0-maven/src/test/java/digital/pragmatech/demo/GoodIT.java b/demo/spring-boot-4.0-maven/src/test/java/digital/pragmatech/demo/GoodIT.java new file mode 100644 index 0000000..17382f3 --- /dev/null +++ b/demo/spring-boot-4.0-maven/src/test/java/digital/pragmatech/demo/GoodIT.java @@ -0,0 +1,78 @@ +package digital.pragmatech.demo; + +import java.math.BigDecimal; + +import digital.pragmatech.demo.entity.Book; +import digital.pragmatech.demo.entity.BookCategory; +import digital.pragmatech.demo.repository.BookRepository; +import digital.pragmatech.demo.service.BookService; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.transaction.annotation.Transactional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * GOOD EXAMPLE: This test class uses consistent configuration that can be + * shared with other tests, resulting in context cache HITs. + * Uses @Transactional to clean up data instead of @DirtiesContext. + */ +@SpringBootTest +@ActiveProfiles("test") +@Transactional // GOOD: Rolls back after each test, no context recreation needed +class GoodIT { + + @Autowired + private BookRepository bookRepository; + + @Autowired + private BookService bookService; + + @Test + void testRepositoryAndService() { + // Test both repository and service in one test class with same context + Book book = new Book("Clean Architecture", "Robert C. Martin", "978-0134494166", + new BigDecimal("39.99"), BookCategory.TECHNOLOGY); + + // Test repository + Book savedViaRepo = bookRepository.save(book); + assertNotNull(savedViaRepo.getId()); + + // Test service (reusing same context) + var foundBooks = bookService.findByAuthor("Robert C. Martin"); + assertEquals(1, foundBooks.size()); + assertEquals("Clean Architecture", foundBooks.get(0).getTitle()); + } + + @Test + void testAnotherOperationSameContext() { + // This test will reuse the same Spring context as the previous test + Book book1 = new Book("Microservices Patterns", "Chris Richardson", "978-1617294549", + new BigDecimal("49.99"), BookCategory.TECHNOLOGY); + Book book2 = new Book("Domain-Driven Design", "Eric Evans", "978-0321125217", + new BigDecimal("54.99"), BookCategory.TECHNOLOGY); + + bookService.createBook(book1); + bookService.createBook(book2); + + long techBookCount = bookService.countByCategory(BookCategory.TECHNOLOGY); + assertEquals(2, techBookCount); + } + + @Test + void testContextReuseAgain() { + // Yet another test that will reuse the same context + Book book = new Book("Building Microservices", "Sam Newman", "978-1492034025", + new BigDecimal("44.99"), BookCategory.TECHNOLOGY); + + Book created = bookService.createBook(book); + var found = bookRepository.findByIsbn("978-1492034025"); + + assertTrue(found.isPresent()); + assertEquals(created.getId(), found.get().getId()); + } +} diff --git a/demo/spring-boot-4.0-maven/src/test/java/digital/pragmatech/demo/SimpleUnitTest.java b/demo/spring-boot-4.0-maven/src/test/java/digital/pragmatech/demo/SimpleUnitTest.java new file mode 100644 index 0000000..f4472b9 --- /dev/null +++ b/demo/spring-boot-4.0-maven/src/test/java/digital/pragmatech/demo/SimpleUnitTest.java @@ -0,0 +1,32 @@ +package digital.pragmatech.demo; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Simple Spring unit test to demonstrate surefire phase report generation. + * Unit tests (not ending with IT) are run by maven-surefire-plugin. + */ +class SimpleUnitTest { + + @Test + void testSimpleAssertion() { + String message = "Hello, World!"; + assertEquals("Hello, World!", message); + } + + @Test + void testArithmetic() { + int result = 2 + 2; + assertEquals(4, result); + } + + @Test + void testBooleanLogic() { + assertTrue(true); + assertFalse(false); + } +} diff --git a/demo/spring-boot-4.0-maven/src/test/resources/META-INF/spring.factories b/demo/spring-boot-4.0-maven/src/test/resources/META-INF/spring.factories new file mode 100644 index 0000000..4ffb86a --- /dev/null +++ b/demo/spring-boot-4.0-maven/src/test/resources/META-INF/spring.factories @@ -0,0 +1,4 @@ +org.springframework.test.context.TestExecutionListener=\ +digital.pragmatech.testing.SpringTestProfilerListener +org.springframework.context.ApplicationContextInitializer=\ +digital.pragmatech.testing.diagnostic.ContextDiagnosticApplicationInitializer diff --git a/src/main/java/digital/pragmatech/testing/diagnostic/ContextDiagnosticApplicationInitializer.java b/src/main/java/digital/pragmatech/testing/diagnostic/ContextDiagnosticApplicationInitializer.java index c2c4ee5..f396bfd 100644 --- a/src/main/java/digital/pragmatech/testing/diagnostic/ContextDiagnosticApplicationInitializer.java +++ b/src/main/java/digital/pragmatech/testing/diagnostic/ContextDiagnosticApplicationInitializer.java @@ -21,12 +21,15 @@ public void initialize(ConfigurableApplicationContext applicationContext) { applicationContext.addApplicationListener( event -> { - if (event instanceof ContextRefreshedEvent) { - ContextDiagnostic completedDiagnostic = contextDiagnostic.completed(); - applicationContext - .getBeanFactory() - .registerSingleton("contextDiagnostic", completedDiagnostic); - LOG.debug("Context Diagnostic Completed: {}", completedDiagnostic); + if (event instanceof ContextRefreshedEvent contextEvent) { + if (contextEvent.getApplicationContext().getParent() == null + && !applicationContext.getBeanFactory().containsSingleton("contextDiagnostic")) { + ContextDiagnostic completedDiagnostic = contextDiagnostic.completed(); + applicationContext + .getBeanFactory() + .registerSingleton("contextDiagnostic", completedDiagnostic); + LOG.debug("Context Diagnostic Completed: {}", completedDiagnostic); + } } }); } diff --git a/src/test/java/digital/pragmatech/testing/diagnostic/ContextDiagnosticApplicationInitializerTest.java b/src/test/java/digital/pragmatech/testing/diagnostic/ContextDiagnosticApplicationInitializerTest.java new file mode 100644 index 0000000..fcde119 --- /dev/null +++ b/src/test/java/digital/pragmatech/testing/diagnostic/ContextDiagnosticApplicationInitializerTest.java @@ -0,0 +1,85 @@ +package digital.pragmatech.testing.diagnostic; + +import org.junit.jupiter.api.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.event.ContextRefreshedEvent; + +import static org.assertj.core.api.Assertions.assertThat; + +class ContextDiagnosticApplicationInitializerTest { + + @Test + void shouldRegisterBeanInParentContextOnly() { + // Create parent context + var parentContext = new AnnotationConfigApplicationContext(); + + var initializer = new ContextDiagnosticApplicationInitializer(); + initializer.initialize(parentContext); + + // Refresh parent context - should register bean + parentContext.refresh(); + + assertThat(parentContext.containsBean("contextDiagnostic")).isTrue(); + + // Create child context + AnnotationConfigApplicationContext childContext = new AnnotationConfigApplicationContext(); + childContext.setParent(parentContext); + + initializer.initialize(childContext); + childContext.refresh(); + + // Child should not have the bean locally (parent still has it) + assertThat(childContext.containsLocalBean("contextDiagnostic")).isFalse(); + assertThat(parentContext.containsBean("contextDiagnostic")).isTrue(); + + parentContext.close(); + childContext.close(); + } + + @Test + void shouldNotRegisterBeanTwiceOnMultipleRefresh() { + var context = new AnnotationConfigApplicationContext(); + var initializer = new ContextDiagnosticApplicationInitializer(); + + initializer.initialize(context); + + context.refresh(); + + // Simulate additional refresh events + context.publishEvent(new ContextRefreshedEvent(context)); + context.publishEvent(new ContextRefreshedEvent(context)); + + // Should still have only one bean + assertThat(context.getBeansOfType(ContextDiagnostic.class)).hasSize(1); + + context.close(); + } + + @Test + void shouldHaveCorrectEqualsAndHashCode() { + var initializer1 = new ContextDiagnosticApplicationInitializer(); + var initializer2 = new ContextDiagnosticApplicationInitializer(); + + assertThat(initializer1).isEqualTo(initializer2); + assertThat(initializer1.hashCode()).isEqualTo(initializer2.hashCode()); + } + + @Test + void shouldNotThrowExceptionWithHierarchicalContexts() { + // This test verifies the fix doesn't break with real hierarchical contexts + try (var parentContext = new AnnotationConfigApplicationContext(); + var childContext = new AnnotationConfigApplicationContext()) { + childContext.setParent(parentContext); + + var initializer = new ContextDiagnosticApplicationInitializer(); + initializer.initialize(parentContext); + initializer.initialize(childContext); + + parentContext.refresh(); + childContext.refresh(); + + assertThat(parentContext.isActive()).isTrue(); + assertThat(childContext.isActive()).isTrue(); + } + } +}