1616package org .gnucash .android .test .unit .export ;
1717
1818import android .database .sqlite .SQLiteDatabase ;
19+ import android .support .annotation .NonNull ;
1920
2021import org .gnucash .android .app .GnuCashApplication ;
2122import org .gnucash .android .db .BookDbHelper ;
4546import java .io .FileReader ;
4647import java .io .IOException ;
4748import java .util .List ;
49+ import java .util .zip .ZipFile ;
4850
4951import static org .assertj .core .api .Assertions .assertThat ;
5052
@@ -83,7 +85,7 @@ public void testWithNoTransactionsToExport_shouldNotCreateAnyFile(){
8385 /**
8486 * Test that QIF files are generated
8587 */
86- //// FIXME: 20.04.2017 Test failing with NPE
88+ @ Test
8789 public void testGenerateQIFExport (){
8890 AccountsDbAdapter accountsDbAdapter = new AccountsDbAdapter (mDb );
8991
@@ -109,10 +111,11 @@ public void testGenerateQIFExport(){
109111 }
110112
111113 /**
112- * Test that when more than one currency is in use, multiple QIF files will be generated
114+ * Test that when more than one currency is in use, a zip with multiple QIF files
115+ * will be generated
113116 */
114- //// FIXME: 20.04.2017 test failing with NPE
115- public void multiCurrencyTransactions_shouldResultInMultipleQifFiles () {
117+ // @Test Fails randomly. Sometimes it doesn't split the QIF.
118+ public void multiCurrencyTransactions_shouldResultInMultipleZippedQifFiles () throws IOException {
116119 AccountsDbAdapter accountsDbAdapter = new AccountsDbAdapter (mDb );
117120
118121 Account account = new Account ("Basic Account" , Commodity .getInstance ("EUR" ));
@@ -139,52 +142,56 @@ public void multiCurrencyTransactions_shouldResultInMultipleQifFiles(){
139142 QifExporter qifExporter = new QifExporter (exportParameters , mDb );
140143 List <String > exportedFiles = qifExporter .generateExport ();
141144
142- assertThat (exportedFiles ).hasSize (2 );
145+ assertThat (exportedFiles ).hasSize (1 );
143146 File file = new File (exportedFiles .get (0 ));
144- assertThat (file ).exists ().hasExtension ("qif " );
145- assertThat (file . length ()).isGreaterThan ( 0L );
147+ assertThat (file ).exists ().hasExtension ("zip " );
148+ assertThat (new ZipFile ( file ). size ()).isEqualTo ( 2 );
146149 }
147150
148- //@Test
149- public void description_and_memo_field_test () {
150- // arrange
151-
151+ /**
152+ * Test that the memo and description fields of transactions are exported.
153+ */
154+ @ Test
155+ public void memoAndDescription_shouldBeExported () throws IOException {
152156 String expectedDescription = "my description" ;
153157 String expectedMemo = "my memo" ;
154158
155159 AccountsDbAdapter accountsDbAdapter = new AccountsDbAdapter (mDb );
160+
156161 Account account = new Account ("Basic Account" );
157162 Transaction transaction = new Transaction ("One transaction" );
163+ transaction .addSplit (new Split (Money .createZeroInstance ("EUR" ), account .getUID ()));
158164 transaction .setDescription (expectedDescription );
159165 transaction .setNote (expectedMemo );
160166 account .addTransaction (transaction );
167+
161168 accountsDbAdapter .addRecord (account );
162169
163170 ExportParams exportParameters = new ExportParams (ExportFormat .QIF );
164171 exportParameters .setExportStartTime (TimestampHelper .getTimestampFromEpochZero ());
165172 exportParameters .setExportTarget (ExportParams .ExportTarget .SD_CARD );
166173 exportParameters .setDeleteTransactionsAfterExport (false );
167174
168- // act
169-
170175 QifExporter qifExporter = new QifExporter (exportParameters , mDb );
171176 List <String > exportedFiles = qifExporter .generateExport ();
172177
173- // assert
174-
175178 assertThat (exportedFiles ).hasSize (1 );
176179 File file = new File (exportedFiles .get (0 ));
180+ String fileContent = readFileContent (file );
177181 assertThat (file ).exists ().hasExtension ("qif" );
178- StringBuilder fileContentsBuilder = new StringBuilder ();
179- try {
180- BufferedReader reader = new BufferedReader (new FileReader (file ));
181- fileContentsBuilder .append (reader .readLine ());
182- } catch (IOException e ) {
183- e .printStackTrace ();
184- }
185- // todo: check the description & memo fields.
186- String fileContent = fileContentsBuilder .toString ();
187182 assertThat (fileContent .contains (expectedDescription ));
188183 assertThat (fileContent .contains (expectedMemo ));
189184 }
185+
186+ @ NonNull
187+ public String readFileContent (File file ) throws IOException {
188+ StringBuilder fileContentsBuilder = new StringBuilder ();
189+ BufferedReader reader = new BufferedReader (new FileReader (file ));
190+ String line ;
191+ while ((line = reader .readLine ()) != null ) {
192+ fileContentsBuilder .append (line ).append ('\n' );
193+ }
194+
195+ return fileContentsBuilder .toString ();
196+ }
190197}
0 commit comments