mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2025-12-06 05:01:12 +01:00
Migrate from Ant to Gradle (SMACK-265)
This commit is contained in:
parent
235eca3a3d
commit
201152ef42
767 changed files with 1088 additions and 4296 deletions
|
|
@ -0,0 +1,32 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2003 Jive Software.
|
||||
*
|
||||
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack.packet;
|
||||
|
||||
/**
|
||||
* A mock implementation of the Packet abstract class. Implements toXML() by returning null.
|
||||
*/
|
||||
public class MockPacket extends Packet {
|
||||
|
||||
/**
|
||||
* Returns null always.
|
||||
* @return null
|
||||
*/
|
||||
public String toXML() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2003-2007 Jive Software.
|
||||
*
|
||||
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack.packet;
|
||||
|
||||
import org.jivesoftware.smack.filter.PacketFilter;
|
||||
|
||||
/**
|
||||
* A mock implementation of the PacketFilter class. Pass in the value you want the
|
||||
* accept(..) method to return.
|
||||
*/
|
||||
public class MockPacketFilter implements PacketFilter {
|
||||
|
||||
private boolean acceptValue;
|
||||
|
||||
public MockPacketFilter(boolean acceptValue) {
|
||||
this.acceptValue = acceptValue;
|
||||
}
|
||||
|
||||
public boolean accept(Packet packet) {
|
||||
return acceptValue;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,384 @@
|
|||
/**
|
||||
*
|
||||
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.packet;
|
||||
|
||||
import org.jivesoftware.smack.provider.PrivacyProvider;
|
||||
import org.jivesoftware.smack.test.SmackTestCase;
|
||||
import org.xmlpull.mxp1.MXParser;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
/**
|
||||
* Test the PrivacyProvider class with valids privacy xmls
|
||||
*
|
||||
* @author Francisco Vives
|
||||
*/
|
||||
public class PrivacyProviderTest extends SmackTestCase {
|
||||
|
||||
/**
|
||||
* Constructor for PrivacyTest.
|
||||
* @param arg0
|
||||
*/
|
||||
public PrivacyProviderTest(String arg0) {
|
||||
super(arg0);
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
try {
|
||||
new PrivacyProviderTest(null).testFull();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the parser with an xml with all kind of stanzas.
|
||||
* To create the xml string based from an xml file, replace:\n with: "\n + "
|
||||
*/
|
||||
public void testFull() {
|
||||
// Make the XML to test
|
||||
String xml = ""
|
||||
+ " <iq type='result' id='getlist2' to='romeo@example.net/orchard'> "
|
||||
+ " <query xmlns='jabber:iq:privacy'> "
|
||||
+ " <active name='testFilter'/> "
|
||||
+ " <default name='testSubscription'/> "
|
||||
+ " <list name='testFilter'> "
|
||||
+ " <item type='jid' "
|
||||
+ " value='tybalt@example.com' "
|
||||
+ " action='deny' "
|
||||
+ " order='1'/> "
|
||||
+ " <item action='allow' order='2'> "
|
||||
+ " <message/> "
|
||||
+ " <presence-in/> "
|
||||
+ " <presence-out/> "
|
||||
+ " <iq/> "
|
||||
+ " </item> "
|
||||
+ " </list> "
|
||||
+ " <list name='testSubscription'> "
|
||||
+ " <item type='subscription' "
|
||||
+ " value='both' "
|
||||
+ " action='allow' "
|
||||
+ " order='10'/> "
|
||||
+ " <item type='subscription' "
|
||||
+ " value='to' "
|
||||
+ " action='allow' "
|
||||
+ " order='11'/> "
|
||||
+ " <item type='subscription' "
|
||||
+ " value='from' "
|
||||
+ " action='allow' "
|
||||
+ " order='12'/> "
|
||||
+ " <item type='subscription' "
|
||||
+ " value='none' "
|
||||
+ " action='deny' "
|
||||
+ " order='5'> "
|
||||
+ " <message/> "
|
||||
+ " </item> "
|
||||
+ " <item action='deny' order='15'/> "
|
||||
+ " </list> "
|
||||
+ " <list name='testJID'> "
|
||||
+ " <item type='jid' "
|
||||
+ " value='juliet@example.com' "
|
||||
+ " action='allow' "
|
||||
+ " order='6'/> "
|
||||
+ " <item type='jid' "
|
||||
+ " value='benvolio@example.org/palm' "
|
||||
+ " action='deny' "
|
||||
+ " order='7'/> "
|
||||
+ " <item type='jid' "
|
||||
+ " action='allow' "
|
||||
+ " order='42'/> "
|
||||
+ " <item action='deny' order='666'/> "
|
||||
+ " </list> "
|
||||
+ " <list name='testGroup'> "
|
||||
+ " <item type='group' "
|
||||
+ " value='Enemies' "
|
||||
+ " action='deny' "
|
||||
+ " order='4'> "
|
||||
+ " <message/> "
|
||||
+ " </item> "
|
||||
+ " <item action='deny' order='666'/> "
|
||||
+ " </list> "
|
||||
+ " <list name='testEmpty'/> "
|
||||
+ " </query> "
|
||||
+ " <error type='cancel'> "
|
||||
+ " <item-not-found "
|
||||
+ " xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/> "
|
||||
+ " </error> "
|
||||
+ "</iq> ";
|
||||
|
||||
try {
|
||||
// Create the xml parser
|
||||
XmlPullParser parser = getParserFromXML(xml);
|
||||
// Create a packet from the xml
|
||||
Privacy packet = (Privacy) (new PrivacyProvider()).parseIQ(parser);
|
||||
|
||||
// check if it exist
|
||||
assertNotNull(packet);
|
||||
// assertEquals(xml, packet.getChildElementXML());
|
||||
|
||||
// check the default and active names
|
||||
assertEquals("testFilter", packet.getActiveName());
|
||||
assertEquals("testSubscription", packet.getDefaultName());
|
||||
|
||||
// check the list
|
||||
assertEquals(2, packet.getPrivacyList("testFilter").size());
|
||||
assertEquals(5, packet.getPrivacyList("testSubscription").size());
|
||||
assertEquals(4, packet.getPrivacyList("testJID").size());
|
||||
assertEquals(2, packet.getPrivacyList("testGroup").size());
|
||||
assertEquals(0, packet.getPrivacyList("testEmpty").size());
|
||||
|
||||
// check each privacy item
|
||||
PrivacyItem item = packet.getItem("testGroup", 4);
|
||||
assertEquals("Enemies", item.getValue());
|
||||
assertEquals(PrivacyItem.Type.group, item.getType());
|
||||
assertEquals(false, item.isAllow());
|
||||
assertEquals(true, item.isFilterMessage());
|
||||
assertEquals(false, item.isFilterIQ());
|
||||
assertEquals(false, item.isFilterPresence_in());
|
||||
assertEquals(false, item.isFilterPresence_out());
|
||||
assertEquals(false, item.isFilterEverything());
|
||||
|
||||
item = packet.getItem("testFilter", 1);
|
||||
assertEquals("tybalt@example.com", item.getValue());
|
||||
assertEquals(PrivacyItem.Type.jid, item.getType());
|
||||
assertEquals(false, item.isAllow());
|
||||
assertEquals(false, item.isFilterMessage());
|
||||
assertEquals(false, item.isFilterIQ());
|
||||
assertEquals(false, item.isFilterPresence_in());
|
||||
assertEquals(false, item.isFilterPresence_out());
|
||||
assertEquals(true, item.isFilterEverything());
|
||||
|
||||
item = packet.getItem("testFilter", 2);
|
||||
assertEquals(null, item.getValue());
|
||||
assertEquals(null, item.getType());
|
||||
assertEquals(true, item.isAllow());
|
||||
assertEquals(true, item.isFilterMessage());
|
||||
assertEquals(true, item.isFilterIQ());
|
||||
assertEquals(true, item.isFilterPresence_in());
|
||||
assertEquals(true, item.isFilterPresence_out());
|
||||
assertEquals(false, item.isFilterEverything());
|
||||
|
||||
// TEST THE testSubscription LIST
|
||||
item = packet.getItem("testSubscription", 10);
|
||||
assertEquals("both", item.getValue());
|
||||
assertEquals(PrivacyItem.Type.subscription, item.getType());
|
||||
assertEquals(true, item.isAllow());
|
||||
assertEquals(false, item.isFilterMessage());
|
||||
assertEquals(false, item.isFilterIQ());
|
||||
assertEquals(false, item.isFilterPresence_in());
|
||||
assertEquals(false, item.isFilterPresence_out());
|
||||
assertEquals(true, item.isFilterEverything());
|
||||
|
||||
item = packet.getItem("testSubscription", 11);
|
||||
assertEquals("to", item.getValue());
|
||||
assertEquals(PrivacyItem.Type.subscription, item.getType());
|
||||
assertEquals(true, item.isAllow());
|
||||
assertEquals(false, item.isFilterMessage());
|
||||
assertEquals(false, item.isFilterIQ());
|
||||
assertEquals(false, item.isFilterPresence_in());
|
||||
assertEquals(false, item.isFilterPresence_out());
|
||||
assertEquals(true, item.isFilterEverything());
|
||||
|
||||
item = packet.getItem("testSubscription", 12);
|
||||
assertEquals("from", item.getValue());
|
||||
assertEquals(PrivacyItem.Type.subscription, item.getType());
|
||||
assertEquals(true, item.isAllow());
|
||||
assertEquals(false, item.isFilterMessage());
|
||||
assertEquals(false, item.isFilterIQ());
|
||||
assertEquals(false, item.isFilterPresence_in());
|
||||
assertEquals(false, item.isFilterPresence_out());
|
||||
assertEquals(true, item.isFilterEverything());
|
||||
|
||||
item = packet.getItem("testSubscription", 5);
|
||||
assertEquals("none", item.getValue());
|
||||
assertEquals(PrivacyItem.Type.subscription, item.getType());
|
||||
assertEquals(false, item.isAllow());
|
||||
assertEquals(true, item.isFilterMessage());
|
||||
assertEquals(false, item.isFilterIQ());
|
||||
assertEquals(false, item.isFilterPresence_in());
|
||||
assertEquals(false, item.isFilterPresence_out());
|
||||
assertEquals(false, item.isFilterEverything());
|
||||
|
||||
item = packet.getItem("testSubscription", 15);
|
||||
assertEquals(null, item.getValue());
|
||||
assertEquals(null, item.getType());
|
||||
assertEquals(false, item.isAllow());
|
||||
assertEquals(false, item.isFilterMessage());
|
||||
assertEquals(false, item.isFilterIQ());
|
||||
assertEquals(false, item.isFilterPresence_in());
|
||||
assertEquals(false, item.isFilterPresence_out());
|
||||
assertEquals(true, item.isFilterEverything());
|
||||
|
||||
// TEST THE testJID LIST
|
||||
|
||||
item = packet.getItem("testJID", 6);
|
||||
assertEquals("juliet@example.com", item.getValue());
|
||||
assertEquals(PrivacyItem.Type.jid, item.getType());
|
||||
assertEquals(true, item.isAllow());
|
||||
assertEquals(false, item.isFilterMessage());
|
||||
assertEquals(false, item.isFilterIQ());
|
||||
assertEquals(false, item.isFilterPresence_in());
|
||||
assertEquals(false, item.isFilterPresence_out());
|
||||
assertEquals(true, item.isFilterEverything());
|
||||
|
||||
item = packet.getItem("testJID", 7);
|
||||
assertEquals("benvolio@example.org/palm", item.getValue());
|
||||
assertEquals(PrivacyItem.Type.jid, item.getType());
|
||||
assertEquals(false, item.isAllow());
|
||||
assertEquals(false, item.isFilterMessage());
|
||||
assertEquals(false, item.isFilterIQ());
|
||||
assertEquals(false, item.isFilterPresence_in());
|
||||
assertEquals(false, item.isFilterPresence_out());
|
||||
assertEquals(true, item.isFilterEverything());
|
||||
|
||||
item = packet.getItem("testJID", 42);
|
||||
assertEquals(null, item.getValue());
|
||||
assertEquals(PrivacyItem.Type.jid, item.getType());
|
||||
assertEquals(true, item.isAllow());
|
||||
assertEquals(false, item.isFilterMessage());
|
||||
assertEquals(false, item.isFilterIQ());
|
||||
assertEquals(false, item.isFilterPresence_in());
|
||||
assertEquals(false, item.isFilterPresence_out());
|
||||
assertEquals(true, item.isFilterEverything());
|
||||
|
||||
item = packet.getItem("testJID", 666);
|
||||
assertEquals(null, item.getValue());
|
||||
assertEquals(null, item.getType());
|
||||
assertEquals(false, item.isAllow());
|
||||
assertEquals(false, item.isFilterMessage());
|
||||
assertEquals(false, item.isFilterIQ());
|
||||
assertEquals(false, item.isFilterPresence_in());
|
||||
assertEquals(false, item.isFilterPresence_out());
|
||||
assertEquals(true, item.isFilterEverything());
|
||||
|
||||
// TEST THE testGroup LIST
|
||||
|
||||
item = packet.getItem("testGroup", 4);
|
||||
assertEquals("Enemies", item.getValue());
|
||||
assertEquals(PrivacyItem.Type.group, item.getType());
|
||||
assertEquals(false, item.isAllow());
|
||||
assertEquals(true, item.isFilterMessage());
|
||||
assertEquals(false, item.isFilterIQ());
|
||||
assertEquals(false, item.isFilterPresence_in());
|
||||
assertEquals(false, item.isFilterPresence_out());
|
||||
assertEquals(false, item.isFilterEverything());
|
||||
|
||||
item = packet.getItem("testGroup", 666);
|
||||
assertEquals(null, item.getValue());
|
||||
assertEquals(null, item.getType());
|
||||
assertEquals(false, item.isAllow());
|
||||
assertEquals(false, item.isFilterMessage());
|
||||
assertEquals(false, item.isFilterIQ());
|
||||
assertEquals(false, item.isFilterPresence_in());
|
||||
assertEquals(false, item.isFilterPresence_out());
|
||||
assertEquals(true, item.isFilterEverything());
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check the parser with an xml with empty lists. It includes the active,
|
||||
* default and special list.
|
||||
* To create the xml string based from an xml file, replace:\n with: "\n + "
|
||||
*/
|
||||
public void testEmptyLists() {
|
||||
// Make the XML to test
|
||||
String xml = ""
|
||||
+ " <iq type='result' id='getlist1' to='romeo@example.net/orchard'> "
|
||||
+ " <query xmlns='jabber:iq:privacy'> "
|
||||
+ " <active/> "
|
||||
+ " <default name='public'/> "
|
||||
+ " <list name='public'/> "
|
||||
+ " <list name='private'/> "
|
||||
+ " <list name='special'/> "
|
||||
+ " </query> "
|
||||
+ " </iq> ";
|
||||
|
||||
try {
|
||||
// Create the xml parser
|
||||
XmlPullParser parser = getParserFromXML(xml);
|
||||
// Create a packet from the xml
|
||||
Privacy packet = (Privacy) (new PrivacyProvider()).parseIQ(parser);
|
||||
|
||||
assertNotNull(packet);
|
||||
assertNotNull(packet.getChildElementXML());
|
||||
|
||||
assertEquals("public", packet.getDefaultName());
|
||||
assertEquals(null, packet.getActiveName());
|
||||
|
||||
assertEquals(0, packet.getPrivacyList("public").size());
|
||||
assertEquals(0, packet.getPrivacyList("private").size());
|
||||
assertEquals(0, packet.getPrivacyList("special").size());
|
||||
|
||||
assertEquals(true, packet.isDeclineActiveList());
|
||||
assertEquals(false, packet.isDeclineDefaultList());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the parser with an xml with empty lists. It includes the active,
|
||||
* default and special list.
|
||||
* To create the xml string based from an xml file, replace:\n with: "\n + "
|
||||
*/
|
||||
public void testDeclineLists() {
|
||||
// Make the XML to test
|
||||
String xml = ""
|
||||
+ " <iq type='result' id='getlist1' to='romeo@example.net/orchard'> "
|
||||
+ " <query xmlns='jabber:iq:privacy'> "
|
||||
+ " <active/> "
|
||||
+ " <default/> "
|
||||
+ " </query> "
|
||||
+ " </iq> ";
|
||||
|
||||
try {
|
||||
// Create the xml parser
|
||||
XmlPullParser parser = getParserFromXML(xml);
|
||||
// Create a packet from the xml
|
||||
Privacy packet = (Privacy) (new PrivacyProvider()).parseIQ(parser);
|
||||
|
||||
assertNotNull(packet);
|
||||
|
||||
assertEquals(null, packet.getDefaultName());
|
||||
assertEquals(null, packet.getActiveName());
|
||||
|
||||
assertEquals(true, packet.isDeclineActiveList());
|
||||
assertEquals(true, packet.isDeclineDefaultList());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private XmlPullParser getParserFromXML(String xml) throws XmlPullParserException {
|
||||
MXParser parser = new MXParser();
|
||||
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
|
||||
parser.setInput(new StringReader(xml));
|
||||
return parser;
|
||||
}
|
||||
|
||||
protected int getMaxConnections() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
521
integration-test/org/jivesoftware/smack/packet/PrivacyTest.java
Normal file
521
integration-test/org/jivesoftware/smack/packet/PrivacyTest.java
Normal file
|
|
@ -0,0 +1,521 @@
|
|||
/**
|
||||
*
|
||||
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.packet;
|
||||
|
||||
import org.jivesoftware.smack.PrivacyList;
|
||||
import org.jivesoftware.smack.PrivacyListListener;
|
||||
import org.jivesoftware.smack.PrivacyListManager;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smack.packet.PrivacyItem.PrivacyRule;
|
||||
import org.jivesoftware.smack.test.SmackTestCase;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class PrivacyTest extends SmackTestCase {
|
||||
|
||||
public PrivacyTest(String arg0) {
|
||||
super(arg0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check when a client set a new active list.
|
||||
*/
|
||||
public void testCreateActiveList() {
|
||||
try {
|
||||
String listName = "testCreateActiveList";
|
||||
|
||||
PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(getConnection(0));
|
||||
PrivacyClient client = new PrivacyClient(privacyManager);
|
||||
privacyManager.addListener(client);
|
||||
|
||||
// Add the list that will be set as the active
|
||||
ArrayList<PrivacyItem> items = new ArrayList<PrivacyItem>();
|
||||
PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid.name(), true, 1);
|
||||
item.setValue(getConnection(0).getUser());
|
||||
items.add(item);
|
||||
privacyManager.createPrivacyList(listName, items);
|
||||
|
||||
Thread.sleep(500);
|
||||
|
||||
// Set the active list
|
||||
privacyManager.setActiveListName(listName);
|
||||
|
||||
Thread.sleep(500);
|
||||
|
||||
// Assert the list composition.
|
||||
assertEquals(listName, privacyManager.getActiveList().toString());
|
||||
List<PrivacyItem> privacyItems = privacyManager.getPrivacyList(listName).getItems();
|
||||
assertEquals(1, privacyItems.size());
|
||||
|
||||
// Assert the privacy item composition
|
||||
PrivacyItem receivedItem = privacyItems.get(0);
|
||||
assertEquals(1, receivedItem.getOrder());
|
||||
assertEquals(PrivacyItem.Type.jid, receivedItem.getType());
|
||||
assertEquals(true, receivedItem.isAllow());
|
||||
|
||||
privacyManager.deletePrivacyList(listName);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check when a client set more than one list.
|
||||
*/
|
||||
public void testCreateTwoLists() {
|
||||
try {
|
||||
String listName1 = "1testCreateTwoLists";
|
||||
String listName2 = "2testCreateTwoLists";
|
||||
String groupName = "testCreateTwoListsGroup";
|
||||
|
||||
PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(getConnection(0));
|
||||
PrivacyClient client = new PrivacyClient(privacyManager);
|
||||
privacyManager.addListener(client);
|
||||
|
||||
// Add a list
|
||||
ArrayList<PrivacyItem> items = new ArrayList<PrivacyItem>();
|
||||
PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid.name(), true, 1);
|
||||
item.setValue(getConnection(0).getUser());
|
||||
items.add(item);
|
||||
privacyManager.createPrivacyList(listName1, items);
|
||||
|
||||
Thread.sleep(500);
|
||||
|
||||
// Add the another list
|
||||
ArrayList<PrivacyItem> itemsList2 = new ArrayList<PrivacyItem>();
|
||||
item = new PrivacyItem(PrivacyItem.Type.group.name(), false, 2);
|
||||
item.setValue(groupName);
|
||||
item.setFilterMessage(true);
|
||||
itemsList2.add(item);
|
||||
privacyManager.createPrivacyList(listName2, itemsList2);
|
||||
|
||||
Thread.sleep(500);
|
||||
|
||||
// Assert the list composition.
|
||||
PrivacyList[] privacyLists = privacyManager.getPrivacyLists();
|
||||
PrivacyList receivedList1 = null;
|
||||
PrivacyList receivedList2 = null;
|
||||
for (PrivacyList privacyList : privacyLists) {
|
||||
if (listName1.equals(privacyList.toString())) {
|
||||
receivedList1 = privacyList;
|
||||
}
|
||||
if (listName2.equals(privacyList.toString())) {
|
||||
receivedList2 = privacyList;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
PrivacyItem receivedItem;
|
||||
// Assert on the list 1
|
||||
assertNotNull(receivedList1);
|
||||
assertEquals(1, receivedList1.getItems().size());
|
||||
receivedItem = receivedList1.getItems().get(0);
|
||||
assertEquals(1, receivedItem.getOrder());
|
||||
assertEquals(PrivacyItem.Type.jid, receivedItem.getType());
|
||||
assertEquals(true, receivedItem.isAllow());
|
||||
assertEquals(getConnection(0).getUser(), receivedItem.getValue());
|
||||
|
||||
// Assert on the list 2
|
||||
assertNotNull(receivedList2);
|
||||
assertEquals(1, receivedList2.getItems().size());
|
||||
receivedItem = receivedList2.getItems().get(0);
|
||||
assertEquals(2, receivedItem.getOrder());
|
||||
assertEquals(PrivacyItem.Type.group, receivedItem.getType());
|
||||
assertEquals(groupName, receivedItem.getValue());
|
||||
assertEquals(false, receivedItem.isAllow());
|
||||
assertEquals(groupName, receivedItem.getValue());
|
||||
assertEquals(false, receivedItem.isFilterEverything());
|
||||
assertEquals(true, receivedItem.isFilterMessage());
|
||||
assertEquals(false, receivedItem.isFilterPresence_in());
|
||||
assertEquals(false, receivedItem.isFilterPresence_out());
|
||||
|
||||
privacyManager.deletePrivacyList(listName1);
|
||||
privacyManager.deletePrivacyList(listName2);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check when a client set a new list and then update its content.
|
||||
*/
|
||||
public void testCreateAndUpdateList() {
|
||||
try {
|
||||
String listName = "testCreateAndUpdateList";
|
||||
String user = "tybalt@example.com";
|
||||
|
||||
PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(getConnection(0));
|
||||
PrivacyClient client = new PrivacyClient(privacyManager);
|
||||
privacyManager.addListener(client);
|
||||
|
||||
// Add the list that will be set as the active
|
||||
ArrayList<PrivacyItem> items = new ArrayList<PrivacyItem>();
|
||||
PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid.name(), true, 1);
|
||||
item.setValue(getConnection(0).getUser());
|
||||
items.add(item);
|
||||
privacyManager.createPrivacyList(listName, items);
|
||||
|
||||
Thread.sleep(500);
|
||||
|
||||
// Remove the existing item and add a new one.
|
||||
items.remove(item);
|
||||
item = new PrivacyItem(PrivacyItem.Type.jid.name(), false, 2);
|
||||
item.setValue(user);
|
||||
item.setFilterPresence_out(true);
|
||||
item.setFilterPresence_in(true);
|
||||
item.setFilterMessage(true);
|
||||
items.add(item);
|
||||
|
||||
// Update the list on server
|
||||
privacyManager.updatePrivacyList(listName, items);
|
||||
|
||||
Thread.sleep(500);
|
||||
|
||||
// Assert the list composition.
|
||||
PrivacyList list = privacyManager.getPrivacyList(listName);
|
||||
assertEquals(1, list.getItems().size());
|
||||
|
||||
// Assert the privacy item composition
|
||||
PrivacyItem receivedItem = list.getItems().get(0);
|
||||
assertEquals(2, receivedItem.getOrder());
|
||||
assertEquals(PrivacyItem.Type.jid, receivedItem.getType());
|
||||
assertEquals(false, receivedItem.isAllow());
|
||||
assertEquals(user, receivedItem.getValue());
|
||||
assertEquals(false, receivedItem.isFilterEverything());
|
||||
assertEquals(true, receivedItem.isFilterMessage());
|
||||
assertEquals(true, receivedItem.isFilterPresence_in());
|
||||
assertEquals(true, receivedItem.isFilterPresence_out());
|
||||
assertEquals(true, client.wasModified());
|
||||
|
||||
privacyManager.deletePrivacyList(listName);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check when a client denies the use of a default list.
|
||||
*/
|
||||
public void testDenyDefaultList() {
|
||||
try {
|
||||
PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(getConnection(0));
|
||||
PrivacyClient client = new PrivacyClient(privacyManager);
|
||||
privacyManager.addListener(client);
|
||||
|
||||
privacyManager.declineDefaultList();
|
||||
|
||||
Thread.sleep(500);
|
||||
|
||||
try {
|
||||
// The list should not exist and an error will be raised
|
||||
privacyManager.getDefaultList();
|
||||
} catch (XMPPException xmppException) {
|
||||
assertEquals(404, xmppException.getXMPPError().getCode());
|
||||
}
|
||||
|
||||
assertEquals(null, null);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check when a client denies the use of the active list.
|
||||
*/
|
||||
public void testDenyActiveList() {
|
||||
try {
|
||||
PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(getConnection(0));
|
||||
PrivacyClient client = new PrivacyClient(privacyManager);
|
||||
privacyManager.addListener(client);
|
||||
|
||||
privacyManager.declineActiveList();
|
||||
|
||||
Thread.sleep(500);
|
||||
|
||||
try {
|
||||
// The list should not exist and an error will be raised
|
||||
privacyManager.getActiveList();
|
||||
} catch (XMPPException xmppException) {
|
||||
assertEquals(404, xmppException.getXMPPError().getCode());
|
||||
}
|
||||
|
||||
assertEquals(null, null);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check when a client set a new default list.
|
||||
*/
|
||||
public void testCreateDefaultList() {
|
||||
try {
|
||||
String listName = "testCreateDefaultList";
|
||||
|
||||
PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(getConnection(0));
|
||||
PrivacyClient client = new PrivacyClient(privacyManager);
|
||||
privacyManager.addListener(client);
|
||||
|
||||
// Add the list that will be set as the Default
|
||||
ArrayList<PrivacyItem> items = new ArrayList<PrivacyItem>();
|
||||
PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid.name(), true, 1);
|
||||
item.setValue(getConnection(0).getUser());
|
||||
items.add(item);
|
||||
privacyManager.createPrivacyList(listName, items);
|
||||
|
||||
Thread.sleep(500);
|
||||
|
||||
// Set the Default list
|
||||
privacyManager.setDefaultListName(listName);
|
||||
|
||||
Thread.sleep(500);
|
||||
|
||||
// Assert the list composition.
|
||||
assertEquals(listName, privacyManager.getDefaultList().toString());
|
||||
List<PrivacyItem> privacyItems = privacyManager.getPrivacyList(listName).getItems();
|
||||
assertEquals(1, privacyItems.size());
|
||||
|
||||
// Assert the privacy item composition
|
||||
PrivacyItem receivedItem = privacyItems.get(0);
|
||||
assertEquals(1, receivedItem.getOrder());
|
||||
assertEquals(PrivacyItem.Type.jid, receivedItem.getType());
|
||||
assertEquals(true, receivedItem.isAllow());
|
||||
|
||||
privacyManager.deletePrivacyList(listName);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check when a client add a new list and then remove it.
|
||||
*/
|
||||
public void testRemoveList() {
|
||||
try {
|
||||
String listName = "testRemoveList";
|
||||
|
||||
PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(getConnection(0));
|
||||
PrivacyClient client = new PrivacyClient(privacyManager);
|
||||
privacyManager.addListener(client);
|
||||
|
||||
// Add the list that will be set as the Default
|
||||
ArrayList<PrivacyItem> items = new ArrayList<PrivacyItem>();
|
||||
PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid.name(), true, 1);
|
||||
item.setValue(getConnection(0).getUser());
|
||||
items.add(item);
|
||||
privacyManager.createPrivacyList(listName, items);
|
||||
|
||||
Thread.sleep(500);
|
||||
|
||||
// Set the Default list
|
||||
privacyManager.setDefaultListName(listName);
|
||||
|
||||
Thread.sleep(500);
|
||||
|
||||
privacyManager.deletePrivacyList(listName);
|
||||
|
||||
Thread.sleep(500);
|
||||
|
||||
try {
|
||||
// The list should not exist and an error will be raised
|
||||
privacyManager.getPrivacyList(listName);
|
||||
} catch (XMPPException xmppException) {
|
||||
assertEquals(404, xmppException.getXMPPError().getCode());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check different types of privacy items.
|
||||
*/
|
||||
public void testPrivacyItems() {
|
||||
try {
|
||||
String listName = "testPrivacyItems";
|
||||
String user = "tybalt@example.com";
|
||||
String groupName = "enemies";
|
||||
|
||||
PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(getConnection(0));
|
||||
PrivacyClient client = new PrivacyClient(privacyManager);
|
||||
privacyManager.addListener(client);
|
||||
|
||||
PrivacyItem[] originalPrivacyItems = new PrivacyItem[12];
|
||||
int i=0;
|
||||
|
||||
// Items to test JID
|
||||
PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid.name(), true, i);
|
||||
item.setValue(i + "_" + user);
|
||||
originalPrivacyItems[i] = item;
|
||||
i = i + 1;
|
||||
|
||||
item = new PrivacyItem(PrivacyItem.Type.jid.name(), false, i);
|
||||
item.setValue(i + "_" + user);
|
||||
originalPrivacyItems[i] = item;
|
||||
i = i + 1;
|
||||
|
||||
// Items to test suscription
|
||||
item = new PrivacyItem(PrivacyItem.Type.subscription.name(), true, i);
|
||||
item.setValue(PrivacyRule.SUBSCRIPTION_BOTH);
|
||||
originalPrivacyItems[i] = item;
|
||||
i = i + 1;
|
||||
|
||||
item = new PrivacyItem(PrivacyItem.Type.subscription.name(), false, i);
|
||||
item.setValue(PrivacyRule.SUBSCRIPTION_FROM);
|
||||
originalPrivacyItems[i] = item;
|
||||
i = i + 1;
|
||||
|
||||
item = new PrivacyItem(PrivacyItem.Type.subscription.name(), true, i);
|
||||
item.setValue(PrivacyRule.SUBSCRIPTION_TO);
|
||||
originalPrivacyItems[i] = item;
|
||||
i = i + 1;
|
||||
|
||||
item = new PrivacyItem(PrivacyItem.Type.subscription.name(), false, i);
|
||||
item.setValue(PrivacyRule.SUBSCRIPTION_NONE);
|
||||
originalPrivacyItems[i] = item;
|
||||
i = i + 1;
|
||||
|
||||
// Items to test Group
|
||||
item = new PrivacyItem(PrivacyItem.Type.group.name(), false, i);
|
||||
item.setValue(groupName);
|
||||
originalPrivacyItems[i] = item;
|
||||
i = i + 1;
|
||||
|
||||
// Items to test messages
|
||||
item = new PrivacyItem(PrivacyItem.Type.group.name(), false, i);
|
||||
item.setValue(groupName);
|
||||
item.setFilterMessage(true);
|
||||
originalPrivacyItems[i] = item;
|
||||
i = i + 1;
|
||||
|
||||
// Items to test presence notifications
|
||||
item = new PrivacyItem(PrivacyItem.Type.group.name(), false, i);
|
||||
item.setValue(groupName);
|
||||
item.setFilterMessage(true);
|
||||
originalPrivacyItems[i] = item;
|
||||
i = i + 1;
|
||||
|
||||
item = new PrivacyItem(null, false, i);
|
||||
item.setFilterPresence_in(true);
|
||||
originalPrivacyItems[i] = item;
|
||||
i = i + 1;
|
||||
|
||||
item = new PrivacyItem(PrivacyItem.Type.subscription.name(), false, i);
|
||||
item.setValue(PrivacyRule.SUBSCRIPTION_TO);
|
||||
item.setFilterPresence_out(true);
|
||||
originalPrivacyItems[i] = item;
|
||||
i = i + 1;
|
||||
|
||||
item = new PrivacyItem(PrivacyItem.Type.jid.name(), false, i);
|
||||
item.setValue(i + "_" + user);
|
||||
item.setFilterPresence_out(true);
|
||||
item.setFilterPresence_in(true);
|
||||
item.setFilterMessage(true);
|
||||
originalPrivacyItems[i] = item;
|
||||
|
||||
// Set the new privacy list
|
||||
privacyManager.createPrivacyList(listName, Arrays.asList(originalPrivacyItems));
|
||||
|
||||
Thread.sleep(500);
|
||||
|
||||
// Assert the server list composition.
|
||||
List<PrivacyItem> privacyItems = privacyManager.getPrivacyList(listName).getItems();
|
||||
assertEquals(originalPrivacyItems.length, privacyItems.size());
|
||||
|
||||
// Assert the local and server privacy item composition
|
||||
PrivacyItem originalItem;
|
||||
PrivacyItem receivedItem;
|
||||
int index;
|
||||
for (int j = 0; j < originalPrivacyItems.length; j++) {
|
||||
// Look for the same server and original items
|
||||
receivedItem = privacyItems.get(j);
|
||||
index = 0;
|
||||
while ((index < originalPrivacyItems.length)
|
||||
&& (originalPrivacyItems[index].getOrder() != receivedItem.getOrder())) {
|
||||
index++;
|
||||
}
|
||||
originalItem = originalPrivacyItems[index];
|
||||
|
||||
// Assert the items
|
||||
assertEquals(originalItem.getOrder(), receivedItem.getOrder());
|
||||
assertEquals(originalItem.getType(), receivedItem.getType());
|
||||
assertEquals(originalItem.isAllow(), receivedItem.isAllow());
|
||||
assertEquals(originalItem.getValue(), receivedItem.getValue());
|
||||
assertEquals(originalItem.isFilterEverything(), receivedItem.isFilterEverything());
|
||||
assertEquals(originalItem.isFilterIQ(), receivedItem.isFilterIQ());
|
||||
assertEquals(originalItem.isFilterMessage(), receivedItem.isFilterMessage());
|
||||
assertEquals(originalItem.isFilterPresence_in(), receivedItem.isFilterPresence_in());
|
||||
assertEquals(originalItem.isFilterPresence_out(), receivedItem.isFilterPresence_out());
|
||||
}
|
||||
|
||||
privacyManager.deletePrivacyList(listName);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
protected int getMaxConnections() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* This class supports automated tests about privacy communication from the
|
||||
* server to the client.
|
||||
*
|
||||
* @author Francisco Vives
|
||||
*/
|
||||
|
||||
public class PrivacyClient implements PrivacyListListener {
|
||||
/**
|
||||
* holds if the receiver list was modified
|
||||
*/
|
||||
private boolean wasModified = false;
|
||||
|
||||
/**
|
||||
* holds a privacy to hold server requests Clients should not use Privacy
|
||||
* class since it is private for the smack framework.
|
||||
*/
|
||||
private Privacy privacy = new Privacy();
|
||||
|
||||
public PrivacyClient(PrivacyListManager manager) {
|
||||
super();
|
||||
}
|
||||
|
||||
public void setPrivacyList(String listName, List<PrivacyItem> listItem) {
|
||||
privacy.setPrivacyList(listName, listItem);
|
||||
}
|
||||
|
||||
public void updatedPrivacyList(String listName) {
|
||||
this.wasModified = true;
|
||||
}
|
||||
|
||||
public boolean wasModified() {
|
||||
return this.wasModified;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue