diff --git a/invoice/src/main/java/com/ning/billing/invoice/tree/SubscriptionItemTree.java b/invoice/src/main/java/com/ning/billing/invoice/tree/SubscriptionItemTree.java
index 49eb390..0c4a250 100644
--- a/invoice/src/main/java/com/ning/billing/invoice/tree/SubscriptionItemTree.java
+++ b/invoice/src/main/java/com/ning/billing/invoice/tree/SubscriptionItemTree.java
@@ -58,7 +58,8 @@ public class SubscriptionItemTree {
if (startDateComp != 0) {
return startDateComp;
}
- int itemTypeComp = Integer.compare(o1.getInvoiceItemType().ordinal(), o2.getInvoiceItemType().ordinal());
+ int itemTypeComp = (o1.getInvoiceItemType().ordinal()<o2.getInvoiceItemType().ordinal() ? -1 :
+ (o1.getInvoiceItemType().ordinal()==o2.getInvoiceItemType().ordinal() ? 0 : 1));
if (itemTypeComp != 0) {
return itemTypeComp;
}
@@ -92,7 +93,7 @@ public class SubscriptionItemTree {
}
/**
- * Flattens the tree so its depth only has one levl below root -- becomes a list.
+ * Flattens the tree so its depth only has one level below root -- becomes a list.
* <p>
* If the tree was not built, it is first built. The list of items is cleared and the state is now reset to unbuilt.
*
diff --git a/invoice/src/test/java/com/ning/billing/invoice/tree/TestSubscriptionItemTree.java b/invoice/src/test/java/com/ning/billing/invoice/tree/TestSubscriptionItemTree.java
index f157cdf..9152117 100644
--- a/invoice/src/test/java/com/ning/billing/invoice/tree/TestSubscriptionItemTree.java
+++ b/invoice/src/test/java/com/ning/billing/invoice/tree/TestSubscriptionItemTree.java
@@ -524,6 +524,7 @@ public class TestSubscriptionItemTree /* extends InvoiceTestSuiteNoDB */ {
tree.mergeProposedItem(proposed1);
tree.mergeProposedItem(proposed2);
+ tree.mergeProposedItem(proposed1);
tree.mergeProposedItem(proposed3);
tree.buildForMerge();
@@ -533,6 +534,27 @@ public class TestSubscriptionItemTree /* extends InvoiceTestSuiteNoDB */ {
expectedResult.add(repair1);
expectedResult.add(repair2);
verifyResult(tree.getView(), expectedResult);
+
+
+ // Dot it again but with propsoed items out of order
+ final SubscriptionItemTree treeAgain = new SubscriptionItemTree(subscriptionId);
+ final InvoiceItem monthlyAgain = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, startDate, endDate, monthlyAmount, monthlyRate, currency);
+ treeAgain.addItem(monthlyAgain);
+ treeAgain.flatten(true);
+
+ final InvoiceItem proposed2Again = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, unblockDate1, blockDate2, monthlyAmount, monthlyRate, currency);
+ final InvoiceItem proposed1Again = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, startDate, blockDate1, monthlyAmount, monthlyRate, currency);
+ final InvoiceItem proposed3Again = new RecurringInvoiceItem(invoiceId, accountId, bundleId, subscriptionId, planName, phaseName, unblockDate2, endDate, monthlyAmount, monthlyRate, currency);
+
+ treeAgain.mergeProposedItem(proposed1Again);
+ treeAgain.mergeProposedItem(proposed2Again);
+ treeAgain.mergeProposedItem(proposed1Again);
+ treeAgain.mergeProposedItem(proposed3Again);
+ treeAgain.buildForMerge();
+
+ verifyResult(treeAgain.getView(), expectedResult);
+
+
}
@Test(groups = "fast")