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); KeyProvider kp = get(uri, conf); if (kp != null) { result.add(kp); } else { 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 KeyProvider get(URI uri, Configuration conf); static final String KEY_PROVIDER_PATH; }
@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()); } }