}
@VisibleForTesting
- int countIssuesAfterDate(Collection<Issue> issues, @Nullable Date targetDate) {
+ int countIssuesAfterDate(Collection<Issue> issues, @Nullable Date date) {
if (issues == null) {
return 0;
}
int count = 0;
for (Issue issue : issues) {
- if (isAfter(issue, targetDate)) {
+ if (isAfter(issue, date)) {
count++;
}
}
import org.sonar.api.rules.RuleQuery;
import org.sonar.api.technicaldebt.batch.internal.DefaultCharacteristic;
import org.sonar.api.technicaldebt.batch.internal.DefaultRequirement;
+import org.sonar.api.utils.WorkDuration;
import org.sonar.api.utils.WorkUnit;
import org.sonar.core.technicaldebt.DefaultTechnicalDebtModel;
import org.sonar.core.technicaldebt.db.CharacteristicDao;
DefaultRequirement requirement = result.requirementsByRule(ruleKey);
assertThat(requirement.ruleKey()).isEqualTo(ruleKey);
assertThat(requirement.function()).isEqualTo("linear");
- assertThat(requirement.factor()).isEqualTo(WorkUnit.create(2d, WorkUnit.DAYS));
- assertThat(requirement.offset()).isEqualTo(WorkUnit.create(0d, WorkUnit.DAYS));
+ assertThat(requirement.factorValue()).isEqualTo(2);
+ assertThat(requirement.factorUnit()).isEqualTo(WorkDuration.UNIT.DAYS);
+ assertThat(requirement.offsetValue()).isEqualTo(0);
+ assertThat(requirement.offsetUnit()).isEqualTo(WorkDuration.UNIT.DAYS);
}
}
import org.sonar.api.technicaldebt.server.Characteristic;
import org.sonar.api.technicaldebt.server.TechnicalDebtManager;
import org.sonar.api.technicaldebt.server.internal.DefaultCharacteristic;
-import org.sonar.api.utils.WorkUnit;
import org.sonar.core.technicaldebt.db.CharacteristicDao;
import org.sonar.core.technicaldebt.db.CharacteristicDto;
}
private static Characteristic toCharacteristic(CharacteristicDto dto, @Nullable RuleKey ruleKey) {
+ Double factorValue = dto.getFactorValue();
+ Double offsetValue = dto.getOffsetValue();
return new DefaultCharacteristic()
.setId(dto.getId())
.setKey(dto.getKey())
.setRootId(dto.getRootId())
.setRuleKey(ruleKey)
.setFunction(dto.getFunction())
- .setFactor(WorkUnit.create(dto.getFactorValue(), dto.getFactorUnit()))
- .setOffset(WorkUnit.create(dto.getOffsetValue(), dto.getOffsetUnit()));
+ .setFactorValue(factorValue != null ? factorValue.intValue() : null)
+ .setFactorUnit(DefaultCharacteristic.toUnit(dto.getFactorUnit()))
+ .setOffsetValue(offsetValue != null ? offsetValue.intValue() : null)
+ .setOffsetUnit(DefaultCharacteristic.toUnit(dto.getOffsetUnit()));
}
}
import org.sonar.api.technicaldebt.batch.internal.DefaultCharacteristic;
import org.sonar.api.technicaldebt.batch.internal.DefaultRequirement;
import org.sonar.api.utils.ValidationMessages;
-import org.sonar.api.utils.WorkUnit;
+import org.sonar.api.utils.WorkDuration;
+import org.sonar.core.technicaldebt.db.CharacteristicDto;
+import javax.annotation.CheckForNull;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
private Property processProperty(SMInputCursor cursor, ValidationMessages messages) throws XMLStreamException {
SMInputCursor c = cursor.childElementCursor();
String key = null;
- Double value = null;
+ int value = 0;
String textValue = null;
while (c.getNext() != null) {
String node = c.getLocalName();
} else if (StringUtils.equals(node, PROPERTY_VALUE)) {
String s = c.collectDescendantText().trim();
try {
- value = NumberUtils.createDouble(s);
+ // The value is still a double for the moment
+ Double valueDouble = NumberUtils.createDouble(s);
+ value = valueDouble.intValue();
} catch (NumberFormatException ex) {
messages.addErrorText(String.format("Cannot import value '%s' for field %s - Expected a numeric value instead", s, key));
}
return new Property(key, value, textValue);
}
+ @CheckForNull
private DefaultRequirement processFunctionsOnRequirement(DefaultRequirement requirement, Properties properties, ValidationMessages messages) {
Property function = properties.function();
Property factor = properties.factor();
Property offset = properties.offset();
if (function != null) {
+ // Requirements should always have values, so we init it with default values
+ requirement.setFactorValue(0);
+ requirement.setFactorUnit(WorkDuration.UNIT.DAYS);
+ requirement.setOffsetValue(0);
+ requirement.setOffsetUnit(WorkDuration.UNIT.DAYS);
+
String functionKey = function.getTextValue();
if ("linear_threshold".equals(functionKey)) {
function.setTextValue(DefaultRequirement.FUNCTION_LINEAR);
- offset.setValue(0d);
+ offset.setValue(0);
+ offset.setTextValue(CharacteristicDto.DAYS);
messages.addWarningText(String.format("Linear with threshold function is no more used, function of the requirement '%s' is replaced by linear.", requirement.ruleKey()));
} else if ("constant_resource".equals(functionKey)) {
messages.addWarningText(String.format("Constant/file function is no more used, requirements '%s' are ignored.", requirement.ruleKey()));
requirement.setFunction(function.getTextValue());
if (factor != null) {
- requirement.setFactor(WorkUnit.create(factor.getValue(), factor.getTextValue()));
+ requirement.setFactorValue(factor.getValue());
+ requirement.setFactorUnit(DefaultRequirement.toUnit(factor.getTextValue()));
}
if (offset != null) {
- requirement.setOffset(WorkUnit.create(offset.getValue(), offset.getTextValue()));
+ requirement.setOffsetValue(offset.getValue());
+ requirement.setOffsetUnit(DefaultRequirement.toUnit(offset.getTextValue()));
}
return requirement;
}
private static class Property {
String key;
- Double value;
+ int value;
String textValue;
- private Property(String key, Double value, String textValue) {
+ private Property(String key, int value, String textValue) {
this.key = key;
this.value = value;
this.textValue = textValue;
}
- private Property setValue(Double value) {
+ private Property setValue(int value) {
this.value = value;
return this;
}
return key;
}
- private Double getValue() {
+ private int getValue() {
return value;
}
import org.sonar.api.rule.RuleKey;
import org.sonar.api.technicaldebt.batch.internal.DefaultCharacteristic;
import org.sonar.api.technicaldebt.batch.internal.DefaultRequirement;
-import org.sonar.api.utils.WorkUnit;
+import org.sonar.api.utils.WorkDuration;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
public class CharacteristicDto implements Serializable {
+ public static final String DAYS = "d";
+ public static final String MINUTES = "mn";
+ public static final String HOURS = "h";
+
private Integer id;
private String kee;
private String name;
.setCharacteristic(characteristic)
.setRootCharacteristic(rootCharacteristic)
.setFunction(functionKey)
- .setFactor(WorkUnit.create(factorValue, factorUnit))
- .setOffset(WorkUnit.create(offsetValue, offsetUnit))
+ .setFactorValue(factorValue.intValue())
+ .setFactorUnit(toUnit(factorUnit))
+ .setOffsetValue(offsetValue.intValue())
+ .setOffsetUnit(toUnit(offsetUnit))
.setCreatedAt(createdAt)
.setUpdatedAt(updatedAt);
}
.setParentId(characteristicId)
.setRootId(rootCharacteristicId)
.setFunction(requirement.function())
- .setFactorValue(requirement.factor().getValue())
- .setFactorUnit(requirement.factor().getUnit())
- .setOffsetValue(requirement.offset().getValue())
- .setOffsetUnit(requirement.offset().getUnit())
+ .setFactorValue((double) requirement.factorValue())
+ .setFactorUnit(fromUnit(requirement.factorUnit()))
+ .setOffsetValue((double) requirement.offsetValue())
+ .setOffsetUnit(fromUnit(requirement.offsetUnit()))
.setEnabled(true)
.setCreatedAt(requirement.createdAt())
.setUpdatedAt(requirement.updatedAt());
}
+ public static WorkDuration.UNIT toUnit(@Nullable String requirementUnit) {
+ if (requirementUnit != null) {
+ if (requirementUnit.equals(DAYS)) {
+ return WorkDuration.UNIT.DAYS;
+ } else if (requirementUnit.equals(HOURS)) {
+ return WorkDuration.UNIT.HOURS;
+ } else if (requirementUnit.equals(MINUTES)) {
+ return WorkDuration.UNIT.MINUTES;
+ }
+ throw new IllegalStateException("Invalid unit : " + requirementUnit);
+ }
+ return null;
+ }
+
+ public static String fromUnit(@Nullable WorkDuration.UNIT unit) {
+ if (unit != null) {
+ if (unit.equals(WorkDuration.UNIT.DAYS)) {
+ return DAYS;
+ } else if (unit.equals(WorkDuration.UNIT.HOURS)) {
+ return HOURS;
+ } else if (unit.equals(WorkDuration.UNIT.MINUTES)) {
+ return MINUTES;
+ }
+ throw new IllegalStateException("Invalid unit : " + unit);
+ }
+ return null;
+ }
+
}
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleFinder;
import org.sonar.api.technicaldebt.server.Characteristic;
-import org.sonar.api.utils.WorkUnit;
+import org.sonar.api.utils.WorkDuration;
import org.sonar.core.technicaldebt.db.CharacteristicDao;
import org.sonar.core.technicaldebt.db.CharacteristicDto;
rule.setId(1);
when(dao.selectByRuleId(rule.getId())).thenReturn(
- new CharacteristicDto().setId(3).setRuleId(10).setParentId(2).setRootId(1).setFunction("linear").setFactorValue(30.0).setFactorUnit("mn"));
+ new CharacteristicDto().setId(3).setRuleId(10).setParentId(2).setRootId(1).setFunction("linear")
+ .setFactorValue(30.0).setFactorUnit("mn")
+ .setOffsetValue(0.0).setOffsetUnit("d")
+ );
Characteristic result = finder.findRequirementByRule(rule);
assertThat(result.rootId()).isEqualTo(1);
assertThat(result.ruleKey()).isEqualTo(RuleKey.of("repo", "key"));
assertThat(result.function()).isEqualTo("linear");
- assertThat(result.factor()).isEqualTo(WorkUnit.create(30d, WorkUnit.MINUTES));
- assertThat(result.offset()).isEqualTo(WorkUnit.create(0d, WorkUnit.DAYS));
+ assertThat(result.factorValue()).isEqualTo(30);
+ assertThat(result.factorUnit()).isEqualTo(WorkDuration.UNIT.MINUTES);
+ assertThat(result.offsetValue()).isEqualTo(0);
+ assertThat(result.offsetUnit()).isEqualTo(WorkDuration.UNIT.DAYS);
}
@Test
when(ruleFinder.findById(1)).thenReturn(rule);
when(dao.selectByRuleId(rule.getId())).thenReturn(
- new CharacteristicDto().setId(3).setRuleId(10).setParentId(2).setRootId(1).setFunction("linear").setFactorValue(30.0).setFactorUnit("mn"));
+ new CharacteristicDto().setId(3).setRuleId(10).setParentId(2).setRootId(1).setFunction("linear")
+ .setFactorValue(30.0).setFactorUnit("mn")
+ .setOffsetValue(0.0).setOffsetUnit("d")
+ );
Characteristic result = finder.findRequirementByRuleId(1);
assertThat(result.rootId()).isEqualTo(1);
assertThat(result.ruleKey()).isEqualTo(RuleKey.of("repo", "key"));
assertThat(result.function()).isEqualTo("linear");
- assertThat(result.factor()).isEqualTo(WorkUnit.create(30d, WorkUnit.MINUTES));
- assertThat(result.offset()).isEqualTo(WorkUnit.create(0d, WorkUnit.DAYS));
+ assertThat(result.factorValue()).isEqualTo(30);
+ assertThat(result.factorUnit()).isEqualTo(WorkDuration.UNIT.MINUTES);
+ assertThat(result.offsetValue()).isEqualTo(0);
+ assertThat(result.offsetUnit()).isEqualTo(WorkDuration.UNIT.DAYS);
+
}
@Test
import org.sonar.api.rule.RuleKey;
import org.sonar.api.technicaldebt.batch.internal.DefaultCharacteristic;
import org.sonar.api.technicaldebt.batch.internal.DefaultRequirement;
-import org.sonar.api.utils.WorkUnit;
+import org.sonar.api.utils.WorkDuration;
import static org.fest.assertions.Assertions.assertThat;
.setCharacteristic(characteristic)
.setRuleKey(ruleKey)
.setFunction("linear")
- .setFactor(WorkUnit.create(2d, WorkUnit.HOURS))
- .setOffset(WorkUnit.create(0d, WorkUnit.HOURS));
+ .setFactorValue(2)
+ .setFactorUnit(WorkDuration.UNIT.HOURS)
+ .setOffsetValue(0)
+ .setOffsetUnit(WorkDuration.UNIT.HOURS);
sqaleModel.addRootCharacteristic(rootCharacteristic);
import org.sonar.api.technicaldebt.batch.internal.DefaultCharacteristic;
import org.sonar.api.technicaldebt.batch.internal.DefaultRequirement;
import org.sonar.api.utils.ValidationMessages;
-import org.sonar.api.utils.WorkUnit;
+import org.sonar.api.utils.WorkDuration;
import org.sonar.core.persistence.MyBatis;
import org.sonar.core.technicaldebt.db.CharacteristicDao;
import org.sonar.core.technicaldebt.db.CharacteristicDto;
RuleKey ruleKey = RuleKey.of("checkstyle", "import");
when(ruleCache.getByRuleKey(ruleKey)).thenReturn(rule);
new DefaultRequirement().setRuleKey(ruleKey)
- .setFunction("linear").setFactor(WorkUnit.create(30d, WorkUnit.MINUTES)).setCharacteristic(javaCharacteristic).setRootCharacteristic(javaRootCharacteristic);
+ .setFunction("linear")
+ .setFactorValue(30)
+ .setFactorUnit(WorkDuration.UNIT.MINUTES)
+ .setCharacteristic(javaCharacteristic)
+ .setRootCharacteristic(javaRootCharacteristic);
Reader javaModelReader = mock(Reader.class);
when(xmlImporter.importXML(eq(javaModelReader), any(ValidationMessages.class), eq(ruleCache))).thenReturn(javaModel);
// New requirement
new DefaultRequirement().setRuleKey(ruleKey2)
- .setFunction("linear").setFactor(WorkUnit.create(1d, WorkUnit.HOURS)).setCharacteristic(javaCharacteristic).setRootCharacteristic(javaRootCharacteristic);
+ .setFunction("linear")
+ .setFactorValue(1)
+ .setFactorUnit(WorkDuration.UNIT.HOURS)
+ .setCharacteristic(javaCharacteristic)
+ .setRootCharacteristic(javaRootCharacteristic);
Reader javaModelReader = mock(Reader.class);
when(technicalDebtModelRepository.createReaderForXMLFile("java")).thenReturn(javaModelReader);
import org.sonar.api.technicaldebt.batch.internal.DefaultCharacteristic;
import org.sonar.api.technicaldebt.batch.internal.DefaultRequirement;
import org.sonar.api.utils.ValidationMessages;
-import org.sonar.api.utils.WorkUnit;
+import org.sonar.api.utils.WorkDuration;
import java.io.IOException;
ValidationMessages messages = ValidationMessages.create();
DefaultTechnicalDebtModel sqale = new TechnicalDebtXMLImporter().importXML(xml, messages, technicalDebtRuleCache);
- checkXmlCorrectlyImported(sqale, WorkUnit.create(1.0, "h"), messages);
+ checkXmlCorrectlyImported(sqale, 1, WorkDuration.UNIT.HOURS, messages);
}
@Test
ValidationMessages messages = ValidationMessages.create();
DefaultTechnicalDebtModel sqale = new TechnicalDebtXMLImporter().importXML(xml, messages, technicalDebtRuleCache);
- checkXmlCorrectlyImported(sqale, WorkUnit.create(0.0, "h"), messages);
+ checkXmlCorrectlyImported(sqale, 0, WorkDuration.UNIT.DAYS, messages);
assertThat(messages.getWarnings()).hasSize(1);
}
}
private void checkXmlCorrectlyImported(DefaultTechnicalDebtModel sqale, ValidationMessages messages) {
- checkXmlCorrectlyImported(sqale, WorkUnit.create(0d, WorkUnit.DAYS), messages);
+ checkXmlCorrectlyImported(sqale, 0, WorkDuration.UNIT.DAYS, messages);
}
- private void checkXmlCorrectlyImported(DefaultTechnicalDebtModel sqale, WorkUnit offset, ValidationMessages messages) {
+ private void checkXmlCorrectlyImported(DefaultTechnicalDebtModel sqale, Integer offsetValue, WorkDuration.UNIT offsetUnit, ValidationMessages messages) {
assertThat(messages.getErrors()).isEmpty();
// characteristics
assertThat(requirement.ruleKey().repository()).isEqualTo("checkstyle");
assertThat(requirement.ruleKey().rule()).isEqualTo("Regexp");
assertThat(requirement.function()).isEqualTo("linear");
- assertThat(requirement.factor()).isEqualTo(WorkUnit.create(3.2, "h"));
- assertThat(requirement.offset()).isEqualTo(offset);
+ assertThat(requirement.factorValue()).isEqualTo(3);
+ assertThat(requirement.factorUnit()).isEqualTo(WorkDuration.UNIT.HOURS);
+ assertThat(requirement.offsetValue()).isEqualTo(offsetValue);
+ assertThat(requirement.offsetUnit()).isEqualTo(offsetUnit);
assertThat(requirement.characteristic().key()).isEqualTo("MEMORY_EFFICIENCY");
assertThat(requirement.rootCharacteristic().key()).isEqualTo("EFFICIENCY");
}
<rule-key>Regexp</rule-key>
<prop>
<key>remediationFactor</key>
- <val>3.2</val>
+ <val>3.0</val>
<txt>h</txt>
</prop>
<prop>
<prop>
<key>remediationFactor
</key>
- <val>3.2
+ <val>3.0
</val>
<txt>h
</txt>
<rule-key>Regexp</rule-key>
<prop>
<key>remediationFactor</key>
- <val>3.2</val>
+ <val>3.0</val>
<txt>h</txt>
</prop>
<prop>
</prop>
<prop>
<key>remediationFactor</key>
- <val>3.2</val>
+ <val>3.0</val>
<txt>h</txt>
</prop>
<!-- Should be ignored -->
<rule-key>Regexp</rule-key>
<prop>
<key>remediationFactor</key>
- <val>3.2</val>
+ <val>3.0</val>
<txt>h</txt>
</prop>
<prop>
<rule-key>Regexp</rule-key>
<prop>
<key>remediationFactor</key>
- <val>3.2</val>
+ <val>3.0</val>
<txt>h</txt>
</prop>
<prop>
<rule-key>Foo</rule-key>
<prop>
<key>remediationFactor</key>
- <val>3.2</val>
+ <val>3.0</val>
<txt>h</txt>
</prop>
<prop>
package org.sonar.api.technicaldebt.batch;
import org.sonar.api.rule.RuleKey;
+import org.sonar.api.utils.WorkDuration;
import org.sonar.api.utils.WorkUnit;
import java.util.Date;
String function();
+ /**
+ * @deprecated since 4.2
+ */
+ @Deprecated
WorkUnit factor();
+ /**
+ * @since 4.2
+ */
+ int factorValue();
+
+ /**
+ * @since 4.2
+ */
+ WorkDuration.UNIT factorUnit();
+
+ /**
+ * @deprecated since 4.2
+ */
+ @Deprecated
WorkUnit offset();
+ /**
+ * @since 4.2
+ */
+ int offsetValue();
+
+ /**
+ * @since 4.2
+ */
+ WorkDuration.UNIT offsetUnit();
+
Date createdAt();
Date updatedAt();
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
+import org.picocontainer.annotations.Nullable;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.technicaldebt.batch.Requirement;
import org.sonar.api.utils.WorkDuration;
import org.sonar.api.utils.WorkUnit;
+import javax.annotation.CheckForNull;
+
import java.util.Date;
public class DefaultRequirement implements Requirement {
private WorkDuration.UNIT factorUnit;
private int offsetValue;
private WorkDuration.UNIT offsetUnit;
- private WorkUnit factor;
- private WorkUnit offset;
private Date createdAt;
private Date updatedAt;
- public DefaultRequirement() {
- this.factor = WorkUnit.create(0d, WorkUnit.DAYS);
- this.offset = WorkUnit.create(0d, WorkUnit.DAYS);
- }
-
public Integer id() {
return id;
}
*/
@Deprecated
public WorkUnit factor() {
- return factor;
-// return WorkUnit.create((double) factorValue, fromUnit(factorUnit));
+ return WorkUnit.create((double) factorValue, fromUnit(factorUnit));
}
/**
*/
@Deprecated
public DefaultRequirement setFactor(WorkUnit factor) {
- this.factor = factor;
- return this;
- }
-
- /**
- * @deprecated since 4.2
- */
- @Deprecated
- public WorkUnit offset() {
- return offset;
- }
-
- /**
- * @deprecated since 4.2
- */
- @Deprecated
- public DefaultRequirement setOffset(WorkUnit offset) {
- this.offset = offset;
+ this.factorValue = (int) factor.getValue();
+ this.factorUnit = toUnit(factor.getUnit());
return this;
}
return this;
}
+ @CheckForNull
public WorkDuration.UNIT factorUnit() {
return factorUnit;
}
- public DefaultRequirement setFactorUnit(WorkDuration.UNIT factorUnit) {
+ public DefaultRequirement setFactorUnit(@Nullable WorkDuration.UNIT factorUnit) {
this.factorUnit = factorUnit;
return this;
}
+ /**
+ * @deprecated since 4.2
+ */
+ @Deprecated
+ public WorkUnit offset() {
+ return WorkUnit.create((double) offsetValue, fromUnit(offsetUnit));
+ }
+
+ /**
+ * @deprecated since 4.2
+ */
+ @Deprecated
+ public DefaultRequirement setOffset(WorkUnit offset) {
+ this.offsetValue = (int) offset.getValue();
+ this.offsetUnit = toUnit(offset.getUnit());
+ return this;
+ }
+
public int offsetValue() {
return offsetValue;
}
return this;
}
+ @CheckForNull
public WorkDuration.UNIT offsetUnit() {
return offsetUnit;
}
- public DefaultRequirement setOffsetUnit(WorkDuration.UNIT offsetUnit) {
+ public DefaultRequirement setOffsetUnit(@Nullable WorkDuration.UNIT offsetUnit) {
this.offsetUnit = offsetUnit;
return this;
}
return this;
}
- private static WorkDuration.UNIT toUnit(String requirementUnit){
+ public static WorkDuration.UNIT toUnit(String requirementUnit){
if (requirementUnit.equals(WorkUnit.DAYS)) {
return WorkDuration.UNIT.DAYS;
} else if (requirementUnit.equals(WorkUnit.HOURS)) {
package org.sonar.api.technicaldebt.server;
import org.sonar.api.rule.RuleKey;
+import org.sonar.api.utils.WorkDuration;
import org.sonar.api.utils.WorkUnit;
import javax.annotation.CheckForNull;
String function();
+ /**
+ * @deprecated since 4.2
+ */
+ @Deprecated
+ @CheckForNull
WorkUnit factor();
+ /**
+ * @since 4.2
+ */
+ @CheckForNull
+ Integer factorValue();
+
+ /**
+ * @since 4.2
+ */
+ @CheckForNull
+ WorkDuration.UNIT factorUnit();
+
+ /**
+ * @deprecated since 4.2
+ */
+ @Deprecated
+ @CheckForNull
WorkUnit offset();
+ /**
+ * @since 4.2
+ */
+ @CheckForNull
+ Integer offsetValue();
+
+ /**
+ * @since 4.2
+ */
+ @CheckForNull
+ WorkDuration.UNIT offsetUnit();
+
boolean isRoot();
boolean isRequirement();
import org.apache.commons.lang.builder.ToStringStyle;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.technicaldebt.server.Characteristic;
+import org.sonar.api.utils.WorkDuration;
import org.sonar.api.utils.WorkUnit;
import javax.annotation.CheckForNull;
private Integer rootId;
private RuleKey ruleKey;
private String function;
- private WorkUnit factor;
- private WorkUnit offset;
+ private Integer factorValue;
+ private WorkDuration.UNIT factorUnit;
+ private Integer offsetValue;
+ private WorkDuration.UNIT offsetUnit;
public Integer id() {
return id;
return this;
}
+ /**
+ * @deprecated since 4.2
+ */
+ @Deprecated
@CheckForNull
public WorkUnit factor() {
- return factor;
+ if (factorValue!= null && factorUnit!= null) {
+ return WorkUnit.create((double) factorValue, fromUnit(factorUnit));
+ }
+ return null;
}
+ /**
+ * @deprecated since 4.2
+ */
+ @Deprecated
public DefaultCharacteristic setFactor(@Nullable WorkUnit factor) {
- this.factor = factor;
+ if (factor != null) {
+ this.factorValue = (int) factor.getValue();
+ this.factorUnit = toUnit(factor.getUnit());
+ }
return this;
}
@CheckForNull
+ public Integer factorValue() {
+ return factorValue;
+ }
+
+ public DefaultCharacteristic setFactorValue(@Nullable Integer factorValue) {
+ this.factorValue = factorValue;
+ return this;
+ }
+
+ @CheckForNull
+ public WorkDuration.UNIT factorUnit() {
+ return factorUnit;
+ }
+
+ public DefaultCharacteristic setFactorUnit(@Nullable WorkDuration.UNIT factorUnit) {
+ this.factorUnit = factorUnit;
+ return this;
+ }
+
+ /**
+ * @deprecated since 4.2
+ */
+ @Deprecated
public WorkUnit offset() {
- return offset;
+ if (offsetValue!= null && offsetUnit!= null) {
+ return WorkUnit.create((double) offsetValue, fromUnit(offsetUnit));
+ }
+ return null;
}
+ /**
+ * @deprecated since 4.2
+ */
+ @Deprecated
public DefaultCharacteristic setOffset(@Nullable WorkUnit offset) {
- this.offset = offset;
+ if (offset != null) {
+ this.offsetValue = (int) offset.getValue();
+ this.offsetUnit = toUnit(offset.getUnit());
+ }
return this;
}
+ @CheckForNull
+ public Integer offsetValue() {
+ return offsetValue;
+ }
+
+ public DefaultCharacteristic setOffsetValue(@Nullable Integer offsetValue) {
+ this.offsetValue = offsetValue;
+ return this;
+ }
+
+ @CheckForNull
+ public WorkDuration.UNIT offsetUnit() {
+ return offsetUnit;
+ }
+
+ public DefaultCharacteristic setOffsetUnit(@Nullable WorkDuration.UNIT offsetUnit) {
+ this.offsetUnit = offsetUnit;
+ return this;
+ }
+
+ public static WorkDuration.UNIT toUnit(@Nullable String requirementUnit) {
+ if (requirementUnit != null) {
+ if (requirementUnit.equals(WorkUnit.DAYS)) {
+ return WorkDuration.UNIT.DAYS;
+ } else if (requirementUnit.equals(WorkUnit.HOURS)) {
+ return WorkDuration.UNIT.HOURS;
+ } else if (requirementUnit.equals(WorkUnit.MINUTES)) {
+ return WorkDuration.UNIT.MINUTES;
+ }
+ throw new IllegalStateException("Invalid unit : " + requirementUnit);
+ }
+ return null;
+ }
+
+ private static String fromUnit(WorkDuration.UNIT unit){
+ if (unit.equals(WorkDuration.UNIT.DAYS)) {
+ return WorkUnit.DAYS;
+ } else if (unit.equals(WorkDuration.UNIT.HOURS)) {
+ return WorkUnit.HOURS;
+ } else if (unit.equals(WorkDuration.UNIT.MINUTES)) {
+ return WorkUnit.MINUTES;
+ }
+ throw new IllegalStateException("Invalid unit : " + unit);
+ }
+
public boolean isRoot() {
return parentId == null;
}
import org.junit.Test;
import org.sonar.api.rule.RuleKey;
+import org.sonar.api.utils.WorkDuration;
import org.sonar.api.utils.WorkUnit;
import java.text.SimpleDateFormat;
.setCharacteristic(characteristic)
.setRootCharacteristic(root)
.setFunction("linear_offset")
- .setFactor(WorkUnit.create(2d, WorkUnit.MINUTES))
- .setOffset(WorkUnit.create(1d, WorkUnit.HOURS))
+ .setFactorValue(2)
+ .setFactorUnit(WorkDuration.UNIT.MINUTES)
+ .setOffsetValue(1)
+ .setOffsetUnit(WorkDuration.UNIT.HOURS)
.setCreatedAt(new SimpleDateFormat("yyyy-MM-dd").parse("2013-08-19"))
.setUpdatedAt(new SimpleDateFormat("yyyy-MM-dd").parse("2013-08-19"));
assertThat(requirement.characteristic()).isEqualTo(characteristic);
assertThat(requirement.rootCharacteristic()).isEqualTo(root);
assertThat(requirement.function()).isEqualTo("linear_offset");
+ assertThat(requirement.factorValue()).isEqualTo(2);
+ assertThat(requirement.factorUnit()).isEqualTo(WorkDuration.UNIT.MINUTES);
assertThat(requirement.factor()).isEqualTo(WorkUnit.create(2d, WorkUnit.MINUTES));
+ assertThat(requirement.offsetValue()).isEqualTo(1);
+ assertThat(requirement.offsetUnit()).isEqualTo(WorkDuration.UNIT.HOURS);
assertThat(requirement.offset()).isEqualTo(WorkUnit.create(1d, WorkUnit.HOURS));
assertThat(requirement.createdAt()).isEqualTo(new SimpleDateFormat("yyyy-MM-dd").parse("2013-08-19"));
assertThat(requirement.updatedAt()).isEqualTo(new SimpleDateFormat("yyyy-MM-dd").parse("2013-08-19"));
import org.junit.Test;
import org.sonar.api.rule.RuleKey;
+import org.sonar.api.utils.WorkDuration;
import org.sonar.api.utils.WorkUnit;
import static org.fest.assertions.Assertions.assertThat;
assertThat(characteristic.order()).isEqualTo(5);
assertThat(characteristic.ruleKey()).isNull();
assertThat(characteristic.function()).isNull();
+ assertThat(characteristic.factorValue()).isNull();
+ assertThat(characteristic.factorUnit()).isNull();
assertThat(characteristic.factor()).isNull();
+ assertThat(characteristic.offsetValue()).isNull();
+ assertThat(characteristic.offsetUnit()).isNull();
assertThat(characteristic.offset()).isNull();
assertThat(characteristic.parentId()).isEqualTo(2);
assertThat(characteristic.rootId()).isEqualTo(2);
.setId(1)
.setRuleKey(RuleKey.of("repo", "rule"))
.setFunction("linear_offset")
- .setFactor(WorkUnit.create(2d, WorkUnit.MINUTES))
- .setOffset(WorkUnit.create(1d, WorkUnit.HOURS))
+ .setFactorValue(2)
+ .setFactorUnit(WorkDuration.UNIT.MINUTES)
+ .setOffsetValue(1)
+ .setOffsetUnit(WorkDuration.UNIT.HOURS)
.setRootId(3)
.setParentId(2);
assertThat(requirement.order()).isNull();
assertThat(requirement.ruleKey()).isEqualTo(RuleKey.of("repo", "rule"));
assertThat(requirement.function()).isEqualTo("linear_offset");
+ assertThat(requirement.factorValue()).isEqualTo(2);
+ assertThat(requirement.factorUnit()).isEqualTo(WorkDuration.UNIT.MINUTES);
assertThat(requirement.factor()).isEqualTo(WorkUnit.create(2d, WorkUnit.MINUTES));
+ assertThat(requirement.offsetValue()).isEqualTo(1);
+ assertThat(requirement.offsetUnit()).isEqualTo(WorkDuration.UNIT.HOURS);
assertThat(requirement.offset()).isEqualTo(WorkUnit.create(1d, WorkUnit.HOURS));
assertThat(requirement.parentId()).isEqualTo(2);
assertThat(requirement.rootId()).isEqualTo(3);
.setId(1)
.setRuleKey(RuleKey.of("repo", "rule"))
.setFunction("linear_offset")
- .setFactor(WorkUnit.create(2d, WorkUnit.MINUTES))
- .setOffset(WorkUnit.create(1d, WorkUnit.HOURS))
+ .setFactorValue(2)
+ .setFactorUnit(WorkDuration.UNIT.MINUTES)
+ .setOffsetValue(1)
+ .setOffsetUnit(WorkDuration.UNIT.HOURS)
.setRootId(3)
.setParentId(2);
assertThat(requirement.isRequirement()).isTrue();
}
-
@Test
public void test_equals() throws Exception {
assertThat(new DefaultCharacteristic().setKey("NETWORK_USE")).isEqualTo(new DefaultCharacteristic().setKey("NETWORK_USE"));