/** * If <code>propertyValue</code> is not null or empty, returns the result * of <code>KeyStoreFileType.fromPropertyValue(propertyValue)</code>. Else, * returns the result of <code>KeyStoreFileType.fromFileName(filename)</code>. * @param propertyValue property value describing the KeyStoreFileType, or * null/empty to auto-detect the type from the file * name. * @param filename file name of the key store file. The file extension is * used to auto-detect the KeyStoreFileType when * <code>propertyValue</code> is null or empty. * @return a KeyStoreFileType. * @throws IllegalArgumentException if <code>propertyValue</code> is not * one of "JKS", "PEM", or empty/null. * @throws IllegalArgumentException if <code>propertyValue</code>is empty * or null and the type could not be determined from the file name. */ public static KeyStoreFileType fromPropertyValueOrFileName(String propertyValue, String filename) { KeyStoreFileType result = KeyStoreFileType.fromPropertyValue(propertyValue); if (result == null) { result = KeyStoreFileType.fromFilename(filename); } return result; } }
@Test(expected = IllegalArgumentException.class) public void testFromFilenameThrowsOnBadFileExtension() { KeyStoreFileType.fromFilename("prod.key"); }
@Test public void testFromFilename() { Assert.assertEquals(KeyStoreFileType.JKS, KeyStoreFileType.fromFilename("mykey.jks")); Assert.assertEquals(KeyStoreFileType.JKS, KeyStoreFileType.fromFilename("/path/to/key/dir/mykey.jks")); Assert.assertEquals(KeyStoreFileType.PEM, KeyStoreFileType.fromFilename("mykey.pem")); Assert.assertEquals(KeyStoreFileType.PEM, KeyStoreFileType.fromFilename("/path/to/key/dir/mykey.pem")); }