Ejemplo n.º 1
0
 @Test
 public void testQuoteIfString() {
   assertEquals("'myString'", StringUtils.quoteIfString("myString"));
   assertEquals("''", StringUtils.quoteIfString(""));
   assertEquals(new Integer(5), StringUtils.quoteIfString(5));
   assertNull(StringUtils.quoteIfString(null));
 }
Ejemplo n.º 2
0
  @Test
  public void testSortStringArray() {
    String[] input = new String[] {"myString2"};
    input = StringUtils.addStringToArray(input, "myString1");
    assertEquals("myString2", input[0]);
    assertEquals("myString1", input[1]);

    StringUtils.sortStringArray(input);
    assertEquals("myString1", input[0]);
    assertEquals("myString2", input[1]);
  }
Ejemplo n.º 3
0
  @Test
  public void testMergeStringArrays() {
    String[] input1 = new String[] {"myString2"};
    String[] input2 = new String[] {"myString1", "myString2"};
    String[] result = StringUtils.mergeStringArrays(input1, input2);
    assertEquals(2, result.length);
    assertEquals("myString2", result[0]);
    assertEquals("myString1", result[1]);

    assertArrayEquals(input1, StringUtils.mergeStringArrays(input1, null));
    assertArrayEquals(input2, StringUtils.mergeStringArrays(null, input2));
    assertNull(StringUtils.mergeStringArrays(null, null));
  }
Ejemplo n.º 4
0
 @Test
 public void testGetFilenameExtension() {
   assertEquals(null, StringUtils.getFilenameExtension(null));
   assertEquals(null, StringUtils.getFilenameExtension(""));
   assertEquals(null, StringUtils.getFilenameExtension("myfile"));
   assertEquals(null, StringUtils.getFilenameExtension("myPath/myfile"));
   assertEquals(null, StringUtils.getFilenameExtension("/home/user/.m2/settings/myfile"));
   assertEquals("", StringUtils.getFilenameExtension("myfile."));
   assertEquals("", StringUtils.getFilenameExtension("myPath/myfile."));
   assertEquals("txt", StringUtils.getFilenameExtension("myfile.txt"));
   assertEquals("txt", StringUtils.getFilenameExtension("mypath/myfile.txt"));
   assertEquals("txt", StringUtils.getFilenameExtension("/home/user/.m2/settings/myfile.txt"));
 }
Ejemplo n.º 5
0
 @Test
 public void testCleanPath() {
   assertEquals("mypath/myfile", StringUtils.cleanPath("mypath/myfile"));
   assertEquals("mypath/myfile", StringUtils.cleanPath("mypath\\myfile"));
   assertEquals("mypath/myfile", StringUtils.cleanPath("mypath/../mypath/myfile"));
   assertEquals("mypath/myfile", StringUtils.cleanPath("mypath/myfile/../../mypath/myfile"));
   assertEquals("../mypath/myfile", StringUtils.cleanPath("../mypath/myfile"));
   assertEquals("../mypath/myfile", StringUtils.cleanPath("../mypath/../mypath/myfile"));
   assertEquals("../mypath/myfile", StringUtils.cleanPath("mypath/../../mypath/myfile"));
   assertEquals("/../mypath/myfile", StringUtils.cleanPath("/../mypath/myfile"));
   assertEquals("/mypath/myfile", StringUtils.cleanPath("/a/:b/../../mypath/myfile"));
   assertEquals(
       "file:///c:/path/to/the%20file.txt",
       StringUtils.cleanPath("file:///c:/some/../path/to/the%20file.txt"));
 }
Ejemplo n.º 6
0
 @Test
 public void testTrimTrailingCharacter() throws Exception {
   assertEquals(null, StringUtils.trimTrailingCharacter(null, ' '));
   assertEquals("", StringUtils.trimTrailingCharacter("", ' '));
   assertEquals("", StringUtils.trimTrailingCharacter(" ", ' '));
   assertEquals("\t", StringUtils.trimTrailingCharacter("\t", ' '));
   assertEquals("a", StringUtils.trimTrailingCharacter("a ", ' '));
   assertEquals(" a", StringUtils.trimTrailingCharacter(" a", ' '));
   assertEquals(" a", StringUtils.trimTrailingCharacter(" a ", ' '));
   assertEquals(" a b", StringUtils.trimTrailingCharacter(" a b ", ' '));
   assertEquals(" a b  c", StringUtils.trimTrailingCharacter(" a b  c ", ' '));
 }
Ejemplo n.º 7
0
 @Test
 public void testContainsWhitespace() throws Exception {
   assertFalse(StringUtils.containsWhitespace(null));
   assertFalse(StringUtils.containsWhitespace(""));
   assertFalse(StringUtils.containsWhitespace("a"));
   assertFalse(StringUtils.containsWhitespace("abc"));
   assertTrue(StringUtils.containsWhitespace(" "));
   assertTrue(StringUtils.containsWhitespace(" a"));
   assertTrue(StringUtils.containsWhitespace("abc "));
   assertTrue(StringUtils.containsWhitespace("a b"));
   assertTrue(StringUtils.containsWhitespace("a  b"));
 }
Ejemplo n.º 8
0
 @Test
 public void testTrimLeadingWhitespace() throws Exception {
   assertEquals(null, StringUtils.trimLeadingWhitespace(null));
   assertEquals("", StringUtils.trimLeadingWhitespace(""));
   assertEquals("", StringUtils.trimLeadingWhitespace(" "));
   assertEquals("", StringUtils.trimLeadingWhitespace("\t"));
   assertEquals("a", StringUtils.trimLeadingWhitespace(" a"));
   assertEquals("a ", StringUtils.trimLeadingWhitespace("a "));
   assertEquals("a ", StringUtils.trimLeadingWhitespace(" a "));
   assertEquals("a b ", StringUtils.trimLeadingWhitespace(" a b "));
   assertEquals("a b  c ", StringUtils.trimLeadingWhitespace(" a b  c "));
 }
Ejemplo n.º 9
0
 @Test
 public void testTokenizeToStringArrayWithNotTrimTokens() {
   String[] sa = StringUtils.tokenizeToStringArray("a,b ,c", ",", false, true);
   assertEquals(3, sa.length);
   assertTrue(
       "components are correct", sa[0].equals("a") && sa[1].equals("b ") && sa[2].equals("c"));
 }
Ejemplo n.º 10
0
 @Test
 public void testDelimitedListToStringArrayWithComma() {
   String[] sa = StringUtils.delimitedListToStringArray("a,b", ",");
   assertEquals(2, sa.length);
   assertEquals("a", sa[0]);
   assertEquals("b", sa[1]);
 }
Ejemplo n.º 11
0
 @Test
 public void testDelimitedListToStringArrayWithSemicolon() {
   String[] sa = StringUtils.delimitedListToStringArray("a;b", ";");
   assertEquals(2, sa.length);
   assertEquals("a", sa[0]);
   assertEquals("b", sa[1]);
 }
Ejemplo n.º 12
0
 @Test
 public void testSplitArrayElementsIntoPropertiesAndDeletedChars() {
   String[] input = new String[] {"key1=value1 ", "key2 =\"value2\""};
   Properties result = StringUtils.splitArrayElementsIntoProperties(input, "=", "\"");
   assertEquals("value1", result.getProperty("key1"));
   assertEquals("value2", result.getProperty("key2"));
 }
Ejemplo n.º 13
0
 @Test
 public void testRemoveDuplicateStrings() {
   String[] input = new String[] {"myString2", "myString1", "myString2"};
   input = StringUtils.removeDuplicateStrings(input);
   assertEquals("myString2", input[0]);
   assertEquals("myString1", input[1]);
 }
Ejemplo n.º 14
0
 @Test
 public void testParseLocaleStringSunnyDay() throws Exception {
   Locale expectedLocale = Locale.UK;
   Locale locale = StringUtils.parseLocaleString(expectedLocale.toString());
   assertNotNull("When given a bona-fide Locale string, must not return null.", locale);
   assertEquals(expectedLocale, locale);
 }
Ejemplo n.º 15
0
 @Test
 public void testCommaDelimitedListToStringArraySingleString() {
   // Could read these from files
   String s = "woeirqupoiewuropqiewuorpqiwueopriquwopeiurqopwieur";
   String[] sa = StringUtils.commaDelimitedListToStringArray(s);
   assertTrue("Found one String with no delimiters", sa.length == 1);
   assertTrue("Single array entry matches input String with no delimiters", sa[0].equals(s));
 }
Ejemplo n.º 16
0
 // SPR-11806
 @Test
 public void testParseLocaleWithVariantContainingCountryCode() {
   String variant = "GBtest";
   String localeString = "en_GB_" + variant;
   Locale locale = StringUtils.parseLocaleString(localeString);
   assertEquals(
       "Variant containing country code not extracted correctly", variant, locale.getVariant());
 }
Ejemplo n.º 17
0
 @Test
 public void testDelimitedListToStringArrayWithEmptyString() {
   String[] sa = StringUtils.delimitedListToStringArray("a,b", "");
   assertEquals(3, sa.length);
   assertEquals("a", sa[0]);
   assertEquals(",", sa[1]);
   assertEquals("b", sa[2]);
 }
 //	@Test
 // for performance test
 public void testAddLongPerformance() {
   String big = Long.MAX_VALUE + "";
   long t1 = System.currentTimeMillis();
   for (long i = 0; i < Long.MAX_VALUE; i++) {
     big = StringUtils.addLong(big, i);
   }
   long t2 = System.currentTimeMillis();
   System.out.println("spend" + (t2 - t1) + "ms");
 }
Ejemplo n.º 19
0
 @Test
 public void testGetFilename() {
   assertEquals(null, StringUtils.getFilename(null));
   assertEquals("", StringUtils.getFilename(""));
   assertEquals("myfile", StringUtils.getFilename("myfile"));
   assertEquals("myfile", StringUtils.getFilename("mypath/myfile"));
   assertEquals("myfile.", StringUtils.getFilename("myfile."));
   assertEquals("myfile.", StringUtils.getFilename("mypath/myfile."));
   assertEquals("myfile.txt", StringUtils.getFilename("myfile.txt"));
   assertEquals("myfile.txt", StringUtils.getFilename("mypath/myfile.txt"));
 }
Ejemplo n.º 20
0
 // SPR-8637
 @Test
 public void testParseLocaleWithMultiSpecialCharactersInVariant() throws Exception {
   final String variant = "proper-northern";
   final String localeString = "en_GB_" + variant;
   Locale locale = StringUtils.parseLocaleString(localeString);
   assertEquals(
       "Multi-valued variant portion of the Locale not extracted correctly.",
       variant,
       locale.getVariant());
 }
Ejemplo n.º 21
0
 // SPR-7779
 @Test
 public void testParseLocaleWithInvalidCharacters() {
   try {
     StringUtils.parseLocaleString(
         "%0D%0AContent-length:30%0D%0A%0D%0A%3Cscript%3Ealert%28123%29%3C/script%3E");
     fail("Should have thrown IllegalArgumentException");
   } catch (IllegalArgumentException ex) {
     // expected
   }
 }
Ejemplo n.º 22
0
 // SPR-3671
 @Test
 public void testParseLocaleWithMultiValuedVariantUsingSpacesAsSeparators() throws Exception {
   final String variant = "proper northern";
   final String localeString = "en GB " + variant;
   Locale locale = StringUtils.parseLocaleString(localeString);
   assertEquals(
       "Multi-valued variant portion of the Locale not extracted correctly.",
       variant,
       locale.getVariant());
 }
Ejemplo n.º 23
0
  /** We expect to see the empty Strings in the output. */
  @Test
  public void testCommaDelimitedListToStringArrayEmptyStrings() {
    // Could read these from files
    String[] sa = StringUtils.commaDelimitedListToStringArray("a,,b");
    assertEquals("a,,b produces array length 3", 3, sa.length);
    assertTrue(
        "components are correct", sa[0].equals("a") && sa[1].equals("") && sa[2].equals("b"));

    sa = new String[] {"", "", "a", ""};
    doTestCommaDelimitedListToStringArrayLegalMatch(sa);
  }
Ejemplo n.º 24
0
 // SPR-3671
 @Test
 public void
     testParseLocaleWithMultiValuedVariantUsingUnderscoresAsSeparatorsWithLotsOfLeadingWhitespace()
         throws Exception {
   final String variant = "proper_northern";
   final String localeString = "en_GB_____" + variant; // lots of underscores
   Locale locale = StringUtils.parseLocaleString(localeString);
   assertEquals(
       "Multi-valued variant portion of the Locale not extracted correctly.",
       variant,
       locale.getVariant());
 }
Ejemplo n.º 25
0
 /** Test of formatCommandOutput method, of class StrUt. */
 @Test
 public void testFormatCommandOutput() {
   System.out.println("formatCommandOutput");
   String inlist = "hdfs:///user/in1.txt,hdfs:///user/in2.txt";
   String outlist = "hdfs:///user/out1.txt,hdfs:///user/out2.txt";
   String expResult =
       "--input=\"hdfs:///./\" --inputlist=\"hdfs:///user/in1.txt,hdfs:///user/in2.txt\" --output=\"hdfs:///./\" --outputlist=\"hdfs:///user/out1.txt,hdfs:///user/out2.txt\"";
   String pattern =
       "--input=\"hdfs:///./\" --inputlist=\"%1$s\" --output=\"hdfs:///./\" --outputlist=\"%2$s\"";
   String result = StringUtils.formatCommandOutput(pattern, inlist, outlist);
   assertEquals(expResult, result);
 }
Ejemplo n.º 26
0
  @Test
  public void testDelete() throws Exception {
    String inString = "The quick brown fox jumped over the lazy dog";

    String noThe = StringUtils.delete(inString, "the");
    assertTrue(
        "Result has no the [" + noThe + "]",
        noThe.equals("The quick brown fox jumped over  lazy dog"));

    String nohe = StringUtils.delete(inString, "he");
    assertTrue(
        "Result has no he [" + nohe + "]", nohe.equals("T quick brown fox jumped over t lazy dog"));

    String nosp = StringUtils.delete(inString, " ");
    assertTrue("Result has no spaces", nosp.equals("Thequickbrownfoxjumpedoverthelazydog"));

    String killEnd = StringUtils.delete(inString, "dog");
    assertTrue("Result has no dog", killEnd.equals("The quick brown fox jumped over the lazy "));

    String mismatch = StringUtils.delete(inString, "dxxcxcxog");
    assertTrue("Result is unchanged", mismatch.equals(inString));

    String nochange = StringUtils.delete(inString, "");
    assertTrue("Result is unchanged", nochange.equals(inString));
  }
Ejemplo n.º 27
0
  @Test
  public void testReplace() throws Exception {
    String inString = "a6AazAaa77abaa";
    String oldPattern = "aa";
    String newPattern = "foo";

    // Simple replace
    String s = StringUtils.replace(inString, oldPattern, newPattern);
    assertTrue("Replace 1 worked", s.equals("a6AazAfoo77abfoo"));

    // Non match: no change
    s = StringUtils.replace(inString, "qwoeiruqopwieurpoqwieur", newPattern);
    assertTrue("Replace non matched is equal", s.equals(inString));

    // Null new pattern: should ignore
    s = StringUtils.replace(inString, oldPattern, null);
    assertTrue("Replace non matched is equal", s.equals(inString));

    // Null old pattern: should ignore
    s = StringUtils.replace(inString, null, newPattern);
    assertTrue("Replace non matched is equal", s.equals(inString));
  }
Ejemplo n.º 28
0
 private void doTestCommaDelimitedListToStringArrayLegalMatch(String[] components) {
   StringBuilder sb = new StringBuilder();
   for (int i = 0; i < components.length; i++) {
     if (i != 0) {
       sb.append(",");
     }
     sb.append(components[i]);
   }
   String[] sa = StringUtils.commaDelimitedListToStringArray(sb.toString());
   assertTrue("String array isn't null with legal match", sa != null);
   assertEquals("String array length is correct with legal match", components.length, sa.length);
   assertTrue("Output equals input", Arrays.equals(sa, components));
 }
Ejemplo n.º 29
0
  @Test
  public void testDeleteAny() throws Exception {
    String inString = "Able was I ere I saw Elba";

    String res = StringUtils.deleteAny(inString, "I");
    assertTrue("Result has no Is [" + res + "]", res.equals("Able was  ere  saw Elba"));

    res = StringUtils.deleteAny(inString, "AeEba!");
    assertTrue("Result has no Is [" + res + "]", res.equals("l ws I r I sw l"));

    String mismatch = StringUtils.deleteAny(inString, "#@$#$^");
    assertTrue("Result is unchanged", mismatch.equals(inString));

    String whitespace = "This is\n\n\n    \t   a messagy string with whitespace\n";
    assertTrue("Has CR", whitespace.contains("\n"));
    assertTrue("Has tab", whitespace.contains("\t"));
    assertTrue("Has  sp", whitespace.contains(" "));
    String cleaned = StringUtils.deleteAny(whitespace, "\n\t ");
    assertTrue("Has no CR", !cleaned.contains("\n"));
    assertTrue("Has no tab", !cleaned.contains("\t"));
    assertTrue("Has no sp", !cleaned.contains(" "));
    assertTrue("Still has chars", cleaned.length() > 10);
  }
Ejemplo n.º 30
0
 /** Test of normdir method, of class StrUt. */
 @Test
 public void testNormdir() {
   System.out.println("normdir");
   String dir = "test";
   String expResult = "test/";
   String result = StringUtils.normdir(dir);
   assertEquals(expResult, result);
   dir = "test/";
   result = StringUtils.normdir(dir);
   assertEquals(expResult, result);
   expResult = "test/test2/test3/";
   result = StringUtils.normdir("test", "test2", "test3");
   assertEquals(expResult, result);
   result = StringUtils.normdir("test/", "test2/", "test3/");
   assertEquals(expResult, result);
   result = StringUtils.normdir("test", "test2/", "/test3");
   assertEquals(expResult, result);
   result = StringUtils.normdir("test", "/test2/", "/test3/");
   assertEquals(expResult, result);
 }