Skip to content

Commit

Permalink
Merge pull request #36 from webcompere/non-illegal-reflective-access-…
Browse files Browse the repository at this point in the history
…version

Remove illegal reflective access from `EnvironmentVariables`
  • Loading branch information
ashleyfrieze committed Jan 9, 2022
2 parents c6ee659 + f24467e commit ede7d20
Show file tree
Hide file tree
Showing 15 changed files with 456 additions and 426 deletions.
117 changes: 0 additions & 117 deletions .mvn/wrapper/MavenWrapperDownloader.java

This file was deleted.

Binary file modified .mvn/wrapper/maven-wrapper.jar
Binary file not shown.
18 changes: 17 additions & 1 deletion .mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
# 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.
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar
67 changes: 57 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,32 @@
[![Build status](https://ci.appveyor.com/api/projects/status/r943gjn189rlxts9/branch/main?svg=true)](https://ci.appveyor.com/project/ashleyfrieze/system-stubs/branch/main)
[![codecov](https://codecov.io/gh/webcompere/system-stubs/branch/main/graph/badge.svg?token=J0N9VCXFQ1)](https://codecov.io/gh/webcompere/system-stubs)

> **⚠ WARNING: JDK Compatibility.**
> From JDK16 onwards, there's deep restrictons on the ability to use reflection to modify the Unmodifiable `Map` where `System` stores the
> environment variables. We're looking for a fix to this. Until then, this library is not recommended for environment variable settings in Java 16+.
> **⚠ WARNING: JDK Compatibility.**
> From JDK16 onwards, there's deep restrictons on the ability to use reflection.
>
> Consequently, this library now uses `mockito-inline` version 3.x to enable
> the interception of calls for reading environment variables. This requires consumers
> to both use a compatible version of Mockito AND be prepared for the _inline_
> implementation of Mockito mocks.
>
> Where this isn't appropriate, the [v1.x](https://github.com/webcompere/system-stubs/tree/1.x)
> version of this will still work for Java versions below 16, and may also be
> co-erced into working with via the java [command line](https://github.com/stefanbirkner/system-lambda/issues/23#issuecomment-1007608124).
>
> The long-term plan for this library is to use more interceptors of system
> functions with Mockito where possible, unless a more direct route becomes available.
> While this library will continue to be built for Java 8 at the moment, in future it
> may also move to a higher base version. The v1.x branch will stay on Java 8.
## Overview
System Stubs is used to test code which depends on methods in `java.lang.System`.

It is published under the [MIT license](http://opensource.org/licenses/MIT) and requires at least Java 8. There is a [walkthrough of its main features](https://www.baeldung.com/java-system-stubs) over on [Baeldung.com](https://www.baeldung.com).

System Stubs [originated](History.md) as a fork of System Lambda,
originally by Stefan Birkner, and is a partial rewrite and refactor of it. It has diverged in implementation
from the original, but largely retains compatibility.

It is divided into:

- `system-stubs-core` - can be used stand-alone to stub system resources around test code
Expand All @@ -22,17 +39,31 @@ It is divided into:
- [`system-stubs-jupiter`](system-stubs-jupiter/README.md) - a JUnit 5 extension that automatically injects
System Stubs into JUnit 5 tests.

System Stubs [originated](History.md) as a fork of System Lambda.

## Installation

### Dependencies

This library now depends heavily on `mockito-inline`. This is to
avoid the Illegal Reflective Access from previous attempts to override
`System.getenv`. To use this library, you need a version of `mockito-inline`
of `3.12.4` or later.

```xml
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>3.12.4</version>
<scope>test</scope>
</dependency>
```

### Core

```xml
<dependency>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-core</artifactId>
<version>1.2.0</version>
<version>2.0.0</version>
</dependency>
```

Expand All @@ -42,7 +73,7 @@ System Stubs [originated](History.md) as a fork of System Lambda.
<dependency>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-junit4</artifactId>
<version>1.2.0</version>
<version>2.0.0</version>
</dependency>
```

Expand All @@ -52,7 +83,7 @@ System Stubs [originated](History.md) as a fork of System Lambda.
<dependency>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-jupiter</artifactId>
<version>1.2.0</version>
<version>2.0.0</version>
</dependency>
```

Expand Down Expand Up @@ -749,6 +780,20 @@ new SecurityManagerStub(otherManager)

This is used internally by the stubbing for `System.exit`.

## Thread Safety

**⚠ WARNING: Don't Break Your Parallel Tests!**

If you are using System Stubs alongside concurrent test execution, then you MUST ensure that
the concurrency is achieved by forking multiple JVMs to run different test classes,
rather than running multiple threads within the same JVMs executing tests in parallel.

This is because many of the stubs modify global system properties which would bleed to
other tests in the same JVM runtime and **cannot** be localised to just the current test.

Build tools allow the test runner to fork separate processes for running subsets of the
test classes, and this is the only safe way to use System Stubs with concurrent testing.

## Contributing

You have two options if you have a feature request, found a bug or
Expand All @@ -759,8 +804,10 @@ simply have a question.

## Development Guide

System Stubs is built with [Maven](http://maven.apache.org/). If you
want to contribute code then
System Stubs is built with [Maven](http://maven.apache.org/). **It requires JDK8
to build** as it depends on some deprecated system class functions.

If you want to contribute code then:

* Please write a test for your change.
* Ensure that you didn't break the build by running `mvnw test`.
Expand Down
4 changes: 4 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ version: '{build}'
image:
- Visual Studio 2015
- Ubuntu
stack: jdk 8
cache:
- /home/appveyor/.m2
- C:\Users\appveyor\.m2
build_script:
- ./mvnw clean package -DskipTests
test_script:
Expand Down
18 changes: 12 additions & 6 deletions mvnw
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@

if [ -z "$MAVEN_SKIP_RC" ] ; then

if [ -f /usr/local/etc/mavenrc ] ; then
. /usr/local/etc/mavenrc
fi

if [ -f /etc/mavenrc ] ; then
. /etc/mavenrc
fi
Expand Down Expand Up @@ -145,7 +149,7 @@ if [ -z "$JAVACMD" ] ; then
JAVACMD="$JAVA_HOME/bin/java"
fi
else
JAVACMD="`which java`"
JAVACMD="`\\unset -f command; \\command -v java`"
fi
fi

Expand Down Expand Up @@ -212,9 +216,9 @@ else
echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..."
fi
if [ -n "$MVNW_REPOURL" ]; then
jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
jarUrl="$MVNW_REPOURL/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar"
else
jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
jarUrl="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar"
fi
while IFS="=" read key value; do
case "$key" in (wrapperUrl) jarUrl="$value"; break ;;
Expand All @@ -233,9 +237,9 @@ else
echo "Found wget ... using wget"
fi
if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then
wget "$jarUrl" -O "$wrapperJarPath"
wget "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath"
else
wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath"
wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath"
fi
elif command -v curl > /dev/null; then
if [ "$MVNW_VERBOSE" = true ]; then
Expand Down Expand Up @@ -305,6 +309,8 @@ WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain

exec "$JAVACMD" \
$MAVEN_OPTS \
$MAVEN_DEBUG_OPTS \
-classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \
"-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
"-Dmaven.home=${M2_HOME}" \
"-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@"

0 comments on commit ede7d20

Please sign in to comment.