From acbc4a507b279c0ad703c130963c995d707a078f Mon Sep 17 00:00:00 2001 From: Guus der Kinderen Date: Sun, 26 Nov 2023 12:09:03 +0100 Subject: [PATCH] [sint] use idiomatic filters in presence subscription tests --- .../SubscriptionIntegrationTest.java | 79 +++++++------------ 1 file changed, 28 insertions(+), 51 deletions(-) diff --git a/smack-integration-test/src/main/java/org/jivesoftware/smack/subscription/SubscriptionIntegrationTest.java b/smack-integration-test/src/main/java/org/jivesoftware/smack/subscription/SubscriptionIntegrationTest.java index f07039c93e..7551adbafa 100644 --- a/smack-integration-test/src/main/java/org/jivesoftware/smack/subscription/SubscriptionIntegrationTest.java +++ b/smack-integration-test/src/main/java/org/jivesoftware/smack/subscription/SubscriptionIntegrationTest.java @@ -1,6 +1,6 @@ /** * - * Copyright 2021 Guus der Kinderen + * Copyright 2021-2023 Guus der Kinderen * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,11 @@ package org.jivesoftware.smack.subscription; import org.jivesoftware.smack.AbstractXMPPConnection; +import org.jivesoftware.smack.filter.AndFilter; +import org.jivesoftware.smack.filter.FromMatchesFilter; +import org.jivesoftware.smack.filter.PresenceTypeFilter; +import org.jivesoftware.smack.filter.StanzaExtensionFilter; +import org.jivesoftware.smack.filter.StanzaFilter; import org.jivesoftware.smack.packet.Presence; import org.jivesoftware.smack.packet.StandardExtensionElement; @@ -53,19 +58,13 @@ public void testSubscriptionRequest() throws Exception { final SimpleResultSyncPoint received = new SimpleResultSyncPoint(); - conOne.addAsyncStanzaListener(p -> received.signal(), - stanza -> { - if (!(stanza instanceof Presence)) { - return false; - } - if (!stanza.getFrom().asBareJid().equals(conTwo.getUser().asBareJid())) { - return false; - } - final Presence presence = (Presence) stanza; - return Presence.Type.subscribe.equals(presence.getType()); - } + final StanzaFilter resultFilter = new AndFilter( + PresenceTypeFilter.SUBSCRIBE, + FromMatchesFilter.createBare(conTwo.getUser()) ); + conOne.addAsyncStanzaListener(p -> received.signal(), resultFilter); + conTwo.sendStanza(subscriptionRequest); received.waitForResult(timeout); } @@ -93,19 +92,13 @@ public void testSubscriptionRequestOffline() throws Exception { final SimpleResultSyncPoint received = new SimpleResultSyncPoint(); - conOne.addAsyncStanzaListener(p -> received.signal(), - stanza -> { - if (!(stanza instanceof Presence)) { - return false; - } - if (!stanza.getFrom().asBareJid().equals(conTwo.getUser().asBareJid())) { - return false; - } - final Presence presence = (Presence) stanza; - return Presence.Type.subscribe.equals(presence.getType()); - } + final StanzaFilter resultFilter = new AndFilter( + PresenceTypeFilter.SUBSCRIBE, + FromMatchesFilter.createBare(conTwo.getUser()) ); + conOne.addAsyncStanzaListener(p -> received.signal(), resultFilter); + ((AbstractXMPPConnection) conOne).login(); received.waitForResult(timeout); } @@ -128,22 +121,14 @@ public void testSubscriptionRequestWithExtension() throws Exception { final SimpleResultSyncPoint received = new SimpleResultSyncPoint(); - conOne.addAsyncStanzaListener(p -> received.signal(), - stanza -> { - if (!(stanza instanceof Presence)) { - return false; - } - if (!stanza.getFrom().asBareJid().equals(conTwo.getUser().asBareJid())) { - return false; - } - final Presence presence = (Presence) stanza; - if (!Presence.Type.subscribe.equals(presence.getType())) { - return false; - } - return stanza.hasExtension("test", "org.example.test"); - } + final StanzaFilter resultFilter = new AndFilter( + PresenceTypeFilter.SUBSCRIBE, + FromMatchesFilter.createBare(conTwo.getUser()), + new StanzaExtensionFilter("test", "org.example.test") ); + conOne.addAsyncStanzaListener(p -> received.signal(), resultFilter); + conTwo.sendStanza(subscriptionRequest); received.waitForResult(timeout); } @@ -174,22 +159,14 @@ public void testSubscriptionRequestOfflineWithExtension() throws Exception { final SimpleResultSyncPoint received = new SimpleResultSyncPoint(); - conOne.addAsyncStanzaListener(p -> received.signal(), - stanza -> { - if (!(stanza instanceof Presence)) { - return false; - } - if (!stanza.getFrom().asBareJid().equals(conTwo.getUser().asBareJid())) { - return false; - } - final Presence presence = (Presence) stanza; - if (!Presence.Type.subscribe.equals(presence.getType())) { - return false; - } - return stanza.hasExtension("test", "org.example.test"); - } + final StanzaFilter resultFilter = new AndFilter( + PresenceTypeFilter.SUBSCRIBE, + FromMatchesFilter.createBare(conTwo.getUser()), + new StanzaExtensionFilter("test", "org.example.test") ); + conOne.addAsyncStanzaListener(p -> received.signal(), resultFilter); + ((AbstractXMPPConnection) conOne).login(); received.waitForResult(timeout); }