Skip to content

Commit

Permalink
test: avoid concurrent executions of tests that update environment an…
Browse files Browse the repository at this point in the history
…d system properties

JUnit's @isolated is helpful to run certain tests in isolation.
  • Loading branch information
vlsi committed Jul 19, 2022
1 parent aa5758a commit b452d8c
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
1 change: 1 addition & 0 deletions packaging/rpm/postgresql-jdbc.spec.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ find -type f \( -name "*.jar" -or -name "*.class" \) | xargs rm -f
rm src/test/java/org/postgresql/test/jdbc2/DriverTest.java \
src/test/java/org/postgresql/util/OSUtilTest.java \
src/test/java/org/postgresql/util/PGPropertyPasswordParserTest.java \
src/test/java/org/postgresql/util/StubEnvironmentAndProperties.java \
src/test/java/org/postgresql/util/PGPropertyServiceParserTest.java

# compat symlink: requested by dtardon (libreoffice), reverts part of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
import org.postgresql.PGEnvironment;
import org.postgresql.PGProperty;
import org.postgresql.test.TestUtil;
import org.postgresql.util.StubEnvironmentAndProperties;
import org.postgresql.util.URLCoder;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import uk.org.webcompere.systemstubs.properties.SystemProperties;
import uk.org.webcompere.systemstubs.resource.Resources;

Expand All @@ -43,7 +42,7 @@
* Tests the dynamically created class org.postgresql.Driver
*
*/
@ExtendWith(SystemStubsExtension.class)
@StubEnvironmentAndProperties
public class DriverTest {

@Test
Expand Down
4 changes: 1 addition & 3 deletions pgjdbc/src/test/java/org/postgresql/util/OSUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import uk.org.webcompere.systemstubs.properties.SystemProperties;
import uk.org.webcompere.systemstubs.resource.Resources;

import java.io.File;

@ExtendWith(SystemStubsExtension.class)
@StubEnvironmentAndProperties
class OSUtilTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*
* @author Marek Läll
*/
@StubEnvironmentAndProperties
class PGPropertyPasswordParserTest {

// "org.postgresql.pgpassfile" : missing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
import org.postgresql.PGEnvironment;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import uk.org.webcompere.systemstubs.properties.SystemProperties;
import uk.org.webcompere.systemstubs.resource.Resources;

Expand All @@ -30,7 +28,7 @@
*
* @author Marek Läll
*/
@ExtendWith(SystemStubsExtension.class)
@StubEnvironmentAndProperties
class PGPropertyServiceParserTest {

// "org.postgresql.pgservicefile" : missing
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2022, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/

package org.postgresql.util;

import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.parallel.Isolated;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;

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

/**
* This annotation is used to mark a test method as a test that should be run with stubbing system
* calls like {@code System#getProperty} and {@code System#getenv}.
* <p>The tests should be run in isolation to prevent concurrent modification of properties and
* the environment.</p>
*/
@Isolated
@ExtendWith(SystemStubsExtension.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface StubEnvironmentAndProperties {
}

0 comments on commit b452d8c

Please sign in to comment.