Skip to content

Commit 4b88fca

Browse files
authored
Merge pull request #55 from fracz/specs
Specification of the project
2 parents a76e8e3 + b9686ee commit 4b88fca

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package pl.edu.agh.mwo.invoice;
2+
3+
import java.math.BigDecimal;
4+
5+
import org.hamcrest.Matchers;
6+
import org.junit.Assert;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
import pl.edu.agh.mwo.invoice.Invoice;
11+
import pl.edu.agh.mwo.invoice.product.DairyProduct;
12+
import pl.edu.agh.mwo.invoice.product.OtherProduct;
13+
import pl.edu.agh.mwo.invoice.product.Product;
14+
import pl.edu.agh.mwo.invoice.product.TaxFreeProduct;
15+
16+
public class InvoiceTest {
17+
private Invoice invoice;
18+
19+
@Before
20+
public void createEmptyInvoiceForTheTest() {
21+
invoice = new Invoice();
22+
}
23+
24+
@Test
25+
public void testEmptyInvoiceHasEmptySubtotal() {
26+
Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getSubtotal()));
27+
}
28+
29+
@Test
30+
public void testEmptyInvoiceHasEmptyTaxAmount() {
31+
Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getTax()));
32+
}
33+
34+
@Test
35+
public void testEmptyInvoiceHasEmptyTotal() {
36+
Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getTotal()));
37+
}
38+
39+
@Test
40+
public void testInvoiceSubtotalWithTwoDifferentProducts() {
41+
Product onions = new TaxFreeProduct("Warzywa", new BigDecimal("10"));
42+
Product apples = new TaxFreeProduct("Owoce", new BigDecimal("10"));
43+
invoice.addProduct(onions);
44+
invoice.addProduct(apples);
45+
Assert.assertThat(new BigDecimal("20"), Matchers.comparesEqualTo(invoice.getSubtotal()));
46+
}
47+
48+
@Test
49+
public void testInvoiceSubtotalWithManySameProducts() {
50+
Product onions = new TaxFreeProduct("Warzywa", new BigDecimal("10"));
51+
invoice.addProduct(onions, 100);
52+
Assert.assertThat(new BigDecimal("1000"), Matchers.comparesEqualTo(invoice.getSubtotal()));
53+
}
54+
55+
@Test
56+
public void testInvoiceHasTheSameSubtotalAndTotalIfTaxIsZero() {
57+
Product taxFreeProduct = new TaxFreeProduct("Warzywa", new BigDecimal("199.99"));
58+
invoice.addProduct(taxFreeProduct);
59+
Assert.assertThat(invoice.getTotal(), Matchers.comparesEqualTo(invoice.getSubtotal()));
60+
}
61+
62+
@Test
63+
public void testInvoiceHasProperSubtotalForManyProducts() {
64+
invoice.addProduct(new TaxFreeProduct("Owoce", new BigDecimal("200")));
65+
invoice.addProduct(new DairyProduct("Maslanka", new BigDecimal("100")));
66+
invoice.addProduct(new OtherProduct("Wino", new BigDecimal("10")));
67+
Assert.assertThat(new BigDecimal("310"), Matchers.comparesEqualTo(invoice.getSubtotal()));
68+
}
69+
70+
@Test
71+
public void testInvoiceHasProperTaxValueForManyProduct() {
72+
// tax: 0
73+
invoice.addProduct(new TaxFreeProduct("Pampersy", new BigDecimal("200")));
74+
// tax: 8
75+
invoice.addProduct(new DairyProduct("Kefir", new BigDecimal("100")));
76+
// tax: 2.30
77+
invoice.addProduct(new OtherProduct("Piwko", new BigDecimal("10")));
78+
Assert.assertThat(new BigDecimal("10.30"), Matchers.comparesEqualTo(invoice.getTax()));
79+
}
80+
81+
@Test
82+
public void testInvoiceHasProperTotalValueForManyProduct() {
83+
// price with tax: 200
84+
invoice.addProduct(new TaxFreeProduct("Maskotki", new BigDecimal("200")));
85+
// price with tax: 108
86+
invoice.addProduct(new DairyProduct("Maslo", new BigDecimal("100")));
87+
// price with tax: 12.30
88+
invoice.addProduct(new OtherProduct("Chipsy", new BigDecimal("10")));
89+
Assert.assertThat(new BigDecimal("320.30"), Matchers.comparesEqualTo(invoice.getTotal()));
90+
}
91+
92+
@Test
93+
public void testInvoiceHasPropoerSubtotalWithQuantityMoreThanOne() {
94+
// 2x kubek - price: 10
95+
invoice.addProduct(new TaxFreeProduct("Kubek", new BigDecimal("5")), 2);
96+
// 3x kozi serek - price: 30
97+
invoice.addProduct(new DairyProduct("Kozi Serek", new BigDecimal("10")), 3);
98+
// 1000x pinezka - price: 10
99+
invoice.addProduct(new OtherProduct("Pinezka", new BigDecimal("0.01")), 1000);
100+
Assert.assertThat(new BigDecimal("50"), Matchers.comparesEqualTo(invoice.getSubtotal()));
101+
}
102+
103+
@Test
104+
public void testInvoiceHasPropoerTotalWithQuantityMoreThanOne() {
105+
// 2x chleb - price with tax: 10
106+
invoice.addProduct(new TaxFreeProduct("Chleb", new BigDecimal("5")), 2);
107+
// 3x chedar - price with tax: 32.40
108+
invoice.addProduct(new DairyProduct("Chedar", new BigDecimal("10")), 3);
109+
// 1000x pinezka - price with tax: 12.30
110+
invoice.addProduct(new OtherProduct("Pinezka", new BigDecimal("0.01")), 1000);
111+
Assert.assertThat(new BigDecimal("54.70"), Matchers.comparesEqualTo(invoice.getTotal()));
112+
}
113+
114+
@Test(expected = IllegalArgumentException.class)
115+
public void testInvoiceWithZeroQuantity() {
116+
invoice.addProduct(new TaxFreeProduct("Tablet", new BigDecimal("1678")), 0);
117+
}
118+
119+
@Test(expected = IllegalArgumentException.class)
120+
public void testInvoiceWithNegativeQuantity() {
121+
invoice.addProduct(new DairyProduct("Zsiadle mleko", new BigDecimal("5.55")), -1);
122+
}
123+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package pl.edu.agh.mwo.invoice.product;
2+
3+
import java.math.BigDecimal;
4+
5+
import org.hamcrest.Matchers;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
import pl.edu.agh.mwo.invoice.product.Product;
10+
11+
public class ProductTest {
12+
@Test
13+
public void testProductNameIsCorrect() {
14+
Product product = new OtherProduct("buty", new BigDecimal("100.0"));
15+
Assert.assertEquals("buty", product.getName());
16+
}
17+
18+
@Test
19+
public void testProductPriceAndTaxWithDefaultTax() {
20+
Product product = new OtherProduct("Ogorki", new BigDecimal("100.0"));
21+
Assert.assertThat(new BigDecimal("100"), Matchers.comparesEqualTo(product.getPrice()));
22+
Assert.assertThat(new BigDecimal("0.23"), Matchers.comparesEqualTo(product.getTaxPercent()));
23+
}
24+
25+
@Test
26+
public void testProductPriceAndTaxWithDairyProduct() {
27+
Product product = new DairyProduct("Szarlotka", new BigDecimal("100.0"));
28+
Assert.assertThat(new BigDecimal("100"), Matchers.comparesEqualTo(product.getPrice()));
29+
Assert.assertThat(new BigDecimal("0.08"), Matchers.comparesEqualTo(product.getTaxPercent()));
30+
}
31+
32+
@Test
33+
public void testPriceWithTax() {
34+
Product product = new DairyProduct("Oscypek", new BigDecimal("100.0"));
35+
Assert.assertThat(new BigDecimal("108"), Matchers.comparesEqualTo(product.getPriceWithTax()));
36+
}
37+
38+
@Test(expected = IllegalArgumentException.class)
39+
public void testProductWithNullName() {
40+
new OtherProduct(null, new BigDecimal("100.0"));
41+
}
42+
43+
@Test(expected = IllegalArgumentException.class)
44+
public void testProductWithEmptyName() {
45+
new TaxFreeProduct("", new BigDecimal("100.0"));
46+
}
47+
48+
@Test(expected = IllegalArgumentException.class)
49+
public void testProductWithNullPrice() {
50+
new DairyProduct("Banany", null);
51+
}
52+
53+
@Test(expected = IllegalArgumentException.class)
54+
public void testProductWithNegativePrice() {
55+
new TaxFreeProduct("Mandarynki", new BigDecimal("-1.00"));
56+
}
57+
}

0 commit comments

Comments
 (0)