*/
package org.sonar.batch.bootstrap;
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.commons.lang.StringUtils;
+
import java.text.MessageFormat;
public class DurationLabel {
- private String prefixAgo = null;
private String suffixAgo = "ago";
private String seconds = "less than a minute";
private String minute = "about a minute";
private String years = "{0} years";
public String label(long durationInMillis) {
- double seconds = durationInMillis / 1000;
+ double seconds = durationInMillis / 1000.0;
double minutes = seconds / 60;
double hours = minutes / 60;
double days = hours / 24;
time = this.year;
}
- return join(prefixAgo, time, suffixAgo);
+ return join(time, suffixAgo);
}
- public String join(String prefix, String time, String suffix) {
+ @VisibleForTesting
+ String join(String time, String suffix) {
StringBuilder joined = new StringBuilder();
- if (prefix != null && prefix.length() > 0) {
- joined.append(prefix).append(' ');
- }
joined.append(time);
- if (suffix != null && suffix.length() > 0) {
+ if (StringUtils.isNotBlank(suffix)) {
joined.append(' ').append(suffix);
}
return joined.toString();
}
- public String getPrefixAgo() {
- return prefixAgo;
- }
-
public String getSuffixAgo() {
return suffixAgo;
}
public void testAgoSeconds() {
DurationLabel durationLabel = new DurationLabel();
String label = durationLabel.label(now() - System.currentTimeMillis());
- String expected = durationLabel.join(durationLabel.getPrefixAgo(), durationLabel.getSeconds(), durationLabel.getSuffixAgo());
+ String expected = durationLabel.join(durationLabel.getSeconds(), durationLabel.getSuffixAgo());
assertThat(label).isEqualTo(expected);
}
public void testAgoMinute() {
DurationLabel durationLabel = new DurationLabel();
String label = durationLabel.label(now() - ago(MINUTE));
- String expected = durationLabel.join(durationLabel.getPrefixAgo(), durationLabel.getMinute(), durationLabel.getSuffixAgo());
+ String expected = durationLabel.join(durationLabel.getMinute(), durationLabel.getSuffixAgo());
assertThat(label).isEqualTo(expected);
}
DurationLabel durationlabel = new DurationLabel();
int minutes = 2;
String label = durationlabel.label(now() - ago(minutes * MINUTE));
- String expected = durationlabel.join(durationlabel.getPrefixAgo(),
+ String expected = durationlabel.join(
MessageFormat.format(durationlabel.getMinutes(), minutes), durationlabel.getSuffixAgo());
assertThat(label).isEqualTo(expected);
}
public void testAgoHour() {
DurationLabel durationLabel = new DurationLabel();
String label = durationLabel.label(now() - ago(HOUR));
- String expected = durationLabel.join(durationLabel.getPrefixAgo(), durationLabel.getHour(), durationLabel.getSuffixAgo());
+ String expected = durationLabel.join(durationLabel.getHour(), durationLabel.getSuffixAgo());
assertThat(label).isEqualTo(expected);
}
DurationLabel durationLabel = new DurationLabel();
long hours = 3;
String label = durationLabel.label(now() - ago(hours * HOUR));
- String expected = durationLabel.join(durationLabel.getPrefixAgo(), MessageFormat.format(durationLabel.getHours(), hours), durationLabel.getSuffixAgo());
+ String expected = durationLabel.join(MessageFormat.format(durationLabel.getHours(), hours), durationLabel.getSuffixAgo());
assertThat(label).isEqualTo(expected);
}
public void testAgoDay() {
DurationLabel durationLabel = new DurationLabel();
String label = durationLabel.label(now() - ago(30 * HOUR));
- String expected = durationLabel.join(durationLabel.getPrefixAgo(), durationLabel.getDay(), durationLabel.getSuffixAgo());
+ String expected = durationLabel.join(durationLabel.getDay(), durationLabel.getSuffixAgo());
assertThat(label).isEqualTo(expected);
}
DurationLabel durationLabel = new DurationLabel();
long days = 4;
String label = durationLabel.label(now() - ago(days * DAY));
- String expected = durationLabel.join(durationLabel.getPrefixAgo(), MessageFormat.format(durationLabel.getDays(), days), durationLabel.getSuffixAgo());
+ String expected = durationLabel.join(MessageFormat.format(durationLabel.getDays(), days), durationLabel.getSuffixAgo());
assertThat(label).isEqualTo(expected);
}
public void testAgoMonth() {
DurationLabel durationLabel = new DurationLabel();
String label = durationLabel.label(now() - ago(35 * DAY));
- String expected = durationLabel.join(durationLabel.getPrefixAgo(), durationLabel.getMonth(), durationLabel.getSuffixAgo());
+ String expected = durationLabel.join(durationLabel.getMonth(), durationLabel.getSuffixAgo());
assertThat(label).isEqualTo(expected);
}
DurationLabel durationLabel = new DurationLabel();
long months = 2;
String label = durationLabel.label(now() - ago(months * MONTH));
- String expected = durationLabel.join(durationLabel.getPrefixAgo(), MessageFormat.format(durationLabel.getMonths(), months), durationLabel.getSuffixAgo());
+ String expected = durationLabel.join(MessageFormat.format(durationLabel.getMonths(), months), durationLabel.getSuffixAgo());
assertThat(label).isEqualTo(expected);
}
public void testYearAgo() {
DurationLabel durationLabel = new DurationLabel();
String label = durationLabel.label(now() - ago(14 * MONTH));
- String expected = durationLabel.join(durationLabel.getPrefixAgo(), durationLabel.getYear(), durationLabel.getSuffixAgo());
+ String expected = durationLabel.join(durationLabel.getYear(), durationLabel.getSuffixAgo());
assertThat(label).isEqualTo(expected);
}
DurationLabel durationLabel = new DurationLabel();
long years = 7;
String label = durationLabel.label(now() - ago(years * YEAR));
- String expected = durationLabel.join(durationLabel.getPrefixAgo(), MessageFormat.format(durationLabel.getYears(), years), durationLabel.getSuffixAgo());
+ String expected = durationLabel.join(MessageFormat.format(durationLabel.getYears(), years), durationLabel.getSuffixAgo());
assertThat(label).isEqualTo(expected);
}