@@ -21,8 +21,8 @@ public String ReturnStringFunction() {
2121
2222 @ Test
2323 public void legacyPlaintextTransportStillWorks () throws Exception {
24- System . setProperty ( "azure.functions.worker.java.skip.testing" , "true" );
25- try ( FunctionsTestHost host = new FunctionsTestHost ()) {
24+ try ( SkipTestingScope ignored = SkipTestingScope . enable ( );
25+ FunctionsTestHost host = new FunctionsTestHost ()) {
2626 InvocationResponse response = this .invokeReturnString (host , "plaintext-function" , "plaintext-request" );
2727
2828 assertEquals (TypedData .DataCase .STRING , response .getReturnValue ().getDataCase ());
@@ -32,8 +32,8 @@ public void legacyPlaintextTransportStillWorks() throws Exception {
3232
3333 @ Test
3434 public void trustedHttpsFunctionsUriConnectsToTlsHost () throws Exception {
35- System . setProperty ( "azure.functions.worker.java.skip.testing" , "true" );
36- try ( TrustStoreScope ignored = TrustStoreScope .use (TRUSTSTORE_RESOURCE , TRUSTSTORE_PASSWORD , "PKCS12" );
35+ try ( SkipTestingScope ignored = SkipTestingScope . enable ( );
36+ TrustStoreScope ignoredTrustStore = TrustStoreScope .use (TRUSTSTORE_RESOURCE , TRUSTSTORE_PASSWORD , "PKCS12" );
3737 FunctionsTestHost host = new FunctionsTestHost (FunctionsTestHost .ServerTransport .TLS , FunctionsTestHost .ClientTransport .HTTPS )) {
3838 InvocationResponse response = this .invokeReturnString (host , "tls-function" , "tls-request" );
3939
@@ -44,12 +44,11 @@ public void trustedHttpsFunctionsUriConnectsToTlsHost() throws Exception {
4444
4545 @ Test
4646 public void httpsFunctionsUriDoesNotDowngradeToPlaintextWhenTlsFails () {
47- System .setProperty ("azure.functions.worker.java.skip.testing" , "true" );
48- ExecutionException exception = assertThrows (ExecutionException .class ,
49- () -> {
50- try (FunctionsTestHost ignored = new FunctionsTestHost (FunctionsTestHost .ServerTransport .PLAINTEXT , FunctionsTestHost .ClientTransport .HTTPS )) {
51- }
52- });
47+ ExecutionException exception = assertThrows (ExecutionException .class , () -> {
48+ try (SkipTestingScope ignored = SkipTestingScope .enable ();
49+ FunctionsTestHost ignoredHost = new FunctionsTestHost (FunctionsTestHost .ServerTransport .PLAINTEXT , FunctionsTestHost .ClientTransport .HTTPS )) {
50+ }
51+ });
5352
5453 assertTrue (hasCause (exception , SSLException .class ), "Expected TLS failure but got: " + exception );
5554 }
@@ -115,4 +114,28 @@ private static void restore(String key, String value) {
115114 }
116115 }
117116 }
117+
118+ private static final class SkipTestingScope implements AutoCloseable {
119+ private static final String SKIP_TESTING_PROPERTY = "azure.functions.worker.java.skip.testing" ;
120+ private final String originalValue ;
121+
122+ private SkipTestingScope (String originalValue ) {
123+ this .originalValue = originalValue ;
124+ }
125+
126+ static SkipTestingScope enable () {
127+ String originalValue = System .getProperty (SKIP_TESTING_PROPERTY );
128+ System .setProperty (SKIP_TESTING_PROPERTY , "true" );
129+ return new SkipTestingScope (originalValue );
130+ }
131+
132+ @ Override
133+ public void close () {
134+ if (this .originalValue == null ) {
135+ System .clearProperty (SKIP_TESTING_PROPERTY );
136+ } else {
137+ System .setProperty (SKIP_TESTING_PROPERTY , this .originalValue );
138+ }
139+ }
140+ }
118141}
0 commit comments