@@ -1112,7 +1112,8 @@ public void build_withDefaultCertificate_throwsOnTransportInitFailure() {
11121112 @ Test
11131113 public void build_withCustomProvider_throwsOnGetKeyStore ()
11141114 throws IOException , KeyStoreException , CertificateException , NoSuchAlgorithmException {
1115- // Setup a custom provider configured to throw during getKeyStore().
1115+ // Simulate a scenario where the X509Provider fails to load the KeyStore, typically due to an
1116+ // IOException when reading the certificate or private key files.
11161117 KeyStore keyStore = KeyStore .getInstance ("JKS" );
11171118 keyStore .load (null , null );
11181119 TestX509Provider x509Provider = new TestX509Provider (keyStore , "/path/to/certificate.json" );
@@ -1121,19 +1122,28 @@ public void build_withCustomProvider_throwsOnGetKeyStore()
11211122 Map <String , Object > certificateMap = new HashMap <>();
11221123 certificateMap .put ("certificate_config_location" , "/path/to/certificate.json" );
11231124
1124- // Expect RuntimeException from the custom provider during build .
1125+ // Expect RuntimeException because the constructor wraps the IOException .
11251126 RuntimeException exception =
11261127 assertThrows (
11271128 RuntimeException .class ,
11281129 () -> createCredentialsWithCertificate (x509Provider , certificateMap ));
11291130
1130- assertEquals ("Exception on get keystore" , exception .getMessage ());
1131+ // Verify the cause is the expected IOException from the mock.
1132+ assertNotNull (exception .getCause ());
1133+ assertTrue (exception .getCause () instanceof IOException );
1134+ assertEquals ("Simulated IOException on get keystore" , exception .getCause ().getMessage ());
1135+
1136+ // Verify the wrapper exception message
1137+ assertEquals (
1138+ "Failed to initialize IdentityPoolCredentials from certificate source due to an I/O error." ,
1139+ exception .getMessage ());
11311140 }
11321141
11331142 @ Test
11341143 public void build_withCustomProvider_throwsOnGetCertificatePath ()
11351144 throws IOException , KeyStoreException , CertificateException , NoSuchAlgorithmException {
1136- // Setup a custom provider configured to throw during getCertificatePath().
1145+ // Simulate a scenario where the X509Provider cannot access or read the certificate
1146+ // configuration file needed to determine the certificate path, resulting in an IOException.
11371147 KeyStore keyStore = KeyStore .getInstance ("JKS" );
11381148 keyStore .load (null , null );
11391149 TestX509Provider x509Provider = new TestX509Provider (keyStore , "/path/to/certificate.json" );
@@ -1142,13 +1152,21 @@ public void build_withCustomProvider_throwsOnGetCertificatePath()
11421152 Map <String , Object > certificateMap = new HashMap <>();
11431153 certificateMap .put ("certificate_config_location" , "/path/to/certificate.json" );
11441154
1145- // Expect RuntimeException from the custom provider during build .
1155+ // Expect RuntimeException because the constructor wraps the IOException .
11461156 RuntimeException exception =
11471157 assertThrows (
11481158 RuntimeException .class ,
11491159 () -> createCredentialsWithCertificate (x509Provider , certificateMap ));
11501160
1151- assertEquals ("Exception on get certificate path" , exception .getMessage ());
1161+ // Verify the cause is the expected IOException from the mock.
1162+ assertNotNull (exception .getCause ());
1163+ assertTrue (exception .getCause () instanceof IOException );
1164+ assertEquals ("Simulated IOException on certificate path" , exception .getCause ().getMessage ());
1165+
1166+ // Verify the wrapper exception message
1167+ assertEquals (
1168+ "Failed to initialize IdentityPoolCredentials from certificate source due to an I/O error." ,
1169+ exception .getMessage ());
11521170 }
11531171
11541172 private void createCredentialsWithCertificate (
@@ -1261,17 +1279,17 @@ private static class TestX509Provider extends X509Provider {
12611279 }
12621280
12631281 @ Override
1264- public KeyStore getKeyStore () {
1282+ public KeyStore getKeyStore () throws IOException {
12651283 if (shouldThrowOnGetKeyStore ) {
1266- throw new RuntimeException ( "Exception on get keystore" );
1284+ throw new IOException ( "Simulated IOException on get keystore" );
12671285 }
12681286 return keyStore ;
12691287 }
12701288
12711289 @ Override
1272- public String getCertificatePath () {
1290+ public String getCertificatePath () throws IOException {
12731291 if (shouldThrowOnGetCertificatePath ) {
1274- throw new RuntimeException ( "Exception on get certificate path" );
1292+ throw new IOException ( "Simulated IOException on certificate path" );
12751293 }
12761294 return certificatePath ;
12771295 }
0 commit comments