Skip to content

Commit b452d8c

Browse files
committed
test: avoid concurrent executions of tests that update environment and system properties
JUnit's @isolated is helpful to run certain tests in isolation.
1 parent aa5758a commit b452d8c

File tree

6 files changed

+34
-9
lines changed

6 files changed

+34
-9
lines changed

packaging/rpm/postgresql-jdbc.spec.tpl

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ find -type f \( -name "*.jar" -or -name "*.class" \) | xargs rm -f
112112
rm src/test/java/org/postgresql/test/jdbc2/DriverTest.java \
113113
src/test/java/org/postgresql/util/OSUtilTest.java \
114114
src/test/java/org/postgresql/util/PGPropertyPasswordParserTest.java \
115+
src/test/java/org/postgresql/util/StubEnvironmentAndProperties.java \
115116
src/test/java/org/postgresql/util/PGPropertyServiceParserTest.java
116117

117118
# compat symlink: requested by dtardon (libreoffice), reverts part of

pgjdbc/src/test/java/org/postgresql/test/jdbc2/DriverTest.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@
1717
import org.postgresql.PGEnvironment;
1818
import org.postgresql.PGProperty;
1919
import org.postgresql.test.TestUtil;
20+
import org.postgresql.util.StubEnvironmentAndProperties;
2021
import org.postgresql.util.URLCoder;
2122

2223
import org.junit.jupiter.api.Test;
23-
import org.junit.jupiter.api.extension.ExtendWith;
2424
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
25-
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
2625
import uk.org.webcompere.systemstubs.properties.SystemProperties;
2726
import uk.org.webcompere.systemstubs.resource.Resources;
2827

@@ -43,7 +42,7 @@
4342
* Tests the dynamically created class org.postgresql.Driver
4443
*
4544
*/
46-
@ExtendWith(SystemStubsExtension.class)
45+
@StubEnvironmentAndProperties
4746
public class DriverTest {
4847

4948
@Test

pgjdbc/src/test/java/org/postgresql/util/OSUtilTest.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@
88
import static org.junit.jupiter.api.Assertions.assertEquals;
99

1010
import org.junit.jupiter.api.Test;
11-
import org.junit.jupiter.api.extension.ExtendWith;
1211
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
13-
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
1412
import uk.org.webcompere.systemstubs.properties.SystemProperties;
1513
import uk.org.webcompere.systemstubs.resource.Resources;
1614

1715
import java.io.File;
1816

19-
@ExtendWith(SystemStubsExtension.class)
17+
@StubEnvironmentAndProperties
2018
class OSUtilTest {
2119

2220
@Test

pgjdbc/src/test/java/org/postgresql/util/PGPropertyPasswordParserTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*
2727
* @author Marek Läll
2828
*/
29+
@StubEnvironmentAndProperties
2930
class PGPropertyPasswordParserTest {
3031

3132
// "org.postgresql.pgpassfile" : missing

pgjdbc/src/test/java/org/postgresql/util/PGPropertyServiceParserTest.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
import org.postgresql.PGEnvironment;
1414

1515
import org.junit.jupiter.api.Test;
16-
import org.junit.jupiter.api.extension.ExtendWith;
1716
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
18-
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
1917
import uk.org.webcompere.systemstubs.properties.SystemProperties;
2018
import uk.org.webcompere.systemstubs.resource.Resources;
2119

@@ -30,7 +28,7 @@
3028
*
3129
* @author Marek Läll
3230
*/
33-
@ExtendWith(SystemStubsExtension.class)
31+
@StubEnvironmentAndProperties
3432
class PGPropertyServiceParserTest {
3533

3634
// "org.postgresql.pgservicefile" : missing
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2022, PostgreSQL Global Development Group
3+
* See the LICENSE file in the project root for more information.
4+
*/
5+
6+
package org.postgresql.util;
7+
8+
import org.junit.jupiter.api.extension.ExtendWith;
9+
import org.junit.jupiter.api.parallel.Isolated;
10+
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
11+
12+
import java.lang.annotation.ElementType;
13+
import java.lang.annotation.Retention;
14+
import java.lang.annotation.RetentionPolicy;
15+
import java.lang.annotation.Target;
16+
17+
/**
18+
* This annotation is used to mark a test method as a test that should be run with stubbing system
19+
* calls like {@code System#getProperty} and {@code System#getenv}.
20+
* <p>The tests should be run in isolation to prevent concurrent modification of properties and
21+
* the environment.</p>
22+
*/
23+
@Isolated
24+
@ExtendWith(SystemStubsExtension.class)
25+
@Retention(RetentionPolicy.RUNTIME)
26+
@Target({ElementType.METHOD, ElementType.TYPE})
27+
public @interface StubEnvironmentAndProperties {
28+
}

0 commit comments

Comments
 (0)