replace legacy JUnit 4 rule for system mock by custom helper

This commit is contained in:
Stefan Kalscheuer 2019-10-20 13:48:41 +02:00
parent 596a097707
commit 9fedb3f88b
5 changed files with 62 additions and 30 deletions

View File

@ -137,12 +137,6 @@
<version>2.1</version> <version>2.1</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.17.2</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId> <artifactId>mockito-core</artifactId>

View File

@ -19,11 +19,9 @@ package de.stklcode.jvault.connector.builder;
import de.stklcode.jvault.connector.HTTPVaultConnector; import de.stklcode.jvault.connector.HTTPVaultConnector;
import de.stklcode.jvault.connector.exception.TlsException; import de.stklcode.jvault.connector.exception.TlsException;
import de.stklcode.jvault.connector.exception.VaultConnectorException; import de.stklcode.jvault.connector.exception.VaultConnectorException;
import org.junit.Rule; import de.stklcode.jvault.connector.test.EnvironmentMock;
import org.junit.contrib.java.lang.system.EnvironmentVariables;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -40,7 +38,6 @@ import static org.junit.jupiter.api.Assertions.fail;
* @author Stefan Kalscheuer * @author Stefan Kalscheuer
* @since 0.8.0 * @since 0.8.0
*/ */
@EnableRuleMigrationSupport
public class HTTPVaultConnectorBuilderTest { public class HTTPVaultConnectorBuilderTest {
private static final String VAULT_ADDR = "https://localhost:8201"; private static final String VAULT_ADDR = "https://localhost:8201";
private static final Integer VAULT_MAX_RETRIES = 13; private static final Integer VAULT_MAX_RETRIES = 13;
@ -49,9 +46,6 @@ public class HTTPVaultConnectorBuilderTest {
@TempDir @TempDir
File tempDir; File tempDir;
@Rule
public final EnvironmentVariables environment = new EnvironmentVariables();
/** /**
* Test building from environment variables * Test building from environment variables
*/ */
@ -112,10 +106,10 @@ public class HTTPVaultConnectorBuilderTest {
} }
private void setenv(String vault_addr, String vault_cacert, String vault_max_retries, String vault_token) { private void setenv(String vault_addr, String vault_cacert, String vault_max_retries, String vault_token) {
environment.set("VAULT_ADDR", vault_addr); EnvironmentMock.setenv("VAULT_ADDR", vault_addr);
environment.set("VAULT_CACERT", vault_cacert); EnvironmentMock.setenv("VAULT_CACERT", vault_cacert);
environment.set("VAULT_MAX_RETRIES", vault_max_retries); EnvironmentMock.setenv("VAULT_MAX_RETRIES", vault_max_retries);
environment.set("VAULT_TOKEN", vault_token); EnvironmentMock.setenv("VAULT_TOKEN", vault_token);
} }
private Object getRequestHelperPrivate(HTTPVaultConnector connector, String fieldName) throws NoSuchFieldException, IllegalAccessException { private Object getRequestHelperPrivate(HTTPVaultConnector connector, String fieldName) throws NoSuchFieldException, IllegalAccessException {

View File

@ -19,11 +19,9 @@ package de.stklcode.jvault.connector.factory;
import de.stklcode.jvault.connector.HTTPVaultConnector; import de.stklcode.jvault.connector.HTTPVaultConnector;
import de.stklcode.jvault.connector.exception.TlsException; import de.stklcode.jvault.connector.exception.TlsException;
import de.stklcode.jvault.connector.exception.VaultConnectorException; import de.stklcode.jvault.connector.exception.VaultConnectorException;
import org.junit.Rule; import de.stklcode.jvault.connector.test.EnvironmentMock;
import org.junit.contrib.java.lang.system.EnvironmentVariables;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -40,7 +38,6 @@ import static org.junit.jupiter.api.Assertions.fail;
* @author Stefan Kalscheuer * @author Stefan Kalscheuer
* @since 0.6.0 * @since 0.6.0
*/ */
@EnableRuleMigrationSupport
public class HTTPVaultConnectorFactoryTest { public class HTTPVaultConnectorFactoryTest {
private static String VAULT_ADDR = "https://localhost:8201"; private static String VAULT_ADDR = "https://localhost:8201";
private static Integer VAULT_MAX_RETRIES = 13; private static Integer VAULT_MAX_RETRIES = 13;
@ -49,9 +46,6 @@ public class HTTPVaultConnectorFactoryTest {
@TempDir @TempDir
File tempDir; File tempDir;
@Rule
public final EnvironmentVariables environment = new EnvironmentVariables();
/** /**
* Test building from environment variables * Test building from environment variables
*/ */
@ -112,10 +106,10 @@ public class HTTPVaultConnectorFactoryTest {
} }
private void setenv(String vault_addr, String vault_cacert, String vault_max_retries, String vault_token) { private void setenv(String vault_addr, String vault_cacert, String vault_max_retries, String vault_token) {
environment.set("VAULT_ADDR", vault_addr); EnvironmentMock.setenv("VAULT_ADDR", vault_addr);
environment.set("VAULT_CACERT", vault_cacert); EnvironmentMock.setenv("VAULT_CACERT", vault_cacert);
environment.set("VAULT_MAX_RETRIES", vault_max_retries); EnvironmentMock.setenv("VAULT_MAX_RETRIES", vault_max_retries);
environment.set("VAULT_TOKEN", vault_token); EnvironmentMock.setenv("VAULT_TOKEN", vault_token);
} }
private Object getRequestHelperPrivate(HTTPVaultConnector connector, String fieldName) throws NoSuchFieldException, IllegalAccessException { private Object getRequestHelperPrivate(HTTPVaultConnector connector, String fieldName) throws NoSuchFieldException, IllegalAccessException {

View File

@ -26,7 +26,7 @@ import java.util.Map;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
import static org.junit.Assert.fail; import static org.junit.jupiter.api.Assertions.fail;
/** /**
* JUnit Test for {@link SecretListResponse} model. * JUnit Test for {@link SecretListResponse} model.
@ -50,7 +50,6 @@ public class SecretListResponseTest {
* @throws InvalidResponseException Should not occur * @throws InvalidResponseException Should not occur
*/ */
@Test @Test
@SuppressWarnings("unchecked")
public void getKeysTest() throws InvalidResponseException { public void getKeysTest() throws InvalidResponseException {
// Create empty Object. // Create empty Object.
SecretListResponse res = new SecretListResponse(); SecretListResponse res = new SecretListResponse();

View File

@ -0,0 +1,51 @@
/*
* Copyright 2016-2019 Stefan Kalscheuer
*
* Licensed 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.
*/
package de.stklcode.jvault.connector.test;
import java.lang.reflect.Field;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.fail;
/**
* Test helper to modify system environment.
*
* @author Stefan Kalscheuer
*/
@SuppressWarnings("unchecked")
public class EnvironmentMock {
private static Map<String, String> environment;
static {
try {
Map<String, String> originalEnv = System.getenv();
Field mapField = originalEnv.getClass().getDeclaredField("m");
mapField.setAccessible(true);
environment = (Map<String, String>) mapField.get(originalEnv);
} catch (NoSuchFieldException | IllegalAccessException | ClassCastException e) {
fail("Failed to intercept unmodifiable system environment");
}
}
public static void setenv(String key, String value) {
if (value != null) {
environment.put(key, value);
} else {
environment.remove(key);
}
}
}