2003-08-02 19:33:50 +02:00
|
|
|
/**
|
|
|
|
*
|
2019-03-25 12:11:59 +01:00
|
|
|
* Copyright 2003-2007 Jive Software, 2019 Florian Schmaus.
|
2003-08-02 19:33:50 +02:00
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2013-02-07 15:19:47 +01:00
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2003-08-02 19:33:50 +02:00
|
|
|
*
|
2013-02-07 15:19:47 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2003-08-02 19:33:50 +02:00
|
|
|
*
|
2013-02-07 15:19:47 +01:00
|
|
|
* 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.
|
2003-08-02 19:33:50 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jivesoftware.smack.util;
|
|
|
|
|
2013-02-04 04:45:37 +01:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertNull;
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
|
2019-05-08 11:34:40 +02:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2017-02-11 16:16:41 +01:00
|
|
|
|
2013-02-04 04:45:37 +01:00
|
|
|
import org.junit.Test;
|
2003-08-02 19:33:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A test case for the StringUtils class.
|
|
|
|
*/
|
2013-02-04 04:45:37 +01:00
|
|
|
public class StringUtilsTest {
|
2017-02-07 22:02:40 +01:00
|
|
|
@Test
|
2016-01-13 15:04:01 +01:00
|
|
|
public void testEscapeForXml() {
|
|
|
|
assertNull(StringUtils.escapeForXml(null));
|
2003-08-02 19:33:50 +02:00
|
|
|
|
2019-05-07 22:58:02 +02:00
|
|
|
String input = "<b>";
|
2016-01-13 15:04:01 +01:00
|
|
|
assertCharSequenceEquals("<b>", StringUtils.escapeForXml(input));
|
2003-08-02 19:33:50 +02:00
|
|
|
|
|
|
|
input = "\"";
|
2016-01-13 15:04:01 +01:00
|
|
|
assertCharSequenceEquals(""", StringUtils.escapeForXml(input));
|
2003-08-02 19:33:50 +02:00
|
|
|
|
|
|
|
input = "&";
|
2016-01-13 15:04:01 +01:00
|
|
|
assertCharSequenceEquals("&", StringUtils.escapeForXml(input));
|
2003-08-02 19:33:50 +02:00
|
|
|
|
|
|
|
input = "<b>\n\t\r</b>";
|
2016-01-13 15:04:01 +01:00
|
|
|
assertCharSequenceEquals("<b>\n\t\r</b>", StringUtils.escapeForXml(input));
|
2003-08-02 19:33:50 +02:00
|
|
|
|
|
|
|
input = " & ";
|
2016-01-13 15:04:01 +01:00
|
|
|
assertCharSequenceEquals(" & ", StringUtils.escapeForXml(input));
|
2003-08-02 19:33:50 +02:00
|
|
|
|
|
|
|
input = " \" ";
|
2016-01-13 15:04:01 +01:00
|
|
|
assertCharSequenceEquals(" " ", StringUtils.escapeForXml(input));
|
2005-04-15 02:56:37 +02:00
|
|
|
|
|
|
|
input = "> of me <";
|
2016-01-13 15:04:01 +01:00
|
|
|
assertCharSequenceEquals("> of me <", StringUtils.escapeForXml(input));
|
2005-04-15 02:56:37 +02:00
|
|
|
|
|
|
|
input = "> of me & you<";
|
2016-01-13 15:04:01 +01:00
|
|
|
assertCharSequenceEquals("> of me & you<", StringUtils.escapeForXml(input));
|
2005-04-15 02:56:37 +02:00
|
|
|
|
|
|
|
input = "& <";
|
2016-01-13 15:04:01 +01:00
|
|
|
assertCharSequenceEquals("& <", StringUtils.escapeForXml(input));
|
2005-04-15 02:56:37 +02:00
|
|
|
|
|
|
|
input = "&";
|
2016-01-13 15:04:01 +01:00
|
|
|
assertCharSequenceEquals("&", StringUtils.escapeForXml(input));
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2009-06-04 13:58:25 +02:00
|
|
|
input = "It's a good day today";
|
2016-01-13 15:04:01 +01:00
|
|
|
assertCharSequenceEquals("It's a good day today", StringUtils.escapeForXml(input));
|
2003-08-02 19:33:50 +02:00
|
|
|
}
|
|
|
|
|
2017-02-07 22:02:40 +01:00
|
|
|
public static void assertCharSequenceEquals(CharSequence expected, CharSequence actual) {
|
2015-03-18 21:51:25 +01:00
|
|
|
assertEquals(expected.toString(), actual.toString());
|
2017-02-07 22:02:40 +01:00
|
|
|
}
|
2014-03-21 09:51:52 +01:00
|
|
|
|
2017-02-07 22:02:40 +01:00
|
|
|
@Test
|
2019-05-08 11:34:40 +02:00
|
|
|
public void testEncodeHex() {
|
2003-08-02 19:33:50 +02:00
|
|
|
String input = "";
|
|
|
|
String output = "";
|
2019-05-08 11:34:40 +02:00
|
|
|
assertEquals(new String(StringUtils.encodeHex(input.getBytes(StandardCharsets.UTF_8))),
|
2017-02-11 16:16:41 +01:00
|
|
|
output);
|
2003-08-02 19:33:50 +02:00
|
|
|
|
|
|
|
input = "foo bar 123";
|
|
|
|
output = "666f6f2062617220313233";
|
2019-05-08 11:34:40 +02:00
|
|
|
assertEquals(new String(StringUtils.encodeHex(input.getBytes(StandardCharsets.UTF_8))),
|
2017-02-11 16:16:41 +01:00
|
|
|
output);
|
2003-08-02 19:33:50 +02:00
|
|
|
}
|
|
|
|
|
2017-02-07 22:02:40 +01:00
|
|
|
@Test
|
2003-08-02 19:33:50 +02:00
|
|
|
public void testRandomString() {
|
2019-03-25 12:11:59 +01:00
|
|
|
String result;
|
2003-08-02 19:33:50 +02:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
2019-03-25 12:11:59 +01:00
|
|
|
|
|
|
|
@Test(expected = NegativeArraySizeException.class)
|
|
|
|
public void testNegativeArraySizeException() {
|
|
|
|
// Boundary test
|
|
|
|
StringUtils.randomString(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testZeroLengthRandomString() {
|
|
|
|
// Zero length string test
|
|
|
|
String result = StringUtils.randomString(0);
|
|
|
|
assertEquals("", result);
|
|
|
|
}
|
2019-08-05 22:20:21 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testeDeleteXmlWhitespace() {
|
|
|
|
String noWhitespace = StringUtils.deleteXmlWhitespace(" foo\nbar ");
|
|
|
|
assertEquals("foobar", noWhitespace);
|
|
|
|
|
|
|
|
noWhitespace = StringUtils.deleteXmlWhitespace(" \tbaz\rbarz\t ");
|
|
|
|
assertEquals("bazbarz", noWhitespace);
|
|
|
|
|
|
|
|
noWhitespace = StringUtils.deleteXmlWhitespace("SNAFU");
|
|
|
|
assertEquals("SNAFU", noWhitespace);
|
|
|
|
}
|
2003-08-02 19:33:50 +02:00
|
|
|
}
|