• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

igniterealtime / Smack / #2912

25 Nov 2023 07:08PM UTC coverage: 39.139%. Remained the same
#2912

push

github

Flowdalic
[sinttest] Fix typos in log and exception messages

0 of 3 new or added lines in 1 file covered. (0.0%)

3 existing lines in 1 file now uncovered.

16383 of 41859 relevant lines covered (39.14%)

0.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

27.23
/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/XmppConnectionManager.java
1
/**
2
 *
3
 * Copyright 2018-2023 Florian Schmaus
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
package org.igniterealtime.smack.inttest;
18

19
import java.io.IOException;
20
import java.lang.reflect.InvocationTargetException;
21
import java.security.KeyManagementException;
22
import java.security.NoSuchAlgorithmException;
23
import java.util.ArrayList;
24
import java.util.Collection;
25
import java.util.Collections;
26
import java.util.HashMap;
27
import java.util.List;
28
import java.util.Map;
29
import java.util.Set;
30
import java.util.concurrent.ConcurrentHashMap;
31
import java.util.logging.Level;
32
import java.util.logging.Logger;
33

34
import org.jivesoftware.smack.AbstractXMPPConnection;
35
import org.jivesoftware.smack.ConnectionConfiguration;
36
import org.jivesoftware.smack.SmackException;
37
import org.jivesoftware.smack.SmackException.NoResponseException;
38
import org.jivesoftware.smack.SmackException.NotConnectedException;
39
import org.jivesoftware.smack.XMPPException;
40
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
41
import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection;
42
import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration;
43
import org.jivesoftware.smack.compression.CompressionModuleDescriptor;
44
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
45
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
46
import org.jivesoftware.smack.util.MultiMap;
47
import org.jivesoftware.smack.util.StringUtils;
48
import org.jivesoftware.smack.websocket.java11.Java11WebSocketFactory;
49
import org.jivesoftware.smack.websocket.okhttp.OkHttpWebSocketFactory;
50
import org.jivesoftware.smackx.admin.ServiceAdministrationManager;
51
import org.jivesoftware.smackx.iqregister.AccountManager;
52

53
import org.igniterealtime.smack.inttest.Configuration.AccountRegistration;
54
import org.igniterealtime.smack.inttest.SmackIntegrationTestFramework.AccountNum;
55
import org.jxmpp.jid.EntityBareJid;
56
import org.jxmpp.jid.impl.JidCreate;
57
import org.jxmpp.jid.parts.Localpart;
58
import org.jxmpp.stringprep.XmppStringprepException;
59

60
public class XmppConnectionManager {
1✔
61

62
    private static final Logger LOGGER = Logger.getLogger(XmppConnectionManager.class.getName());
1✔
63

64
    private static final XmppConnectionDescriptor<? extends AbstractXMPPConnection, ? extends ConnectionConfiguration, ? extends ConnectionConfiguration.Builder<?, ?>> DEFAULT_CONNECTION_DESCRIPTOR;
65

66
    private static final Map<String, XmppConnectionDescriptor<? extends AbstractXMPPConnection, ? extends ConnectionConfiguration, ? extends ConnectionConfiguration.Builder<?, ?>>> NICKNAME_CONNECTION_DESCRIPTORS = new HashMap<>();
1✔
67

68
    private static final MultiMap<
69
        Class<? extends AbstractXMPPConnection>,
70
        XmppConnectionDescriptor<? extends AbstractXMPPConnection, ? extends ConnectionConfiguration, ? extends ConnectionConfiguration.Builder<?, ?>>
71
    > CONNECTION_DESCRIPTORS = new MultiMap<>();
1✔
72

73
    static {
74
        try {
75
            DEFAULT_CONNECTION_DESCRIPTOR = XmppConnectionDescriptor.buildWith(XMPPTCPConnection.class, XMPPTCPConnectionConfiguration.class)
1✔
76
                            .withNickname("tcp")
1✔
77
                            .build();
1✔
78
            addConnectionDescriptor(DEFAULT_CONNECTION_DESCRIPTOR);
1✔
79

80
            addConnectionDescriptor(
1✔
81
                            XmppConnectionDescriptor.buildWith(ModularXmppClientToServerConnection.class, ModularXmppClientToServerConnectionConfiguration.class)
1✔
82
                            .withNickname("modular")
1✔
83
                            .build()
1✔
84
            );
85
            addConnectionDescriptor(
1✔
86
                            XmppConnectionDescriptor.buildWith(ModularXmppClientToServerConnection.class, ModularXmppClientToServerConnectionConfiguration.class, ModularXmppClientToServerConnectionConfiguration.Builder.class)
1✔
87
                            .withNickname("modular-nocompress")
1✔
88
                            .applyExtraConfguration(cb -> cb.removeModule(CompressionModuleDescriptor.class))
1✔
89
                            .build()
1✔
90
            );
91
            addConnectionDescriptor(
1✔
92
                            XmppConnectionDescriptor.buildWebsocketDescriptor("modular-websocket-okhttp", OkHttpWebSocketFactory.class)
1✔
93
            );
94
            addConnectionDescriptor(
1✔
95
                            XmppConnectionDescriptor.buildWebsocketDescriptor("modular-websocket-java11", Java11WebSocketFactory.class)
1✔
96
            );
97
        } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException
×
98
                        | IllegalArgumentException | InvocationTargetException e) {
99
            throw new AssertionError(e);
×
100
        }
1✔
101
    }
1✔
102

103
    public static boolean addConnectionDescriptor(
104
                    XmppConnectionDescriptor<? extends AbstractXMPPConnection, ? extends ConnectionConfiguration, ? extends ConnectionConfiguration.Builder<?, ?>> connectionDescriptor) {
105
        String nickname = connectionDescriptor.getNickname();
1✔
106
        Class<? extends AbstractXMPPConnection> connectionClass = connectionDescriptor.getConnectionClass();
1✔
107

108
        boolean alreadyExisted;
109
        synchronized (CONNECTION_DESCRIPTORS) {
1✔
110
            alreadyExisted = removeConnectionDescriptor(nickname);
1✔
111

112
            CONNECTION_DESCRIPTORS.put(connectionClass, connectionDescriptor);
1✔
113
            NICKNAME_CONNECTION_DESCRIPTORS.put(connectionDescriptor.getNickname(), connectionDescriptor);
1✔
114
        }
1✔
115
        return alreadyExisted;
1✔
116
    }
117

118
    public static boolean removeConnectionDescriptor(String nickname) {
119
        synchronized (CONNECTION_DESCRIPTORS) {
1✔
120
            XmppConnectionDescriptor<? extends AbstractXMPPConnection, ? extends ConnectionConfiguration, ? extends ConnectionConfiguration.Builder<?, ?>> connectionDescriptor = NICKNAME_CONNECTION_DESCRIPTORS.remove(nickname);
1✔
121
            if (connectionDescriptor == null) {
1✔
122
                return false;
1✔
123
            }
124

125
            boolean removed = CONNECTION_DESCRIPTORS.removeOne(connectionDescriptor.getConnectionClass(), connectionDescriptor);
×
126
            assert removed;
×
127
        }
×
128

129
        return true;
×
130
    }
131

132
    private final XmppConnectionDescriptor<? extends AbstractXMPPConnection, ? extends ConnectionConfiguration, ? extends ConnectionConfiguration.Builder<?, ?>> defaultConnectionDescriptor;
133

134
    private final Map<String, XmppConnectionDescriptor<? extends AbstractXMPPConnection, ? extends ConnectionConfiguration, ? extends ConnectionConfiguration.Builder<?, ?>>> nicknameConnectionDescriptors;
135

136
    private final MultiMap<
137
        Class<? extends AbstractXMPPConnection>,
138
        XmppConnectionDescriptor<? extends AbstractXMPPConnection, ? extends ConnectionConfiguration, ? extends ConnectionConfiguration.Builder<?, ?>>
139
    > connectionDescriptors;
140

141
    private final SmackIntegrationTestFramework sinttestFramework;
142
    private final Configuration sinttestConfiguration;
143
    private final String testRunId;
144

145
    private final AbstractXMPPConnection accountRegistrationConnection;
146
    private final ServiceAdministrationManager adminManager;
147
    private final AccountManager accountManager;
148

149
    /**
150
     * One of the three main connections. The type of the main connections is the default connection type.
151
     */
152
    AbstractXMPPConnection conOne, conTwo, conThree;
153

154
    /**
155
     * A pool of authenticated and free to use connections.
156
     */
157
    private final MultiMap<XmppConnectionDescriptor<?, ?, ?>, AbstractXMPPConnection> connectionPool = new MultiMap<>();
1✔
158

159
    /**
160
     * A list of all ever created connections.
161
     */
162
    private final Map<AbstractXMPPConnection, XmppConnectionDescriptor<?, ?, ?>> connections = new ConcurrentHashMap<>();
1✔
163

164
    XmppConnectionManager(SmackIntegrationTestFramework sinttestFramework)
165
            throws SmackException, IOException, XMPPException, InterruptedException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1✔
166
        synchronized (CONNECTION_DESCRIPTORS) {
1✔
167
            connectionDescriptors = CONNECTION_DESCRIPTORS.clone();
1✔
168
            nicknameConnectionDescriptors = new HashMap<>(NICKNAME_CONNECTION_DESCRIPTORS);
1✔
169
        }
1✔
170

171
        this.sinttestFramework = sinttestFramework;
1✔
172
        this.sinttestConfiguration = sinttestFramework.config;
1✔
173
        this.testRunId = sinttestFramework.testRunResult.testRunId;
1✔
174

175
        String configuredDefaultConnectionNickname = sinttestConfiguration.defaultConnectionNickname;
1✔
176
        if (configuredDefaultConnectionNickname != null) {
1✔
177
            defaultConnectionDescriptor = nicknameConnectionDescriptors.get(configuredDefaultConnectionNickname);
1✔
178
            if (defaultConnectionDescriptor == null) {
1✔
179
                throw new IllegalArgumentException("Could not find a connection descriptor for connection nickname '" + configuredDefaultConnectionNickname + "'");
×
180
            }
181
        } else {
182
            defaultConnectionDescriptor = DEFAULT_CONNECTION_DESCRIPTOR;
×
183
        }
184

185
        switch (sinttestConfiguration.accountRegistration) {
1✔
186
        case serviceAdministration:
187
        case inBandRegistration:
188
            accountRegistrationConnection = defaultConnectionDescriptor.construct(sinttestConfiguration);
×
189
            accountRegistrationConnection.connect();
×
190
            accountRegistrationConnection.login(sinttestConfiguration.adminAccountUsername,
×
191
                            sinttestConfiguration.adminAccountPassword);
192

193
            if (sinttestConfiguration.accountRegistration == AccountRegistration.inBandRegistration) {
×
194

195
                adminManager = null;
×
196
                accountManager = AccountManager.getInstance(accountRegistrationConnection);
×
197
            } else {
198
                adminManager = ServiceAdministrationManager.getInstanceFor(accountRegistrationConnection);
×
199
                accountManager = null;
×
200
            }
201
            break;
×
202
        case disabled:
203
            accountRegistrationConnection = null;
1✔
204
            adminManager = null;
1✔
205
            accountManager = null;
1✔
206
            break;
1✔
207
        default:
208
            throw new AssertionError();
×
209
        }
210
    }
1✔
211

212
    SmackIntegrationTestEnvironment prepareEnvironment() throws KeyManagementException, NoSuchAlgorithmException,
213
            InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException,
214
            SmackException, IOException, XMPPException, InterruptedException {
215
        prepareMainConnections();
×
216
        return new SmackIntegrationTestEnvironment(conOne, conTwo, conThree,
×
217
                sinttestFramework.testRunResult.testRunId, sinttestConfiguration, this);
218
    }
219

220
    private void prepareMainConnections() throws KeyManagementException, NoSuchAlgorithmException, InstantiationException,
221
            IllegalAccessException, IllegalArgumentException, InvocationTargetException, SmackException, IOException,
222
            XMPPException, InterruptedException {
223
        final int mainAccountCount = AccountNum.values().length;
×
224
        List<AbstractXMPPConnection> connections = new ArrayList<>(mainAccountCount);
×
225
        for (AccountNum mainAccountNum : AccountNum.values()) {
×
226
            AbstractXMPPConnection mainConnection = getConnectedMainConnectionFor(mainAccountNum);
×
227
            connections.add(mainConnection);
×
228
        }
229
        conOne = connections.get(0);
×
230
        conTwo = connections.get(1);
×
231
        conThree = connections.get(2);
×
232
    }
×
233

234
    public XmppConnectionDescriptor<? extends AbstractXMPPConnection, ? extends ConnectionConfiguration, ? extends ConnectionConfiguration.Builder<?, ?>> getDefaultConnectionDescriptor() {
235
        return defaultConnectionDescriptor;
×
236
    }
237

238
    public Collection<XmppConnectionDescriptor<? extends AbstractXMPPConnection, ? extends ConnectionConfiguration, ? extends ConnectionConfiguration.Builder<?, ?>>> getConnectionDescriptors() {
239
        return Collections.unmodifiableCollection(nicknameConnectionDescriptors.values());
×
240
    }
241

242
    @SuppressWarnings("unchecked")
243
    public <C extends AbstractXMPPConnection> XmppConnectionDescriptor<C, ? extends ConnectionConfiguration, ? extends ConnectionConfiguration.Builder<?, ?>> getConnectionDescriptorFor(
244
                    Class<C> connectionClass) {
245
        return (XmppConnectionDescriptor<C, ? extends ConnectionConfiguration, ? extends ConnectionConfiguration.Builder<?, ?>>) connectionDescriptors.getFirst(
×
246
                        connectionClass);
247
    }
248

249
    void disconnectAndCleanup() throws InterruptedException {
250
        int successfullyDeletedAccountsCount = 0;
1✔
251
        for (AbstractXMPPConnection connection : connections.keySet()) {
1✔
252
            if (sinttestConfiguration.accountRegistration == AccountRegistration.inBandRegistration) {
×
253
                // Note that we use the account manager from the to-be-deleted connection.
254
                AccountManager accountManager = AccountManager.getInstance(connection);
×
255
                try {
256
                    accountManager.deleteAccount();
×
257
                    successfullyDeletedAccountsCount++;
×
258
                } catch (NoResponseException | XMPPErrorException | NotConnectedException e) {
×
259
                    LOGGER.log(Level.WARNING, "Could not delete dynamically registered account", e);
×
260
                }
×
261
            }
262

263
            connection.disconnect();
×
264

265
            if (sinttestConfiguration.accountRegistration == AccountRegistration.serviceAdministration) {
×
266
                String username = connection.getConfiguration().getUsername().toString();
×
267
                Localpart usernameAsLocalpart;
268
                try {
269
                    usernameAsLocalpart = Localpart.from(username);
×
270
                } catch (XmppStringprepException e) {
×
271
                    throw new AssertionError(e);
×
272
                }
×
273

274
                EntityBareJid connectionAddress = JidCreate.entityBareFrom(usernameAsLocalpart, sinttestConfiguration.service);
×
275

276
                try {
277
                    adminManager.deleteUser(connectionAddress);
×
278
                    successfullyDeletedAccountsCount++;
×
279
                } catch (NoResponseException | XMPPErrorException | NotConnectedException e) {
×
280
                    LOGGER.log(Level.WARNING, "Could not delete dynamically registered account", e);
×
281
                }
×
282
            }
283
        }
×
284

285
        if (sinttestConfiguration.isAccountRegistrationPossible()) {
1✔
286
            int unsuccessfullyDeletedAccountsCount = connections.size() - successfullyDeletedAccountsCount;
×
287
            if (unsuccessfullyDeletedAccountsCount == 0) {
×
288
                LOGGER.info("Successfully deleted all created accounts ✔");
×
289
            } else {
NEW
290
                LOGGER.warning("Could not delete all created accounts, " + unsuccessfullyDeletedAccountsCount + " remaining");
×
291
            }
292
        }
293

294
        connections.clear();
1✔
295

296
        if (accountRegistrationConnection != null) {
1✔
297
            accountRegistrationConnection.disconnect();
×
298
        }
299
    }
1✔
300

301

302
    private static final String USERNAME_PREFIX = "smack-inttest";
303

304
    private AbstractXMPPConnection getConnectedMainConnectionFor(AccountNum accountNum) throws SmackException, IOException, XMPPException,
305
            InterruptedException, KeyManagementException, NoSuchAlgorithmException, InstantiationException,
306
            IllegalAccessException, IllegalArgumentException, InvocationTargetException {
307
        String middlefix;
308
        String accountUsername;
309
        String accountPassword;
310
        switch (accountNum) {
×
311
        case One:
312
            accountUsername = sinttestConfiguration.accountOneUsername;
×
313
            accountPassword = sinttestConfiguration.accountOnePassword;
×
314
            middlefix = "one";
×
315
            break;
×
316
        case Two:
317
            accountUsername = sinttestConfiguration.accountTwoUsername;
×
318
            accountPassword = sinttestConfiguration.accountTwoPassword;
×
319
            middlefix = "two";
×
320
            break;
×
321
        case Three:
322
            accountUsername = sinttestConfiguration.accountThreeUsername;
×
323
            accountPassword = sinttestConfiguration.accountThreePassword;
×
324
            middlefix = "three";
×
325
            break;
×
326
        default:
327
            throw new IllegalStateException();
×
328
        }
329

330
        // Note that it is perfectly fine for account(Username|Password) to be 'null' at this point.
331
        final String finalAccountUsername = StringUtils.isNullOrEmpty(accountUsername) ? USERNAME_PREFIX + '-' + middlefix + '-' + testRunId : accountUsername;
×
332
        final String finalAccountPassword = StringUtils.isNullOrEmpty(accountPassword) ? StringUtils.insecureRandomString(16) : accountPassword;
×
333

334
        if (sinttestConfiguration.isAccountRegistrationPossible()) {
×
335
            registerAccount(finalAccountUsername, finalAccountPassword);
×
336
        }
337

338
        AbstractXMPPConnection mainConnection = defaultConnectionDescriptor.construct(sinttestConfiguration, builder -> {
×
339
            try {
340
                builder.setUsernameAndPassword(finalAccountUsername, finalAccountPassword)
×
341
                    .setResource(middlefix + '-' + testRunId);
×
342
            } catch (XmppStringprepException e) {
×
343
                throw new IllegalArgumentException(e);
×
344
            }
×
345
        });
×
346

347
        connections.put(mainConnection, defaultConnectionDescriptor);
×
348

349
        mainConnection.connect();
×
350
        mainConnection.login();
×
351

352
        return mainConnection;
×
353
    }
354

355
    private void registerAccount(String username, String password) throws NoResponseException, XMPPErrorException,
356
                    NotConnectedException, InterruptedException, XmppStringprepException {
357
        if (accountRegistrationConnection == null) {
×
358
            throw new IllegalStateException("Account registration not configured");
×
359
        }
360

361
        switch (sinttestConfiguration.accountRegistration) {
×
362
        case serviceAdministration:
363
            EntityBareJid userJid = JidCreate.entityBareFrom(Localpart.from(username),
×
364
                            accountRegistrationConnection.getXMPPServiceDomain());
×
365
            adminManager.addUser(userJid, password);
×
366
            break;
×
367
        case inBandRegistration:
368
            if (!accountManager.supportsAccountCreation()) {
×
NEW
369
                throw new UnsupportedOperationException("Account creation/registration is not supported");
×
370
            }
371
            Set<String> requiredAttributes = accountManager.getAccountAttributes();
×
372
            if (requiredAttributes.size() > 4) {
×
NEW
373
                throw new IllegalStateException("Unknown required attributes");
×
374
            }
375
            Map<String, String> additionalAttributes = new HashMap<>();
×
376
            additionalAttributes.put("name", "Smack Integration Test");
×
377
            additionalAttributes.put("email", "flow@igniterealtime.org");
×
378
            Localpart usernameLocalpart = Localpart.from(username);
×
379
            accountManager.createAccount(usernameLocalpart, password, additionalAttributes);
×
380
            break;
×
381
        case disabled:
382
            throw new IllegalStateException("Account creation no possible");
×
383
        }
384
    }
×
385

386
    <C extends AbstractXMPPConnection> List<C> constructConnectedConnections(XmppConnectionDescriptor<C, ? extends ConnectionConfiguration, ? extends ConnectionConfiguration.Builder<?, ?>>  connectionDescriptor, int count)
387
                    throws InterruptedException, SmackException, IOException, XMPPException {
388
        List<C> connections = new ArrayList<>(count);
×
389

390
        synchronized (connectionPool) {
×
391
            @SuppressWarnings("unchecked")
392
            List<C> pooledConnections = (List<C>) connectionPool.getAll(connectionDescriptor);
×
393
            while (count > 0 && !pooledConnections.isEmpty()) {
×
394
                C connection = pooledConnections.remove(pooledConnections.size() - 1);
×
395
                connections.add(connection);
×
396
                count--;
×
397
            }
×
398
        }
×
399

400
        for (int i = 0; i < count; i++) {
×
401
            C connection = constructConnectedConnection(connectionDescriptor);
×
402
            connections.add(connection);
×
403
        }
404

405
        return connections;
×
406
    }
407

408
    private <C extends AbstractXMPPConnection> C constructConnectedConnection(
409
                    XmppConnectionDescriptor<C, ? extends ConnectionConfiguration, ? extends ConnectionConfiguration.Builder<?, ?>> connectionDescriptor)
410
                    throws InterruptedException, SmackException, IOException, XMPPException {
411
        C connection = constructConnection(connectionDescriptor, null);
×
412

413
        connection.connect();
×
414
        connection.login();
×
415

416
        return connection;
×
417
    }
418

419
    AbstractXMPPConnection constructConnection()
420
                    throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
421
        return constructConnection(defaultConnectionDescriptor);
×
422
    }
423

424
    AbstractXMPPConnection constructConnectedConnection()
425
                    throws InterruptedException, SmackException, IOException, XMPPException {
426
        return constructConnectedConnection(defaultConnectionDescriptor);
×
427
    }
428

429
    <C extends AbstractXMPPConnection> C constructConnection(
430
                    XmppConnectionDescriptor<C, ? extends ConnectionConfiguration, ? extends ConnectionConfiguration.Builder<?, ?>> connectionDescriptor)
431
                    throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
432
        return constructConnection(connectionDescriptor, null);
×
433
    }
434

435
    private <C extends AbstractXMPPConnection> C constructConnection(
436
            XmppConnectionDescriptor<C, ? extends ConnectionConfiguration, ? extends ConnectionConfiguration.Builder<?, ?>> connectionDescriptor,
437
            Collection<ConnectionConfigurationBuilderApplier> customConnectionConfigurationAppliers)
438
            throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
439
        String username = "sinttest-" + testRunId + '-' + (connections.size() + 1);
×
440
        String password = StringUtils.randomString(24);
×
441

442
        return constructConnection(username, password, connectionDescriptor, customConnectionConfigurationAppliers);
×
443
    }
444

445
    private <C extends AbstractXMPPConnection> C constructConnection(final String username, final String password,
446
                    XmppConnectionDescriptor<C, ? extends ConnectionConfiguration, ? extends ConnectionConfiguration.Builder<?, ?>> connectionDescriptor,
447
                    Collection<ConnectionConfigurationBuilderApplier> customConnectionConfigurationAppliers)
448
                    throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
449
        try {
450
            registerAccount(username, password);
×
451
        } catch (XmppStringprepException e) {
×
452
            throw new IllegalArgumentException(e);
×
453
        }
×
454

455
        ConnectionConfigurationBuilderApplier usernameAndPasswordApplier = configurationBuilder -> {
×
456
            configurationBuilder.setUsernameAndPassword(username, password);
×
457
        };
×
458

459
        if (customConnectionConfigurationAppliers == null) {
×
460
            customConnectionConfigurationAppliers = Collections.singleton(usernameAndPasswordApplier);
×
461
        } else {
462
            customConnectionConfigurationAppliers.add(usernameAndPasswordApplier);
×
463
        }
464

465
        C connection;
466
        try {
467
            connection = connectionDescriptor.construct(sinttestConfiguration, customConnectionConfigurationAppliers);
×
468
        } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
×
469
                        | InvocationTargetException e) {
470
            throw new IllegalStateException(e);
×
471
        }
×
472

473
        connections.put(connection, connectionDescriptor);
×
474

475
        return connection;
×
476
    }
477

478
    void recycle(Collection<? extends AbstractXMPPConnection> connections) {
479
        for (AbstractXMPPConnection connection : connections) {
×
480
            recycle(connection);
×
481
        }
×
482
    }
×
483

484
    void recycle(AbstractXMPPConnection connection) {
485
        Class<? extends AbstractXMPPConnection> connectionClass = connection.getClass();
×
486
        if (!connectionDescriptors.containsKey(connectionClass)) {
×
487
            throw new IllegalStateException("Attempt to recycle unknown connection of class '" + connectionClass + "'");
×
488
        }
489

490
        if (connection.isAuthenticated()) {
×
491
            XmppConnectionDescriptor<?, ?, ?> connectionDescriptor = connections.get(connection);
×
492
            if (connectionDescriptor == null) {
×
493
                throw new IllegalStateException("Attempt to recycle unknown connection: " + connection);
×
494
            }
495

496
            synchronized (connectionPool) {
×
497
                connectionPool.put(connectionDescriptor, connection);
×
498
            }
×
499
        } else {
×
500
            connection.disconnect();
×
501
        }
502
        // Note that we do not delete the account of the unauthenticated connection here, as it is done at the end of
503
        // the test run together with all other dynamically created accounts.
504
    }
×
505

506
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc