public void testNotEmptyTrim() {
    String testStr = "x";
    assertTrue(!StringUtil.isEmptyTrim(testStr));

    testStr = "notherstring";
    assertTrue(!StringUtil.isEmptyTrim(testStr));
  }
  public void testLastToken() {

    String lastToken = StringUtil.lastToken("foo,cow,moo", ",");
    assertEquals(lastToken, "moo");

    assertNull(StringUtil.lastToken("foo,cow,moo", "|"));
  }
  public void testEmpty() {
    String testStr = "";
    assertTrue(StringUtil.isEmpty(testStr));

    testStr = null;
    assertTrue(StringUtil.isEmpty(testStr));
  }
  private static boolean allRangeComponentsAreEmpty(
      String opLeft, String valLeft, String opRight, String valRight) {
    boolean fromOpBlank = StringUtil.isEmpty(opLeft);
    boolean toOpBlank = StringUtil.isEmpty(opRight);
    boolean fromValueBlank = StringUtil.isEmpty(valLeft);
    boolean toValueBlank = StringUtil.isEmpty(valRight);

    return (fromOpBlank && toOpBlank && fromValueBlank && toValueBlank);
  }
  public void testRemoveCharacters() {

    boolean doesContainspChars = false;
    String test =
        StringUtil.removeIllegitCharacters(
            "1234%20STYLE=xss:e/**/xpression(try{a=firstTime}catch(e){firstTime=1;alert(42646)})");
    doesContainspChars = StringUtil.doesContainIllegitCharacters(test);
    assertFalse("Does not contain sp chars", doesContainspChars);

    assertTrue(StringUtil.doesContainIllegitCharacters("foo/cow"));
  }
  public void testDisplayAsSixDigitString() {
    String actual = StringUtil.displayAsSixDigitString(0);

    assertEquals("000000", actual);

    actual = StringUtil.displayAsSixDigitString(10);

    assertEquals("000010", actual);

    actual = StringUtil.displayAsSixDigitString(100);

    assertEquals("000100", actual);
  }
  /**
   * This method validates a range of numeric values.
   *
   * <p>Each side of the range includes a relational operator (>,>=,<,<=,=) and the value. The value
   * can include a decimal number with an optional space and then anything to the right of the space
   * is ignored (like units: mm).
   *
   * <p>Though vacuous, if neither side is specified, the range is considered to be valid. Not
   * counting when nothing is specified, the left "side" is required, and the right side is
   * optional. When specifying a side, both the operator and value must be specified.
   *
   * <p>When the range is ok, null is returned. Otherwise a cryptic string is returned.... this
   * string is a key to a resource bundle or something? no clue.
   *
   * <p>(Apparently) bad things about this method that am afraid to change:
   *
   * <ul>
   *   <li>This is a ** validation ** method whose main point is to return a _message_ if something
   *       is wrong, but if the value isn't a decimal number it throws an exception instead of
   *       returning a message.
   *   <li>The left value being 0 is considered an error. What about negative numbers!!!!?!?!!?!??!?
   *   <li>when an operator is = and there is both a left and right side this method considers that
   *       valid....
   * </ul>
   */
  public static String validateRange(
      String opLeft, String valLeft, String opRight, String valRight) {

    logger.debug(
        "validation for range: " + opLeft + "|" + valLeft + "|" + opRight + "|" + valRight);

    if (allRangeComponentsAreEmpty(opLeft, valLeft, opRight, valRight)) {
      return null;
    }

    // Make sure that the left operator and value are filled out
    if (leftSideHasEmptyComponent(opLeft, valLeft)) {
      return ResourceBundleUtil.getString("searchNoRange");
    }

    // Makes sure both right values are either selected or deselected
    if (rightSideHasOnlyOneComponent(opRight, valRight)) {
      return ResourceBundleUtil.getString("searchInvalidOperator");
    }

    // Look for a space in the value (e.g. '0 mm') and ignore anything after the space
    valLeft = stripUnits(valLeft);

    double leftVal = Double.parseDouble(valLeft);

    // Makes sure no values are selected on the right if you are using less
    // than
    if (opLeft.equals(RangeData.LESS_THAN) || opLeft.equals(RangeData.LESS_THAN_EQUAL)) {
      if (!StringUtil.isEmpty(opRight)) {
        return ResourceBundleUtil.getString("searchInvalidRightOperator");
      }

      if (leftVal == 0) {
        return ResourceBundleUtil.getString("searchValueZero");
      }
    } else {
      if (!StringUtil.isEmpty(valRight)) {
        valRight = stripUnits(valRight);

        double rightVal = Double.parseDouble(valRight);

        // Makes sure that the values of the range selected are valid
        if (rightVal <= leftVal) {
          return ResourceBundleUtil.getString("searchInvalidRange");
        }
      }
    }

    // Return null if no errors found
    return null;
  }
  public void testEncodeListEntriesWithSingleQuotes() {
    List<String> emptyList = new ArrayList<String>();
    String result = StringUtil.encodeListEntriesWithSingleQuotes(emptyList);
    assertEquals(result, "");

    List<String> singletonList = java.util.Collections.singletonList("foo");
    result = StringUtil.encodeListEntriesWithSingleQuotes(singletonList);
    assertEquals(result, "'foo'");

    List<String> twoEntryList = new ArrayList<String>();
    twoEntryList.add("foo1");
    twoEntryList.add("foo2");
    result = StringUtil.encodeListEntriesWithSingleQuotes(twoEntryList);
    assertEquals(result, "'foo1','foo2'");
  }
 public void testToHexString() {
   byte[] bytes = new byte[5];
   bytes[0] = 0x01;
   bytes[1] = 0x0a;
   bytes[2] = 0x16;
   bytes[3] = 0x60;
   bytes[4] = 0x04;
   String output = StringUtil.toHexString(bytes);
   assertEquals(output, "01 0a 16 60 04 ");
 }
  /**
   * This range is "empty" if:
   *
   * <ul>
   *   <li>neither operator is specified (regardless of value??)
   *   <li>assuming both operators are not blank, then if either side has an operator it must have a
   *       value (or be considered empty).
   * </ul>
   *
   * If there's no from:/left side specified, but there's a to:/right side specified, it's NOT empty
   * which doesn't really jive with {@link #validateRange}
   */
  public boolean isEmpty() {
    boolean fromOpBlank = StringUtil.isEmpty(fromOperator);
    boolean toOpBlank = StringUtil.isEmpty(toOperator);
    boolean fromValueBlank = (fromValue == null);
    boolean toValueBlank = (toValue == null);

    if (fromOpBlank && toOpBlank) {
      return true;
    }

    if (!fromOpBlank && fromValueBlank) {
      return true;
    }

    if (!toOpBlank && toValueBlank) {
      return true;
    }

    return false;
  }
 public void testRemoveHTML() {
   String s = "<p>This is a paragraph of text.</p><b>A bold text</b> and another text.";
   String output = StringUtil.removeHTML(s);
   System.out.println("output: " + output);
   assertEquals(output, "This is a paragraph of text.A bold text and another text.");
 }
 private static boolean rightSideHasOnlyOneComponent(String opRight, String valRight) {
   return StringUtil.isEmpty(opRight) ^ StringUtil.isEmpty(valRight);
 }
 private static boolean leftSideHasEmptyComponent(String opLeft, String valLeft) {
   return StringUtil.isEmpty(opLeft) || StringUtil.isEmpty(valLeft);
 }