/** * {@inheritDoc} */ public void start(BundleContext context) throws Exception { Dictionary<String, Object> props = new Hashtable<>(); props.put(Constants.SERVICE_PID, ConnectionFactoryImpl.FACTORY_PID); props.put("impl.type", "jdk"); context.registerService(new String[]{ ConnectionFactory.class.getName(), ManagedServiceFactory.class.getName() }, new ConnectionFactoryImpl(), props); }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.ConnectionFactoryImpl#createConnection(java.net.URL, org.osgi.service.useradmin.User)}. */ @Test(expectedExceptions = IllegalArgumentException.class) public void testCreateConnectionNullUserFail() throws Exception { new ConnectionFactoryImpl().createConnection(new URL("file:///tmp/foo"), null); }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.ConnectionFactoryImpl#createConnection(java.net.URL)}. */ @Test(expectedExceptions = IllegalArgumentException.class) public void testCreateConnectionNullUrlFail() throws Exception { new ConnectionFactoryImpl().createConnection(null); }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.ConnectionFactoryImpl#createConnection(java.net.URL, org.osgi.service.useradmin.User)}. */ @Test() public void testCreateConnectionOk() throws Exception { URLConnection conn = new ConnectionFactoryImpl().createConnection(new URL("file:///tmp/foo")); assert conn != null : "Expected valid connection to be created!"; }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.ConnectionFactoryImpl#getBasicAuthCredentials(UrlCredentials)}. */ @Test() public void testGetBasicAuthCredentialsOk() throws Exception { ConnectionFactoryImpl connFactory = new ConnectionFactoryImpl(); Dictionary<String, ?> props = createBasicAuthConfig(TEST_URL.toExternalForm()); connFactory.updated("pid1", props); UrlCredentials credentials = connFactory.getCredentials(TEST_URL); assert credentials != null : "Expected valid credentials to be found!"; String header = new ConnectionFactoryImpl().getBasicAuthCredentials(credentials.getCredentials()); assert header != null : "Expected valid HTTP header to be returned!"; assert header.equals(header.trim()) : "Expected HTTP header not to contain any leading/trailing whitespace!"; assert "Basic Zm9vOmJhcg==".equals(header) : "Expected HTTP header to be constant!"; }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.ConnectionFactoryImpl#updated(java.lang.String, java.util.Dictionary)}. */ @Test() public void testUpdatedInsertsCredentialsOk() throws Exception { ConnectionFactoryImpl connFactory = new ConnectionFactoryImpl(); UrlCredentials credentials = connFactory.getCredentials(TEST_URL); assert credentials == null : "Expected no credentials to be found!"; Dictionary<String, ?> props = createBasicAuthConfig(TEST_URL.toExternalForm()); connFactory.updated("pid1", props); credentials = connFactory.getCredentials(TEST_URL); assert credentials != null : "Expected valid credentials to be found!"; }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.ConnectionFactoryImpl#deleted(java.lang.String)}. */ @Test() public void testDeleted() throws Exception { ConnectionFactoryImpl connFactory = new ConnectionFactoryImpl(); Dictionary<String, ?> props = createBasicAuthConfig(TEST_URL.toExternalForm()); connFactory.updated("pid1", props); UrlCredentials credentials = connFactory.getCredentials(TEST_URL); assert credentials != null : "Expected valid credentials to be found!"; connFactory.deleted("pid1"); credentials = connFactory.getCredentials(TEST_URL); assert credentials == null : "Expected no credentials to be found!"; }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.ConnectionFactoryImpl#updated(java.lang.String, java.util.Dictionary)}. */ @Test() public void testUpdatedUpdatesCredentialsOk() throws Exception { ConnectionFactoryImpl connFactory = new ConnectionFactoryImpl(); Dictionary<String, Object> props = createBasicAuthConfig(TEST_URL.toExternalForm()); connFactory.updated("pid1", props); UrlCredentials credentials1 = connFactory.getCredentials(TEST_URL); assert credentials1 != null : "Expected valid credentials to be found!"; URL newURL = new URL("http://localhost:8181/test/"); props.put(UrlCredentialsFactory.KEY_AUTH_BASE_URL, newURL.toExternalForm()); connFactory.updated("pid1", props); UrlCredentials credentials2 = connFactory.getCredentials(TEST_URL); assert credentials2 == null : "Expected no credentials to be found!"; credentials2 = connFactory.getCredentials(newURL); assert credentials2 != null : "Expected valid credentials to be found!"; assert credentials1 != credentials2 && !credentials1.equals(credentials2) : "Expected not the same credentials to be returned!"; }