use of com.rabbitmq.client.impl.CredentialsProvider in project rabbitmq-java-client by rabbitmq.
the class ConnectionFactoryTest method customizeCredentialsProvider.
// see https://github.com/rabbitmq/rabbitmq-java-client/pull/350
@Test
public void customizeCredentialsProvider() throws Exception {
final CredentialsProvider provider = mock(CredentialsProvider.class);
final AMQConnection connection = mock(AMQConnection.class);
final AtomicBoolean createCalled = new AtomicBoolean(false);
ConnectionFactory connectionFactory = new ConnectionFactory() {
@Override
protected AMQConnection createConnection(ConnectionParams params, FrameHandler frameHandler, MetricsCollector metricsCollector) {
assertSame(provider, params.getCredentialsProvider());
createCalled.set(true);
return connection;
}
};
connectionFactory.setCredentialsProvider(provider);
connectionFactory.setAutomaticRecoveryEnabled(false);
doNothing().when(connection).start();
Connection returnedConnection = connectionFactory.newConnection();
assertSame(returnedConnection, connection);
assertTrue(createCalled.get());
}
use of com.rabbitmq.client.impl.CredentialsProvider in project rabbitmq-java-client by rabbitmq.
the class ConnectionRecovery method connectionRecoveryRequestsCredentialsAgain.
// See https://github.com/rabbitmq/rabbitmq-java-client/pull/350 .
// We want to request fresh creds when recovering.
@Test
public void connectionRecoveryRequestsCredentialsAgain() throws Exception {
ConnectionFactory cf = buildConnectionFactoryWithRecoveryEnabled(false);
final String username = cf.getUsername();
final String password = cf.getPassword();
final AtomicInteger usernameRequested = new AtomicInteger(0);
final AtomicInteger passwordRequested = new AtomicInteger(0);
cf.setCredentialsProvider(new CredentialsProvider() {
@Override
public String getUsername() {
usernameRequested.incrementAndGet();
return username;
}
@Override
public String getPassword() {
passwordRequested.incrementAndGet();
return password;
}
});
RecoverableConnection c = (RecoverableConnection) cf.newConnection();
try {
assertTrue(c.isOpen());
assertThat(usernameRequested.get(), is(1));
assertThat(passwordRequested.get(), is(1));
closeAndWaitForRecovery(c);
assertTrue(c.isOpen());
// username is requested in AMQConnection#toString, so it can be accessed at any time
assertThat(usernameRequested.get(), greaterThanOrEqualTo(2));
assertThat(passwordRequested.get(), is(2));
} finally {
c.abort();
}
}
Aggregations