예제 #1
0
 /** Test the Date the feature was designed for (http://en.wikipedia.org/wiki/Year_2038_problem) */
 @Test
 public void testParseCaLatestValidDateTime() {
   LOG.trace(">testParseCaLatestValidDateTime");
   final String bug2038Hex = "80000000";
   LOG.info("bug2038Hex: " + bug2038Hex);
   final String bug2038Iso =
       FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ssZZ", TimeZone.getTimeZone("UTC"))
           .format(Long.parseLong("80000000", 16) * 1000);
   LOG.info("bug2038Iso: " + bug2038Iso);
   final Date bug2038HexDate = ValidityDate.parseCaLatestValidDateTime(bug2038Hex);
   LOG.info("bug2038HexDate: " + bug2038HexDate);
   final Date bug2038IsoDate = ValidityDate.parseCaLatestValidDateTime(bug2038Iso);
   LOG.info("bug2038IsoDate: " + bug2038IsoDate);
   Assert.assertEquals(
       "The two date formats should yield the same Date!", bug2038HexDate, bug2038IsoDate);
   // Test now also
   final Date now = new Date();
   LOG.info("now:        " + now);
   final String nowIso =
       FastDateFormat.getInstance(ValidityDate.ISO8601_DATE_FORMAT, TimeZone.getTimeZone("UTC"))
           .format(now);
   LOG.info("nowIso:     " + nowIso);
   final Date nowIsoDate = ValidityDate.parseCaLatestValidDateTime(nowIso);
   LOG.info("nowIsoDate: " + nowIsoDate);
   // Compare as strings since we will loose milliseconds in the conversion to ISO8601 format
   Assert.assertEquals(
       "Unable to parse current time correctly!", now.toString(), nowIsoDate.toString());
   // Test unhappy path (return of default value)
   final Date defaultIsoDate = ValidityDate.parseCaLatestValidDateTime("COFFEE");
   Assert.assertEquals(
       "Default value not returned when invalid date-time specified!",
       new Date(Long.MAX_VALUE).toString(),
       defaultIsoDate.toString());
   LOG.trace("<testParseCaLatestValidDateTime");
 }
예제 #2
0
 private void getDateInternal(
     final long subjectLEncoded, final Date subjectFromDate, final Date result) {
   Assert.assertEquals(
       "Failed to fetch date for " + subjectLEncoded + " and " + subjectFromDate,
       result,
       ValidityDate.getDate(subjectLEncoded, subjectFromDate));
 }
예제 #3
0
 /** 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");
 }
예제 #4
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);
  }
예제 #5
0
 @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");
 }
예제 #6
0
 @Test
 public void testGetEncode() {
   LOG.trace(">testGetEncode");
   // Test relative times (<Integer.MAX_VALUE)
   Assert.assertEquals("", 0, ValidityDate.encode(ValidityDate.getString(0)));
   Assert.assertEquals("", 1, ValidityDate.encode(ValidityDate.getString(1)));
   // Test absolute times (>Integer.MAX_VALUE)
   final long nowWithOutSeconds = (new Date().getTime() / 60000) * 60000;
   Assert.assertEquals(
       "", nowWithOutSeconds, ValidityDate.encode(ValidityDate.getString(nowWithOutSeconds)));
   LOG.trace("<testGetEncode");
 }
예제 #7
0
 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)));
 }
예제 #8
0
 private void getStringInternalRel(final long subject, final String result) throws ParseException {
   Assert.assertEquals(
       "Failed to fetch relative time for " + subject, result, ValidityDate.getString(subject));
 }
예제 #9
0
 private void encodeInternal(final String type, final String subject, final long result) {
   Assert.assertEquals(
       "Test of " + type + " date " + subject + " failed.", result, ValidityDate.encode(subject));
 }