Ejemplo n.º 1
0
 @Test
 public void testFormatDurationISO() {
   assertEquals("P0Y0M0DT0H0M0.000S", DurationFormatUtils.formatDurationISO(0L));
   assertEquals("P0Y0M0DT0H0M0.001S", DurationFormatUtils.formatDurationISO(1L));
   assertEquals("P0Y0M0DT0H0M0.010S", DurationFormatUtils.formatDurationISO(10L));
   assertEquals("P0Y0M0DT0H0M0.100S", DurationFormatUtils.formatDurationISO(100L));
   assertEquals("P0Y0M0DT0H1M15.321S", DurationFormatUtils.formatDurationISO(75321L));
 }
Ejemplo n.º 2
0
  @Test
  public void testFormatDurationHMS() {
    long time = 0;
    assertEquals("0:00:00.000", DurationFormatUtils.formatDurationHMS(time));

    time = 1;
    assertEquals("0:00:00.001", DurationFormatUtils.formatDurationHMS(time));

    time = 15;
    assertEquals("0:00:00.015", DurationFormatUtils.formatDurationHMS(time));

    time = 165;
    assertEquals("0:00:00.165", DurationFormatUtils.formatDurationHMS(time));

    time = 1675;
    assertEquals("0:00:01.675", DurationFormatUtils.formatDurationHMS(time));

    time = 13465;
    assertEquals("0:00:13.465", DurationFormatUtils.formatDurationHMS(time));

    time = 72789;
    assertEquals("0:01:12.789", DurationFormatUtils.formatDurationHMS(time));

    time = 12789 + 32 * 60000;
    assertEquals("0:32:12.789", DurationFormatUtils.formatDurationHMS(time));

    time = 12789 + 62 * 60000;
    assertEquals("1:02:12.789", DurationFormatUtils.formatDurationHMS(time));
  }
Ejemplo n.º 3
0
 @Test
 public void testLANG984() { // Long durations
   assertEquals("0", DurationFormatUtils.formatDuration(0, "S"));
   assertEquals(
       Integer.toString(Integer.MAX_VALUE),
       DurationFormatUtils.formatDuration(Integer.MAX_VALUE, "S"));
   long maxIntPlus = Integer.MAX_VALUE;
   maxIntPlus++;
   assertEquals(Long.toString(maxIntPlus), DurationFormatUtils.formatDuration(maxIntPlus, "S"));
   assertEquals(
       Long.toString(Long.MAX_VALUE), DurationFormatUtils.formatDuration(Long.MAX_VALUE, "S"));
 }
Ejemplo n.º 4
0
  public Result test(@Param("query") String query) throws SQLException {
    long _start = System.currentTimeMillis();

    HikariDataSource hds = null;
    if (ds instanceof HikariDataSource) {
      hds = (HikariDataSource) ds;
    } else {
      hds = ((P6DataSource) ds).unwrap(HikariDataSource.class);
    }

    if (query != null) {
      //            try(Connection con = DriverManager.getConnection(hds.getJdbcUrl()); Statement
      // stmt = con.createStatement() ){
      try (Connection con = ds.getConnection();
          Statement stmt = con.createStatement()) {
        stmt.execute(query);
      }
    }

    String duration = DurationFormatUtils.formatDurationHMS(System.currentTimeMillis() - _start);

    LOG.info(duration);

    return Results.ok().text().render(duration);
  }
Ejemplo n.º 5
0
  @Override
  public void start(Stage primaryStage) {
    this.primaryStage = primaryStage;
    ApplicationContextHolder.getContext();
    startLogAppender();
    Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionLogger());
    primaryStage.setScene(initScene());
    primaryStage
        .getIcons()
        .addAll(ApplicationContextHolder.getContext().getBeansOfType(Image.class).values());
    primaryStage.setTitle(ApplicationContextHolder.getContext().getBean(Pdfsam.class).name());
    primaryStage.setOnCloseRequest(e -> Platform.exit());
    initWindowsStatusController(primaryStage);
    initOverwriteDialogController(primaryStage);
    initActiveModule();
    primaryStage.show();

    requestCheckForUpdateIfNecessary();
    requestLatestNewsPanelDisplay();
    eventStudio().addAnnotatedListeners(this);
    closeSplash();
    STOPWATCH.stop();
    LOG.info(
        DefaultI18nContext.getInstance()
            .i18n(
                "Started in {0}",
                DurationFormatUtils.formatDurationWords(STOPWATCH.getTime(), true, true)));
  }
Ejemplo n.º 6
0
 @Test
 public void testLANG982() { // More than 3 millisecond digits following a second
   assertEquals("61.999", DurationFormatUtils.formatDuration(61999, "s.S"));
   assertEquals("1 1999", DurationFormatUtils.formatDuration(61999, "m S"));
   assertEquals("61.999", DurationFormatUtils.formatDuration(61999, "s.SSS"));
   assertEquals("1 1999", DurationFormatUtils.formatDuration(61999, "m SSS"));
   assertEquals("61.0999", DurationFormatUtils.formatDuration(61999, "s.SSSS"));
   assertEquals("1 1999", DurationFormatUtils.formatDuration(61999, "m SSSS"));
   assertEquals("61.00999", DurationFormatUtils.formatDuration(61999, "s.SSSSS"));
   assertEquals("1 01999", DurationFormatUtils.formatDuration(61999, "m SSSSS"));
 }
Ejemplo n.º 7
0
  @Test
  public void testLANG815() {
    final Calendar calendar = Calendar.getInstance();
    calendar.set(2012, 6, 30, 0, 0, 0);
    final long startMillis = calendar.getTimeInMillis();

    calendar.set(2012, 8, 8);
    final long endMillis = calendar.getTimeInMillis();

    assertEquals("1 9", DurationFormatUtils.formatPeriod(startMillis, endMillis, "M d"));
  }
Ejemplo n.º 8
0
  @Test
  public void testFormatPeriodISO() {
    final TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
    final Calendar base = Calendar.getInstance(timeZone);
    base.set(1970, 0, 1, 0, 0, 0);
    base.set(Calendar.MILLISECOND, 0);

    final Calendar cal = Calendar.getInstance(timeZone);
    cal.set(2002, 1, 23, 9, 11, 12);
    cal.set(Calendar.MILLISECOND, 1);
    String text;
    // repeat a test from testDateTimeISO to compare extended and not extended.
    text = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(cal);
    assertEquals("2002-02-23T09:11:12-03:00", text);
    // test fixture is the same as above, but now with extended format.
    text =
        DurationFormatUtils.formatPeriod(
            base.getTime().getTime(),
            cal.getTime().getTime(),
            DurationFormatUtils.ISO_EXTENDED_FORMAT_PATTERN,
            false,
            timeZone);
    assertEquals("P32Y1M22DT9H11M12.001S", text);
    // test fixture from example in http://www.w3.org/TR/xmlschema-2/#duration
    cal.set(1971, 1, 3, 10, 30, 0);
    cal.set(Calendar.MILLISECOND, 0);
    text =
        DurationFormatUtils.formatPeriod(
            base.getTime().getTime(),
            cal.getTime().getTime(),
            DurationFormatUtils.ISO_EXTENDED_FORMAT_PATTERN,
            false,
            timeZone);
    assertEquals("P1Y1M2DT10H30M0.000S", text);
    // want a way to say 'don't print the seconds in format()' or other fields for that matter:
    // assertEquals("P1Y2M3DT10H30M", text);
  }
Ejemplo n.º 9
0
  public Map<String, String> getProperties() {
    Map<String, String> map = new TreeMap<String, String>();
    RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();

    List<String> aList = runtimeMXBean.getInputArguments();
    String parameters = "";
    for (int i = 0; i < aList.size(); i++) {
      parameters = parameters + " " + aList.get(i);
    }
    map.put("params", parameters);

    map.put("name", runtimeMXBean.getVmName());
    map.put("vendor", runtimeMXBean.getVmVendor());
    map.put("version", runtimeMXBean.getVmVersion());
    map.put("specification", runtimeMXBean.getSpecVersion());
    map.put("uptime", DurationFormatUtils.formatDurationHMS(runtimeMXBean.getUptime()));

    Date date = new Date(runtimeMXBean.getStartTime());
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    map.put("start time", sdf.format(date));

    MemoryMXBean memory = ManagementFactory.getMemoryMXBean();

    MemoryUsage heap = memory.getHeapMemoryUsage();
    map.put(
        "memory. (heap)",
        readableFileSize(heap.getUsed())
            + "/"
            + readableFileSize(heap.getCommitted())
            + " min:"
            + readableFileSize(heap.getInit())
            + " max:"
            + readableFileSize(heap.getMax()));
    MemoryUsage nonheap = memory.getNonHeapMemoryUsage();
    map.put(
        "memory (non heap)",
        readableFileSize(nonheap.getUsed())
            + "/"
            + readableFileSize(nonheap.getCommitted())
            + " min:"
            + readableFileSize(nonheap.getInit())
            + " max:"
            + readableFileSize(nonheap.getMax()));

    return map;
  }
Ejemplo n.º 10
0
  public static void main(String[] args) throws Exception {
    IndexArgs indexArgs = new IndexArgs();

    CmdLineParser parser =
        new CmdLineParser(indexArgs, ParserProperties.defaults().withUsageWidth(90));

    try {
      parser.parseArgument(args);
    } catch (CmdLineException e) {
      System.err.println(e.getMessage());
      parser.printUsage(System.err);
      System.err.println(
          "Example: "
              + IndexCollection.class.getSimpleName()
              + parser.printExample(OptionHandlerFilter.REQUIRED));
      return;
    }

    if (indexArgs.docvectors && !indexArgs.positions) {
      LOG.warn(
          "to store docVectors you must store positions too. With this configuration, both positions and docVectors will not be stored!");
    }

    final long start = System.nanoTime();
    MultithreadedIndexer indexer = new MultithreadedIndexer(indexArgs);

    LOG.info("Index path: " + indexArgs.index);
    LOG.info("Threads: " + indexArgs.threads);
    LOG.info("Keep Stopwords: " + indexArgs.keepstop);
    LOG.info("Positions: " + indexArgs.positions);
    LOG.info("Store docVectors: " + indexArgs.docvectors);
    LOG.info("Optimize (merge segments): " + indexArgs.optimize);

    LOG.info("Starting indexer...");

    int numIndexed = indexer.run();
    final long durationMillis =
        TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS);
    LOG.info(
        "Total "
            + numIndexed
            + " documents indexed in "
            + DurationFormatUtils.formatDuration(durationMillis, "HH:mm:ss"));
  }
Ejemplo n.º 11
0
 private void assertEqualDuration(
     final String message,
     final String expected,
     final int[] start,
     final int[] end,
     final String format) {
   final Calendar cal1 = Calendar.getInstance();
   cal1.set(start[0], start[1], start[2], start[3], start[4], start[5]);
   cal1.set(Calendar.MILLISECOND, 0);
   final Calendar cal2 = Calendar.getInstance();
   cal2.set(end[0], end[1], end[2], end[3], end[4], end[5]);
   cal2.set(Calendar.MILLISECOND, 0);
   final long milli1 = cal1.getTime().getTime();
   final long milli2 = cal2.getTime().getTime();
   final String result = DurationFormatUtils.formatPeriod(milli1, milli2, format);
   if (message == null) {
     assertEquals(expected, result);
   } else {
     assertEquals(message, expected, result);
   }
 }
Ejemplo n.º 12
0
  @Test
  public void testFormatPeriod() {
    final Calendar cal1970 = Calendar.getInstance();
    cal1970.set(1970, 0, 1, 0, 0, 0);
    cal1970.set(Calendar.MILLISECOND, 0);
    final long time1970 = cal1970.getTime().getTime();

    assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time1970, "y"));
    assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time1970, "M"));
    assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time1970, "d"));
    assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time1970, "H"));
    assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time1970, "m"));
    assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time1970, "s"));
    assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time1970, "S"));
    assertEquals("0000", DurationFormatUtils.formatPeriod(time1970, time1970, "SSSS"));
    assertEquals("0000", DurationFormatUtils.formatPeriod(time1970, time1970, "yyyy"));
    assertEquals("0000", DurationFormatUtils.formatPeriod(time1970, time1970, "yyMM"));

    long time = time1970 + 60 * 1000;
    assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time, "y"));
    assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time, "M"));
    assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time, "d"));
    assertEquals("0", DurationFormatUtils.formatPeriod(time1970, time, "H"));
    assertEquals("1", DurationFormatUtils.formatPeriod(time1970, time, "m"));
    assertEquals("60", DurationFormatUtils.formatPeriod(time1970, time, "s"));
    assertEquals("60000", DurationFormatUtils.formatPeriod(time1970, time, "S"));
    assertEquals("01:00", DurationFormatUtils.formatPeriod(time1970, time, "mm:ss"));

    final Calendar cal = Calendar.getInstance();
    cal.set(1973, 6, 1, 0, 0, 0);
    cal.set(Calendar.MILLISECOND, 0);
    time = cal.getTime().getTime();
    assertEquals("36", DurationFormatUtils.formatPeriod(time1970, time, "yM"));
    assertEquals(
        "3 years 6 months",
        DurationFormatUtils.formatPeriod(time1970, time, "y' years 'M' months'"));
    assertEquals("03/06", DurationFormatUtils.formatPeriod(time1970, time, "yy/MM"));

    cal.set(1973, 10, 1, 0, 0, 0);
    cal.set(Calendar.MILLISECOND, 0);
    time = cal.getTime().getTime();
    assertEquals("310", DurationFormatUtils.formatPeriod(time1970, time, "yM"));
    assertEquals(
        "3 years 10 months",
        DurationFormatUtils.formatPeriod(time1970, time, "y' years 'M' months'"));
    assertEquals("03/10", DurationFormatUtils.formatPeriod(time1970, time, "yy/MM"));

    cal.set(1974, 0, 1, 0, 0, 0);
    cal.set(Calendar.MILLISECOND, 0);
    time = cal.getTime().getTime();
    assertEquals("40", DurationFormatUtils.formatPeriod(time1970, time, "yM"));
    assertEquals(
        "4 years 0 months",
        DurationFormatUtils.formatPeriod(time1970, time, "y' years 'M' months'"));
    assertEquals("04/00", DurationFormatUtils.formatPeriod(time1970, time, "yy/MM"));
    assertEquals("48", DurationFormatUtils.formatPeriod(time1970, time, "M"));
    assertEquals("48", DurationFormatUtils.formatPeriod(time1970, time, "MM"));
    assertEquals("048", DurationFormatUtils.formatPeriod(time1970, time, "MMM"));
  }
Ejemplo n.º 13
0
 public String toString() {
   return DurationFormatUtils.formatDuration(duration.toMillis(), "HH:mm:ss");
 }
Ejemplo n.º 14
0
 @Test(expected = IllegalArgumentException.class)
 public void testLANG981() { // unmatched quote char in lexx
   DurationFormatUtils.lexx("'yMdHms''S");
 }
Ejemplo n.º 15
0
  /** Tests that "1 <unit>s" gets converted to "1 <unit>" but that "11 <unit>s" is left alone. */
  @Test
  public void testFormatDurationPluralWords() {
    final long oneSecond = 1000;
    final long oneMinute = oneSecond * 60;
    final long oneHour = oneMinute * 60;
    final long oneDay = oneHour * 24;
    String text = null;

    text = DurationFormatUtils.formatDurationWords(oneSecond, false, false);
    assertEquals("0 days 0 hours 0 minutes 1 second", text);
    text = DurationFormatUtils.formatDurationWords(oneSecond * 2, false, false);
    assertEquals("0 days 0 hours 0 minutes 2 seconds", text);
    text = DurationFormatUtils.formatDurationWords(oneSecond * 11, false, false);
    assertEquals("0 days 0 hours 0 minutes 11 seconds", text);

    text = DurationFormatUtils.formatDurationWords(oneMinute, false, false);
    assertEquals("0 days 0 hours 1 minute 0 seconds", text);
    text = DurationFormatUtils.formatDurationWords(oneMinute * 2, false, false);
    assertEquals("0 days 0 hours 2 minutes 0 seconds", text);
    text = DurationFormatUtils.formatDurationWords(oneMinute * 11, false, false);
    assertEquals("0 days 0 hours 11 minutes 0 seconds", text);
    text = DurationFormatUtils.formatDurationWords(oneMinute + oneSecond, false, false);
    assertEquals("0 days 0 hours 1 minute 1 second", text);

    text = DurationFormatUtils.formatDurationWords(oneHour, false, false);
    assertEquals("0 days 1 hour 0 minutes 0 seconds", text);
    text = DurationFormatUtils.formatDurationWords(oneHour * 2, false, false);
    assertEquals("0 days 2 hours 0 minutes 0 seconds", text);
    text = DurationFormatUtils.formatDurationWords(oneHour * 11, false, false);
    assertEquals("0 days 11 hours 0 minutes 0 seconds", text);
    text = DurationFormatUtils.formatDurationWords(oneHour + oneMinute + oneSecond, false, false);
    assertEquals("0 days 1 hour 1 minute 1 second", text);

    text = DurationFormatUtils.formatDurationWords(oneDay, false, false);
    assertEquals("1 day 0 hours 0 minutes 0 seconds", text);
    text = DurationFormatUtils.formatDurationWords(oneDay * 2, false, false);
    assertEquals("2 days 0 hours 0 minutes 0 seconds", text);
    text = DurationFormatUtils.formatDurationWords(oneDay * 11, false, false);
    assertEquals("11 days 0 hours 0 minutes 0 seconds", text);
    text =
        DurationFormatUtils.formatDurationWords(
            oneDay + oneHour + oneMinute + oneSecond, false, false);
    assertEquals("1 day 1 hour 1 minute 1 second", text);
  }
Ejemplo n.º 16
0
  @Test
  public void testFormatDuration() {
    long duration = 0;
    assertEquals("0", DurationFormatUtils.formatDuration(duration, "y"));
    assertEquals("0", DurationFormatUtils.formatDuration(duration, "M"));
    assertEquals("0", DurationFormatUtils.formatDuration(duration, "d"));
    assertEquals("0", DurationFormatUtils.formatDuration(duration, "H"));
    assertEquals("0", DurationFormatUtils.formatDuration(duration, "m"));
    assertEquals("0", DurationFormatUtils.formatDuration(duration, "s"));
    assertEquals("0", DurationFormatUtils.formatDuration(duration, "S"));
    assertEquals("0000", DurationFormatUtils.formatDuration(duration, "SSSS"));
    assertEquals("0000", DurationFormatUtils.formatDuration(duration, "yyyy"));
    assertEquals("0000", DurationFormatUtils.formatDuration(duration, "yyMM"));

    duration = 60 * 1000;
    assertEquals("0", DurationFormatUtils.formatDuration(duration, "y"));
    assertEquals("0", DurationFormatUtils.formatDuration(duration, "M"));
    assertEquals("0", DurationFormatUtils.formatDuration(duration, "d"));
    assertEquals("0", DurationFormatUtils.formatDuration(duration, "H"));
    assertEquals("1", DurationFormatUtils.formatDuration(duration, "m"));
    assertEquals("60", DurationFormatUtils.formatDuration(duration, "s"));
    assertEquals("60000", DurationFormatUtils.formatDuration(duration, "S"));
    assertEquals("01:00", DurationFormatUtils.formatDuration(duration, "mm:ss"));

    final Calendar base = Calendar.getInstance();
    base.set(2000, 0, 1, 0, 0, 0);
    base.set(Calendar.MILLISECOND, 0);

    final Calendar cal = Calendar.getInstance();
    cal.set(2003, 1, 1, 0, 0, 0);
    cal.set(Calendar.MILLISECOND, 0);
    duration =
        cal.getTime().getTime() - base.getTime().getTime(); // duration from 2000-01-01 to cal
    // don't use 1970 in test as time zones were less reliable in 1970 than now
    // remember that duration formatting ignores time zones, working on strict hour lengths
    final int days = 366 + 365 + 365 + 31;
    assertEquals("0 0 " + days, DurationFormatUtils.formatDuration(duration, "y M d"));
  }
Ejemplo n.º 17
0
  @FilterWith(XSRFFilter.class)
  public Result dryRun(
      Context context, @Param("startDate") String start, @Param("endDate") String end) {
    long _start = System.currentTimeMillis();
    FlashScope flash = context.getFlashScope();

    LocalDate startDate = null;
    LocalDate endDate = null;

    try {
      startDate = LocalDate.parse(start);
      endDate = LocalDate.parse(end);
    } catch (Exception ex) {
    }

    if (startDate == null || endDate == null || startDate.isAfter(endDate)) {
      flash.error("error.invalidDate");
      return Results.redirect(router.getReverseRoute(DebugController.class, "debug"));
    }

    Run lastRun = baseDB.run.findLast(Module.GOOGLE, null, null);
    if (lastRun != null && lastRun.getDay().isAfter(startDate)) {
      flash.error("error.invalidDate");
      return Results.redirect(router.getReverseRoute(DebugController.class, "debug"));
    }

    LocalDate date = LocalDate.from(startDate);

    GoogleSettings ggOptions = googleDB.options.get();

    int minPauseBetweenPageSec = ggOptions.getMinPauseBetweenPageSec();
    int maxPauseBetweenPageSec = ggOptions.getMaxPauseBetweenPageSec();
    ggOptions.setMinPauseBetweenPageSec(0);
    ggOptions.setMaxPauseBetweenPageSec(0);
    googleDB.options.update(ggOptions);

    try {
      while (date.isBefore(endDate)) {
        LOG.debug("dry run {}", date);
        if (!taskManager.startGoogleTask(
            new Run(Run.Mode.MANUAL, Module.GOOGLE, date.atTime(13, 37, 00)))) {
          LOG.error("can't startGoogleTask");
          flash.error("can't startGoogleTask");
          return Results.redirect(router.getReverseRoute(DebugController.class, "debug"));
        }
        taskManager.joinGoogleTask();
        date = date.plusDays(1);
      }
    } catch (Exception ex) {
      LOG.error("an error occured", ex);
      flash.error("an error occured");
      return Results.redirect(router.getReverseRoute(DebugController.class, "debug"));
    } finally {
      ggOptions.setMinPauseBetweenPageSec(minPauseBetweenPageSec);
      ggOptions.setMaxPauseBetweenPageSec(maxPauseBetweenPageSec);
      googleDB.options.update(ggOptions);
    }

    LOG.debug(
        "dry run timing : {}",
        DurationFormatUtils.formatDurationHMS(System.currentTimeMillis() - _start));
    flash.success("ok");
    return Results.redirect(router.getReverseRoute(DebugController.class, "debug"));
  }
Ejemplo n.º 18
0
  // -----------------------------------------------------------------------
  @Test
  public void testFormatDurationWords() {
    String text = null;

    text = DurationFormatUtils.formatDurationWords(50 * 1000, true, false);
    assertEquals("50 seconds", text);
    text = DurationFormatUtils.formatDurationWords(65 * 1000, true, false);
    assertEquals("1 minute 5 seconds", text);
    text = DurationFormatUtils.formatDurationWords(120 * 1000, true, false);
    assertEquals("2 minutes 0 seconds", text);
    text = DurationFormatUtils.formatDurationWords(121 * 1000, true, false);
    assertEquals("2 minutes 1 second", text);
    text = DurationFormatUtils.formatDurationWords(72 * 60 * 1000, true, false);
    assertEquals("1 hour 12 minutes 0 seconds", text);
    text = DurationFormatUtils.formatDurationWords(24 * 60 * 60 * 1000, true, false);
    assertEquals("1 day 0 hours 0 minutes 0 seconds", text);

    text = DurationFormatUtils.formatDurationWords(50 * 1000, true, true);
    assertEquals("50 seconds", text);
    text = DurationFormatUtils.formatDurationWords(65 * 1000, true, true);
    assertEquals("1 minute 5 seconds", text);
    text = DurationFormatUtils.formatDurationWords(120 * 1000, true, true);
    assertEquals("2 minutes", text);
    text = DurationFormatUtils.formatDurationWords(121 * 1000, true, true);
    assertEquals("2 minutes 1 second", text);
    text = DurationFormatUtils.formatDurationWords(72 * 60 * 1000, true, true);
    assertEquals("1 hour 12 minutes", text);
    text = DurationFormatUtils.formatDurationWords(24 * 60 * 60 * 1000, true, true);
    assertEquals("1 day", text);

    text = DurationFormatUtils.formatDurationWords(50 * 1000, false, true);
    assertEquals("0 days 0 hours 0 minutes 50 seconds", text);
    text = DurationFormatUtils.formatDurationWords(65 * 1000, false, true);
    assertEquals("0 days 0 hours 1 minute 5 seconds", text);
    text = DurationFormatUtils.formatDurationWords(120 * 1000, false, true);
    assertEquals("0 days 0 hours 2 minutes", text);
    text = DurationFormatUtils.formatDurationWords(121 * 1000, false, true);
    assertEquals("0 days 0 hours 2 minutes 1 second", text);
    text = DurationFormatUtils.formatDurationWords(72 * 60 * 1000, false, true);
    assertEquals("0 days 1 hour 12 minutes", text);
    text = DurationFormatUtils.formatDurationWords(24 * 60 * 60 * 1000, false, true);
    assertEquals("1 day", text);

    text = DurationFormatUtils.formatDurationWords(50 * 1000, false, false);
    assertEquals("0 days 0 hours 0 minutes 50 seconds", text);
    text = DurationFormatUtils.formatDurationWords(65 * 1000, false, false);
    assertEquals("0 days 0 hours 1 minute 5 seconds", text);
    text = DurationFormatUtils.formatDurationWords(120 * 1000, false, false);
    assertEquals("0 days 0 hours 2 minutes 0 seconds", text);
    text = DurationFormatUtils.formatDurationWords(121 * 1000, false, false);
    assertEquals("0 days 0 hours 2 minutes 1 second", text);
    text = DurationFormatUtils.formatDurationWords(72 * 60 * 1000, false, false);
    assertEquals("0 days 1 hour 12 minutes 0 seconds", text);
    text =
        DurationFormatUtils.formatDurationWords(24 * 60 * 60 * 1000 + 72 * 60 * 1000, false, false);
    assertEquals("1 day 1 hour 12 minutes 0 seconds", text);
    text =
        DurationFormatUtils.formatDurationWords(
            2 * 24 * 60 * 60 * 1000 + 72 * 60 * 1000, false, false);
    assertEquals("2 days 1 hour 12 minutes 0 seconds", text);
    for (int i = 2; i < 31; i++) {
      text = DurationFormatUtils.formatDurationWords(i * 24 * 60 * 60 * 1000L, false, false);
      // assertEquals(i + " days 0 hours 0 minutes 0 seconds", text);
      //
      // junit.framework.ComparisonFailure: expected:<25 days 0 hours 0 minutes 0...> but was:<-24
      // days -17 hours
      // -2 minutes -47...>
      // at junit.framework.Assert.assertEquals(Assert.java:81)
      // at junit.framework.Assert.assertEquals(Assert.java:87)
      // at
      // org.apache.commons.lang.time.DurationFormatUtilsTest.testFormatDurationWords(DurationFormatUtilsTest.java:124)
      // at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      // at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      // at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      // at java.lang.reflect.Method.invoke(Method.java:324)
      // at junit.framework.TestCase.runTest(TestCase.java:154)
      // at junit.framework.TestCase.runBare(TestCase.java:127)
      // at junit.framework.TestResult$1.protect(TestResult.java:106)
      // at junit.framework.TestResult.runProtected(TestResult.java:124)
      // at junit.framework.TestResult.run(TestResult.java:109)
      // at junit.framework.TestCase.run(TestCase.java:118)
      // at junit.framework.TestSuite.runTest(TestSuite.java:208)
      // at junit.framework.TestSuite.run(TestSuite.java:203)
      // at
      // org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
      // at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
      // at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
    }
  }
Ejemplo n.º 19
0
  @Test
  public void testLexx() {
    // tests each constant
    assertArrayEquals(
        new DurationFormatUtils.Token[] {
          new DurationFormatUtils.Token(DurationFormatUtils.y, 1),
          new DurationFormatUtils.Token(DurationFormatUtils.M, 1),
          new DurationFormatUtils.Token(DurationFormatUtils.d, 1),
          new DurationFormatUtils.Token(DurationFormatUtils.H, 1),
          new DurationFormatUtils.Token(DurationFormatUtils.m, 1),
          new DurationFormatUtils.Token(DurationFormatUtils.s, 1),
          new DurationFormatUtils.Token(DurationFormatUtils.S, 1)
        },
        DurationFormatUtils.lexx("yMdHmsS"));

    // tests the ISO8601-like
    assertArrayEquals(
        new DurationFormatUtils.Token[] {
          new DurationFormatUtils.Token(DurationFormatUtils.H, 1),
          new DurationFormatUtils.Token(new StringBuilder(":"), 1),
          new DurationFormatUtils.Token(DurationFormatUtils.m, 2),
          new DurationFormatUtils.Token(new StringBuilder(":"), 1),
          new DurationFormatUtils.Token(DurationFormatUtils.s, 2),
          new DurationFormatUtils.Token(new StringBuilder("."), 1),
          new DurationFormatUtils.Token(DurationFormatUtils.S, 3)
        },
        DurationFormatUtils.lexx("H:mm:ss.SSS"));

    // test the iso extended format
    assertArrayEquals(
        new DurationFormatUtils.Token[] {
          new DurationFormatUtils.Token(new StringBuilder("P"), 1),
          new DurationFormatUtils.Token(DurationFormatUtils.y, 4),
          new DurationFormatUtils.Token(new StringBuilder("Y"), 1),
          new DurationFormatUtils.Token(DurationFormatUtils.M, 1),
          new DurationFormatUtils.Token(new StringBuilder("M"), 1),
          new DurationFormatUtils.Token(DurationFormatUtils.d, 1),
          new DurationFormatUtils.Token(new StringBuilder("DT"), 1),
          new DurationFormatUtils.Token(DurationFormatUtils.H, 1),
          new DurationFormatUtils.Token(new StringBuilder("H"), 1),
          new DurationFormatUtils.Token(DurationFormatUtils.m, 1),
          new DurationFormatUtils.Token(new StringBuilder("M"), 1),
          new DurationFormatUtils.Token(DurationFormatUtils.s, 1),
          new DurationFormatUtils.Token(new StringBuilder("."), 1),
          new DurationFormatUtils.Token(DurationFormatUtils.S, 1),
          new DurationFormatUtils.Token(new StringBuilder("S"), 1)
        },
        DurationFormatUtils.lexx(DurationFormatUtils.ISO_EXTENDED_FORMAT_PATTERN));

    // test failures in equals
    final DurationFormatUtils.Token token = new DurationFormatUtils.Token(DurationFormatUtils.y, 4);
    assertFalse("Token equal to non-Token class. ", token.equals(new Object()));
    assertFalse(
        "Token equal to Token with wrong value class. ",
        token.equals(new DurationFormatUtils.Token(new Object())));
    assertFalse(
        "Token equal to Token with different count. ",
        token.equals(new DurationFormatUtils.Token(DurationFormatUtils.y, 1)));
    final DurationFormatUtils.Token numToken = new DurationFormatUtils.Token(Integer.valueOf(1), 4);
    assertTrue("Token with Number value not equal to itself. ", numToken.equals(numToken));
  }