Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 8 commits
  • 25 files changed
  • 0 commit comments
  • 1 contributor
Showing with 459 additions and 241 deletions.
  1. +0 −3  scratchpad.txt
  2. +8 −37 src/com/jamesshore/finances/domain/Dollars.java
  3. +11 −2 src/com/jamesshore/finances/domain/InvalidDollars.java
  4. +1 −1  src/com/jamesshore/finances/domain/StockMarketYear.java
  5. +104 −0 src/com/jamesshore/finances/domain/UserEnteredDollars.java
  6. +29 −20 src/com/jamesshore/finances/domain/ValidDollars.java
  7. +1 −48 src/com/jamesshore/finances/domain/_DollarsTest.java
  8. +1 −1  src/com/jamesshore/finances/domain/_GrowthRateTest.java
  9. +12 −3 src/com/jamesshore/finances/domain/_InvalidDollarsTest.java
  10. +13 −13 src/com/jamesshore/finances/domain/_StockMarketProjectionTest.java
  11. +29 −29 src/com/jamesshore/finances/domain/_StockMarketYearTest.java
  12. +2 −2 src/com/jamesshore/finances/domain/_TaxRateTest.java
  13. +134 −0 src/com/jamesshore/finances/domain/_UserEnteredDollarsTest.java
  14. +69 −38 src/com/jamesshore/finances/domain/_ValidDollarsTest.java
  15. +1 −1  src/com/jamesshore/finances/persistence/SaveFile.java
  16. +8 −7 src/com/jamesshore/finances/persistence/_SaveFileTest.java
  17. +3 −3 src/com/jamesshore/finances/ui/ApplicationModel.java
  18. +1 −1  src/com/jamesshore/finances/ui/DollarsTextField.java
  19. +0 −1  src/com/jamesshore/finances/ui/_ApplicationFrameTest.java
  20. +6 −6 src/com/jamesshore/finances/ui/_ApplicationModelTest.java
  21. +3 −3 src/com/jamesshore/finances/ui/_ConfigurationPanelTest.java
  22. +3 −3 src/com/jamesshore/finances/ui/_DollarsTextFieldTest.java
  23. +10 −10 src/com/jamesshore/finances/ui/_StockMarketTableModelTest.java
  24. +0 −9 src/com/jamesshore/finances/util/Require.java
  25. +10 −0 src/com/jamesshore/finances/util/RequireException.java
View
3  scratchpad.txt
@@ -1,9 +1,6 @@
Need to decide what to do with original user-entered values.
- Reformat them when moving away from field? What about invalid values?
-
-Bogus assertion in ApplicationFrame.saveAsMenuItemShouldShowSaveAsDialog() -- ref Kim Gr�sman, ep 153 comments
-
Consider eliminating __ApplicationModelSpy entirely
Deal with opening same file twice (see episode 137 comments, Aleksi Niiranen)
View
45 src/com/jamesshore/finances/domain/Dollars.java
@@ -2,55 +2,26 @@
public abstract class Dollars implements SelfRenderable {
- public static Dollars parse(String text) {
- if (text.equals(")")) return new InvalidDollars();
- if (text.contains("a")) return new InvalidDollars();
- if (text.endsWith("d")) return new InvalidDollars();
- if (text.contains("e")) return new InvalidDollars();
- if (text.endsWith("f")) return new InvalidDollars();
-
- boolean parenthesis = false;
- if (text.startsWith("(")) {
- text = text.substring(1);
- parenthesis = true;
- }
- if (text.endsWith(")")) {
- text = text.substring(0, text.length() - 1);
- parenthesis = true;
- }
- if (parenthesis) text = "-" + text;
-
- if (text.startsWith("$")) text = text.substring(1);
- if (text.startsWith("-$")) text = "-" + text.substring(2);
- if (text.isEmpty()) return ValidDollars.create(0);
- if (text.equals("-")) return ValidDollars.create(0);
- text = text.replace(",", "");
-
- try {
- return ValidDollars.create(Double.parseDouble(text));
- }
- catch (NumberFormatException e) {
- return new InvalidDollars();
- }
- }
-
public static Dollars min(Dollars value1, Dollars value2) {
return value1.min(value2);
}
public Dollars flipSign() {
- return ValidDollars.create(0).minus(this);
+ return new ValidDollars(0).minus(this);
}
public abstract boolean isValid();
- public abstract Dollars plus(Dollars dollars);
+ protected abstract double toCoreDataType();
- public abstract Dollars minus(Dollars dollars);
+ public abstract Dollars plus(Dollars operand);
- public abstract Dollars subtractToZero(Dollars dollars);
+ public abstract Dollars minus(Dollars operand);
+
+ public abstract Dollars subtractToZero(Dollars operand);
public abstract Dollars percentage(double percent);
- public abstract Dollars min(Dollars value2);
+ public abstract Dollars min(Dollars operand);
+
}
View
13 src/com/jamesshore/finances/domain/InvalidDollars.java
@@ -1,6 +1,7 @@
package com.jamesshore.finances.domain;
import com.jamesshore.finances.ui.*;
+import com.jamesshore.finances.util.*;
public class InvalidDollars extends Dollars {
@@ -10,6 +11,11 @@ public boolean isValid() {
}
@Override
+ protected double toCoreDataType() {
+ throw new UnreachableCodeException();
+ }
+
+ @Override
public Dollars plus(Dollars dollars) {
return new InvalidDollars();
}
@@ -45,8 +51,11 @@ public String toString() {
}
@Override
- public boolean equals(Object o) {
- return (o instanceof InvalidDollars);
+ public boolean equals(Object obj) {
+ if (obj == null) return false;
+ Dollars that = (Dollars)obj;
+
+ return !that.isValid();
}
@Override
View
2  src/com/jamesshore/finances/domain/StockMarketYear.java
@@ -15,7 +15,7 @@ public StockMarketYear(Year year, Dollars startingBalance, Dollars costBasis, Gr
this.costBasis = costBasis;
this.growthRate = growthRate;
this.capitalGainsTaxRate = capitalGainsTaxRate;
- this.totalSellOrders = ValidDollars.create(0);
+ this.totalSellOrders = new ValidDollars(0);
}
public Year year() {
View
104 src/com/jamesshore/finances/domain/UserEnteredDollars.java
@@ -0,0 +1,104 @@
+package com.jamesshore.finances.domain;
+
+import com.jamesshore.finances.ui.*;
+
+public class UserEnteredDollars extends Dollars {
+
+ private String userText;
+ private Dollars backingDollars;
+
+ public UserEnteredDollars(String userText) {
+ this.userText = userText;
+ this.backingDollars = this.parse(userText);
+ }
+
+ private Dollars parse(String text) {
+ if (text.equals(")")) return new InvalidDollars();
+ if (text.contains("a")) return new InvalidDollars();
+ if (text.endsWith("d")) return new InvalidDollars();
+ if (text.contains("e")) return new InvalidDollars();
+ if (text.endsWith("f")) return new InvalidDollars();
+
+ boolean parenthesis = false;
+ if (text.startsWith("(")) {
+ text = text.substring(1);
+ parenthesis = true;
+ }
+ if (text.endsWith(")")) {
+ text = text.substring(0, text.length() - 1);
+ parenthesis = true;
+ }
+ if (parenthesis) text = "-" + text;
+
+ if (text.startsWith("$")) text = text.substring(1);
+ if (text.startsWith("-$")) text = "-" + text.substring(2);
+ if (text.isEmpty()) return new ValidDollars(0);
+ if (text.equals("-")) return new ValidDollars(0);
+ text = text.replace(",", "");
+
+ try {
+ return new ValidDollars(Double.parseDouble(text));
+ }
+ catch (NumberFormatException e) {
+ return new InvalidDollars();
+ }
+ }
+
+ @Override
+ public boolean isValid() {
+ return backingDollars.isValid();
+ }
+
+ @Override
+ protected double toCoreDataType() {
+ return backingDollars.toCoreDataType();
+ }
+
+ @Override
+ public Dollars plus(Dollars operand) {
+ return backingDollars.plus(operand);
+ }
+
+ @Override
+ public Dollars minus(Dollars operand) {
+ return backingDollars.minus(operand);
+ }
+
+ @Override
+ public Dollars subtractToZero(Dollars operand) {
+ return backingDollars.subtractToZero(operand);
+ }
+
+ @Override
+ public Dollars percentage(double percent) {
+ return backingDollars.percentage(percent);
+ }
+
+ @Override
+ public Dollars min(Dollars operand) {
+ return backingDollars.min(operand);
+ }
+
+ @Override
+ public void render(Resources resources, RenderTarget target) {
+ backingDollars.render(resources, target);
+ }
+
+ @Override
+ public String toString() {
+ return backingDollars.toString();
+ }
+
+ @Override
+ public int hashCode() {
+ return backingDollars.hashCode();
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == null) return false;
+
+ Dollars that = (Dollars)obj;
+ return this.backingDollars.equals(that);
+ }
+}
View
49 src/com/jamesshore/finances/domain/ValidDollars.java
@@ -4,6 +4,7 @@
import java.text.*;
import java.util.*;
import com.jamesshore.finances.ui.*;
+import com.jamesshore.finances.util.*;
public class ValidDollars extends Dollars {
@@ -12,12 +13,13 @@
private double amount;
- public static Dollars create(double amount) {
+ private static Dollars create(double amount) {
if (inRange(amount)) return new ValidDollars(amount);
else return new InvalidDollars();
}
- private ValidDollars(double amount) {
+ public ValidDollars(double amount) {
+ Require.that(inRange(amount), "dollar amount [" + amount + "] outside valid range");
this.amount = amount;
}
@@ -25,27 +27,28 @@ public boolean isValid() {
return true;
}
- private double amount(Dollars dollars) {
- return ((ValidDollars)dollars).amount;
+ @Override
+ protected double toCoreDataType() {
+ return amount;
}
private static boolean inRange(double value) {
return (value >= MIN_VALUE) && (value <= MAX_VALUE);
}
- public Dollars plus(Dollars dollars) {
- if (!dollars.isValid()) return new InvalidDollars();
- return create(this.amount + amount(dollars));
+ public Dollars plus(Dollars operand) {
+ if (!operand.isValid()) return new InvalidDollars();
+ return create(this.amount + operand.toCoreDataType());
}
- public Dollars minus(Dollars dollars) {
- if (!dollars.isValid()) return new InvalidDollars();
- return create(this.amount - amount(dollars));
+ public Dollars minus(Dollars operand) {
+ if (!operand.isValid()) return new InvalidDollars();
+ return create(this.amount - operand.toCoreDataType());
}
- public Dollars subtractToZero(Dollars dollars) {
- if (!dollars.isValid()) return new InvalidDollars();
- double result = this.amount - amount(dollars);
+ public Dollars subtractToZero(Dollars operand) {
+ if (!operand.isValid()) return new InvalidDollars();
+ double result = this.amount - operand.toCoreDataType();
return create(Math.max(0, result));
}
@@ -53,9 +56,9 @@ public Dollars percentage(double percent) {
return create(amount * percent / 100.0);
}
- public Dollars min(Dollars value2) {
- if (!value2.isValid()) return new InvalidDollars();
- return create(Math.min(this.amount, amount(value2)));
+ public Dollars min(Dollars operand) {
+ if (!operand.isValid()) return new InvalidDollars();
+ return create(Math.min(this.amount, operand.toCoreDataType()));
}
private boolean isNegative() {
@@ -63,7 +66,11 @@ private boolean isNegative() {
}
private long roundOffPennies() {
- return Math.round(this.amount);
+ return roundOffPennies(this.amount);
+ }
+
+ private long roundOffPennies(double amount) {
+ return Math.round(amount);
}
public void render(Resources resources, RenderTarget target) {
@@ -96,8 +103,10 @@ public int hashCode() {
@Override
public boolean equals(Object obj) {
- if (obj == null || obj instanceof InvalidDollars) return false;
- ValidDollars that = (ValidDollars)obj;
- return this.roundOffPennies() == that.roundOffPennies();
+ if (obj == null) return false;
+ Dollars that = (Dollars)obj;
+
+ if (!that.isValid()) return false;
+ return roundOffPennies(this.toCoreDataType()) == roundOffPennies(that.toCoreDataType());
}
}
View
49 src/com/jamesshore/finances/domain/_DollarsTest.java
@@ -1,53 +1,6 @@
package com.jamesshore.finances.domain;
-import static org.junit.Assert.*;
-import org.junit.*;
public class _DollarsTest {
-
- @Test
- public void parseNumbersAndDollarsAndNegativeSigns() {
- assertEquals("empty string", ValidDollars.create(0), Dollars.parse(""));
- assertEquals("just a number", ValidDollars.create(42), Dollars.parse("42"));
- assertEquals("beginning dollar sign", ValidDollars.create(42), Dollars.parse("$42"));
- assertEquals("dollar sign only", ValidDollars.create(0), Dollars.parse("$"));
- assertEquals("decimals", ValidDollars.create(42.13), Dollars.parse("42.13"));
- assertEquals("one comma", ValidDollars.create(1234), Dollars.parse("1,234"));
- assertEquals("several commas", ValidDollars.create(1234567), Dollars.parse("1,234,567"));
- assertEquals("dysfunctional commas", ValidDollars.create(42), Dollars.parse(",,,4,,,,,,2,,,"));
- assertEquals("negative number", ValidDollars.create(-42), Dollars.parse("-42"));
- assertEquals("negative dollars", ValidDollars.create(-42), Dollars.parse("-$42"));
- assertEquals("dollars negative", ValidDollars.create(-42), Dollars.parse("$-42"));
- assertEquals("negative sign only", ValidDollars.create(0), Dollars.parse("-"));
- assertEquals("negative and dollar sign only", ValidDollars.create(0), Dollars.parse("-$"));
- assertEquals("dollar and negative sign only", ValidDollars.create(0), Dollars.parse("$-"));
- }
-
- @Test
- public void parseParentheses() {
- assertEquals("open parenthesis only", ValidDollars.create(0), Dollars.parse("("));
- assertEquals("close parenthesis only", new InvalidDollars(), Dollars.parse(")"));
- assertEquals("both parenthesis only", ValidDollars.create(0), Dollars.parse("()"));
- assertEquals("number in parentheses", ValidDollars.create(-42), Dollars.parse("(42)"));
- assertEquals("open parenthesis and number", ValidDollars.create(-42), Dollars.parse("(42"));
- assertEquals("close parenthesis and number", ValidDollars.create(-42), Dollars.parse("42)"));
- }
-
- @Test
- public void parseIllegals() {
- InvalidDollars invalid = new InvalidDollars();
- assertEquals(invalid, Dollars.parse("x"));
- assertEquals(invalid, Dollars.parse("40d"));
- assertEquals(invalid, Dollars.parse("40f"));
- assertEquals(invalid, Dollars.parse("NaN"));
- }
-
- @Test
- // This test handles the special case where the core Java library hangs when
- // parsing a magic number
- public void parsingTheDoubleOfDeathDoesntHangMachine() {
- Dollars.parse("2.2250738585072012e-308");
- // should not hang -- if we reached this line, everything is okay.
- }
-
+ // TODO: delete this class?
}
View
2  src/com/jamesshore/finances/domain/_GrowthRateTest.java
@@ -9,7 +9,7 @@
@Test
public void interest() {
GrowthRate rate = new GrowthRate(10);
- assertEquals(ValidDollars.create(100), rate.growthFor(ValidDollars.create(1000)));
+ assertEquals(new ValidDollars(100), rate.growthFor(new ValidDollars(1000)));
}
@Test
View
15 src/com/jamesshore/finances/domain/_InvalidDollarsTest.java
@@ -4,6 +4,7 @@
import javax.swing.*;
import org.junit.*;
import com.jamesshore.finances.ui.*;
+import com.jamesshore.finances.util.*;
public class _InvalidDollarsTest {
@@ -15,7 +16,7 @@
public void setup() {
invalidA = new InvalidDollars();
invalidB = new InvalidDollars();
- valid = (ValidDollars)ValidDollars.create(13);
+ valid = (ValidDollars)new ValidDollars(13);
}
@Test
@@ -23,6 +24,11 @@ public void isValid() {
assertFalse(invalidA.isValid());
}
+ @Test(expected = UnreachableCodeException.class)
+ public void toCoreDataType_IsAnError() {
+ invalidA.toCoreDataType();
+ }
+
@Test
public void plus() {
assertEquals(new InvalidDollars(), invalidA.plus(invalidB));
@@ -86,8 +92,11 @@ public void renderingShouldResetLabelToDefaultState() {
@Test
public void valueObject() {
assertEquals("$???", invalidA.toString());
- assertTrue("invalid dollars are always equal", invalidA.equals(invalidB));
- assertFalse("invalid dollars don't equal anything else", invalidA.equals(valid));
+ assertTrue("invalid dollars should always be equal", invalidA.equals(invalidB));
+ assertFalse("invalid dollars shouldn't equal valid dollars", invalidA.equals(valid));
+ assertFalse("invalid dollars shouldn't equal valid user-entered dollars", invalidA.equals(new UserEnteredDollars("1")));
+ assertTrue("invalid dollars should equal invalid user-entered dollars", invalidA.equals(new UserEnteredDollars("xxx")));
+
assertFalse("shouldn't blow up when comparing to null", invalidA.equals(null));
assertTrue("equal dollars should have same hash code", invalidA.hashCode() == invalidB.hashCode());
}
View
26 src/com/jamesshore/finances/domain/_StockMarketProjectionTest.java
@@ -7,8 +7,8 @@
private static final Year STARTING_YEAR = new Year(2010);
private static final Year ENDING_YEAR = new Year(2050);
- private static final Dollars STARTING_BALANCE = ValidDollars.create(10000);
- private static final Dollars COST_BASIS = ValidDollars.create(7000);
+ private static final Dollars STARTING_BALANCE = new ValidDollars(10000);
+ private static final Dollars COST_BASIS = new ValidDollars(7000);
private static final GrowthRate GROWTH_RATE = new GrowthRate(10);
private static final TaxRate CAPITAL_GAINS_TAX_RATE = new TaxRate(25);
@@ -21,32 +21,32 @@ public void setup() {
@Test
public void stockMarketContainsMultipleYears() {
- StockMarketProjection account = new StockMarketProjection(firstYear, ENDING_YEAR, ValidDollars.create(0));
+ StockMarketProjection account = new StockMarketProjection(firstYear, ENDING_YEAR, new ValidDollars(0));
assertEquals("# of years", 41, account.numberOfYears());
assertEquals(STARTING_BALANCE, account.getYearOffset(0).startingBalance());
- assertEquals(ValidDollars.create(11000), account.getYearOffset(1).startingBalance());
- assertEquals(ValidDollars.create(12100), account.getYearOffset(2).startingBalance());
+ assertEquals(new ValidDollars(11000), account.getYearOffset(1).startingBalance());
+ assertEquals(new ValidDollars(12100), account.getYearOffset(2).startingBalance());
assertEquals(new Year(2050), account.getYearOffset(40).year());
}
@Test
public void stockMarketWithdrawsAStandardAmountEveryYear() {
- StockMarketProjection account = new StockMarketProjection(firstYear, ENDING_YEAR, ValidDollars.create(10));
- assertEquals("year 0", ValidDollars.create(10), account.getYearOffset(0).totalSellOrders());
- assertEquals("year 1", ValidDollars.create(10), account.getYearOffset(1).totalSellOrders());
- assertEquals("year 40", ValidDollars.create(10), account.getYearOffset(40).totalSellOrders());
+ StockMarketProjection account = new StockMarketProjection(firstYear, ENDING_YEAR, new ValidDollars(10));
+ assertEquals("year 0", new ValidDollars(10), account.getYearOffset(0).totalSellOrders());
+ assertEquals("year 1", new ValidDollars(10), account.getYearOffset(1).totalSellOrders());
+ assertEquals("year 40", new ValidDollars(10), account.getYearOffset(40).totalSellOrders());
}
@Test
public void noCumulativeRoundingErrorInInterestCalculations() {
- StockMarketProjection account = new StockMarketProjection(firstYear, ENDING_YEAR, ValidDollars.create(0));
- assertEquals(ValidDollars.create(497852), account.getYearOffset(40).endingBalance());
+ StockMarketProjection account = new StockMarketProjection(firstYear, ENDING_YEAR, new ValidDollars(0));
+ assertEquals(new ValidDollars(497852), account.getYearOffset(40).endingBalance());
}
@Test
public void capitalGainsTaxCalculationWorksTheSameWayAsSpreadsheet() {
- StockMarketProjection account = new StockMarketProjection(firstYear, ENDING_YEAR, ValidDollars.create(695));
- assertEquals(ValidDollars.create(2067), account.getYearOffset(40).endingBalance());
+ StockMarketProjection account = new StockMarketProjection(firstYear, ENDING_YEAR, new ValidDollars(695));
+ assertEquals(new ValidDollars(2067), account.getYearOffset(40).endingBalance());
}
}
View
58 src/com/jamesshore/finances/domain/_StockMarketYearTest.java
@@ -8,8 +8,8 @@
private static final Year YEAR = new Year(2010);
private static final GrowthRate INTEREST_RATE = new GrowthRate(10);
- private static final Dollars STARTING_BALANCE = ValidDollars.create(10000);
- private static final Dollars STARTING_PRINCIPAL = ValidDollars.create(3000);
+ private static final Dollars STARTING_BALANCE = new ValidDollars(10000);
+ private static final Dollars STARTING_PRINCIPAL = new ValidDollars(3000);
private static final TaxRate CAPITAL_GAINS_TAX_RATE = new TaxRate(25);
@Test
@@ -20,26 +20,26 @@ public void startingValues() {
assertEquals("starting principal", STARTING_PRINCIPAL, year.startingCostBasis());
assertEquals("interest rate", INTEREST_RATE, year.growthRate());
assertEquals("capital gains tax rate", CAPITAL_GAINS_TAX_RATE, year.capitalGainsTaxRate());
- assertEquals("total withdrawn default", ValidDollars.create(0), year.totalSold());
+ assertEquals("total withdrawn default", new ValidDollars(0), year.totalSold());
}
@Test
public void totalSold() {
StockMarketYear year = newYear();
- assertEquals("no sales", ValidDollars.create(0), year.totalSellOrders());
- year.sell(ValidDollars.create(3000));
- assertEquals("one sale", ValidDollars.create(3000), year.totalSellOrders());
- year.sell(ValidDollars.create(750));
- year.sell(ValidDollars.create(1350));
- assertEquals("multiple sales", ValidDollars.create(5100), year.totalSellOrders());
+ assertEquals("no sales", new ValidDollars(0), year.totalSellOrders());
+ year.sell(new ValidDollars(3000));
+ assertEquals("one sale", new ValidDollars(3000), year.totalSellOrders());
+ year.sell(new ValidDollars(750));
+ year.sell(new ValidDollars(1350));
+ assertEquals("multiple sales", new ValidDollars(5100), year.totalSellOrders());
}
@Test
public void capitalGainsTax() {
StockMarketYear year = newYear();
- year.sell(ValidDollars.create(4000));
- assertEquals("capital gains tax includes tax on withdrawals to cover capital gains", ValidDollars.create(1333), year.capitalGainsTaxIncurred());
- assertEquals("total withdrawn includes capital gains tax", ValidDollars.create(5333), year.totalSold());
+ year.sell(new ValidDollars(4000));
+ assertEquals("capital gains tax includes tax on withdrawals to cover capital gains", new ValidDollars(1333), year.capitalGainsTaxIncurred());
+ assertEquals("total withdrawn includes capital gains tax", new ValidDollars(5333), year.totalSold());
}
@Test
@@ -48,45 +48,45 @@ public void treatAllWithdrawalsAsSubjectToCapitalGainsTaxUntilAllCapitalGainsHav
Dollars capitalGains = STARTING_BALANCE.minus(STARTING_PRINCIPAL);
- year.sell(ValidDollars.create(500));
- assertEquals("pay tax on all entire withdrawal", ValidDollars.create(167), year.capitalGainsTaxIncurred());
+ year.sell(new ValidDollars(500));
+ assertEquals("pay tax on all entire withdrawal", new ValidDollars(167), year.capitalGainsTaxIncurred());
year.sell(capitalGains);
- assertEquals("to match spreadsheet, we pay compounding tax on capital gains even when compounded amount is not capital gains", ValidDollars.create(2333), year.capitalGainsTaxIncurred());
- year.sell(ValidDollars.create(1000));
- assertEquals("pay no more tax once all capital gains withdrawn", ValidDollars.create(2333), year.capitalGainsTaxIncurred());
+ assertEquals("to match spreadsheet, we pay compounding tax on capital gains even when compounded amount is not capital gains", new ValidDollars(2333), year.capitalGainsTaxIncurred());
+ year.sell(new ValidDollars(1000));
+ assertEquals("pay no more tax once all capital gains withdrawn", new ValidDollars(2333), year.capitalGainsTaxIncurred());
}
@Test
public void interestEarned() {
StockMarketYear year = newYear();
- assertEquals("basic interest earned", ValidDollars.create(1000), year.growth());
- year.sell(ValidDollars.create(2000));
- assertEquals("withdrawals (which pay capital gains tax) don't earn interest", ValidDollars.create(733), year.growth());
+ assertEquals("basic interest earned", new ValidDollars(1000), year.growth());
+ year.sell(new ValidDollars(2000));
+ assertEquals("withdrawals (which pay capital gains tax) don't earn interest", new ValidDollars(733), year.growth());
}
@Test
public void endingPrincipal() {
StockMarketYear year = newYear();
- year.sell(ValidDollars.create(500));
+ year.sell(new ValidDollars(500));
assertEquals("withdrawals less than capital gains do not reduce principal", STARTING_PRINCIPAL, year.endingCostBasis());
- year.sell(ValidDollars.create(6500));
+ year.sell(new ValidDollars(6500));
- Dollars totalWithdrawn = ValidDollars.create(9333);
- Dollars capitalGains = ValidDollars.create(7000);
+ Dollars totalWithdrawn = new ValidDollars(9333);
+ Dollars capitalGains = new ValidDollars(7000);
Dollars principalReducedBy = totalWithdrawn.minus(capitalGains);
Dollars expectedPrincipal = STARTING_PRINCIPAL.minus(principalReducedBy);
assertEquals("principal should be reduced by difference between total withdrawals and capital gains", expectedPrincipal, year.endingCostBasis());
- year.sell(ValidDollars.create(1000));
- assertEquals("principal goes negative when we're overdrawn", ValidDollars.create(-333), year.endingCostBasis());
+ year.sell(new ValidDollars(1000));
+ assertEquals("principal goes negative when we're overdrawn", new ValidDollars(-333), year.endingCostBasis());
}
@Test
public void endingBalance() {
StockMarketYear year = newYear();
- assertEquals("ending balance includes interest", ValidDollars.create(11000), year.endingBalance());
- year.sell(ValidDollars.create(1000));
- assertEquals("ending balance includes withdrawals (which pay capital gains tax) and interest", ValidDollars.create(9533), year.endingBalance());
+ assertEquals("ending balance includes interest", new ValidDollars(11000), year.endingBalance());
+ year.sell(new ValidDollars(1000));
+ assertEquals("ending balance includes withdrawals (which pay capital gains tax) and interest", new ValidDollars(9533), year.endingBalance());
}
@Test
View
4 src/com/jamesshore/finances/domain/_TaxRateTest.java
@@ -9,13 +9,13 @@
@Test
public void simpleTaxJustAppliesTaxRateToAmount() {
TaxRate taxRate = new TaxRate(25);
- assertEquals(ValidDollars.create(250), taxRate.simpleTaxFor(ValidDollars.create(1000)));
+ assertEquals(new ValidDollars(250), taxRate.simpleTaxFor(new ValidDollars(1000)));
}
@Test
public void compoundTaxIsTheAmountOfTaxThatIsIncurredIfYouAlsoPayTaxOnTheTax() {
TaxRate taxRate = new TaxRate(25);
- assertEquals(ValidDollars.create(333), taxRate.compoundTaxFor(ValidDollars.create(1000)));
+ assertEquals(new ValidDollars(333), taxRate.compoundTaxFor(new ValidDollars(1000)));
}
@Test
View
134 src/com/jamesshore/finances/domain/_UserEnteredDollarsTest.java
@@ -0,0 +1,134 @@
+package com.jamesshore.finances.domain;
+
+import static org.junit.Assert.*;
+import org.junit.*;
+import com.jamesshore.finances.ui.*;
+
+public class _UserEnteredDollarsTest {
+
+ private UserEnteredDollars dollars1a = new UserEnteredDollars("1");
+ private UserEnteredDollars dollars1b = new UserEnteredDollars("1");
+ private UserEnteredDollars dollars1Spaces = new UserEnteredDollars(" 1 ");
+ private UserEnteredDollars dollars2 = new UserEnteredDollars("2");
+ private UserEnteredDollars invalid = new UserEnteredDollars("xxx");
+
+ @Test
+ public void parseNumbersAndDollarsAndNegativeSigns() {
+ assertEquals("empty string", new ValidDollars(0), new UserEnteredDollars(""));
+ assertEquals("just a number", new ValidDollars(42), new UserEnteredDollars("42"));
+ assertEquals("beginning dollar sign", new ValidDollars(42), new UserEnteredDollars("$42"));
+ assertEquals("dollar sign only", new ValidDollars(0), new UserEnteredDollars("$"));
+ assertEquals("decimals", new ValidDollars(42.13), new UserEnteredDollars("42.13"));
+ assertEquals("one comma", new ValidDollars(1234), new UserEnteredDollars("1,234"));
+ assertEquals("several commas", new ValidDollars(1234567), new UserEnteredDollars("1,234,567"));
+ assertEquals("dysfunctional commas", new ValidDollars(42), new UserEnteredDollars(",,,4,,,,,,2,,,"));
+ assertEquals("negative number", new ValidDollars(-42), new UserEnteredDollars("-42"));
+ assertEquals("negative dollars", new ValidDollars(-42), new UserEnteredDollars("-$42"));
+ assertEquals("dollars negative", new ValidDollars(-42), new UserEnteredDollars("$-42"));
+ assertEquals("negative sign only", new ValidDollars(0), new UserEnteredDollars("-"));
+ assertEquals("negative and dollar sign only", new ValidDollars(0), new UserEnteredDollars("-$"));
+ assertEquals("dollar and negative sign only", new ValidDollars(0), new UserEnteredDollars("$-"));
+ }
+
+ @Test
+ public void parseParentheses() {
+ assertEquals("open parenthesis only", new ValidDollars(0), new UserEnteredDollars("("));
+ assertEquals("close parenthesis only", new InvalidDollars(), new UserEnteredDollars(")"));
+ assertEquals("both parenthesis only", new ValidDollars(0), new UserEnteredDollars("()"));
+ assertEquals("number in parentheses", new ValidDollars(-42), new UserEnteredDollars("(42)"));
+ assertEquals("open parenthesis and number", new ValidDollars(-42), new UserEnteredDollars("(42"));
+ assertEquals("close parenthesis and number", new ValidDollars(-42), new UserEnteredDollars("42)"));
+ }
+
+ @Test
+ public void parseIllegals() {
+ InvalidDollars invalid = new InvalidDollars();
+ assertEquals(invalid, new UserEnteredDollars("x"));
+ assertEquals(invalid, new UserEnteredDollars("40d"));
+ assertEquals(invalid, new UserEnteredDollars("40f"));
+ assertEquals(invalid, new UserEnteredDollars("NaN"));
+ }
+
+ @Test
+ // This test handles the special case where the core Java library hangs when
+ // parsing a magic number
+ public void parsingTheDoubleOfDeathDoesntHangMachine() {
+ new UserEnteredDollars("2.2250738585072012e-308");
+ // should not hang -- if we reached this line, everything is okay.
+ }
+
+ @Test
+ public void isValid() {
+ assertTrue(dollars1a.isValid());
+ }
+
+ @Test
+ public void toCoreDataType() {
+ assertEquals(1.234, new UserEnteredDollars("1.234").toCoreDataType(), 0);
+ }
+
+ @Test
+ public void rendersItself() {
+ __RenderTargetStub target = new __RenderTargetStub();
+ dollars1a.render(new Resources(), target);
+ assertEquals("label text should be toString() value", dollars1a.toString(), target.text);
+ }
+
+ @Test
+ public void plus() {
+ assertEquals("should be able to add two user-entered dollars", new ValidDollars(3), dollars1a.plus(dollars2));
+ assertEquals("should be able to add user-entered dollars to valid dollars", new ValidDollars(4), dollars1a.plus(new ValidDollars(3)));
+ assertEquals("should be able to add user-entered dollars to invalid dollars", new InvalidDollars(), dollars1a.plus(new InvalidDollars()));
+ assertEquals("should be able to add valid dollars to user-entered dollars", new ValidDollars(5), new ValidDollars(4).plus(dollars1a));
+ assertEquals("should be able to add invalid dollars to user-entered dollars", new InvalidDollars(), new InvalidDollars().plus(dollars1a));
+ }
+
+ @Test
+ public void minus() {
+ assertEquals("should to able to subtract two user-entered dollars", new ValidDollars(-1), dollars1a.minus(dollars2));
+ assertEquals("should be able to minus user-entered dollars to valid dollars", new ValidDollars(-2), dollars1a.minus(new ValidDollars(3)));
+ assertEquals("should be able to minus user-entered dollars to invalid dollars", new InvalidDollars(), dollars1a.minus(new InvalidDollars()));
+ assertEquals("should be able to minus valid dollars to user-entered dollars", new ValidDollars(3), new ValidDollars(4).minus(dollars1a));
+ assertEquals("should be able to minus invalid dollars to user-entered dollars", new InvalidDollars(), new InvalidDollars().minus(dollars1a));
+ }
+
+ @Test
+ public void subtractToZero() {
+ assertEquals("should to able to subtract-to-zero two user-entered dollars", new ValidDollars(0), dollars1a.subtractToZero(dollars2));
+ assertEquals("should be able to subtract-to-zero user-entered dollars to valid dollars", new ValidDollars(0), dollars1a.subtractToZero(new ValidDollars(3)));
+ assertEquals("should be able to subtract-to-zero user-entered dollars to invalid dollars", new InvalidDollars(), dollars1a.subtractToZero(new InvalidDollars()));
+ assertEquals("should be able to subtract-to-zero valid dollars to user-entered dollars", new ValidDollars(3), new ValidDollars(4).subtractToZero(dollars1a));
+ assertEquals("should be able to subtract-to-zero invalid dollars to user-entered dollars", new InvalidDollars(), new InvalidDollars().subtractToZero(dollars1a));
+ }
+
+ @Test
+ public void percentage() {
+ assertEquals("should to able to percentage user-entered dollars", new ValidDollars(5), new UserEnteredDollars("50").percentage(10));
+ }
+
+ @Test
+ public void min() {
+ assertEquals("should to able to min two user-entered dollars", new ValidDollars(1), dollars1a.min(dollars2));
+ assertEquals("should be able to min user-entered dollars to valid dollars", new ValidDollars(1), dollars1a.min(new ValidDollars(3)));
+ assertEquals("should be able to min user-entered dollars to invalid dollars", new InvalidDollars(), dollars1a.min(new InvalidDollars()));
+ assertEquals("should be able to min valid dollars to user-entered dollars", new ValidDollars(1), new ValidDollars(4).min(dollars1a));
+ assertEquals("should be able to min invalid dollars to user-entered dollars", new InvalidDollars(), new InvalidDollars().min(dollars1a));
+ }
+
+ @Test
+ public void valueObject() {
+ assertEquals("$1", dollars1a.toString());
+
+ assertTrue("dollars with same string should be equal", dollars1a.equals(dollars1b));
+ assertTrue("dollars with different string but same value should be equal", dollars1a.equals(dollars1Spaces));
+
+ assertTrue("user-entered dollars should be comparable to valid dollars when equal", dollars1Spaces.equals(new ValidDollars(1)));
+ assertFalse("user-entered dollars should be comparable to valid dollars when unequal", dollars1Spaces.equals(new ValidDollars(2)));
+
+ assertFalse("user-entered dollars should be comparable to invalid dollars when valid", dollars1Spaces.equals(new InvalidDollars()));
+ assertTrue("user-entered dollars should be comparable to invalid dollars when invalid", invalid.equals(new InvalidDollars()));
+
+ assertTrue("equal dollars should have same hash code even if string is different", dollars1a.hashCode() == dollars1Spaces.hashCode());
+ assertFalse("shouldn't blow up when comparing to null", dollars1a.equals(null));
+ }
+}
View
107 src/com/jamesshore/finances/domain/_ValidDollarsTest.java
@@ -6,47 +6,72 @@
import javax.swing.*;
import org.junit.*;
import com.jamesshore.finances.ui.*;
+import com.jamesshore.finances.util.*;
public class _ValidDollarsTest {
- private Dollars zeroDollars = ValidDollars.create(0);
- private Dollars twentyDollars = ValidDollars.create(20);
- private Dollars minusTwentyDollars = ValidDollars.create(-20);
- private Dollars MAX_VALID = ValidDollars.create(ValidDollars.MAX_VALUE);
- private Dollars MIN_VALID = ValidDollars.create(ValidDollars.MIN_VALUE);
+ private Dollars zeroDollars = new ValidDollars(0);
+ private Dollars twentyDollars = new ValidDollars(20);
+ private Dollars minusTwentyDollars = new ValidDollars(-20);
+ private Dollars MAX_VALID = new ValidDollars(ValidDollars.MAX_VALUE);
+ private Dollars MIN_VALID = new ValidDollars(ValidDollars.MIN_VALUE);
@Test
public void cannotConstructDollarsOutsideValidRange() {
- assertEquals("overflow", new InvalidDollars(), ValidDollars.create(ValidDollars.MAX_VALUE + 1));
- assertEquals("underflow", new InvalidDollars(), ValidDollars.create(ValidDollars.MIN_VALUE - 1));
- assertEquals("not a number", new InvalidDollars(), ValidDollars.create(Double.NaN));
+ try {
+ new ValidDollars(ValidDollars.MAX_VALUE + 1);
+ fail("expected overflow");
+ }
+ catch (RequireException e) {
+ // expected
+ }
+ try {
+ new ValidDollars(ValidDollars.MIN_VALUE - 1);
+ fail("expected underflow");
+ }
+ catch (RequireException e) {
+ // expected
+ }
+ try {
+ new ValidDollars(Double.NaN);
+ fail("expected NaN failure");
+ }
+ catch (RequireException e) {
+ // expected
+ }
}
@Test
public void isInvalid() {
- assertTrue(ValidDollars.create(42).isValid());
+ assertTrue(new ValidDollars(42).isValid());
+ }
+
+ @Test
+ // Resist the temptation to make this public! That's a design smell.
+ public void toCoreDataType() {
+ assertEquals(12.34567891, ((ValidDollars)new ValidDollars(12.34567891)).toCoreDataType(), 0);
}
@Test
public void addition() {
- assertEquals("addition", ValidDollars.create(40), ValidDollars.create(10).plus(ValidDollars.create(30)));
- assertEquals("overflow", new InvalidDollars(), MAX_VALID.plus(ValidDollars.create(1)));
- assertEquals("underflow", new InvalidDollars(), MIN_VALID.plus(ValidDollars.create(-1)));
+ assertEquals("addition", new ValidDollars(40), new ValidDollars(10).plus(new ValidDollars(30)));
+ assertEquals("overflow", new InvalidDollars(), MAX_VALID.plus(new ValidDollars(1)));
+ assertEquals("underflow", new InvalidDollars(), MIN_VALID.plus(new ValidDollars(-1)));
}
@Test
public void subtraction() {
- assertEquals("positive result", twentyDollars, ValidDollars.create(50).minus(ValidDollars.create(30)));
- assertEquals("negative result", ValidDollars.create(-60), ValidDollars.create(40).minus(ValidDollars.create(100)));
- assertEquals("overflow", new InvalidDollars(), MAX_VALID.minus(ValidDollars.create(-1)));
- assertEquals("underflow", new InvalidDollars(), MIN_VALID.minus(ValidDollars.create(1)));
+ assertEquals("positive result", twentyDollars, new ValidDollars(50).minus(new ValidDollars(30)));
+ assertEquals("negative result", new ValidDollars(-60), new ValidDollars(40).minus(new ValidDollars(100)));
+ assertEquals("overflow", new InvalidDollars(), MAX_VALID.minus(new ValidDollars(-1)));
+ assertEquals("underflow", new InvalidDollars(), MIN_VALID.minus(new ValidDollars(1)));
}
@Test
public void subtractToZero() {
- assertEquals("positive result", twentyDollars, ValidDollars.create(50).subtractToZero(ValidDollars.create(30)));
- assertEquals("no negative result--return zero instead", ValidDollars.create(0), ValidDollars.create(40).subtractToZero(ValidDollars.create(100)));
- assertEquals("overflow", new InvalidDollars(), MAX_VALID.subtractToZero(ValidDollars.create(-1)));
+ assertEquals("positive result", twentyDollars, new ValidDollars(50).subtractToZero(new ValidDollars(30)));
+ assertEquals("no negative result--return zero instead", new ValidDollars(0), new ValidDollars(40).subtractToZero(new ValidDollars(100)));
+ assertEquals("overflow", new InvalidDollars(), MAX_VALID.subtractToZero(new ValidDollars(-1)));
}
@Test
@@ -58,14 +83,14 @@ public void flipSign() {
@Test
public void percentage() {
- assertEquals("percent", twentyDollars, ValidDollars.create(100).percentage(20));
+ assertEquals("percent", twentyDollars, new ValidDollars(100).percentage(20));
assertEquals("overflow", new InvalidDollars(), MAX_VALID.percentage(200));
}
@Test
public void min() {
Dollars value1 = twentyDollars;
- Dollars value2 = ValidDollars.create(30);
+ Dollars value2 = new ValidDollars(30);
assertEquals("value 1", twentyDollars, Dollars.min(value1, value2));
assertEquals("value 2", twentyDollars, Dollars.min(value2, value1));
}
@@ -110,42 +135,42 @@ public void renderingResetsLabelToDefaultState() {
@Test
public void equalsIgnoresPennies() {
- assertTrue("should round down", ValidDollars.create(10).equals(ValidDollars.create(10.10)));
- assertTrue("should round up", ValidDollars.create(10).equals(ValidDollars.create(9.90)));
- assertTrue("should round up when we have exactly 50 cents", ValidDollars.create(11).equals(ValidDollars.create(10.5)));
+ assertTrue("should round down", new ValidDollars(10).equals(new ValidDollars(10.10)));
+ assertTrue("should round up", new ValidDollars(10).equals(new ValidDollars(9.90)));
+ assertTrue("should round up when we have exactly 50 cents", new ValidDollars(11).equals(new ValidDollars(10.5)));
}
@Test
public void hashcodeIgnoresPenniesToo() {
- assertTrue("should round down", ValidDollars.create(10).hashCode() == ValidDollars.create(10.10).hashCode());
- assertTrue("should round up", ValidDollars.create(10).hashCode() == ValidDollars.create(9.90).hashCode());
- assertTrue("should round up when we have exactly 50 cents", ValidDollars.create(11).hashCode() == ValidDollars.create(10.5).hashCode());
+ assertTrue("should round down", new ValidDollars(10).hashCode() == new ValidDollars(10.10).hashCode());
+ assertTrue("should round up", new ValidDollars(10).hashCode() == new ValidDollars(9.90).hashCode());
+ assertTrue("should round up when we have exactly 50 cents", new ValidDollars(11).hashCode() == new ValidDollars(10.5).hashCode());
}
@Test
public void toStringIgnoresPennies() {
- assertEquals("should round down", "$10", ValidDollars.create(10.10).toString());
- assertEquals("should round up", "$10", ValidDollars.create(9.90).toString());
- assertEquals("should round up when we have exactly 50 cents", "$11", ValidDollars.create(10.5).toString());
+ assertEquals("should round down", "$10", new ValidDollars(10.10).toString());
+ assertEquals("should round up", "$10", new ValidDollars(9.90).toString());
+ assertEquals("should round up when we have exactly 50 cents", "$11", new ValidDollars(10.5).toString());
}
@Test
public void toStringFormatsLongNumbersWithCommas() {
- assertEquals("$1,234", ValidDollars.create(1234).toString());
- assertEquals("$12,345,678", ValidDollars.create(12345678).toString());
- assertEquals("$123,456,789", ValidDollars.create(123456789).toString());
+ assertEquals("$1,234", new ValidDollars(1234).toString());
+ assertEquals("$12,345,678", new ValidDollars(12345678).toString());
+ assertEquals("$123,456,789", new ValidDollars(123456789).toString());
}
@Test
public void toStringFormatsNegativeNumbersWithParentheses() {
- assertEquals("($500)", ValidDollars.create(-500).toString());
+ assertEquals("($500)", new ValidDollars(-500).toString());
}
@Test
public void toStringFormatsInTheUsaStyleEvenWhenInDifferentLocales() {
try {
Locale.setDefault(Locale.FRANCE);
- assertEquals("$1,234", ValidDollars.create(1234).toString());
+ assertEquals("$1,234", new ValidDollars(1234).toString());
}
finally {
Locale.setDefault(Locale.US);
@@ -154,15 +179,21 @@ public void toStringFormatsInTheUsaStyleEvenWhenInDifferentLocales() {
@Test
public void valueObject() {
- Dollars dollars1a = ValidDollars.create(10);
- Dollars dollars1b = ValidDollars.create(10);
- Dollars dollars2 = ValidDollars.create(20);
+ Dollars dollars1a = new ValidDollars(10);
+ Dollars dollars1b = new ValidDollars(10);
+ Dollars dollars2 = new ValidDollars(20);
assertEquals("$10", dollars1a.toString());
assertTrue("dollars with same amount should be equal", dollars1a.equals(dollars1b));
assertFalse("dollars with different amounts should not be equal", dollars1a.equals(dollars2));
+
+ assertTrue("valid dollars should be comparable to user-entered dollars when equal", dollars1a.equals(new UserEnteredDollars("10")));
+ assertFalse("valid dollars should be comparable to user-entered dollars when unequal", dollars1a.equals(new UserEnteredDollars("20")));
+
assertFalse("valid dollars aren't equal to invalid dollars", dollars1a.equals(new InvalidDollars()));
+
assertTrue("equal dollars should have same hash code", dollars1a.hashCode() == dollars1b.hashCode());
+
assertFalse("shouldn't blow up when comparing to null", dollars1a.equals(null));
}
View
2  src/com/jamesshore/finances/persistence/SaveFile.java
@@ -15,7 +15,7 @@ public void save(Dollars startingBalance) throws IOException {
Writer writer = new BufferedWriter(new FileWriter(path));
try {
writer.write("com.jamesshore.finances,1\n");
- writer.write(startingBalance.toCoreDataType() + "\n");
+ writer.write(startingBalance.toString() + "\n");
}
finally {
writer.close();
View
15 src/com/jamesshore/finances/persistence/_SaveFileTest.java
@@ -38,13 +38,14 @@ public void saveOverwritesAnExistingFile() throws IOException {
assertFalse("file should have been overwritten", fileContents.startsWith("test"));
}
- @Test
- public void saveWritesFileContents() throws IOException {
- saveFile.save(ValidDollars.create(1.23));// , ValidDollars.create(10.24), ValidDollars.create(100.25));
-
- String expected = "com.jamesshore.finances,1\n$1.23\n";
- assertEquals(expected, readFile());
- }
+ // TODO: finish this test
+ // @Test
+ // public void saveWritesFileContents() throws IOException {
+ // saveFile.save(new ValidDollars(1.23));// , new ValidDollars(10.24), new ValidDollars(100.25));
+ //
+ // String expected = "com.jamesshore.finances,1\n$1.23\n";
+ // assertEquals(expected, readFile());
+ // }
private String readFile() throws IOException {
BufferedReader input = new BufferedReader(new FileReader(path));
View
6 src/com/jamesshore/finances/ui/ApplicationModel.java
@@ -7,11 +7,11 @@
public static final Year DEFAULT_STARTING_YEAR = new Year(2010);
public static final Year DEFAULT_ENDING_YEAR = new Year(2050);
- public static final Dollars DEFAULT_STARTING_BALANCE = ValidDollars.create(10000);
- public static final Dollars DEFAULT_STARTING_COST_BASIS = ValidDollars.create(7000);
+ public static final Dollars DEFAULT_STARTING_BALANCE = new ValidDollars(10000);
+ public static final Dollars DEFAULT_STARTING_COST_BASIS = new ValidDollars(7000);
public static final GrowthRate DEFAULT_GROWTH_RATE = new GrowthRate(10);
public static final TaxRate DEFAULT_CAPITAL_GAINS_TAX_RATE = new TaxRate(25);
- public static final Dollars DEFAULT_YEARLY_SPENDING = ValidDollars.create(695);
+ public static final Dollars DEFAULT_YEARLY_SPENDING = new ValidDollars(695);
private Year startingYear = DEFAULT_STARTING_YEAR;
private Year endingYear = DEFAULT_ENDING_YEAR;
View
2  src/com/jamesshore/finances/ui/DollarsTextField.java
@@ -74,7 +74,7 @@ public void setForeground(Color color) {
}
public Dollars getDollars() {
- return Dollars.parse(textField.getText());
+ return new UserEnteredDollars(textField.getText());
}
public void addTextChangeListener(final ChangeListener listener) {
View
1  src/com/jamesshore/finances/ui/_ApplicationFrameTest.java
@@ -133,7 +133,6 @@ public void closeMenuItemShouldCloseTheWindow() {
@Test
public void saveAsMenuItemShouldShowSaveAsDialog() throws Throwable {
final FileDialog saveAsDialog = saveAsDialog();
- assertNotNull("Save As dialog should be created");
SwingUtilities.invokeLater(new Runnable() {
@Override
View
12 src/com/jamesshore/finances/ui/_ApplicationModelTest.java
@@ -35,20 +35,20 @@ public void shouldOnlyHaveOneInstanceOfStockMarketTableModel() {
@Test
public void changingStartingBalanceShouldChangeStockMarketTableModel() {
- model.setStartingBalance(ValidDollars.create(123));
- assertEquals(ValidDollars.create(123), model.stockMarketTableModel().startingBalance());
+ model.setStartingBalance(new ValidDollars(123));
+ assertEquals(new ValidDollars(123), model.stockMarketTableModel().startingBalance());
}
@Test
public void changingStartingCostBasisShouldChangeStockMarketTableModel() {
- model.setStartingCostBasis(ValidDollars.create(39));
- assertEquals(ValidDollars.create(39), model.stockMarketTableModel().startingCostBasis());
+ model.setStartingCostBasis(new ValidDollars(39));
+ assertEquals(new ValidDollars(39), model.stockMarketTableModel().startingCostBasis());
}
@Test
public void changingYearlySpendingShouldChangeStockMarketTableModel() {
- model.setYearlySpending(ValidDollars.create(423));
- assertEquals(ValidDollars.create(423), model.stockMarketTableModel().yearlySpending());
+ model.setYearlySpending(new ValidDollars(423));
+ assertEquals(new ValidDollars(423), model.stockMarketTableModel().yearlySpending());
}
}
View
6 src/com/jamesshore/finances/ui/_ConfigurationPanelTest.java
@@ -64,7 +64,7 @@ public void startingBalanceFieldUpdatesApplicationModel() {
panel = new ConfigurationPanel(mockModel);
startingBalanceField().setText("668");
- assertEquals("applicationModel should be updated", ValidDollars.create(668), mockModel.setStartingBalanceCalledWith);
+ assertEquals("applicationModel should be updated", new ValidDollars(668), mockModel.setStartingBalanceCalledWith);
}
@Test
@@ -73,7 +73,7 @@ public void costBasisFieldUpdatesApplicationModel() {
panel = new ConfigurationPanel(mockModel);
costBasisField().setText("670");
- assertEquals("applicationModel should be updated", ValidDollars.create(670), mockModel.setStartingCostBasisCalledWith);
+ assertEquals("applicationModel should be updated", new ValidDollars(670), mockModel.setStartingCostBasisCalledWith);
}
@Test
@@ -82,7 +82,7 @@ public void yearlySpendingFieldUpdatesApplicationModel() {
panel = new ConfigurationPanel(mockModel);
yearlySpendingField().setText("672");
- assertEquals("applicationModel should be updated", ValidDollars.create(672), mockModel.setYearlySpendingCalledWith);
+ assertEquals("applicationModel should be updated", new ValidDollars(672), mockModel.setYearlySpendingCalledWith);
}
View
6 src/com/jamesshore/finances/ui/_DollarsTextFieldTest.java
@@ -15,7 +15,7 @@
@Before
public void setup() {
- field = new DollarsTextField(ValidDollars.create(42));
+ field = new DollarsTextField(new ValidDollars(42));
// Note: for overlay layout to work properly, icon must be first. If you change
// the way components are added to the container, be sure to do a visual check.
@@ -82,13 +82,13 @@ public void textReflectsDollarAmountUponConstruction() {
@Test
public void canRetrieveAmount() {
- assertEquals(ValidDollars.create(42), field.getDollars());
+ assertEquals(new ValidDollars(42), field.getDollars());
}
@Test
public void changingTextChangesDollarAmount() {
field.setText("1024");
- assertEquals(ValidDollars.create(1024), field.getDollars());
+ assertEquals(new ValidDollars(1024), field.getDollars());
}
@Test
View
20 src/com/jamesshore/finances/ui/_StockMarketTableModelTest.java
@@ -9,9 +9,9 @@
private static final Year STARTING_YEAR = new Year(2010);
private static final Year ENDING_YEAR = new Year(2050);
- private static final Dollars STARTING_BALANCE = ValidDollars.create(10000);
- private static final Dollars STARTING_COST_BASIS = ValidDollars.create(7000);
- private static final Dollars YEARLY_SPENDING = ValidDollars.create(36);
+ private static final Dollars STARTING_BALANCE = new ValidDollars(10000);
+ private static final Dollars STARTING_COST_BASIS = new ValidDollars(7000);
+ private static final Dollars YEARLY_SPENDING = new ValidDollars(36);
private StockMarketYear startingYear;
private StockMarketTableModel model;
@@ -36,10 +36,10 @@ public void columnValues() {
assertEquals("year", STARTING_YEAR, model.getValueAt(0, 0));
assertEquals("starting balance", STARTING_BALANCE, model.getValueAt(0, 1));
assertEquals("starting principal", STARTING_COST_BASIS, model.getValueAt(0, 2));
- assertEquals("sell orders", ValidDollars.create(0).minus(YEARLY_SPENDING), model.getValueAt(0, 3));
- assertEquals("taxes", ValidDollars.create(-12), model.getValueAt(0, 4));
- assertEquals("appreciation", ValidDollars.create(995), model.getValueAt(0, 5));
- assertEquals("ending balance", ValidDollars.create(10947), model.getValueAt(0, 6));
+ assertEquals("sell orders", new ValidDollars(0).minus(YEARLY_SPENDING), model.getValueAt(0, 3));
+ assertEquals("taxes", new ValidDollars(-12), model.getValueAt(0, 4));
+ assertEquals("appreciation", new ValidDollars(995), model.getValueAt(0, 5));
+ assertEquals("ending balance", new ValidDollars(10947), model.getValueAt(0, 6));
}
@Test
@@ -65,13 +65,13 @@ public void multipleRows() {
assertEquals(41, model.getRowCount());
assertEquals(STARTING_YEAR, model.getValueAt(0, 0));
assertEquals(STARTING_BALANCE, model.getValueAt(0, 1));
- assertEquals(ValidDollars.create(10947), model.getValueAt(1, 1));
+ assertEquals(new ValidDollars(10947), model.getValueAt(1, 1));
assertEquals(ENDING_YEAR, model.getValueAt(40, 0));
}
@Test
public void setProjection_ShouldChangeTableModel() {
- StockMarketProjection projection = new StockMarketProjection(startingYear, startingYear.year(), ValidDollars.create(0));
+ StockMarketProjection projection = new StockMarketProjection(startingYear, startingYear.year(), new ValidDollars(0));
model.setProjection(projection);
assertEquals("projection should have changed", projection, model.stockMarketProjection());
assertEquals("change to projection should reflect in methods", 1, model.getRowCount());
@@ -79,7 +79,7 @@ public void setProjection_ShouldChangeTableModel() {
@Test
public void setProjection_ShouldFireUpdateEvent() {
- StockMarketProjection projection = new StockMarketProjection(startingYear, startingYear.year(), ValidDollars.create(0));
+ StockMarketProjection projection = new StockMarketProjection(startingYear, startingYear.year(), new ValidDollars(0));
class TestListener implements TableModelListener {
public boolean eventFired = false;
View
9 src/com/jamesshore/finances/util/Require.java
@@ -7,13 +7,4 @@ public static void that(boolean expression, String message) {
}
-}
-
-class RequireException extends RuntimeException {
- private static final long serialVersionUID = 1L;
-
- public RequireException(String message) {
- super(message);
- }
-
}
View
10 src/com/jamesshore/finances/util/RequireException.java
@@ -0,0 +1,10 @@
+package com.jamesshore.finances.util;
+
+public class RequireException extends RuntimeException {
+ private static final long serialVersionUID = 1L;
+
+ public RequireException(String message) {
+ super(message);
+ }
+
+}

No commit comments for this range

Something went wrong with that request. Please try again.