Skip to content

Commit 6723f14

Browse files
Merge pull request #18688 from constantinurs/bael7458-change-alias
BAEL-7458: Add test for changing alias name in a keystore
2 parents ca1d5d8 + f9e38db commit 6723f14

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.keystorealias;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.security.Key;
6+
import java.security.KeyStore;
7+
import java.security.cert.Certificate;
8+
9+
import org.junit.jupiter.api.Test;
10+
11+
public class KeystoreChangeCertificateNameAliasUnitTest {
12+
private static final String KEYSTORE = "my-keystore.jks";
13+
private static final String PWD = "storepw@1";
14+
private static final String OLD_ALIAS = "baeldung";
15+
private static final String NEW_ALIAS = "baeldung.com";
16+
17+
@Test
18+
void whenAliasIsRenamed_thenNewAliasIsCreated() throws Exception {
19+
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
20+
keystore.load(getClass().getResourceAsStream(KEYSTORE), PWD.toCharArray());
21+
22+
assertThat(keystore.containsAlias(OLD_ALIAS)).isTrue();
23+
assertThat(keystore.containsAlias(NEW_ALIAS)).isFalse();
24+
25+
Key key = keystore.getKey(OLD_ALIAS, PWD.toCharArray());
26+
Certificate[] certificateChain = keystore.getCertificateChain(OLD_ALIAS);
27+
28+
keystore.deleteEntry(OLD_ALIAS);
29+
keystore.setKeyEntry(NEW_ALIAS, key, PWD.toCharArray(), certificateChain);
30+
31+
assertThat(keystore.containsAlias(OLD_ALIAS)).isFalse();
32+
assertThat(keystore.containsAlias(NEW_ALIAS)).isTrue();
33+
}
34+
}

0 commit comments

Comments
 (0)