XmlCredentialsUtil { public static Collection<Credentials> getCredentials(final XMLConfiguration xmlConfiguration, final String rootElement, final String urlElement, final String userNameElement, final String passwordElement, final String tokenElement, final String apiVersionElement) { IntegrityUtil.ensure(xmlConfiguration, "Must provide an XMLConfiguration!"); IntegrityUtil.ensure(rootElement, "Must provide a root element!"); IntegrityUtil.ensure(urlElement, "Must provide a url element!"); IntegrityUtil.ensure(userNameElement, "Must provide a user name element!"); IntegrityUtil.ensure(passwordElement, "Must provide a password element!"); IntegrityUtil.ensure(tokenElement, "Must provide a token element!"); IntegrityUtil.ensure(apiVersionElement, "Must provide an api version element!"); final List<HierarchicalConfiguration> configurationList = xmlConfiguration.configurationsAt(rootElement); IntegrityUtil.ensure(configurationList, "No configuration found using [" + rootElement + "]"); final List<Credentials> retVal = new ArrayList(configurationList.size()); for (final HierarchicalConfiguration hierarchicalConfiguration : configurationList) { retVal.add(convertToCredentials(hierarchicalConfiguration, xmlConfiguration.getProperty(urlElement), userNameElement, passwordElement, tokenElement, xmlConfiguration.getProperty(apiVersionElement))); } return retVal; }  static XMLConfiguration createXmlConfiguration(final String fileName); static XMLConfiguration createXmlConfiguration(final File file); static XMLConfiguration createXmlConfiguration(final URL url); static Credentials convertToCredentials(final HierarchicalConfiguration hierarchicalConfiguration, final Object url, final String userNameElement, final String passwordElement, final String tokenElement, final Object apiVersion); static Collection<Credentials> getCredentials(final XMLConfiguration xmlConfiguration, final String rootElement, final String urlElement, final String userNameElement, final String passwordElement, final String tokenElement, final String apiVersionElement); static Collection<Credentials> getCredentials(final String fileName, final String rootElement, final String urlElement, final String userNameElement, final String passwordElement, final String tokenElement, final String apiVersionElement); static Collection<Credentials> getCredentials(final File file, final String rootElement, final String urlElement, final String userNameElement, final String passwordElement, final String tokenElement, final String apiVersionElement); static Collection<Credentials> getCredentials(final URL url, final String rootElement, final String urlElement, final String userNameElement, final String passwordElement, final String tokenElement, final String apiVersionElement);  }
@Test public void test_singleUserDefinedXmlConfiguration() { final Collection<Credentials> credsCollection = XmlCredentialsUtil.getCredentials(getClass().getClassLoader().getResource("singleUserDefinedXmlConfiguration.xml"), "org", "org.url", "user", "password", "token", "org.api"); final Credentials[] creds = credsCollection.toArray(new Credentials[0]); Assert.assertEquals("Should have 1 element", 1, creds.length); Assert.assertEquals("Should be same url", "http: Assert.assertEquals("Should be same user name", "theuser", creds[0].getUserName()); Assert.assertEquals("Should be same user name", "the password", creds[0].getPassword()); Assert.assertEquals("Should be same user name", "the token", creds[0].getToken()); Assert.assertEquals("Should be same user name", "30.0", creds[0].getApiVersion()); }
@Test public void test_multipleUserDefinedXmlConfiguration() { final Collection<Credentials> credsCollection = XmlCredentialsUtil.getCredentials(getClass().getClassLoader().getResource("mutlipleUserDefinedXmlConfiguration.xml"), "orgs.org", "orgs.url", "user", "password", "token", "orgs.api"); final Credentials[] creds = credsCollection.toArray(new Credentials[0]); Assert.assertEquals("Should have 3 elements", 3, creds.length); Assert.assertEquals("Should be same url", "http: Assert.assertEquals("Should be same user name", "theuser1", creds[0].getUserName()); Assert.assertEquals("Should be same user name", "the password1", creds[0].getPassword()); Assert.assertEquals("Should be same user name", "the token1", creds[0].getToken()); Assert.assertEquals("Should be same user name", "30.0", creds[0].getApiVersion()); Assert.assertEquals("Should be same url", "http: Assert.assertEquals("Should be same user name", "theuser2", creds[1].getUserName()); Assert.assertEquals("Should be same user name", "the password2", creds[1].getPassword()); Assert.assertEquals("Should be same user name", "the token2", creds[1].getToken()); Assert.assertEquals("Should be same user name", "30.0", creds[1].getApiVersion()); Assert.assertEquals("Should be same url", "http: Assert.assertEquals("Should be same user name", "theuser3", creds[2].getUserName()); Assert.assertEquals("Should be same user name", "the password3", creds[2].getPassword()); Assert.assertEquals("Should be same user name", "the token3", creds[2].getToken()); Assert.assertEquals("Should be same user name", "30.0", creds[2].getApiVersion()); }