mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-10 17:49:38 +02:00
SMACK-391 Moved all date parsing into StringUtils and made it much more robust in handling multiple valid and invalid formats.
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@13428 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
2afd3bdfa3
commit
1d36ae8db4
5 changed files with 505 additions and 202 deletions
|
@ -7,22 +7,30 @@
|
|||
*/
|
||||
package org.jivesoftware.smack.util;
|
||||
|
||||
import static junit.framework.Assert.*;
|
||||
import static org.custommonkey.xmlunit.XMLAssert.*;
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertFalse;
|
||||
import static junit.framework.Assert.assertNotNull;
|
||||
import static junit.framework.Assert.assertNull;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
import static junit.framework.Assert.fail;
|
||||
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
||||
import static org.custommonkey.xmlunit.XMLAssert.assertXMLNotEqual;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.Properties;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.custommonkey.xmlunit.DetailedDiff;
|
||||
import org.custommonkey.xmlunit.Diff;
|
||||
import org.jivesoftware.smack.TestUtils;
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smack.packet.Presence;
|
||||
import org.jivesoftware.smackx.packet.DelayInformation;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.xmlpull.mxp1.MXParser;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
import com.jamesmurty.utils.XMLBuilder;
|
||||
|
@ -56,7 +64,7 @@ public class PacketParserUtilsTest {
|
|||
.asString(outputProperties);
|
||||
|
||||
Message message = (Message) PacketParserUtils
|
||||
.parseMessage(getParser(control));
|
||||
.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertEquals(defaultLanguage, message.getBody());
|
||||
assertTrue(message.getBodyLanguages().isEmpty());
|
||||
|
@ -75,7 +83,7 @@ public class PacketParserUtilsTest {
|
|||
.t(otherLanguage)
|
||||
.asString(outputProperties);
|
||||
|
||||
message = (Message) PacketParserUtils.parseMessage(getParser(control));
|
||||
message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertEquals(otherLanguage, message.getBody());
|
||||
assertTrue(message.getBodyLanguages().isEmpty());
|
||||
|
@ -93,7 +101,7 @@ public class PacketParserUtilsTest {
|
|||
.t(defaultLanguage)
|
||||
.asString(outputProperties);
|
||||
|
||||
message = (Message) PacketParserUtils.parseMessage(getParser(control));
|
||||
message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertEquals(defaultLanguage, message.getBody());
|
||||
assertTrue(message.getBodyLanguages().isEmpty());
|
||||
|
@ -112,7 +120,7 @@ public class PacketParserUtilsTest {
|
|||
.t(defaultLanguage)
|
||||
.asString(outputProperties);
|
||||
|
||||
message = (Message) PacketParserUtils.parseMessage(getParser(control));
|
||||
message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertEquals(defaultLanguage, message.getBody());
|
||||
assertTrue(message.getBodyLanguages().isEmpty());
|
||||
|
@ -133,7 +141,7 @@ public class PacketParserUtilsTest {
|
|||
.t(otherLanguage)
|
||||
.asString(outputProperties);
|
||||
|
||||
message = (Message) PacketParserUtils.parseMessage(getParser(control));
|
||||
message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertNull(message.getBody());
|
||||
assertFalse(message.getBodyLanguages().isEmpty());
|
||||
|
@ -154,7 +162,7 @@ public class PacketParserUtilsTest {
|
|||
.t(otherLanguage)
|
||||
.asString(outputProperties);
|
||||
|
||||
message = (Message) PacketParserUtils.parseMessage(getParser(control));
|
||||
message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertNull(message.getBody());
|
||||
assertFalse(message.getBodyLanguages().isEmpty());
|
||||
|
@ -175,7 +183,7 @@ public class PacketParserUtilsTest {
|
|||
.t(defaultLanguage)
|
||||
.asString(outputProperties);
|
||||
|
||||
message = (Message) PacketParserUtils.parseMessage(getParser(control));
|
||||
message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertNull(message.getBody());
|
||||
assertFalse(message.getBodyLanguages().isEmpty());
|
||||
|
@ -205,7 +213,7 @@ public class PacketParserUtilsTest {
|
|||
.asString(outputProperties);
|
||||
|
||||
Message message = (Message) PacketParserUtils
|
||||
.parseMessage(getParser(control));
|
||||
.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertEquals(defaultLanguage, message.getSubject());
|
||||
assertTrue(message.getSubjectLanguages().isEmpty());
|
||||
|
@ -224,7 +232,7 @@ public class PacketParserUtilsTest {
|
|||
.t(otherLanguage)
|
||||
.asString(outputProperties);
|
||||
|
||||
message = (Message) PacketParserUtils.parseMessage(getParser(control));
|
||||
message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertEquals(otherLanguage, message.getSubject());
|
||||
assertTrue(message.getSubjectLanguages().isEmpty());
|
||||
|
@ -242,7 +250,7 @@ public class PacketParserUtilsTest {
|
|||
.t(defaultLanguage)
|
||||
.asString(outputProperties);
|
||||
|
||||
message = (Message) PacketParserUtils.parseMessage(getParser(control));
|
||||
message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertEquals(defaultLanguage, message.getSubject());
|
||||
assertTrue(message.getSubjectLanguages().isEmpty());
|
||||
|
@ -261,7 +269,7 @@ public class PacketParserUtilsTest {
|
|||
.t(defaultLanguage)
|
||||
.asString(outputProperties);
|
||||
|
||||
message = (Message) PacketParserUtils.parseMessage(getParser(control));
|
||||
message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertEquals(defaultLanguage, message.getSubject());
|
||||
assertTrue(message.getSubjectLanguages().isEmpty());
|
||||
|
@ -282,7 +290,7 @@ public class PacketParserUtilsTest {
|
|||
.t(otherLanguage)
|
||||
.asString(outputProperties);
|
||||
|
||||
message = (Message) PacketParserUtils.parseMessage(getParser(control));
|
||||
message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertNull(message.getSubject());
|
||||
assertFalse(message.getSubjectLanguages().isEmpty());
|
||||
|
@ -303,7 +311,7 @@ public class PacketParserUtilsTest {
|
|||
.t(otherLanguage)
|
||||
.asString(outputProperties);
|
||||
|
||||
message = (Message) PacketParserUtils.parseMessage(getParser(control));
|
||||
message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertNull(message.getSubject());
|
||||
assertFalse(message.getSubjectLanguages().isEmpty());
|
||||
|
@ -324,7 +332,7 @@ public class PacketParserUtilsTest {
|
|||
.t(defaultLanguage)
|
||||
.asString(outputProperties);
|
||||
|
||||
message = (Message) PacketParserUtils.parseMessage(getParser(control));
|
||||
message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertNull(message.getSubject());
|
||||
assertFalse(message.getSubjectLanguages().isEmpty());
|
||||
|
@ -359,7 +367,7 @@ public class PacketParserUtilsTest {
|
|||
.asString(outputProperties);
|
||||
|
||||
message = (Message) PacketParserUtils
|
||||
.parseMessage(getParser(control));
|
||||
.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertEquals(defaultLanguage, message.getBody());
|
||||
assertEquals(otherLanguage, message.getBody(otherLanguage));
|
||||
|
@ -384,7 +392,7 @@ public class PacketParserUtilsTest {
|
|||
.asString(outputProperties);
|
||||
|
||||
message = (Message) PacketParserUtils
|
||||
.parseMessage(getParser(control));
|
||||
.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertEquals(defaultLanguage, message.getBody());
|
||||
assertEquals(defaultLanguage, message.getBody(defaultLanguage));
|
||||
|
@ -408,7 +416,7 @@ public class PacketParserUtilsTest {
|
|||
.asString(outputProperties);
|
||||
|
||||
message = (Message) PacketParserUtils
|
||||
.parseMessage(getParser(control));
|
||||
.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertEquals(otherLanguage, message.getBody());
|
||||
assertEquals(defaultLanguage, message.getBody(defaultLanguage));
|
||||
|
@ -432,7 +440,7 @@ public class PacketParserUtilsTest {
|
|||
.asString(outputProperties);
|
||||
|
||||
message = (Message) PacketParserUtils
|
||||
.parseMessage(getParser(control));
|
||||
.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertEquals(defaultLanguage, message.getBody());
|
||||
assertEquals(defaultLanguage, message.getBody(defaultLanguage));
|
||||
|
@ -455,7 +463,7 @@ public class PacketParserUtilsTest {
|
|||
.asString(outputProperties);
|
||||
|
||||
message = (Message) PacketParserUtils
|
||||
.parseMessage(getParser(control));
|
||||
.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertEquals(defaultLanguage, message.getBody());
|
||||
assertEquals(defaultLanguage, message.getBody(defaultLanguage));
|
||||
|
@ -478,7 +486,7 @@ public class PacketParserUtilsTest {
|
|||
.asString(outputProperties);
|
||||
|
||||
message = (Message) PacketParserUtils
|
||||
.parseMessage(getParser(control));
|
||||
.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertEquals(defaultLanguage, message.getBody());
|
||||
assertEquals(defaultLanguage, message.getBody(defaultLanguage));
|
||||
|
@ -512,7 +520,7 @@ public class PacketParserUtilsTest {
|
|||
.asString(outputProperties);
|
||||
|
||||
message = (Message) PacketParserUtils
|
||||
.parseMessage(getParser(control));
|
||||
.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertEquals(defaultLanguage, message.getSubject());
|
||||
assertEquals(otherLanguage, message.getSubject(otherLanguage));
|
||||
|
@ -537,7 +545,7 @@ public class PacketParserUtilsTest {
|
|||
.asString(outputProperties);
|
||||
|
||||
message = (Message) PacketParserUtils
|
||||
.parseMessage(getParser(control));
|
||||
.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertEquals(defaultLanguage, message.getSubject());
|
||||
assertEquals(defaultLanguage, message.getSubject(defaultLanguage));
|
||||
|
@ -561,7 +569,7 @@ public class PacketParserUtilsTest {
|
|||
.asString(outputProperties);
|
||||
|
||||
message = (Message) PacketParserUtils
|
||||
.parseMessage(getParser(control));
|
||||
.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertEquals(otherLanguage, message.getSubject());
|
||||
assertEquals(defaultLanguage, message.getSubject(defaultLanguage));
|
||||
|
@ -585,7 +593,7 @@ public class PacketParserUtilsTest {
|
|||
.asString(outputProperties);
|
||||
|
||||
message = (Message) PacketParserUtils
|
||||
.parseMessage(getParser(control));
|
||||
.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertEquals(defaultLanguage, message.getSubject());
|
||||
assertEquals(defaultLanguage, message.getSubject(defaultLanguage));
|
||||
|
@ -608,7 +616,7 @@ public class PacketParserUtilsTest {
|
|||
.asString(outputProperties);
|
||||
|
||||
message = (Message) PacketParserUtils
|
||||
.parseMessage(getParser(control));
|
||||
.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertEquals(defaultLanguage, message.getSubject());
|
||||
assertEquals(defaultLanguage, message.getSubject(defaultLanguage));
|
||||
|
@ -631,7 +639,7 @@ public class PacketParserUtilsTest {
|
|||
.asString(outputProperties);
|
||||
|
||||
message = (Message) PacketParserUtils
|
||||
.parseMessage(getParser(control));
|
||||
.parseMessage(TestUtils.getMessageParser(control));
|
||||
|
||||
assertEquals(defaultLanguage, message.getSubject());
|
||||
assertEquals(defaultLanguage, message.getSubject(defaultLanguage));
|
||||
|
@ -657,7 +665,7 @@ public class PacketParserUtilsTest {
|
|||
.asString(outputProperties);
|
||||
|
||||
try {
|
||||
Message message = (Message) PacketParserUtils.parseMessage(getParser(control));
|
||||
Message message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
|
||||
String body = "<span style=\"font-weight: bold;\">"
|
||||
+ "Bad Message Body</span>";
|
||||
assertEquals(body, message.getBody());
|
||||
|
@ -689,7 +697,7 @@ public class PacketParserUtilsTest {
|
|||
String invalidControl = validControl.replace("Good Message Body", "Bad </span> Body");
|
||||
|
||||
try {
|
||||
PacketParserUtils.parseMessage(getParser(invalidControl));
|
||||
PacketParserUtils.parseMessage(TestUtils.getMessageParser(invalidControl));
|
||||
fail("Exception should be thrown");
|
||||
} catch(XmlPullParserException e) {
|
||||
assertTrue(e.getMessage().contains("end tag name </span>"));
|
||||
|
@ -698,7 +706,7 @@ public class PacketParserUtilsTest {
|
|||
invalidControl = validControl.replace("Good Message Body", "Bad </body> Body");
|
||||
|
||||
try {
|
||||
PacketParserUtils.parseMessage(getParser(invalidControl));
|
||||
PacketParserUtils.parseMessage(TestUtils.getMessageParser(invalidControl));
|
||||
fail("Exception should be thrown");
|
||||
} catch(XmlPullParserException e) {
|
||||
assertTrue(e.getMessage().contains("end tag name </body>"));
|
||||
|
@ -707,7 +715,7 @@ public class PacketParserUtilsTest {
|
|||
invalidControl = validControl.replace("Good Message Body", "Bad </message> Body");
|
||||
|
||||
try {
|
||||
PacketParserUtils.parseMessage(getParser(invalidControl));
|
||||
PacketParserUtils.parseMessage(TestUtils.getMessageParser(invalidControl));
|
||||
fail("Exception should be thrown");
|
||||
} catch(XmlPullParserException e) {
|
||||
assertTrue(e.getMessage().contains("end tag name </message>"));
|
||||
|
@ -736,20 +744,94 @@ public class PacketParserUtilsTest {
|
|||
.t("This is a test of the emergency broadcast system, 3.")
|
||||
.asString(outputProperties);
|
||||
|
||||
Packet message = PacketParserUtils.parseMessage(getParser(control));
|
||||
Packet message = PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
|
||||
assertXMLEqual(control, message.toXML());
|
||||
}
|
||||
|
||||
private XmlPullParser getParser(String control) throws XmlPullParserException, IOException {
|
||||
XmlPullParser parser = new MXParser();
|
||||
parser.setInput(new StringReader(control));
|
||||
while(true) {
|
||||
if(parser.next() == XmlPullParser.START_TAG
|
||||
&& parser.getName().equals("message")) { break; }
|
||||
}
|
||||
return parser;
|
||||
|
||||
@Test
|
||||
public void validateSimplePresence() throws Exception {
|
||||
String stanza = "<presence from='juliet@example.com/balcony' to='romeo@example.net'/>";
|
||||
|
||||
Presence presence = PacketParserUtils.parsePresence(TestUtils.getPresenceParser(stanza));
|
||||
|
||||
assertXMLEqual(stanza, presence.toXML());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validatePresenceProbe() throws Exception {
|
||||
String stanza = "<presence from='mercutio@example.com' id='xv291f38' to='juliet@example.com' type='unsubscribed'/>";
|
||||
|
||||
Presence presence = PacketParserUtils.parsePresence(TestUtils.getPresenceParser(stanza));
|
||||
|
||||
assertXMLEqual(stanza, presence.toXML());
|
||||
assertEquals(Presence.Type.unsubscribed, presence.getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validatePresenceOptionalElements() throws Exception {
|
||||
String stanza = "<presence xml:lang='en' type='unsubscribed'>"
|
||||
+ "<show>dnd</show>"
|
||||
+ "<status>Wooing Juliet</status>"
|
||||
+ "<priority>1</priority>"
|
||||
+ "</presence>";
|
||||
|
||||
Presence presence = PacketParserUtils.parsePresence(TestUtils.getPresenceParser(stanza));
|
||||
assertXMLEqual(stanza, presence.toXML());
|
||||
assertEquals(Presence.Type.unsubscribed, presence.getType());
|
||||
assertEquals("dnd", presence.getMode().name());
|
||||
assertEquals("en", presence.getLanguage());
|
||||
assertEquals("Wooing Juliet", presence.getStatus());
|
||||
assertEquals(1, presence.getPriority());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validatePresenceWithDelayedDelivery() throws Exception {
|
||||
String stanza = "<presence from='mercutio@example.com' to='juliet@example.com'>"
|
||||
+ "<delay xmlns='urn:xmpp:delay' stamp='2002-09-10T23:41:07Z'/></presence>";
|
||||
|
||||
Presence presence = PacketParserUtils.parsePresence(TestUtils.getPresenceParser(stanza));
|
||||
|
||||
DelayInformation delay = (DelayInformation) presence.getExtension("urn:xmpp:delay");
|
||||
assertNotNull(delay);
|
||||
Date date = StringUtils.parseDate("2002-09-10T23:41:07Z");
|
||||
assertEquals(date, delay.getStamp());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validatePresenceWithLegacyDelayed() throws Exception {
|
||||
String stanza = "<presence from='mercutio@example.com' to='juliet@example.com'>"
|
||||
+ "<x xmlns='jabber:x:delay' stamp='20020910T23:41:07'/></presence>";
|
||||
|
||||
Presence presence = PacketParserUtils.parsePresence(TestUtils.getPresenceParser(stanza));
|
||||
|
||||
DelayInformation delay = (DelayInformation) presence.getExtension("jabber:x:delay");
|
||||
assertNotNull(delay);
|
||||
Date date = StringUtils.parseDate("20020910T23:41:07");
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
cal.setTime(date);
|
||||
assertEquals(cal.getTime(), delay.getStamp());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parsePresenceWithInvalidDelayedDelivery() throws Exception {
|
||||
String stanza = "<presence from='mercutio@example.com' to='juliet@example.com'>"
|
||||
+ "<delay xmlns='urn:xmpp:delay'/></presence>";
|
||||
|
||||
Presence presence = PacketParserUtils.parsePresence(TestUtils.getPresenceParser(stanza));
|
||||
assertNull(presence.getExtension("urn:xmpp:delay"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parsePresenceWithInvalidLegacyDelayed() throws Exception {
|
||||
String stanza = "<presence from='mercutio@example.com' to='juliet@example.com'>"
|
||||
+ "<x xmlns='jabber:x:delay'/></presence>";
|
||||
|
||||
Presence presence = PacketParserUtils.parsePresence(TestUtils.getPresenceParser(stanza));
|
||||
DelayInformation delay = (DelayInformation) presence.getExtension("urn:xmpp:delay");
|
||||
assertNull(delay);
|
||||
}
|
||||
|
||||
private String determineNonDefaultLanguage() {
|
||||
String otherLanguage = "jp";
|
||||
Locale[] availableLocales = Locale.getAvailableLocales();
|
||||
|
|
394
test-unit/org/jivesoftware/smack/util/StringUtilsTest.java
Normal file
394
test-unit/org/jivesoftware/smack/util/StringUtilsTest.java
Normal file
|
@ -0,0 +1,394 @@
|
|||
/**
|
||||
* $RCSfile$
|
||||
* $Revision$
|
||||
* $Date$
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||
* ====================================================================
|
||||
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by
|
||||
* Jive Software (http://www.jivesoftware.com)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please
|
||||
* contact webmaster@jivesoftware.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Smack",
|
||||
* nor may "Smack" appear in their name, without prior written
|
||||
* permission of Jive Software.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smack.util;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* A test case for the StringUtils class.
|
||||
*/
|
||||
public class StringUtilsTest {
|
||||
@Test
|
||||
public void testEscapeForXML() {
|
||||
String input = null;
|
||||
|
||||
assertNull(StringUtils.escapeForXML(null));
|
||||
|
||||
input = "<b>";
|
||||
assertEquals("<b>", StringUtils.escapeForXML(input));
|
||||
|
||||
input = "\"";
|
||||
assertEquals(""", StringUtils.escapeForXML(input));
|
||||
|
||||
input = "&";
|
||||
assertEquals("&", StringUtils.escapeForXML(input));
|
||||
|
||||
input = "<b>\n\t\r</b>";
|
||||
assertEquals("<b>\n\t\r</b>", StringUtils.escapeForXML(input));
|
||||
|
||||
input = " & ";
|
||||
assertEquals(" & ", StringUtils.escapeForXML(input));
|
||||
|
||||
input = " \" ";
|
||||
assertEquals(" " ", StringUtils.escapeForXML(input));
|
||||
|
||||
input = "> of me <";
|
||||
assertEquals("> of me <", StringUtils.escapeForXML(input));
|
||||
|
||||
input = "> of me & you<";
|
||||
assertEquals("> of me & you<", StringUtils.escapeForXML(input));
|
||||
|
||||
input = "& <";
|
||||
assertEquals("& <", StringUtils.escapeForXML(input));
|
||||
|
||||
input = "&";
|
||||
assertEquals("&", StringUtils.escapeForXML(input));
|
||||
|
||||
input = "It's a good day today";
|
||||
assertEquals("It's a good day today", StringUtils.escapeForXML(input));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHash() {
|
||||
// Test null
|
||||
// @TODO - should the StringUtils.hash(String) method be fixed to handle null input?
|
||||
try {
|
||||
StringUtils.hash(null);
|
||||
fail();
|
||||
}
|
||||
catch (NullPointerException npe) {
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
// Test empty String
|
||||
String result = StringUtils.hash("");
|
||||
assertEquals("da39a3ee5e6b4b0d3255bfef95601890afd80709", result);
|
||||
|
||||
// Test a known hash
|
||||
String adminInHash = "d033e22ae348aeb5660fc2140aec35850c4da997";
|
||||
result = StringUtils.hash("admin");
|
||||
assertEquals(adminInHash, result);
|
||||
|
||||
// Test a random String - make sure all resulting characters are valid hash characters
|
||||
// and that the returned string is 32 characters long.
|
||||
String random = "jive software blah and stuff this is pretty cool";
|
||||
result = StringUtils.hash(random);
|
||||
assertTrue(isValidHash(result));
|
||||
|
||||
// Test junk input:
|
||||
String junk = "\n\n\t\b\r!@(!)^(#)@+_-\u2031\u09291\u00A9\u00BD\u0394\u00F8";
|
||||
result = StringUtils.hash(junk);
|
||||
assertTrue(isValidHash(result));
|
||||
}
|
||||
|
||||
/* ----- Utility methods and vars ----- */
|
||||
|
||||
private final String HASH_CHARS = "0123456789abcdef";
|
||||
|
||||
/**
|
||||
* Returns true if the input string is valid md5 hash, false otherwise.
|
||||
*/
|
||||
private boolean isValidHash(String result) {
|
||||
boolean valid = true;
|
||||
for (int i=0; i<result.length(); i++) {
|
||||
char c = result.charAt(i);
|
||||
if (HASH_CHARS.indexOf(c) < 0) {
|
||||
valid = false;
|
||||
}
|
||||
}
|
||||
return valid;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncodeHex() {
|
||||
String input = "";
|
||||
String output = "";
|
||||
assertEquals(new String(StringUtils.encodeHex(input.getBytes())),
|
||||
new String(output.getBytes()));
|
||||
|
||||
input = "foo bar 123";
|
||||
output = "666f6f2062617220313233";
|
||||
assertEquals(new String(StringUtils.encodeHex(input.getBytes())),
|
||||
new String(output.getBytes()));
|
||||
}
|
||||
|
||||
/**
|
||||
* This method tests 2 StringUtil methods - encodeBase64(String) and encodeBase64(byte[]).
|
||||
*/
|
||||
@Test
|
||||
public void testEncodeBase64() {
|
||||
String input = "";
|
||||
String output = "";
|
||||
assertEquals(StringUtils.encodeBase64(input), output);
|
||||
|
||||
input = "foo bar 123";
|
||||
output = "Zm9vIGJhciAxMjM=";
|
||||
assertEquals(StringUtils.encodeBase64(input), output);
|
||||
|
||||
input = "=";
|
||||
output = "PQ==";
|
||||
assertEquals(StringUtils.encodeBase64(input), output);
|
||||
|
||||
input = "abcdefghijklmnopqrstuvwxyz0123456789\n\t\"?!.@{}[]();',./<>#$%^&*";
|
||||
output = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5CgkiPyEuQHt9W10oKTsnLC4vPD4jJCVeJio=";
|
||||
assertEquals(StringUtils.encodeBase64(input), output);
|
||||
}
|
||||
|
||||
/***
|
||||
* This method tests 2 StringUtil methods - decodeBase64(String) and decodeBase64(byte[]).
|
||||
*/
|
||||
/*
|
||||
public void testDecodeBase64() {
|
||||
String input = "";
|
||||
String output = "";
|
||||
assertEquals(StringUtils.decodeBase64(input), output);
|
||||
|
||||
input = "Zm9vIGJhciAxMjM=";
|
||||
output = "foo bar 123";
|
||||
assertEquals(StringUtils.decodeBase64(input), output);
|
||||
|
||||
input = "PQ==";
|
||||
output = "=";
|
||||
assertEquals(StringUtils.decodeBase64(input), output);
|
||||
|
||||
input = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5CgkiPyEuQHt9W10oKTsnLC4vPD4jJCVeJio=";
|
||||
output = "abcdefghijklmnopqrstuvwxyz0123456789\n\t\"?!.@{}[]();',./<>#$%^&*";
|
||||
assertEquals(StringUtils.decodeBase64(input), output);
|
||||
}
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void testRandomString() {
|
||||
// Boundary test
|
||||
String result = StringUtils.randomString(-1);
|
||||
assertNull(result);
|
||||
|
||||
// Zero length string test
|
||||
result = StringUtils.randomString(0);
|
||||
assertNull(result);
|
||||
|
||||
// Test various lengths - make sure the same length is returned
|
||||
result = StringUtils.randomString(4);
|
||||
assertTrue(result.length() == 4);
|
||||
result = StringUtils.randomString(16);
|
||||
assertTrue(result.length() == 16);
|
||||
result = StringUtils.randomString(128);
|
||||
assertTrue(result.length() == 128);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParsing() {
|
||||
String error = "Error parsing node name";
|
||||
assertEquals(error, "", StringUtils.parseName("yahoo.myjabber.net"));
|
||||
assertEquals(error, "", StringUtils.parseName("yahoo.myjabber.net/registred"));
|
||||
assertEquals(error, "user", StringUtils.parseName("user@yahoo.myjabber.net/registred"));
|
||||
assertEquals(error, "user", StringUtils.parseName("user@yahoo.myjabber.net"));
|
||||
|
||||
error = "Error parsing server name";
|
||||
String result = "yahoo.myjabber.net";
|
||||
assertEquals(error, result, StringUtils.parseServer("yahoo.myjabber.net"));
|
||||
assertEquals(error, result, StringUtils.parseServer("yahoo.myjabber.net/registred"));
|
||||
assertEquals(error, result, StringUtils.parseServer("user@yahoo.myjabber.net/registred"));
|
||||
assertEquals(error, result, StringUtils.parseServer("user@yahoo.myjabber.net"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseXep0082DateProfile() throws Exception
|
||||
{
|
||||
Date date = StringUtils.parseDate("1971-07-21");
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(date);
|
||||
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
assertEquals(1971, cal.get(Calendar.YEAR));
|
||||
assertEquals(6, cal.get(Calendar.MONTH));
|
||||
assertEquals(21, cal.get(Calendar.DAY_OF_MONTH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseXep0082TimeProfile() throws Exception
|
||||
{
|
||||
Date date = StringUtils.parseDate("02:56:15");
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(date);
|
||||
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
assertEquals(2, cal.get(Calendar.HOUR_OF_DAY));
|
||||
assertEquals(56, cal.get(Calendar.MINUTE));
|
||||
assertEquals(15, cal.get(Calendar.SECOND));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseXep0082TimeWithMillisProfile() throws Exception
|
||||
{
|
||||
Date date = StringUtils.parseDate("02:56:15.123");
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(date);
|
||||
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
assertEquals(2, cal.get(Calendar.HOUR_OF_DAY));
|
||||
assertEquals(56, cal.get(Calendar.MINUTE));
|
||||
assertEquals(15, cal.get(Calendar.SECOND));
|
||||
assertEquals(123, cal.get(Calendar.MILLISECOND));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseXep0082DateTimeProfile() throws Exception
|
||||
{
|
||||
Date date = StringUtils.parseDate("1971-07-21T02:56:15Z");
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(date);
|
||||
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
assertEquals(1971, cal.get(Calendar.YEAR));
|
||||
assertEquals(6, cal.get(Calendar.MONTH));
|
||||
assertEquals(21, cal.get(Calendar.DAY_OF_MONTH));
|
||||
assertEquals(2, cal.get(Calendar.HOUR_OF_DAY));
|
||||
assertEquals(56, cal.get(Calendar.MINUTE));
|
||||
assertEquals(15, cal.get(Calendar.SECOND));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseXep0082DateTimeProfileWithMillis() throws Exception
|
||||
{
|
||||
Date date = StringUtils.parseDate("1971-07-21T02:56:15.123Z");
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(date);
|
||||
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
assertEquals(1971, cal.get(Calendar.YEAR));
|
||||
assertEquals(6, cal.get(Calendar.MONTH));
|
||||
assertEquals(21, cal.get(Calendar.DAY_OF_MONTH));
|
||||
assertEquals(2, cal.get(Calendar.HOUR_OF_DAY));
|
||||
assertEquals(56, cal.get(Calendar.MINUTE));
|
||||
assertEquals(15, cal.get(Calendar.SECOND));
|
||||
assertEquals(123, cal.get(Calendar.MILLISECOND));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseXep0091() throws Exception
|
||||
{
|
||||
Date date = StringUtils.parseDate("20020910T23:08:25");
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(date);
|
||||
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
assertEquals(2002, cal.get(Calendar.YEAR));
|
||||
assertEquals(8, cal.get(Calendar.MONTH));
|
||||
assertEquals(10, cal.get(Calendar.DAY_OF_MONTH));
|
||||
assertEquals(23, cal.get(Calendar.HOUR_OF_DAY));
|
||||
assertEquals(8, cal.get(Calendar.MINUTE));
|
||||
assertEquals(25, cal.get(Calendar.SECOND));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseXep0091NoLeading0() throws Exception
|
||||
{
|
||||
Date date = StringUtils.parseDate("200291T23:08:25");
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(date);
|
||||
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
assertEquals(2002, cal.get(Calendar.YEAR));
|
||||
assertEquals(8, cal.get(Calendar.MONTH));
|
||||
assertEquals(1, cal.get(Calendar.DAY_OF_MONTH));
|
||||
assertEquals(23, cal.get(Calendar.HOUR_OF_DAY));
|
||||
assertEquals(8, cal.get(Calendar.MINUTE));
|
||||
assertEquals(25, cal.get(Calendar.SECOND));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseXep0091AmbiguousMonthDay() throws Exception
|
||||
{
|
||||
Date date = StringUtils.parseDate("2002101T23:08:25");
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(date);
|
||||
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
assertEquals(2002, cal.get(Calendar.YEAR));
|
||||
assertEquals(9, cal.get(Calendar.MONTH));
|
||||
assertEquals(1, cal.get(Calendar.DAY_OF_MONTH));
|
||||
assertEquals(23, cal.get(Calendar.HOUR_OF_DAY));
|
||||
assertEquals(8, cal.get(Calendar.MINUTE));
|
||||
assertEquals(25, cal.get(Calendar.SECOND));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseXep0091SingleDigitMonth() throws Exception
|
||||
{
|
||||
Date date = StringUtils.parseDate("2002130T23:08:25");
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(date);
|
||||
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
assertEquals(2002, cal.get(Calendar.YEAR));
|
||||
assertEquals(0, cal.get(Calendar.MONTH));
|
||||
assertEquals(30, cal.get(Calendar.DAY_OF_MONTH));
|
||||
assertEquals(23, cal.get(Calendar.HOUR_OF_DAY));
|
||||
assertEquals(8, cal.get(Calendar.MINUTE));
|
||||
assertEquals(25, cal.get(Calendar.SECOND));
|
||||
}
|
||||
|
||||
@Test (expected=ParseException.class)
|
||||
public void parseNoMonthDay() throws Exception
|
||||
{
|
||||
StringUtils.parseDate("2002T23:08:25");
|
||||
}
|
||||
|
||||
@Test (expected=ParseException.class)
|
||||
public void parseNoYear() throws Exception
|
||||
{
|
||||
StringUtils.parseDate("130T23:08:25");
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue