### Question:
KeyProviderFactory { public static List<KeyProvider> getProviders(Configuration conf ) throws IOException { List<KeyProvider> result = new ArrayList<KeyProvider>(); for(String path: conf.getStringCollection(KEY_PROVIDER_PATH)) { try { URI uri = new URI(path); boolean found = false; for(KeyProviderFactory factory: serviceLoader) { KeyProvider kp = factory.createProvider(uri, conf); if (kp != null) { result.add(kp); found = true; break; } } if (!found) { throw new IOException("No KeyProviderFactory for " + uri + " in " + KEY_PROVIDER_PATH); } } catch (URISyntaxException error) { throw new IOException("Bad configuration of " + KEY_PROVIDER_PATH + " at " + path, error); } } return result; }  abstract KeyProvider createProvider(URI providerName,
                                             Configuration conf
                                             ); static List<KeyProvider> getProviders(Configuration conf
                                               ); static final String KEY_PROVIDER_PATH; }### Answer:
@Test public void testFactory() throws Exception { Configuration conf = new Configuration(); conf.set(KeyProviderFactory.KEY_PROVIDER_PATH, UserProvider.SCHEME_NAME + ": JavaKeyStoreProvider.SCHEME_NAME + ": List<KeyProvider> providers = KeyProviderFactory.getProviders(conf); assertEquals(2, providers.size()); assertEquals(UserProvider.class, providers.get(0).getClass()); assertEquals(JavaKeyStoreProvider.class, providers.get(1).getClass()); assertEquals(UserProvider.SCHEME_NAME + ": assertEquals(JavaKeyStoreProvider.SCHEME_NAME + ": providers.get(1).toString()); }
@Test public void testFactoryErrors() throws Exception { Configuration conf = new Configuration(); conf.set(KeyProviderFactory.KEY_PROVIDER_PATH, "unknown: try { List<KeyProvider> providers = KeyProviderFactory.getProviders(conf); assertTrue("should throw!", false); } catch (IOException e) { assertEquals("No KeyProviderFactory for unknown: KeyProviderFactory.KEY_PROVIDER_PATH, e.getMessage()); } }
@Test public void testUriErrors() throws Exception { Configuration conf = new Configuration(); conf.set(KeyProviderFactory.KEY_PROVIDER_PATH, "unkn@own:/x/y"); try { List<KeyProvider> providers = KeyProviderFactory.getProviders(conf); assertTrue("should throw!", false); } catch (IOException e) { assertEquals("Bad configuration of " + KeyProviderFactory.KEY_PROVIDER_PATH + " at unkn@own:/x/y", e.getMessage()); } }
@Test public void testJksProviderPasswordViaConfig() throws Exception { Configuration conf = new Configuration(); final String ourUrl = JavaKeyStoreProvider.SCHEME_NAME + ": File file = new File(tmpDir, "test.jks"); file.delete(); try { conf.set(KeyProviderFactory.KEY_PROVIDER_PATH, ourUrl); conf.set(JavaKeyStoreProvider.KEYSTORE_PASSWORD_FILE_KEY, "javakeystoreprovider.password"); KeyProvider provider = KeyProviderFactory.getProviders(conf).get(0); provider.createKey("key3", new byte[32], KeyProvider.options(conf)); provider.flush(); } catch (Exception ex) { Assert.fail("could not create keystore with password file"); } KeyProvider provider = KeyProviderFactory.getProviders(conf).get(0); Assert.assertNotNull(provider.getCurrentKey("key3")); try { conf.set(JavaKeyStoreProvider.KEYSTORE_PASSWORD_FILE_KEY, "bar"); KeyProviderFactory.getProviders(conf).get(0); Assert.fail("using non existing password file, it should fail"); } catch (IOException ex) { } try { conf.set(JavaKeyStoreProvider.KEYSTORE_PASSWORD_FILE_KEY, "core-site.xml"); KeyProviderFactory.getProviders(conf).get(0); Assert.fail("using different password file, it should fail"); } catch (IOException ex) { } try { conf.unset(JavaKeyStoreProvider.KEYSTORE_PASSWORD_FILE_KEY); KeyProviderFactory.getProviders(conf).get(0); Assert.fail("No password file property, env not set, it should fail"); } catch (IOException ex) { } }