Skip to content

Commit

Permalink
Unit Test Setup with @BeforeClass notation (#680)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiazhvera authored Sep 13, 2023
1 parent 8bd267a commit c72d878
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions src/test/java/software/amazon/awssdk/crt/test/CrtTestFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import software.amazon.awssdk.crt.auth.credentials.DefaultChainCredentialsProvider;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.After;
import org.junit.Assume;

Expand All @@ -33,18 +34,16 @@ public final CrtTestContext getContext() {
return context;
}

private void SetPropertyFromEnv(String name){
private static void SetPropertyFromEnv(String name) {
String propertyValue = System.getenv(name);
if (propertyValue != null){
if (propertyValue != null) {
System.setProperty(name, propertyValue);
}
}

// Setup System properties from environment variables set by builder for use by unit tests.
private void SetupTestProperties(){
// Indicate that the system properties have been setup
System.setProperty("are.test.properties.setup", "true");

// Setup System properties from environment variables set by builder for use by
// unit tests.
private static void SetupTestProperties() {
SetPropertyFromEnv("AWS_TEST_IS_CI");
SetPropertyFromEnv("AWS_TEST_MQTT311_ROOT_CA");
SetPropertyFromEnv("ENDPOINT");
Expand All @@ -54,7 +53,7 @@ private void SetupTestProperties(){
SetPropertyFromEnv("AWS_TEST_MQTT311_COGNITO_ENDPOINT");
SetPropertyFromEnv("AWS_TEST_MQTT311_COGNITO_IDENTITY");

//Proxy
// Proxy
SetPropertyFromEnv("AWS_TEST_HTTP_PROXY_HOST");
SetPropertyFromEnv("AWS_TEST_HTTP_PROXY_PORT");
SetPropertyFromEnv("AWS_TEST_HTTPS_PROXY_HOST");
Expand Down Expand Up @@ -197,16 +196,18 @@ private void SetupTestProperties(){
SetPropertyFromEnv("AWS_TEST_MQTT5_IOT_CORE_WINDOWS_PFX_CERT_NO_PASS");
SetPropertyFromEnv("AWS_TEST_MQTT5_IOT_CORE_WINDOWS_CERT_STORE");

// MQTT5 Custom Key Ops (so we don't have to make a new file just for a single test)
// MQTT5 Custom Key Ops (so we don't have to make a new file just for a single
// test)
SetPropertyFromEnv("AWS_TEST_MQTT5_CUSTOM_KEY_OPS_CERT");
SetPropertyFromEnv("AWS_TEST_MQTT5_CUSTOM_KEY_OPS_KEY");

SetPropertyFromEnv("AWS_TEST_BASIC_AUTH_USERNAME");
SetPropertyFromEnv("AWS_TEST_BASIC_AUTH_PASSWORD");
}

@Before
public void setup() {
/* The function will be run once before any of the test methods in the class */
@BeforeClass
public static void setupOnce() {
// We only want to see the CRT logs if the test fails.
// Surefire has a redirectTestOutputToFile option, but that doesn't
// capture what the CRT logger writes to stdout or stderr.
Expand All @@ -217,20 +218,21 @@ public void setup() {
if (System.getProperty("aws.crt.aws_trace_log_per_test") != null) {
Log.initLoggingToFile(Log.LogLevel.Trace, "log.txt");
}
SetupTestProperties();
}

/* The setup function will be run before every test */
@Before
public void setup() {
Log.log(Log.LogLevel.Debug, LogSubject.JavaCrtGeneral, "CrtTestFixture setup begin");

// TODO this CrtTestContext should be removed as we are using System Properties for tests now.
// TODO this CrtTestContext should be removed as we are using System Properties
// for tests now.
context = new CrtTestContext();
// System properties for tests only need to be setup once
if (System.getProperty("are.test.properties.setup") != "true"){
CrtPlatform platform = CRT.getPlatformImpl();
if (platform != null) {
platform.testSetup(context);
} else {
SetupTestProperties();
}
CrtPlatform platform = CRT.getPlatformImpl();
if (platform != null) {
platform.testSetup(context);
}

Log.log(Log.LogLevel.Debug, LogSubject.JavaCrtGeneral, "CrtTestFixture setup end");
}

Expand Down

0 comments on commit c72d878

Please sign in to comment.