1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-12-07 05:31:08 +01:00

Migrate from Ant to Gradle (SMACK-265)

This commit is contained in:
Florian Schmaus 2014-02-14 18:13:51 +01:00
parent 235eca3a3d
commit 201152ef42
767 changed files with 1088 additions and 4296 deletions

View file

@ -0,0 +1,43 @@
/**
*
* Copyright 2005 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.util;
import junit.framework.TestCase;
/**
* A test case for the Cache class.
*/
public class CacheTest extends TestCase {
public void testMaxSize() {
Cache<Integer, String> cache = new Cache<Integer, String>(100, -1);
for (int i=0; i < 1000; i++) {
cache.put(i, "value");
assertTrue("Cache size must never be larger than 100.", cache.size() <= 100);
}
}
public void testLRU() {
Cache<Integer, String> cache = new Cache<Integer, String>(100, -1);
for (int i=0; i < 1000; i++) {
cache.put(i, "value");
assertTrue("LRU algorithm for cache key of '0' failed.",
cache.get(new Integer(0)) != null);
}
}
}

View file

@ -0,0 +1,29 @@
package org.jivesoftware.smack.util;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.XMPPException;
public class ConnectionUtils {
private ConnectionUtils() {}
public static void becomeFriends(Connection con0, Connection con1) throws XMPPException {
Roster r0 = con0.getRoster();
Roster r1 = con1.getRoster();
r0.setSubscriptionMode(Roster.SubscriptionMode.accept_all);
r1.setSubscriptionMode(Roster.SubscriptionMode.accept_all);
r0.createEntry(con1.getUser(), "u2", null);
r1.createEntry(con0.getUser(), "u1", null);
}
public static void letsAllBeFriends(Connection[] connections) throws XMPPException {
for (Connection c1 : connections) {
for (Connection c2 : connections) {
if (c1 == c2)
continue;
becomeFriends(c1, c2);
}
}
}
}

View file

@ -0,0 +1,147 @@
package org.jivesoftware.smack.util;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
import java.util.ArrayList;
import java.util.List;
import org.jivesoftware.smack.util.dns.DNSJavaResolver;
import org.jivesoftware.smack.util.dns.DNSResolver;
import org.jivesoftware.smack.util.dns.HostAddress;
import org.jivesoftware.smack.util.dns.JavaxResolver;
import org.jivesoftware.smack.util.dns.SRVRecord;
import org.junit.Test;
public class DNSUtilTest {
private static final String igniterealtimeDomain = "igniterealtime.org";
private static final String igniterealtimeXMPPServer = "xmpp." + igniterealtimeDomain;
private static final int igniterealtimeClientPort = 5222;
private static final int igniterealtimeServerPort = 5269;
@Test
public void xmppClientDomainJavaXTest() {
DNSResolver resolver = JavaxResolver.getInstance();
assertNotNull(resolver);
DNSUtil.setDNSResolver(resolver);
xmppClientDomainTest();
}
@Test
public void xmppServerDomainJavaXTest() {
DNSResolver resolver = JavaxResolver.getInstance();
assertNotNull(resolver);
DNSUtil.setDNSResolver(resolver);
xmppServerDomainTest();
}
@Test
public void xmppClientDomainDNSJavaTest() {
DNSResolver resolver = DNSJavaResolver.getInstance();
assertNotNull(resolver);
DNSUtil.setDNSResolver(resolver);
xmppClientDomainTest();
}
@Test
public void xmppServerDomainDNSJavaTest() {
DNSResolver resolver = DNSJavaResolver.getInstance();
assertNotNull(resolver);
DNSUtil.setDNSResolver(resolver);
xmppServerDomainTest();
}
@Test
public void sortSRVlowestPrioFirstTest() {
List<HostAddress> sortedRecords = DNSUtil.sortSRVRecords(createSRVRecords());
assertTrue(sortedRecords.get(0).getFQDN().equals("0.20.foo.bar"));
}
@Test
public void sortSRVdistributeOverWeights() {
int weight50 = 0;
int weight20one = 0;
int weight20two = 0;
int weight10 = 0;
for (int i = 0; i < 1000; i++) {
List<HostAddress> sortedRecords = DNSUtil.sortSRVRecords(createSRVRecords());
String host = sortedRecords.get(1).getFQDN();
if (host.equals("5.20.one.foo.bar")) {
weight20one++;
} else if (host.equals("5.20.two.foo.bar")) {
weight20two++;
} else if (host.equals("5.10.foo.bar")) {
weight10++;
} else if (host.equals("5.50.foo.bar")) {
weight50++;
} else {
fail("Wrong host after SRVRecord sorting");
}
}
assertTrue(weight50 > 400 && weight50 < 600);
assertTrue(weight20one > 100 && weight20one < 300);
assertTrue(weight20two > 100 && weight20two < 300);
assertTrue(weight10 > 0&& weight10 < 200);
}
@Test
public void sortSRVdistributeZeroWeights() {
int weightZeroOne = 0;
int weightZeroTwo = 0;
for (int i = 0; i < 1000; i++) {
List<HostAddress> sortedRecords = DNSUtil.sortSRVRecords(createSRVRecords());
// Remove the first 5 records with a lower priority
for (int j = 0; j < 5; j++) {
sortedRecords.remove(0);
}
String host = sortedRecords.remove(0).getFQDN();
if (host.equals("10.0.one.foo.bar")) {
weightZeroOne++;
} else if (host.endsWith("10.0.two.foo.bar")) {
weightZeroTwo++;
} else {
fail("Wrong host after SRVRecord sorting");
}
}
assertTrue(weightZeroOne > 400 && weightZeroOne < 600);
assertTrue(weightZeroTwo > 400 && weightZeroTwo < 600);
}
private void xmppClientDomainTest() {
List<HostAddress> hostAddresses = DNSUtil.resolveXMPPDomain(igniterealtimeDomain);
HostAddress ha = hostAddresses.get(0);
assertEquals(ha.getFQDN(), igniterealtimeXMPPServer);
assertEquals(ha.getPort(), igniterealtimeClientPort);
}
private void xmppServerDomainTest() {
List<HostAddress> hostAddresses = DNSUtil.resolveXMPPServerDomain(igniterealtimeDomain);
HostAddress ha = hostAddresses.get(0);
assertEquals(ha.getFQDN(), igniterealtimeXMPPServer);
assertEquals(ha.getPort(), igniterealtimeServerPort);
}
private static List<SRVRecord> createSRVRecords() {
List<SRVRecord> records = new ArrayList<SRVRecord>();
// We create one record with priority 0 that should also be tried first
// Then 4 records with priority 5 and different weights (50, 20, 20, 10)
// Then 2 records with priority 10 and weight 0 which should be treaded equal
// These records are added in a 'random' way to the list
try {
records.add(new SRVRecord("5.20.one.foo.bar", 42, 5, 20)); // Priority 5, Weight 20
records.add(new SRVRecord("10.0.one.foo.bar", 42, 10, 0)); // Priority 10, Weight 0
records.add(new SRVRecord("5.10.foo.bar", 42, 5, 10)); // Priority 5, Weight 10
records.add(new SRVRecord("10.0.two.foo.bar", 42, 10, 0)); // Priority 10, Weight 0
records.add(new SRVRecord("5.20.two.foo.bar", 42, 5, 20)); // Priority 5, Weight 20
records.add(new SRVRecord("0.20.foo.bar", 42, 0, 20)); // Priority 0, Weight 20
records.add(new SRVRecord("5.50.foo.bar", 42, 5, 50)); // Priority 5, Weight 50
} catch (IllegalArgumentException e) {
// Ignore
}
assertTrue(records.size() > 0);
return records;
}
}

View file

@ -0,0 +1,203 @@
/**
*
* Copyright 2006 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.util;
import java.io.StringReader;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.test.SmackTestCase;
import org.xmlpull.mxp1.MXParser;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
public class XMPPErrorTest extends SmackTestCase {
public XMPPErrorTest(String arg0) {
super(arg0);
}
/**
* Check the creation of a new xmppError locally.
*/
public void testLocalErrorCreation() {
XMPPError error = new XMPPError(XMPPError.Condition.item_not_found);
error.toXML();
assertEquals(error.getCondition(), "item-not-found");
assertEquals(error.getCode(), 404);
assertEquals(error.getType(), XMPPError.Type.CANCEL);
assertNull(error.getMessage());
}
/**
* Check the creation of a new xmppError locally.
*/
public void testLocalErrorWithCommentCreation() {
String message = "Error Message";
XMPPError error = new XMPPError(XMPPError.Condition.item_not_found, message);
error.toXML();
assertEquals(error.getCondition(), "item-not-found");
assertEquals(error.getCode(), 404);
assertEquals(error.getType(), XMPPError.Type.CANCEL);
assertEquals(error.getMessage(), message);
}
/**
* Check the creation of a new xmppError locally where there is not a default defined.
*/
public void testUserDefinedErrorWithCommentCreation() {
String message = "Error Message";
XMPPError error = new XMPPError(new XMPPError.Condition("my_own_error"), message);
error.toXML();
assertEquals(error.getCondition(), "my_own_error");
assertEquals(error.getCode(), 0);
assertNull(error.getType());
assertEquals(error.getMessage(), message);
}
/**
* Check the parser with an xml with the 404 error.
*/
public void test404() {
// Make the XML to test
String xml = "<error code='404' 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
XMPPError packet = parseError(parser);
assertNotNull(packet);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
/**
* Check the parser with an xml with the 404 error.
*/
public void testCancel() {
// Make the XML to test
String xml = "<error type='cancel'>" +
"<conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" +
"</error>";
try {
// Create the xml parser
XmlPullParser parser = getParserFromXML(xml);
// Create a packet from the xml
XMPPError error = parseError(parser);
assertNotNull(error);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
public void testMessageAndApplicationDefinedError() {
String xml = "<error type='modify' code='404'>" +
"<undefined-condition xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" +
"<text xml:lang='en' xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'>" +
"Some special application diagnostic information..." +
"</text>" +
"<special-application-condition xmlns='application-ns'/>" +
"</error>";
try {
// Create the xml parser
XmlPullParser parser = getParserFromXML(xml);
// Create a packet from the xml
XMPPError error = parseError(parser);
String sendingXML = error.toXML();
assertNotNull(error);
assertNotNull(sendingXML);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
/**
* Check the parser with an xml with the 404 error.
*/
public void testCancelWithMessage() {
// Make the XML to test
String xml = "<error type='cancel'>" +
"<conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" +
"<text xmlns='urn:ietf:params:xml:ns:xmpp-stanzas' xml:lang='langcode'>" +
"Some special application diagnostic information!" +
"</text>" +
"</error>";
try {
// Create the xml parser
XmlPullParser parser = getParserFromXML(xml);
// Create a packet from the xml
XMPPError error = parseError(parser);
assertNotNull(error);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
/**
* Check the parser with an xml with the 404 error.
*/
public void testCancelWithMessageAndApplicationError() {
// Make the XML to test
String xml = "<error type='cancel' code='10'>" +
"<conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" +
"<text xml:lang='en' xmlns='urn:ietf:params:xml:ns:xmpp-streams'>" +
"Some special application diagnostic information!" +
"</text>" +
"<application-defined-error xmlns='application-ns'/>" +
"</error>";
try {
// Create the xml parser
XmlPullParser parser = getParserFromXML(xml);
// Create a packet from the xml
XMPPError error = parseError(parser);
assertNotNull(error);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
private XMPPError parseError(XmlPullParser parser) throws Exception {
parser.next();
return PacketParserUtils.parseError(parser);
}
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;
}
}