0001: /*
0002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
0003: * Distributed under the terms of either:
0004: * - the common development and distribution license (CDDL), v1.0; or
0005: * - the GNU Lesser General Public License, v2.1 or later
0006: * $Id: TestStringUtils.java 3634 2007-01-08 21:42:24Z gbevin $
0007: */
0008: package com.uwyn.rife.tools;
0009:
0010: import com.uwyn.rife.datastructures.DocumentPosition;
0011: import com.uwyn.rife.tools.StringUtils;
0012: import java.net.URLDecoder;
0013: import java.util.ArrayList;
0014: import java.util.Vector;
0015: import java.util.regex.Pattern;
0016: import junit.framework.TestCase;
0017:
0018: public class TestStringUtils extends TestCase {
0019: public TestStringUtils(String name) {
0020: super (name);
0021: }
0022:
0023: public void testEncodeClassname() {
0024: assertNull(StringUtils.encodeClassname(null));
0025: assertEquals("just_a_string_", StringUtils
0026: .encodeClassname("just.a:string="));
0027: }
0028:
0029: public void testEncodeURL() {
0030: assertNull(StringUtils.encodeUrl(null));
0031: assertEquals("a+test+%26", StringUtils.encodeUrl("a test &"));
0032: String valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVQXYZ0123456789-_.*";
0033: assertSame(valid, StringUtils.encodeUrl(valid));
0034: assertEquals(
0035: "%21abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVQXYZ0123456789-_.*%3D",
0036: StringUtils
0037: .encodeUrl("!abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVQXYZ0123456789-_.*="));
0038: }
0039:
0040: public void testEncodeURLValue() {
0041: assertNull(StringUtils.encodeUrlValue(null));
0042: assertEquals("a+test+%26", StringUtils
0043: .encodeUrlValue("a test &"));
0044: String valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVQXYZ0123456789-_.*";
0045: assertSame(valid, StringUtils.encodeUrlValue(valid));
0046: assertEquals(
0047: "%21abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVQXYZ0123456789-_.*%3D",
0048: StringUtils
0049: .encodeUrlValue("!abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVQXYZ0123456789-_.*="));
0050: assertEquals(
0051: "%02%02YWJjZGVmw4zDjcOOw4/DkcOSw5PDlMOVw5bDmMOZw5rDm8Ocw5/DoMOhw6I%3D",
0052: StringUtils
0053: .encodeUrlValue("abcdefÌ�Î�ÑÒÓÔÕÖØÙÚÛÜßà áâ"));
0054:
0055: assertFalse(StringUtils.doesUrlValueNeedDecoding("a+test+%26"));
0056: assertFalse(StringUtils
0057: .doesUrlValueNeedDecoding("%02%02YWJjZGVmw4zDjcOOw4/DkcOSw5PDlMOVw5bDmMOZw5rDm8Ocw5/DoMOhw6I%3D"));
0058: assertTrue(StringUtils
0059: .doesUrlValueNeedDecoding(URLDecoder
0060: .decode("%02%02YWJjZGVmw4zDjcOOw4/DkcOSw5PDlMOVw5bDmMOZw5rDm8Ocw5/DoMOhw6I%3D")));
0061: assertEquals(
0062: "abcdefÌ�Î�ÑÒÓÔÕÖØÙÚÛÜßà áâ",
0063: StringUtils
0064: .decodeUrlValue(URLDecoder
0065: .decode("%02%02YWJjZGVmw4zDjcOOw4/DkcOSw5PDlMOVw5bDmMOZw5rDm8Ocw5/DoMOhw6I%3D")));
0066: }
0067:
0068: public void testEncodeHtml() {
0069: assertNull(StringUtils.encodeHtml(null));
0070: assertEquals(
0071: StringUtils
0072: .encodeHtml("&<>\"¡¢£¥§¨©ª«¬®¯°±´µ¶·¸º»¿ÀÃ?ÂÃÄÅÆÇÈÉÊËÌÃ?ÃŽÃ?ÑÒÓÔÕÖØÙÚÛÜßà áâãäåæçèéêëìÃîïñòóôõö÷øùúûüÿƒΩπ•…â?„™∂âˆ?∑√∞∫≈≠≤≥◊ascii∛plainΘâ?¤"),
0073: "&<>"¡¢£¥§¨©ª«¬®¯°±´µ¶·¸º»¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜßàáâãäåæçèéêëìíîïñòóôõö÷øùúûüÿƒΩπ•…⁄™∂∏∑√∞∫≈≠≤≥◊ascii∛plainΘ❤");
0074: }
0075:
0076: public void testDecodeHtml() {
0077: assertNull(StringUtils.decodeHtml(null));
0078:
0079: assertEquals("plain", StringUtils.decodeHtml("plain"));
0080:
0081: assertEquals("Dépôt direct", StringUtils
0082: .decodeHtml("Dépôt direct"));
0083:
0084: assertEquals("trailingchar&a", StringUtils
0085: .decodeHtml("trailingchar&a"));
0086:
0087: assertEquals("ï&badentity", StringUtils
0088: .decodeHtml("ï&badentity"));
0089:
0090: assertEquals("badnumericbad;oo°o", StringUtils
0091: .decodeHtml("badnumericbad;oo°o"));
0092:
0093: assertEquals("à &unknown;é", StringUtils
0094: .decodeHtml("à&unknown;é"));
0095:
0096: assertEquals("µhexaÄ", StringUtils
0097: .decodeHtml("µhexaÄ"));
0098:
0099: assertEquals(
0100: StringUtils
0101: .decodeHtml("&<>"¡¢£¥§¨©ª«¬®¯°±´µ¶·¸º»¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜßàáâãäåæçèéêëìíîïñòóôõö÷øùúûüÿƒΩπ•…⁄™∂∏∑√∞∫≈≠≤≥◊ascii∛plainΘ❤"),
0102: "&<>\"¡¢£¥§¨©ª«¬®¯°±´µ¶·¸º»¿ÀÃ?ÂÃÄÅÆÇÈÉÊËÌÃ?ÃŽÃ?ÑÒÓÔÕÖØÙÚÛÜßà áâãäåæçèéêëìÃîïñòóôõö÷øùúûüÿƒΩπ•…â?„™∂âˆ?∑√∞∫≈≠≤≥◊ascii∛plainΘâ?¤");
0103: }
0104:
0105: public void testEncodeHtmlDefensive() {
0106: assertNull(StringUtils.encodeHtmlDefensive(null));
0107: assertEquals(
0108: StringUtils
0109: .encodeHtmlDefensive("&<>\"¡¢£¥§¨©ª«¬®¯°±´µ¶·¸º»¿ÀÃ?ÂÃÄÅÆÇÈÉÊËÌÃ?ÃŽÃ?ÑÒÓÔÕÖØÙÚÛÜßà áâãäåæçèéêëìÃîïñòóôõö÷øùúûüÿƒΩπ•…â?„™∂âˆ?∑√∞∫≈≠≤≥◊"),
0110: "&<>\"¡¢£¥§¨©ª«¬®¯°±´µ¶·¸º»¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜßàáâãäåæçèéêëìíîïñòóôõö÷øùúûüÿƒΩπ•…⁄™∂∏∑√∞∫≈≠≤≥◊");
0111: }
0112:
0113: public void testEncodeUnicode() {
0114: assertNull(StringUtils.encodeUnicode(null));
0115: assertEquals("\\u0061\\u0062\\u0063\\u0064\\u0065\\u006B",
0116: StringUtils.encodeUnicode("abcdek"));
0117: }
0118:
0119: public void testEncodeXml() {
0120: assertNull(StringUtils.encodeXml(null));
0121: assertEquals(StringUtils.encodeXml("abcd\'\"<>&wxyz"),
0122: "abcd'"<>&wxyz");
0123: }
0124:
0125: public void testEncodeSql() {
0126: assertNull(StringUtils.encodeSql(null));
0127: assertEquals(StringUtils.encodeSql("abcd'wxyz"), "abcd''wxyz");
0128: }
0129:
0130: public void testEncodeString() {
0131: assertNull(StringUtils.encodeString(null));
0132: assertEquals(StringUtils.encodeString("abcd\"\na\t\r\\wxyz"),
0133: "abcd\\\"\\na\\t\\r\\\\wxyz");
0134: }
0135:
0136: public void testEncodeLatex() {
0137: assertNull(StringUtils.encodeLatex(null));
0138: assertEquals(
0139: "\\\\\\#\\$\\%\\&\\~\\_\\^\\{\\}\\`{A}\\'{A}\\^{A}\\H{A}\\\"{A}\\AA\\AE\\c{C}\\`{E}\\'{E}\\^{E}\\\"{E}\\`{I}\\'{I}\\^{I}\\\"{I}\\H{N}\\`{O}\\'{O}\\^{O}\\H{O}\\\"{O}\\O\\`{U}\\'{U}\\^{U}\\\"{U}\\ss\\`{a}\\'{a}\\^{a}\\H{a}\\\"{a}\\aa\\ae\\c{c}\\`{e}\\'{e}\\^{e}\\\"{e}\\`{i}\\'{i}\\^{i}\\\"{i}\\H{n}\\`{o}\\'{o}\\^{o}\\H{o}\\\"{o}\\o\\`{u}\\'{u}\\^{u}\\\"{u}\\\"{y}\\LaTeX\\LaTeX",
0140: StringUtils
0141: .encodeLatex("\\#$%&~_^{}ÀÃ?ÂÃÄÅÆÇÈÉÊËÌÃ?ÃŽÃ?ÑÒÓÔÕÖØÙÚÛÜßà áâãäåæçèéêëìÃîïñòóôõöøùúûüÿlatexLaTeX"));
0142: }
0143:
0144: public void testCount() {
0145: assertEquals(StringUtils.count(null, null), 0);
0146: assertEquals(StringUtils.count("", null), 0);
0147: assertEquals(StringUtils.count("onetwoonethreefouroneONE",
0148: "one"), 3);
0149: assertEquals(
0150: StringUtils.count("onetwoonethreefouroneO", "one"), 3);
0151: assertEquals(StringUtils.count("101010101", "1"), 5);
0152: }
0153:
0154: public void testCountCase() {
0155: assertEquals(StringUtils.count("ONEtwooNethreefourone", "onE",
0156: false), 3);
0157: }
0158:
0159: public void testSplit() {
0160: assertEquals(StringUtils.split(null, null).size(), 0);
0161: assertEquals(StringUtils.split("one", null).size(), 1);
0162: assertEquals(StringUtils.split("one", null).get(0), "one");
0163:
0164: String string_to_split = "onextwoxthreeXfour";
0165: ArrayList<String> string_parts = StringUtils.split(
0166: string_to_split, "x");
0167:
0168: assertNotNull(string_parts);
0169: assertEquals(string_parts.get(0), "one");
0170: assertEquals(string_parts.get(1), "two");
0171: assertEquals(string_parts.get(2), "threeXfour");
0172: }
0173:
0174: public void testSplitCase() {
0175: String string_to_split = "oneaAatwoAAAthree";
0176: ArrayList<String> string_parts = StringUtils.split(
0177: string_to_split, "aaa", false);
0178:
0179: assertNotNull(string_parts);
0180: assertEquals(string_parts.get(0), "one");
0181: assertEquals(string_parts.get(1), "two");
0182: assertEquals(string_parts.get(2), "three");
0183: }
0184:
0185: public void testSplitToArray() {
0186: String string_to_split = "onextwoxthreeXfour";
0187: String[] string_parts = StringUtils.splitToArray(
0188: string_to_split, "x");
0189:
0190: assertNotNull(string_parts);
0191: assertEquals(string_parts[0], "one");
0192: assertEquals(string_parts[1], "two");
0193: assertEquals(string_parts[2], "threeXfour");
0194: }
0195:
0196: public void testSplitToArrayCase() {
0197: String string_to_split = "oneaAatwoAAAthree";
0198: String[] string_parts = StringUtils.splitToArray(
0199: string_to_split, "aaa", false);
0200:
0201: assertNotNull(string_parts);
0202: assertEquals(string_parts[0], "one");
0203: assertEquals(string_parts[1], "two");
0204: assertEquals(string_parts[2], "three");
0205: }
0206:
0207: public void testSplitToIntArray() {
0208: String string_to_split = "1x5x10xezfzefx50x100X200";
0209: int[] string_parts = StringUtils.splitToIntArray(
0210: string_to_split, "x");
0211: assertNotNull(string_parts);
0212: assertTrue(1 == string_parts[0]);
0213: assertTrue(5 == string_parts[1]);
0214: assertTrue(10 == string_parts[2]);
0215: assertTrue(50 == string_parts[3]);
0216: }
0217:
0218: public void testSplitToIntArrayCase() {
0219: String string_to_split = "1xXx5XXX10xxxezfzefXxX50";
0220: int[] string_parts = StringUtils.splitToIntArray(
0221: string_to_split, "XXX", false);
0222: assertNotNull(string_parts);
0223: assertTrue(1 == string_parts[0]);
0224: assertTrue(5 == string_parts[1]);
0225: assertTrue(10 == string_parts[2]);
0226: assertTrue(50 == string_parts[3]);
0227: }
0228:
0229: public void testSplitToByteArray() {
0230: String string_to_split = "sdfsdx5x1078456x50x100X200";
0231: byte[] string_parts = StringUtils.splitToByteArray(
0232: string_to_split, "x");
0233: assertNotNull(string_parts);
0234: assertTrue(5 == string_parts[0]);
0235: assertTrue(50 == string_parts[1]);
0236: }
0237:
0238: public void testSplitToByteArrayCase() {
0239: String string_to_split = "sdfsd_A_5_a_1078456_A_50";
0240: byte[] string_parts = StringUtils.splitToByteArray(
0241: string_to_split, "_a_", false);
0242: assertNotNull(string_parts);
0243: assertTrue(5 == string_parts[0]);
0244: assertTrue(50 == string_parts[1]);
0245: }
0246:
0247: public void testStripFromFront() {
0248: assertNull(StringUtils.stripFromFront(null, null));
0249: assertEquals(StringUtils.stripFromFront("2frontmiddleback",
0250: null), "2frontmiddleback");
0251:
0252: assertEquals(StringUtils.stripFromFront("2frontmiddleback",
0253: "front"), "2frontmiddleback");
0254: assertEquals(StringUtils.stripFromFront("frontmiddleback",
0255: "front"), "middleback");
0256: assertEquals(StringUtils.stripFromFront("FRONTmiddleback",
0257: "front"), "FRONTmiddleback");
0258: assertEquals(StringUtils.stripFromFront(
0259: "frontfront2frontmiddleback", "front"),
0260: "2frontmiddleback");
0261: assertEquals(StringUtils.stripFromFront("frontmiddleback",
0262: "middle"), "frontmiddleback");
0263: assertEquals(StringUtils.stripFromFront("frontmiddleback",
0264: "back"), "frontmiddleback");
0265: }
0266:
0267: public void testStripFromFrontCase() {
0268: assertEquals(StringUtils.stripFromFront("2fRoNtmiddleback",
0269: "front", false), "2fRoNtmiddleback");
0270: assertEquals(StringUtils.stripFromFront("FRONTmiddleback",
0271: "front", false), "middleback");
0272: assertEquals(StringUtils.stripFromFront(
0273: "fROntFRONt2frontmiddleback", "front", false),
0274: "2frontmiddleback");
0275: assertEquals(StringUtils.stripFromFront("frontmIDDLeback",
0276: "middle", false), "frontmIDDLeback");
0277: assertEquals(StringUtils.stripFromFront("frontmiddleBACk",
0278: "back", false), "frontmiddleBACk");
0279: }
0280:
0281: public void testStripFromEnd() {
0282: assertNull(StringUtils.stripFromEnd(null, null));
0283: assertEquals(StringUtils.stripFromEnd("frontmiddleback", null),
0284: "frontmiddleback");
0285:
0286: assertEquals(StringUtils.stripFromEnd("frontmiddleback",
0287: "front"), "frontmiddleback");
0288: assertEquals(StringUtils.stripFromEnd("frontmiddleback",
0289: "middle"), "frontmiddleback");
0290: assertEquals(StringUtils
0291: .stripFromEnd("frontmiddleback", "back"), "frontmiddle");
0292: assertEquals(StringUtils
0293: .stripFromEnd("frontmiddleBACK", "back"),
0294: "frontmiddleBACK");
0295: assertEquals(StringUtils.stripFromEnd("frontmiddleback2",
0296: "back"), "frontmiddleback2");
0297: assertEquals(StringUtils.stripFromEnd(
0298: "frontmiddleback2backbackback", "back"),
0299: "frontmiddleback2");
0300: }
0301:
0302: public void testStripFromEndCase() {
0303: assertEquals(StringUtils.stripFromEnd("fRONtmiddleback",
0304: "front", false), "fRONtmiddleback");
0305: assertEquals(StringUtils.stripFromEnd("frontMIDDLEback",
0306: "middle", false), "frontMIDDLEback");
0307: assertEquals(StringUtils.stripFromEnd("frontmiddleBAcK",
0308: "back", false), "frontmiddle");
0309: assertEquals(StringUtils.stripFromEnd("frontmiddleBACK2",
0310: "back", false), "frontmiddleBACK2");
0311: assertEquals(StringUtils.stripFromEnd(
0312: "frontmiddleback2BAckbackBACK", "back", false),
0313: "frontmiddleback2");
0314: }
0315:
0316: public void testReplace() {
0317: assertNull(StringUtils.replace(null, "one", "five"));
0318: assertEquals(StringUtils.replace("onetwooneTWOthreeONEfourone",
0319: null, "five"), "onetwooneTWOthreeONEfourone");
0320: assertEquals(StringUtils.replace("onetwooneTWOthreeONEfourone",
0321: "one", null), "onetwooneTWOthreeONEfourone");
0322:
0323: assertEquals(StringUtils.replace("onetwooneTWOthreeONEfourone",
0324: "one", "five"), "fivetwofiveTWOthreeONEfourfive");
0325: assertEquals(StringUtils.replace("onetwooneTWOthreeONEfourone",
0326: "two", "five"), "onefiveoneTWOthreeONEfourone");
0327: assertEquals(StringUtils.replace("onetwoonethreefourone",
0328: "six", "five"), "onetwoonethreefourone");
0329: }
0330:
0331: public void testReplaceCase() {
0332: assertEquals(StringUtils.replace("ONEtwoOnEthreefouroNE",
0333: "one", "five", false), "fivetwofivethreefourfive");
0334: assertEquals(StringUtils.replace("onetWOonethreefourone",
0335: "two", "five", false), "onefiveonethreefourone");
0336: assertEquals(StringUtils.replace("onetwoonethreefourone",
0337: "six", "five", false), "onetwoonethreefourone");
0338: }
0339:
0340: public void testRepeat() {
0341: assertNull(StringUtils.repeat(null, 0), null);
0342:
0343: assertEquals(StringUtils.repeat("one", 0), "");
0344: assertEquals(StringUtils.repeat("one", 1), "one");
0345: assertEquals(StringUtils.repeat("one", 3), "oneoneone");
0346: }
0347:
0348: public void testToStringArray() {
0349: assertEquals(StringUtils.toStringArray(null).length, 0);
0350:
0351: Vector<String> strings = new Vector<String>();
0352: strings.add("one");
0353: strings.add("two");
0354: strings.add("three");
0355: String[] string_array = { "one", "two", "three" };
0356:
0357: String[] string_array_new = null;
0358: string_array_new = StringUtils
0359: .toStringArray(strings.iterator());
0360: assertEquals(string_array_new.length, string_array.length);
0361: assertEquals(string_array_new[0], string_array[0]);
0362: assertEquals(string_array_new[1], string_array[1]);
0363: assertEquals(string_array_new[2], string_array[2]);
0364: }
0365:
0366: public void testToArrayList() {
0367: assertEquals(StringUtils.toArrayList(null).size(), 0);
0368:
0369: String[] string_array = { "one", "two", "three" };
0370: ArrayList<String> string_arraylist = StringUtils
0371: .toArrayList(string_array);
0372: assertEquals(string_array.length, string_arraylist.size());
0373: assertEquals(string_array[0], string_arraylist.get(0));
0374: assertEquals(string_array[1], string_arraylist.get(1));
0375: assertEquals(string_array[2], string_arraylist.get(2));
0376: }
0377:
0378: public void testJoinCollection() {
0379: assertNull(StringUtils.join((ArrayList<String>) null, null));
0380: assertEquals("", StringUtils.join(new ArrayList<String>(), ""));
0381: assertEquals("", StringUtils
0382: .join(new ArrayList<String>(), null));
0383:
0384: ArrayList<String> strings = new ArrayList<String>();
0385: strings.add("one");
0386: strings.add("two");
0387: strings.add("three");
0388: String joined_string = StringUtils.join(strings, ",");
0389: assertNotNull(joined_string);
0390: assertEquals(joined_string, "one,two,three");
0391: }
0392:
0393: public void testJoinStringArray() {
0394: assertNull(StringUtils.join((String[]) null, null));
0395: assertEquals("", StringUtils.join(new String[0], ""));
0396: assertEquals("", StringUtils.join(new String[0], null));
0397:
0398: String[] string_array = { "one", "two", "three" };
0399: String joined_string = StringUtils.join(string_array, ",");
0400: assertNotNull(joined_string);
0401: assertEquals(joined_string, "one,two,three");
0402:
0403: joined_string = StringUtils.join(string_array, ",", "'");
0404: assertNotNull(joined_string);
0405: assertEquals(joined_string, "'one','two','three'");
0406: }
0407:
0408: public void testJoinStringArrayEncode() {
0409: String[] string_array = { "one\"", "two", "thr\"ee" };
0410: String joined_string = StringUtils.join(string_array, ",", "'",
0411: true);
0412: assertNotNull(joined_string);
0413: assertEquals(joined_string, "'one\\\"','two','thr\\\"ee'");
0414: }
0415:
0416: public void testJoinBooleanArray() {
0417: assertNull(StringUtils.join((boolean[]) null, null));
0418: assertEquals("", StringUtils.join(new boolean[0], ""));
0419: assertEquals("", StringUtils.join(new boolean[0], null));
0420:
0421: boolean[] boolean_array = { true, false, false, true };
0422: String joined_string = StringUtils.join(boolean_array, ",");
0423: assertNotNull(joined_string);
0424: assertEquals(joined_string, "true,false,false,true");
0425: }
0426:
0427: public void testJoinByteArray() {
0428: assertNull(StringUtils.join((byte[]) null, null));
0429: assertEquals("", StringUtils.join(new byte[0], ""));
0430: assertEquals("", StringUtils.join(new byte[0], null));
0431:
0432: byte[] byte_array = { 1, 6, 3, 5 };
0433: String joined_string = StringUtils.join(byte_array, ",");
0434: assertNotNull(joined_string);
0435: assertEquals(joined_string, "1,6,3,5");
0436: }
0437:
0438: public void testJoinShortArray() {
0439: assertNull(StringUtils.join((short[]) null, null));
0440: assertEquals("", StringUtils.join(new short[0], ""));
0441: assertEquals("", StringUtils.join(new short[0], null));
0442:
0443: short[] short_array = { 8, 3, 9, 6, 4 };
0444: String joined_string = StringUtils.join(short_array, ",");
0445: assertNotNull(joined_string);
0446: assertEquals(joined_string, "8,3,9,6,4");
0447: }
0448:
0449: public void testJoinCharArray() {
0450: assertNull(StringUtils.join((char[]) null, null));
0451: assertEquals("", StringUtils.join(new char[0], ""));
0452: assertEquals("", StringUtils.join(new char[0], null));
0453:
0454: char[] short_array = { 'k', 'L', 's', 'O' };
0455: String joined_string = StringUtils.join(short_array, ",");
0456: assertNotNull(joined_string);
0457: assertEquals(joined_string, "k,L,s,O");
0458: joined_string = StringUtils.join(short_array, ",", "'");
0459: assertNotNull(joined_string);
0460: assertEquals(joined_string, "'k','L','s','O'");
0461: }
0462:
0463: public void testJoinIntArray() {
0464: assertNull(StringUtils.join((int[]) null, null));
0465: assertEquals("", StringUtils.join(new int[0], ""));
0466: assertEquals("", StringUtils.join(new int[0], null));
0467:
0468: int[] int_array = { 1, 5, 10, 50 };
0469: String joined_string = StringUtils.join(int_array, ",");
0470: assertNotNull(joined_string);
0471: assertEquals(joined_string, "1,5,10,50");
0472: }
0473:
0474: public void testJoinLongArray() {
0475: assertNull(StringUtils.join((long[]) null, null));
0476: assertEquals("", StringUtils.join(new long[0], ""));
0477: assertEquals("", StringUtils.join(new long[0], null));
0478:
0479: long[] long_array = { 78L, 98934L, 232L, 97834L };
0480: String joined_string = StringUtils.join(long_array, ",");
0481: assertNotNull(joined_string);
0482: assertEquals(joined_string, "78,98934,232,97834");
0483: }
0484:
0485: public void testJoinFloatArray() {
0486: assertNull(StringUtils.join((float[]) null, null));
0487: assertEquals("", StringUtils.join(new float[0], ""));
0488: assertEquals("", StringUtils.join(new float[0], null));
0489:
0490: float[] float_array = { 23.1f, 76.3f, 3.87f };
0491: String joined_string = StringUtils.join(float_array, ",");
0492: assertNotNull(joined_string);
0493: assertEquals(joined_string, "23.1,76.3,3.87");
0494: }
0495:
0496: public void testJoinDoubleArray() {
0497: assertNull(StringUtils.join((double[]) null, null));
0498: assertEquals("", StringUtils.join(new double[0], ""));
0499: assertEquals("", StringUtils.join(new double[0], null));
0500:
0501: double[] double_array = { 1.2d, 5.7d, 10.12d, 50.98d };
0502: String joined_string = StringUtils.join(double_array, ",");
0503: assertNotNull(joined_string);
0504: assertEquals(joined_string, "1.2,5.7,10.12,50.98");
0505: }
0506:
0507: public void testIndicesOf() {
0508: assertEquals(0, StringUtils.indicesOf(null, "one").length);
0509: assertEquals(0, StringUtils.indicesOf(
0510: "onetwoonethreefouroneONE", null).length);
0511:
0512: int[] indices1 = StringUtils.indicesOf(
0513: "onetwoonethreefouroneONE", "one");
0514: assertEquals(indices1.length, 3);
0515: assertEquals(indices1[0], 0);
0516: assertEquals(indices1[1], 6);
0517: assertEquals(indices1[2], 18);
0518:
0519: int[] indices2 = StringUtils.indicesOf(
0520: "onetwoonethreefouroneO", "one");
0521: assertEquals(indices2.length, 3);
0522: assertEquals(indices2[0], 0);
0523: assertEquals(indices2[1], 6);
0524: assertEquals(indices2[2], 18);
0525:
0526: int[] indices3 = StringUtils.indicesOf("101010101", "1");
0527: assertEquals(indices3.length, 5);
0528: assertEquals(indices3[0], 0);
0529: assertEquals(indices3[1], 2);
0530: assertEquals(indices3[2], 4);
0531: assertEquals(indices3[3], 6);
0532: assertEquals(indices3[4], 8);
0533: }
0534:
0535: public void testIndicesOfCase() {
0536: int[] indices = StringUtils.indicesOf("oNEtwoONEthreefourOne",
0537: "one", false);
0538: assertEquals(indices.length, 3);
0539: assertEquals(indices[0], 0);
0540: assertEquals(indices[1], 6);
0541: assertEquals(indices[2], 18);
0542: }
0543:
0544: public void testGetMatchingRegexp() {
0545: Pattern pattern1 = Pattern.compile("reg(.*)lar");
0546: Pattern pattern2 = Pattern.compile("exp(.*)ion");
0547:
0548: ArrayList<Pattern> regexps = new ArrayList<Pattern>();
0549: regexps.add(pattern1);
0550: regexps.add(pattern2);
0551:
0552: assertSame(pattern1, StringUtils.getMatchingRegexp("regular",
0553: regexps).pattern());
0554: assertSame(pattern2, StringUtils.getMatchingRegexp(
0555: "expression", regexps).pattern());
0556: assertNull(StringUtils.getMatchingRegexp("nomatch", regexps));
0557: }
0558:
0559: public void testGetRegexpMatch() {
0560: String value1 = "regular";
0561: String value2 = "expression";
0562:
0563: ArrayList<String> values = new ArrayList<String>();
0564: values.add(value1);
0565: values.add(value2);
0566:
0567: assertSame(value1, StringUtils.getRegexpMatch(values,
0568: Pattern.compile("reg(.*)lar")).group());
0569: assertSame(value2, StringUtils.getRegexpMatch(values,
0570: Pattern.compile("exp(.*)ion")).group());
0571: assertNull(StringUtils.getRegexpMatch(values, Pattern
0572: .compile("no(.*)match")));
0573: }
0574:
0575: public void testFilterSingular() {
0576: assertFalse(StringUtils.filter(null, (Pattern) null,
0577: (Pattern) null));
0578:
0579: assertTrue(StringUtils.filter("test", (Pattern) null, Pattern
0580: .compile(".*a.*")));
0581: assertFalse(StringUtils.filter("test", (Pattern) null, Pattern
0582: .compile(".*t.*")));
0583:
0584: assertTrue(StringUtils.filter("test", Pattern.compile(".*e.*"),
0585: (Pattern) null));
0586: assertFalse(StringUtils.filter("test",
0587: Pattern.compile(".*x.*"), (Pattern) null));
0588:
0589: assertTrue(StringUtils.filter("test", Pattern.compile(".*e.*"),
0590: Pattern.compile(".*a.*")));
0591: assertFalse(StringUtils.filter("test",
0592: Pattern.compile(".*e.*"), Pattern.compile(".*t.*")));
0593: assertFalse(StringUtils.filter("test",
0594: Pattern.compile(".*x.*"), Pattern.compile(".*a.*")));
0595: }
0596:
0597: public void testFilterMultiple() {
0598: assertFalse(StringUtils.filter(null, (Pattern[]) null,
0599: (Pattern[]) null));
0600:
0601: assertTrue(StringUtils.filter("test", (Pattern[]) null,
0602: new Pattern[] { Pattern.compile(".*a.*"),
0603: Pattern.compile(".*b.*") }));
0604: assertFalse(StringUtils.filter("test", (Pattern[]) null,
0605: new Pattern[] { Pattern.compile(".*a.*"),
0606: Pattern.compile(".*t.*") }));
0607: assertFalse(StringUtils.filter("test", (Pattern[]) null,
0608: new Pattern[] { Pattern.compile(".*e.*"),
0609: Pattern.compile(".*t.*") }));
0610:
0611: assertTrue(StringUtils.filter("test", new Pattern[] {
0612: Pattern.compile(".*e.*"), Pattern.compile(".*s.*") },
0613: (Pattern[]) null));
0614: assertTrue(StringUtils.filter("test", new Pattern[] {
0615: Pattern.compile(".*a.*"), Pattern.compile(".*s.*") },
0616: (Pattern[]) null));
0617: assertFalse(StringUtils.filter("test", new Pattern[] {
0618: Pattern.compile(".*a.*"), Pattern.compile(".*b.*") },
0619: (Pattern[]) null));
0620:
0621: assertTrue(StringUtils.filter("test", new Pattern[] {
0622: Pattern.compile(".*e.*"), Pattern.compile(".*s.*") },
0623: new Pattern[] { Pattern.compile(".*a.*"),
0624: Pattern.compile(".*b.*") }));
0625: assertTrue(StringUtils.filter("test", new Pattern[] {
0626: Pattern.compile(".*a.*"), Pattern.compile(".*s.*") },
0627: new Pattern[] { Pattern.compile(".*a.*"),
0628: Pattern.compile(".*b.*") }));
0629: assertFalse(StringUtils.filter("test", new Pattern[] {
0630: Pattern.compile(".*e.*"), Pattern.compile(".*s.*") },
0631: new Pattern[] { Pattern.compile(".*a.*"),
0632: Pattern.compile(".*t.*") }));
0633: assertFalse(StringUtils.filter("test", new Pattern[] {
0634: Pattern.compile(".*x.*"), Pattern.compile(".*b.*") },
0635: new Pattern[] { Pattern.compile(".*a.*"),
0636: Pattern.compile(".*t.*") }));
0637: }
0638:
0639: public void testCapitalize() {
0640: assertNull(StringUtils.capitalize(null));
0641: assertEquals("", StringUtils.capitalize(""));
0642: assertEquals("Hohoho", StringUtils.capitalize("Hohoho"));
0643: assertEquals("HohoHo", StringUtils.capitalize("hohoHo"));
0644: }
0645:
0646: public void testUncapitalize() {
0647: assertNull(StringUtils.uncapitalize(null));
0648: assertEquals("", StringUtils.uncapitalize(""));
0649: assertEquals("hohoho", StringUtils.uncapitalize("Hohoho"));
0650: assertEquals("hohoHo", StringUtils.uncapitalize("hohoHo"));
0651: }
0652:
0653: public void testConvertBbcode() {
0654: assertNull(StringUtils.convertBbcode(null));
0655:
0656: String source = "[B]text[/B]text[b]text[/b]text\n"
0657: + "[U]text[/U]text[u]text[/u]text\r\n"
0658: + "[I]text[/I]text[i]text[/i]text\n"
0659: + "[I]text[/I]text[i]text[/i]text\r\n"
0660: + "[pre]text[/pre]text[pre]text[/pre]text\n"
0661: + "[LIST]text[/LIST]text[list]text[/list]text\n"
0662: + "[*]text\r\n"
0663: + "[LIST]\n"
0664: + "[*]text\n"
0665: + "[/LIST]\n"
0666: + "[LIST]\r\n"
0667: + "[*]text\r\n"
0668: + "[/LIST]\r\n"
0669: + "[color= #ffcc00 ]text[/color]text[COLOR=#FFCC00]text[/COLOR]text\n"
0670: + "[size= -2 ]text[/size]text[SIZE=3]text[/SIZE]text\r\n"
0671: + "[img] http://www.uwyn.com/images/logo.png [/img]\n"
0672: + "[url] www.uwyn.com [/url]\n"
0673: + "[url] http://www.uwyn.com [/url]\n"
0674: + "[url] /index.html [/url]\n"
0675: + "[url=http://www.uwyn.com] The site of Uwyn. [/url]\n"
0676: + "[url=www.uwyn.com] The site of Uwyn. [/url]\n"
0677: + "[url=index.html] The site of Uwyn. [/url]\n";
0678:
0679: assertEquals(
0680: "<b>text</b>text<b>text</b>text<br />\n"
0681: + "<u>text</u>text<u>text</u>text<br />\r\n"
0682: + "<i>text</i>text<i>text</i>text<br />\n"
0683: + "<i>text</i>text<i>text</i>text<br />\r\n"
0684: + "<pre>text</pre>text<pre>text</pre>text<br />\n"
0685: + "<ul>text</ul>text<ul>text</ul>text<br />\n"
0686: + "<li>text<br />\r\n"
0687: + "</li><ul>\n"
0688: + "<li>text<br />\n"
0689: + "</li></ul>\n"
0690: + "<ul>\r\n"
0691: + "<li>text<br />\r\n"
0692: + "</li></ul>\r\n"
0693: + "<font color=\"#ffcc00\">text</font>text<font color=\"#FFCC00\">text</font>text<br />\n"
0694: + "<font size=\"-2\">text</font>text<font size=\"3\">text</font>text<br />\r\n"
0695: + "<div class=\"bbcode_img\"><img src=\"http://www.uwyn.com/images/logo.png\" border=\"0\" alt=\"\" /></div><br />\n"
0696: + "<a href=\"www.uwyn.com\">www.uwyn.com</a><br />\n"
0697: + "<a href=\"http://www.uwyn.com\" target=\"_blank\">http://www.uwyn.com</a><br />\n"
0698: + "<a href=\"/index.html\">/index.html</a><br />\n"
0699: + "<a href=\"http://www.uwyn.com\" target=\"_blank\"> The site of Uwyn. </a><br />\n"
0700: + "<a href=\"www.uwyn.com\"> The site of Uwyn. </a><br />\n"
0701: + "<a href=\"index.html\"> The site of Uwyn. </a><br />\n",
0702: StringUtils.convertBbcode(source));
0703:
0704: source = "[url]http://www.uwyn.com/page?really=long&statement=that&should=be&reduced=dont&you=think&so=blaat[/url]\n"
0705: + "[url=http://www.uwyn.com/page?really=long&statement=that&should=be&reduced=dont&you=think&so=blaat] The site of Uwyn. [/url]\n"
0706: + "[url]http://www.uwyn.com/page/really/long/statement/that/should/be/reduced/dont/you/think/so/blaat[/url]\n"
0707: + "[url=http://www.uwyn.com/page/really/long/statement/that/should/be/reduced/dont/you/think/so/blaat] The site of Uwyn. [/url]\n"
0708: + "[url]http://www.uwyn.com/other/page?really=long&statement=that&should=be&reduced=dont&you=think&so=blaat[/url]\n"
0709: + "[url]http://www.uwyn.com/page/really/long/statement/that/should/be/reduced/dont/you/think/so/blaat?really=long&statement=that&should=be&reduced=dont&you=think&so=blaat[/url]\n"
0710: + "[url]http://www.uwyn.com/short?url=true[/url]\n";
0711:
0712: assertEquals(
0713: "<a href=\"http://www.uwyn.com/page?really=long&statement=that&should=be&reduced=dont&you=think&so=blaat\" target=\"_blank\">http://www.uwyn.com/page?...</a><br />\n"
0714: + "<a href=\"http://www.uwyn.com/page?really=long&statement=that&should=be&reduced=dont&you=think&so=blaat\" target=\"_blank\"> The site of Uwyn. </a><br />\n"
0715: + "<a href=\"http://www.uwyn.com/page/really/long/statement/that/should/be/reduced/dont/you/think/so/blaat\" target=\"_blank\">http://www.uwyn.com/.../blaat</a><br />\n"
0716: + "<a href=\"http://www.uwyn.com/page/really/long/statement/that/should/be/reduced/dont/you/think/so/blaat\" target=\"_blank\"> The site of Uwyn. </a><br />\n"
0717: + "<a href=\"http://www.uwyn.com/other/page?really=long&statement=that&should=be&reduced=dont&you=think&so=blaat\" target=\"_blank\">http://www.uwyn.com/other/page?...</a><br />\n"
0718: + "<a href=\"http://www.uwyn.com/page/really/long/statement/that/should/be/reduced/dont/you/think/so/blaat?really=long&statement=that&should=be&reduced=dont&you=think&so=blaat\" target=\"_blank\">http://www.uwyn.com/.../blaat?...</a><br />\n"
0719: + "<a href=\"http://www.uwyn.com/short?url=true\" target=\"_blank\">http://www.uwyn.com/short?url=true</a><br />\n",
0720: StringUtils.convertBbcode(source,
0721: StringUtils.SHORTEN_URL));
0722:
0723: source = "[code]This is code[/code]";
0724:
0725: assertEquals(
0726: "<div class=\"codebody\"><pre>This is code</pre></div>",
0727: StringUtils.convertBbcode(source,
0728: StringUtils.SHORTEN_URL));
0729:
0730: source = "[b]Check out my righteous code[/b]\n"
0731: + "[code]This is code[/code]\n" + "[u]VERY COOL[/u]";
0732:
0733: assertEquals(
0734: "<b>Check out my righteous code</b><br />\n"
0735: + "<div class=\"codebody\"><pre>This is code</pre></div><br />\n"
0736: + "<u>VERY COOL</u>", StringUtils
0737: .convertBbcode(source, StringUtils.SHORTEN_URL));
0738:
0739: source = "[url]javascript:self.close();[/url]\n"
0740: + "[url=javascript:self.close();]Click here[/url]";
0741:
0742: assertEquals(
0743: "<a href=\"javascript:self.close();\">javascript:self.close();</a><br />\n"
0744: + "<a href=\"javascript:self.close();\">Click here</a>",
0745: StringUtils.convertBbcode(source,
0746: StringUtils.SHORTEN_URL));
0747:
0748: assertEquals(
0749: "<a href=\"http://self.close();\" target=\"_blank\">javascript:self.close();</a><br />\n"
0750: + "<a href=\"http://self.close();\" target=\"_blank\">Click here</a>",
0751: StringUtils.convertBbcode(source,
0752: StringUtils.SHORTEN_URL,
0753: StringUtils.SANITIZE_URL));
0754:
0755: source = "[quote]This is a quote[/quote]\n"
0756: + "[quote=Bob]This is a quote from Bob[/quote]";
0757:
0758: assertEquals(
0759: "<div class=\"quotebody\">This is a quote</div><br />\n"
0760: + "<div class=\"quoteaccount\">Bob:</div><div class=\"quotebody\">This is a quote from Bob</div>",
0761: StringUtils.convertBbcode(source));
0762:
0763: source = "[code]test1[/code]\n\n" + "[b]mijnstijl.css[/b]\n"
0764: + "[code]test2[/code]\n\n"
0765: + "[b]mijndocument.html[/b]\n" + "[code]test3[/code]";
0766:
0767: assertEquals(
0768: "<div class=\"codebody\"><pre>test1</pre></div><br />\n<br />\n"
0769: + "<b>mijnstijl.css</b><br />\n"
0770: + "<div class=\"codebody\"><pre>test2</pre></div><br />\n<br />\n"
0771: + "<b>mijndocument.html</b><br />\n"
0772: + "<div class=\"codebody\"><pre>test3</pre></div>",
0773: StringUtils.convertBbcode(source));
0774:
0775: source = "[code]test1[/code]\n\n" + "[code]test2[/code]\n\n"
0776: + "[code]test3[/code]";
0777:
0778: assertEquals(
0779: "<div class=\"codebody\"><pre>test1</pre></div><br />\n<br />\n"
0780: + "<div class=\"codebody\"><pre>test2</pre></div><br />\n<br />\n"
0781: + "<div class=\"codebody\"><pre>test3</pre></div>",
0782: StringUtils.convertBbcode(source));
0783:
0784: source = "http://www.uwyn.com";
0785:
0786: assertEquals(
0787: "<a href=\"http://www.uwyn.com\" target=\"_blank\">http://www.uwyn.com</a>",
0788: StringUtils.convertBbcode(source,
0789: StringUtils.CONVERT_BARE_URLS));
0790:
0791: assertEquals("http://www.uwyn.com", StringUtils
0792: .convertBbcode(source));
0793:
0794: source = "Spacing test http://www.uwyn.com tset gnicapS";
0795:
0796: assertEquals(
0797: "Spacing test <a href=\"http://www.uwyn.com\" target=\"_blank\">http://www.uwyn.com</a> tset gnicapS",
0798: StringUtils.convertBbcode(source,
0799: StringUtils.CONVERT_BARE_URLS));
0800:
0801: source = "http://www.uwyn.com\n"
0802: + "[url]http://www.uwyn.com[/url]\n"
0803: + "[url=http://www.uwyn.com]Uwyn[/url]";
0804:
0805: assertEquals(
0806: "<a href=\"http://www.uwyn.com\" target=\"_blank\" rel=\"nofollow\">http://www.uwyn.com</a><br />\n"
0807: + "<a href=\"http://www.uwyn.com\" target=\"_blank\" rel=\"nofollow\">http://www.uwyn.com</a><br />\n"
0808: + "<a href=\"http://www.uwyn.com\" target=\"_blank\" rel=\"nofollow\">Uwyn</a>",
0809: StringUtils.convertBbcode(source,
0810: StringUtils.CONVERT_BARE_URLS,
0811: StringUtils.NO_FOLLOW_LINKS));
0812:
0813: source = "[code]" + "codepart" + "[code]" + "text"
0814: + "[/code]codepart3[/code]" + "[code]codepart4[/code]";
0815:
0816: assertEquals(
0817: "<div class=\"codebody\"><pre>codepart</pre></div><div class=\"codebody\"><pre>text</pre></div>codepart3[/code]<div class=\"codebody\"><pre>codepart4</pre></div>",
0818: StringUtils.convertBbcode(source));
0819: }
0820:
0821: public void testConvertToBoolean() {
0822: assertFalse(StringUtils.convertToBoolean(null));
0823: assertFalse(StringUtils.convertToBoolean("blabla"));
0824: assertFalse(StringUtils.convertToBoolean("0"));
0825: assertTrue(StringUtils.convertToBoolean("1"));
0826: assertTrue(StringUtils.convertToBoolean("t"));
0827: assertTrue(StringUtils.convertToBoolean("true"));
0828: assertTrue(StringUtils.convertToBoolean("y"));
0829: assertTrue(StringUtils.convertToBoolean("yes"));
0830: assertTrue(StringUtils.convertToBoolean("on"));
0831: assertTrue(StringUtils.convertToBoolean("T"));
0832: assertTrue(StringUtils.convertToBoolean("TRUE"));
0833: assertTrue(StringUtils.convertToBoolean("Y"));
0834: assertTrue(StringUtils.convertToBoolean("YES"));
0835: assertTrue(StringUtils.convertToBoolean("ON"));
0836: }
0837:
0838: public void testGetDocumentPosition() {
0839: DocumentPosition position;
0840:
0841: String document1 = "0123456789\n" + "9012345678\n" + "\n"
0842: + "\n" + "8901234567\n" + "7890123456\n" + "\n"
0843: + "6789012345\n";
0844:
0845: assertNull(StringUtils.getDocumentPosition(null, 1));
0846: assertNull(StringUtils.getDocumentPosition(document1, -2));
0847:
0848: position = StringUtils.getDocumentPosition(document1, 0);
0849: assertEquals("0123456789", position.getLineContent());
0850: assertEquals(1, position.getLine());
0851: assertEquals(1, position.getColumn());
0852:
0853: position = StringUtils.getDocumentPosition(document1, 10);
0854: assertEquals("0123456789", position.getLineContent());
0855: assertEquals(1, position.getLine());
0856: assertEquals(11, position.getColumn());
0857:
0858: position = StringUtils.getDocumentPosition(document1, 11);
0859: assertEquals("9012345678", position.getLineContent());
0860: assertEquals(2, position.getLine());
0861: assertEquals(1, position.getColumn());
0862:
0863: position = StringUtils.getDocumentPosition(document1, 12);
0864: assertEquals("9012345678", position.getLineContent());
0865: assertEquals(2, position.getLine());
0866: assertEquals(2, position.getColumn());
0867:
0868: position = StringUtils.getDocumentPosition(document1, 21);
0869: assertEquals("9012345678", position.getLineContent());
0870: assertEquals(2, position.getLine());
0871: assertEquals(11, position.getColumn());
0872:
0873: position = StringUtils.getDocumentPosition(document1, 22);
0874: assertEquals("", position.getLineContent());
0875: assertEquals(3, position.getLine());
0876: assertEquals(1, position.getColumn());
0877:
0878: position = StringUtils.getDocumentPosition(document1, 23);
0879: assertEquals("", position.getLineContent());
0880: assertEquals(4, position.getLine());
0881: assertEquals(1, position.getColumn());
0882:
0883: position = StringUtils.getDocumentPosition(document1, 24);
0884: assertEquals("8901234567", position.getLineContent());
0885: assertEquals(5, position.getLine());
0886: assertEquals(1, position.getColumn());
0887:
0888: position = StringUtils.getDocumentPosition(document1, 25);
0889: assertEquals("8901234567", position.getLineContent());
0890: assertEquals(5, position.getLine());
0891: assertEquals(2, position.getColumn());
0892:
0893: position = StringUtils.getDocumentPosition(document1, 38);
0894: assertEquals("7890123456", position.getLineContent());
0895: assertEquals(6, position.getLine());
0896: assertEquals(4, position.getColumn());
0897:
0898: position = StringUtils.getDocumentPosition(document1, 38);
0899: assertEquals("7890123456", position.getLineContent());
0900: assertEquals(6, position.getLine());
0901: assertEquals(4, position.getColumn());
0902:
0903: position = StringUtils.getDocumentPosition(document1, 47);
0904: assertEquals("6789012345", position.getLineContent());
0905: assertEquals(8, position.getLine());
0906: assertEquals(1, position.getColumn());
0907:
0908: position = StringUtils.getDocumentPosition(document1, 57);
0909: assertEquals("6789012345", position.getLineContent());
0910: assertEquals(8, position.getLine());
0911: assertEquals(11, position.getColumn());
0912:
0913: position = StringUtils.getDocumentPosition(document1, 58);
0914: assertEquals("", position.getLineContent());
0915: assertEquals(9, position.getLine());
0916: assertEquals(1, position.getColumn());
0917:
0918: assertNull(StringUtils.getDocumentPosition(document1, 59));
0919:
0920: String document2 = "0123456789\r" + "9012345678\r" + "\r"
0921: + "\r" + "8901234567\r" + "7890123456\r" + "\r"
0922: + "6789012345\r";
0923:
0924: assertNull(StringUtils.getDocumentPosition(null, 1));
0925: assertNull(StringUtils.getDocumentPosition(document2, -2));
0926:
0927: position = StringUtils.getDocumentPosition(document2, 0);
0928: assertEquals("0123456789", position.getLineContent());
0929: assertEquals(1, position.getLine());
0930: assertEquals(1, position.getColumn());
0931:
0932: position = StringUtils.getDocumentPosition(document2, 10);
0933: assertEquals("0123456789", position.getLineContent());
0934: assertEquals(1, position.getLine());
0935: assertEquals(11, position.getColumn());
0936:
0937: position = StringUtils.getDocumentPosition(document2, 11);
0938: assertEquals("9012345678", position.getLineContent());
0939: assertEquals(2, position.getLine());
0940: assertEquals(1, position.getColumn());
0941:
0942: position = StringUtils.getDocumentPosition(document2, 12);
0943: assertEquals("9012345678", position.getLineContent());
0944: assertEquals(2, position.getLine());
0945: assertEquals(2, position.getColumn());
0946:
0947: position = StringUtils.getDocumentPosition(document2, 21);
0948: assertEquals("9012345678", position.getLineContent());
0949: assertEquals(2, position.getLine());
0950: assertEquals(11, position.getColumn());
0951:
0952: position = StringUtils.getDocumentPosition(document2, 22);
0953: assertEquals("", position.getLineContent());
0954: assertEquals(3, position.getLine());
0955: assertEquals(1, position.getColumn());
0956:
0957: position = StringUtils.getDocumentPosition(document2, 23);
0958: assertEquals("", position.getLineContent());
0959: assertEquals(4, position.getLine());
0960: assertEquals(1, position.getColumn());
0961:
0962: position = StringUtils.getDocumentPosition(document2, 24);
0963: assertEquals("8901234567", position.getLineContent());
0964: assertEquals(5, position.getLine());
0965: assertEquals(1, position.getColumn());
0966:
0967: position = StringUtils.getDocumentPosition(document2, 25);
0968: assertEquals("8901234567", position.getLineContent());
0969: assertEquals(5, position.getLine());
0970: assertEquals(2, position.getColumn());
0971:
0972: position = StringUtils.getDocumentPosition(document2, 38);
0973: assertEquals("7890123456", position.getLineContent());
0974: assertEquals(6, position.getLine());
0975: assertEquals(4, position.getColumn());
0976:
0977: position = StringUtils.getDocumentPosition(document2, 38);
0978: assertEquals(6, position.getLine());
0979: assertEquals("7890123456", position.getLineContent());
0980: assertEquals(4, position.getColumn());
0981:
0982: position = StringUtils.getDocumentPosition(document2, 47);
0983: assertEquals("6789012345", position.getLineContent());
0984: assertEquals(8, position.getLine());
0985: assertEquals(1, position.getColumn());
0986:
0987: position = StringUtils.getDocumentPosition(document2, 57);
0988: assertEquals("6789012345", position.getLineContent());
0989: assertEquals(8, position.getLine());
0990: assertEquals(11, position.getColumn());
0991:
0992: position = StringUtils.getDocumentPosition(document2, 58);
0993: assertEquals("", position.getLineContent());
0994: assertEquals(9, position.getLine());
0995: assertEquals(1, position.getColumn());
0996:
0997: assertNull(StringUtils.getDocumentPosition(document2, 59));
0998:
0999: String document3 = "0123456789\r\n" + "9012345678\r\n" + "\r\n"
1000: + "\r\n" + "8901234567\r\n" + "7890123456\r\n" + "\r\n"
1001: + "6789012345\r\n";
1002:
1003: position = StringUtils.getDocumentPosition(document3, 0);
1004: assertEquals("0123456789", position.getLineContent());
1005: assertEquals(1, position.getLine());
1006: assertEquals(1, position.getColumn());
1007:
1008: position = StringUtils.getDocumentPosition(document3, 10);
1009: assertEquals("0123456789", position.getLineContent());
1010: assertEquals(1, position.getLine());
1011: assertEquals(11, position.getColumn());
1012:
1013: position = StringUtils.getDocumentPosition(document3, 11);
1014: assertEquals("9012345678", position.getLineContent());
1015: assertEquals(2, position.getLine());
1016: assertEquals(0, position.getColumn());
1017:
1018: position = StringUtils.getDocumentPosition(document3, 12);
1019: assertEquals("9012345678", position.getLineContent());
1020: assertEquals(2, position.getLine());
1021: assertEquals(1, position.getColumn());
1022:
1023: position = StringUtils.getDocumentPosition(document3, 13);
1024: assertEquals("9012345678", position.getLineContent());
1025: assertEquals(2, position.getLine());
1026: assertEquals(2, position.getColumn());
1027:
1028: position = StringUtils.getDocumentPosition(document3, 22);
1029: assertEquals("9012345678", position.getLineContent());
1030: assertEquals(2, position.getLine());
1031: assertEquals(11, position.getColumn());
1032:
1033: position = StringUtils.getDocumentPosition(document3, 23);
1034: assertEquals("", position.getLineContent());
1035: assertEquals(3, position.getLine());
1036: assertEquals(0, position.getColumn());
1037:
1038: position = StringUtils.getDocumentPosition(document3, 24);
1039: assertEquals("", position.getLineContent());
1040: assertEquals(3, position.getLine());
1041: assertEquals(1, position.getColumn());
1042:
1043: position = StringUtils.getDocumentPosition(document3, 25);
1044: assertEquals("", position.getLineContent());
1045: assertEquals(4, position.getLine());
1046: assertEquals(0, position.getColumn());
1047:
1048: position = StringUtils.getDocumentPosition(document3, 26);
1049: assertEquals("", position.getLineContent());
1050: assertEquals(4, position.getLine());
1051: assertEquals(1, position.getColumn());
1052:
1053: position = StringUtils.getDocumentPosition(document3, 27);
1054: assertEquals("8901234567", position.getLineContent());
1055: assertEquals(5, position.getLine());
1056: assertEquals(0, position.getColumn());
1057:
1058: position = StringUtils.getDocumentPosition(document3, 28);
1059: assertEquals("8901234567", position.getLineContent());
1060: assertEquals(5, position.getLine());
1061: assertEquals(1, position.getColumn());
1062:
1063: position = StringUtils.getDocumentPosition(document3, 29);
1064: assertEquals("8901234567", position.getLineContent());
1065: assertEquals(5, position.getLine());
1066: assertEquals(2, position.getColumn());
1067:
1068: position = StringUtils.getDocumentPosition(document3, 43);
1069: assertEquals("7890123456", position.getLineContent());
1070: assertEquals(6, position.getLine());
1071: assertEquals(4, position.getColumn());
1072:
1073: position = StringUtils.getDocumentPosition(document3, 54);
1074: assertEquals("6789012345", position.getLineContent());
1075: assertEquals(8, position.getLine());
1076: assertEquals(1, position.getColumn());
1077:
1078: position = StringUtils.getDocumentPosition(document3, 64);
1079: assertEquals("6789012345", position.getLineContent());
1080: assertEquals(8, position.getLine());
1081: assertEquals(11, position.getColumn());
1082:
1083: position = StringUtils.getDocumentPosition(document3, 66);
1084: assertEquals("", position.getLineContent());
1085: assertEquals(9, position.getLine());
1086: assertEquals(1, position.getColumn());
1087:
1088: assertNull(StringUtils.getDocumentPosition(document3, 67));
1089: }
1090:
1091: public void testWordWrap() {
1092: String buffer = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse nisi enim, rutrum eget, condimentum quis, malesuada cursus, magna. Fusce suscipit, lorem accumsan hendrerit convallis, purus sem feugiat nulla, a consequat turpis nisi sed ipsum. Duis iaculis suscipit quam. Praesent placerat nibh lobortis nulla. Morbi scelerisque. Etiam et libero. Aliquam viverra tortor eget lectus. Cras quis sem id massa tempor imperdiet. Morbi posuere purus sit amet tortor. Curabitur venenatis ultrices elit. Integer vitae neque. Suspendisse at ipsum sed orci interdum dictum. Praesent condimentum augue et diam. Nunc a neque. Quisque arcu.\n"
1093: + "Praesent diam dolor, gravida eget, faucibus in, aliquet sed, elit. Sed lacinia lorem eu leo condimentum lacinia. Proin egestas. Sed porta magna. Nunc ut est. Sed vitae sem. Nunc tempor mattis felis. Nunc urna magna, aliquet quis, consequat ut, ullamcorper eget, libero. Mauris eu dui. Integer ante nibh, lobortis ut, sagittis eu, pretium sed, quam. Praesent fringilla nisi non metus mollis cursus. Ut convallis. Pellentesque imperdiet rhoncus nulla. Fusce tempor. Sed mollis. Fusce feugiat. Proin porttitor nulla sit amet velit.\n"
1094: + "\n"
1095: + "Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed eu est. Duis viverra orci non nisi mollis feugiat. Morbi ut felis. Phasellus placerat elit ac ligula. Vivamus vitae augue. Curabitur pharetra porta risus. Nullam eget est nec arcu bibendum condimentum. Quisque sed pede vitae odio tristique interdum. Aenean magna dolor, sagittis eu, vestibulum ut, varius luctus, erat. Duis tempus libero non lacus. Sed sapien enim, elementum at, laoreet vel, adipiscing sed, purus. Vestibulum magna. Quisque in pede. Proin vitae ligula. Aenean accumsan blandit magna. Nullam cursus tellus in urna. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.";
1096:
1097: String result = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse nisi enim,\n"
1098: + "rutrum eget, condimentum quis, malesuada cursus, magna. Fusce suscipit, lorem \n"
1099: + "accumsan hendrerit convallis, purus sem feugiat nulla, a consequat turpis nisi \n"
1100: + "sed ipsum. Duis iaculis suscipit quam. Praesent placerat nibh lobortis nulla. \n"
1101: + "Morbi scelerisque. Etiam et libero. Aliquam viverra tortor eget lectus. Cras \n"
1102: + "quis sem id massa tempor imperdiet. Morbi posuere purus sit amet tortor. \n"
1103: + "Curabitur venenatis ultrices elit. Integer vitae neque. Suspendisse at ipsum sed\n"
1104: + "orci interdum dictum. Praesent condimentum augue et diam. Nunc a neque. Quisque \n"
1105: + "arcu.\n"
1106: + "Praesent diam dolor, gravida eget, faucibus in, aliquet sed, elit. Sed lacinia \n"
1107: + "lorem eu leo condimentum lacinia. Proin egestas. Sed porta magna. Nunc ut est. \n"
1108: + "Sed vitae sem. Nunc tempor mattis felis. Nunc urna magna, aliquet quis, \n"
1109: + "consequat ut, ullamcorper eget, libero. Mauris eu dui. Integer ante nibh, \n"
1110: + "lobortis ut, sagittis eu, pretium sed, quam. Praesent fringilla nisi non metus \n"
1111: + "mollis cursus. Ut convallis. Pellentesque imperdiet rhoncus nulla. Fusce tempor.\n"
1112: + "Sed mollis. Fusce feugiat. Proin porttitor nulla sit amet velit.\n"
1113: + "\n"
1114: + "Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia\n"
1115: + "Curae; Sed eu est. Duis viverra orci non nisi mollis feugiat. Morbi ut felis. \n"
1116: + "Phasellus placerat elit ac ligula. Vivamus vitae augue. Curabitur pharetra porta\n"
1117: + "risus. Nullam eget est nec arcu bibendum condimentum. Quisque sed pede vitae \n"
1118: + "odio tristique interdum. Aenean magna dolor, sagittis eu, vestibulum ut, varius \n"
1119: + "luctus, erat. Duis tempus libero non lacus. Sed sapien enim, elementum at, \n"
1120: + "laoreet vel, adipiscing sed, purus. Vestibulum magna. Quisque in pede. Proin \n"
1121: + "vitae ligula. Aenean accumsan blandit magna. Nullam cursus tellus in urna. Cum \n"
1122: + "sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus \n"
1123: + "mus.";
1124:
1125: assertEquals(result, StringUtils.wordWrap(buffer, 80, null));
1126: }
1127: }
|