1+ package com .blogspot .e_kanivets .moneytracker .report ;
2+
3+ import android .support .annotation .Nullable ;
4+
5+ import com .blogspot .e_kanivets .moneytracker .model .ExchangeRate ;
6+ import com .blogspot .e_kanivets .moneytracker .model .Period ;
7+ import com .blogspot .e_kanivets .moneytracker .model .Record ;
8+ import com .blogspot .e_kanivets .moneytracker .report .base .IExchangeRateProvider ;
9+ import com .blogspot .e_kanivets .moneytracker .report .base .IReport ;
10+
11+ import org .junit .After ;
12+ import org .junit .Before ;
13+ import org .junit .Test ;
14+
15+ import java .util .ArrayList ;
16+ import java .util .Date ;
17+ import java .util .List ;
18+
19+ import static org .junit .Assert .*;
20+
21+ /**
22+ * JUnit4 test case.
23+ * Created on 2/25/16.
24+ *
25+ * @author Evgenii Kanivets
26+ */
27+ public class ReportTest {
28+ private String currency ;
29+ private IExchangeRateProvider rateProvider ;
30+
31+ @ Before
32+ public void setUp () throws Exception {
33+ currency = "UAH" ;
34+ rateProvider = new TestProvider ();
35+ }
36+
37+ @ After
38+ public void tearDown () throws Exception {
39+ currency = null ;
40+ rateProvider = null ;
41+ }
42+
43+ @ Test
44+ public void testForNulls () throws Exception {
45+ IReport report ;
46+
47+ Period period = new Period (new Date (1 ), new Date ());
48+ List <Record > recordList = new ArrayList <>();
49+
50+ try {
51+ report = new Report (null , period , recordList , rateProvider );
52+ } catch (NullPointerException e ) {
53+ report = null ;
54+ }
55+
56+ assertNull (report );
57+
58+ try {
59+ report = new Report (currency , null , recordList , rateProvider );
60+ } catch (NullPointerException e ) {
61+ report = null ;
62+ }
63+
64+ assertNull (report );
65+
66+ try {
67+ report = new Report (currency , period , null , rateProvider );
68+ } catch (NullPointerException e ) {
69+ report = null ;
70+ }
71+
72+ assertNull (report );
73+
74+ try {
75+ report = new Report (currency , period , recordList , null );
76+ } catch (NullPointerException e ) {
77+ report = null ;
78+ }
79+
80+ assertNull (report );
81+
82+ try {
83+ report = new Report (null , null , null , null );
84+ } catch (NullPointerException e ) {
85+ report = null ;
86+ }
87+
88+ assertNull (report );
89+
90+ try {
91+ report = new Report (currency , period , recordList , rateProvider );
92+ } catch (NullPointerException e ) {
93+ report = null ;
94+ }
95+
96+ assertNotNull (report );
97+ }
98+
99+ @ Test
100+ public void testGetCurrency () throws Exception {
101+ Period period = new Period (new Date (1 ), new Date ());
102+ List <Record > recordList = new ArrayList <>();
103+
104+ IReport report = new Report (currency , period , recordList , rateProvider );
105+
106+ assertEquals (currency , report .getCurrency ());
107+
108+ currency = "KHI" ;
109+ report = new Report (currency , period , recordList , rateProvider );
110+
111+ assertEquals (currency , report .getCurrency ());
112+ }
113+
114+ @ Test
115+ public void testGetPeriod () throws Exception {
116+ Period period = new Period (new Date (1 ), new Date ());
117+ List <Record > recordList = new ArrayList <>();
118+
119+ IReport report = new Report (currency , period , recordList , rateProvider );
120+
121+ assertEquals (period , report .getPeriod ());
122+
123+ period = new Period (new Date (3 ), new Date (100 ));
124+ report = new Report (currency , period , recordList , rateProvider );
125+
126+ assertEquals (period , report .getPeriod ());
127+ }
128+
129+ @ Test
130+ public void testGetTotal () throws Exception {
131+ Period period = new Period (new Date (1 ), new Date ());
132+
133+ List <Record > recordList = new ArrayList <>();
134+ recordList .add (new Record (0 , Record .TYPE_INCOME , "1" , "1" , 10 , 1 , "USD" ));
135+ recordList .add (new Record (1 , Record .TYPE_EXPENSE , "2" , "1" , 2 , 2 , "UAH" ));
136+ recordList .add (new Record (2 , Record .TYPE_INCOME , "3" , "1" , 5 , 1 , "UAH" ));
137+ recordList .add (new Record (3 , Record .TYPE_EXPENSE , "4" , "1" , 10 , 2 , "USD" ));
138+
139+ IReport report = new Report (currency , period , recordList , rateProvider );
140+
141+ double expectedTotal = 10 * 4 - 2 + 5 - 10 * 4 ;
142+ assertEquals (expectedTotal , report .getTotal (), 0.0000000001 );
143+ }
144+
145+ @ Test
146+ public void testGetTotalIncome () throws Exception {
147+ Period period = new Period (new Date (1 ), new Date ());
148+
149+ List <Record > recordList = new ArrayList <>();
150+ recordList .add (new Record (0 , Record .TYPE_INCOME , "1" , "1" , 10 , 1 , "USD" ));
151+ recordList .add (new Record (1 , Record .TYPE_EXPENSE , "2" , "1" , 2 , 2 , "UAH" ));
152+ recordList .add (new Record (2 , Record .TYPE_INCOME , "3" , "1" , 5 , 1 , "UAH" ));
153+ recordList .add (new Record (3 , Record .TYPE_EXPENSE , "4" , "1" , 10 , 2 , "USD" ));
154+
155+ IReport report = new Report (currency , period , recordList , rateProvider );
156+
157+ double expectedTotal = 10 * 4 + 5 ;
158+ assertEquals (expectedTotal , report .getTotalIncome (), 0.0000000001 );
159+ }
160+
161+ @ Test
162+ public void testGetTotalExpense () throws Exception {
163+ Period period = new Period (new Date (1 ), new Date ());
164+
165+ List <Record > recordList = new ArrayList <>();
166+ recordList .add (new Record (0 , Record .TYPE_INCOME , "1" , "1" , 10 , 1 , "USD" ));
167+ recordList .add (new Record (1 , Record .TYPE_EXPENSE , "2" , "1" , 2 , 2 , "UAH" ));
168+ recordList .add (new Record (2 , Record .TYPE_INCOME , "3" , "1" , 5 , 1 , "UAH" ));
169+ recordList .add (new Record (3 , Record .TYPE_EXPENSE , "4" , "1" , 10 , 2 , "USD" ));
170+
171+ IReport report = new Report (currency , period , recordList , rateProvider );
172+
173+ double expectedTotal = -2 - 10 * 4 ;
174+ assertEquals (expectedTotal , report .getTotalExpense (), 0.0000000001 );
175+ }
176+
177+ @ Test
178+ public void testGetSummary () throws Exception {
179+
180+ }
181+
182+ private static class TestProvider implements IExchangeRateProvider {
183+
184+ @ Nullable
185+ @ Override
186+ public ExchangeRate getRate (@ Nullable Record record ) {
187+ if (record == null ) return null ;
188+
189+ String fromCurrency = record .getCurrency ();
190+ switch (fromCurrency ) {
191+ case "USD" :
192+ return new ExchangeRate (1 , "USD" , "UAH" , 4 );
193+
194+ case "AFN" :
195+ return new ExchangeRate (0 , "AFN" , "UAH" , 3 );
196+
197+ default :
198+ return null ;
199+
200+ }
201+ }
202+ }
203+ }
0 commit comments