@Test
 public void testEncodeGet() throws ParseException {
   LOG.trace(">testEncodeGet");
   Assert.assertEquals(
       "",
       ValidityDate.parseAsIso8601("2011-05-09 16:58:00+00:00"),
       ValidityDate.parseAsIso8601(
           ValidityDate.getString(ValidityDate.encode("2011-05-09 16:58:00+00:00"))));
   Assert.assertEquals(
       "",
       ValidityDate.parseAsIso8601("1970-01-25 20:32:00+00:00"),
       ValidityDate.parseAsIso8601(
           ValidityDate.getString(ValidityDate.encode("1970-01-25 20:32:00+00:00"))));
   LOG.trace("<testEncodeGet");
 }
 /** Since the test will run in different time zones we will test combined operations. */
 @Test
 public void testParseFormat() throws ParseException {
   LOG.trace(">testParseFormat");
   final Date nowWithOutMillis =
       new Date(
           (new Date().getTime() / 1000) * 1000); // We will loose the millis in the conversion
   Assert.assertEquals(
       nowWithOutMillis,
       ValidityDate.parseAsIso8601(
           ValidityDate.formatAsISO8601(nowWithOutMillis, ValidityDate.TIMEZONE_SERVER)));
   final Date zero = new Date(0);
   Assert.assertEquals(
       zero,
       ValidityDate.parseAsIso8601(
           ValidityDate.formatAsISO8601(zero, ValidityDate.TIMEZONE_SERVER)));
   LOG.trace("<testParseFormat");
 }
Exemple #3
0
  public static Term parseCriteria(
      final String criteria,
      final Set<String> allowedFields,
      final Set<RelationalOperator> noArgOps,
      final Set<String> intFields,
      final Set<String> longFields,
      final Set<String> dateFields)
      throws IllegalArgumentException, NumberFormatException, ParseException {
    // find an operator
    final String[] parts = criteria.split(" ", 3);

    final String field = parts[0];

    if (parts.length < 2) {
      throw new IllegalArgumentException("Missing operator");
    }

    final RelationalOperator op = RelationalOperator.valueOf(parts[1]);
    Object value = null;

    // we will not handle the BETWEEN operator
    // to avoid complicating the parser, the same
    // result can be achieved with two criterias
    if (op == RelationalOperator.BETWEEN) {
      throw new IllegalArgumentException("Operator BETWEEN is not supported");
    }

    if (!allowedFields.contains(field)) {
      throw new IllegalArgumentException("Unrecognized field: " + field);
    }

    if (!noArgOps.contains(op)) {
      if (parts.length < 3) {
        throw new IllegalArgumentException("Missing value");
      }

      if (intFields.contains(parts[0])) {
        value = Integer.parseInt(parts[2]);
      } else if (longFields.contains(parts[0])) {
        value = Long.parseLong(parts[2]);
      } else if (dateFields.contains(parts[0])) {
        try {
          value = Long.parseLong(parts[2]);
        } catch (NumberFormatException e) {
          value = ValidityDate.parseAsIso8601(parts[2]).getTime();
        }
      } else {
        value = parts[2];
      }
    }

    return new Term(op, field, value);
  }
 private void getStringInternalAbs(final long subject, final String result) throws ParseException {
   Assert.assertEquals(
       "Failed to fetch absolute time for " + subject,
       ValidityDate.parseAsIso8601(result),
       ValidityDate.parseAsIso8601(ValidityDate.getString(subject)));
 }