All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.junitpioneer.jupiter.RestoreSystemProperties Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2016-2023 the original author or authors.
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v2.0 which
 * accompanies this distribution and is available at
 *
 * http://www.eclipse.org/legal/epl-v20.html
 */

package org.junitpioneer.jupiter;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.junit.jupiter.api.extension.ExtendWith;

/**
 * {@code @RestoreSystemProperties} is a JUnit Jupiter extension to restore the entire set of
 * system properties to the original value, or the value of the higher-level container, after the
 * annotated element is complete.
 *
 * 

Use this annotation when there is a need programmatically modify system properties in a test * method or in {@code @BeforeAll} / {@code @BeforeEach} blocks. * To simply set or clear a system property, consider {@link SetSystemProperty @SetSystemProperty} or * {@link ClearSystemProperty @ClearSystemProperty} instead.

* *

{@code RestoreSystemProperties} can be used on the method and on the class level. * When placed on a test method, a snapshot of system properties is stored prior to that test. * The snapshot is created before any {@code @BeforeEach} blocks in scope and before any * {@link SetSystemProperty @SetSystemProperty} or {@link ClearSystemProperty @ClearSystemProperty} * annotations on that method. After the test, system properties are restored from the * snapshot after any {@code @AfterEach} have completed. * *

When placed on a test class, a snapshot of system properties is stored prior to any * {@code @BeforeAll} blocks in scope and before any {@link SetSystemProperty @SetSystemProperty} * or {@link ClearSystemProperty @ClearSystemProperty} annotations on that class. * After the test class completes, system properties are restored from the snapshot after any * {@code @AfterAll} blocks have completed. * In addition, a class level annotation is inherited by each test method just as if each one was * annotated with {@code RestoreSystemProperties}. * *

During * parallel test execution, * all tests annotated with {@link RestoreSystemProperties}, {@link SetSystemProperty}, * {@link ReadsSystemProperty}, and {@link WritesSystemProperty} * are scheduled in a way that guarantees correctness under mutation of shared global state.

* *

For more details and examples, see * the documentation on * @ClearSystemProperty, @SetSystemProperty, and @RestoreSystemProperties.

* *

Note: System properties are normally just a hashmap of strings, however, it is * technically possible to store non-string values and create nested {@code Properties} with inherited / * default values. Within the context of an element annotated with {@link RestoreSystemProperties}, * non-String values are not preserved and the structure of nested defaults are flattened. * After the annotated context is exited, the original Properties object is restored with * all its potential (non-standard) richness.

* * @since 2.0.0 */ @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.METHOD, ElementType.TYPE }) @Inherited @WritesSystemProperty @ExtendWith(SystemPropertyExtension.class) public @interface RestoreSystemProperties { }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy