Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Roboelectric complaining about not closing resources. #42

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ dependencies {

testImplementation 'org.robolectric:robolectric:4.11.1'
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.assertj:assertj-core:1.7.1'
testImplementation 'org.assertj:assertj-core:3.22.0'
}

def androidEspressoVersion = "3.5.1"
Expand Down
108 changes: 105 additions & 3 deletions app/src/main/java/org/gnucash/android/app/GnuCashApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.gnucash.android.service.ScheduledActionService;
import org.gnucash.android.ui.settings.PreferenceActivity;

import java.io.IOException;
import java.util.Currency;
import java.util.Locale;

Expand Down Expand Up @@ -131,14 +132,21 @@ public void onCreate() {
mBooksDbAdapter = new BooksDbAdapter(bookDbHelper.getWritableDatabase());

initializeDatabaseAdapters(context);
setDefaultCurrencyCode(getDefaultCurrencyCode());
setDefaultCurrencyCode(context, getDefaultCurrencyCode());

StethoUtils.install(this);
}

@Override
public void onTerminate() {
super.onTerminate();
destroyDatabaseAdapters();
}

/**
* Initialize database adapter singletons for use in the application
* This method should be called every time a new book is opened
* @param context the context.
*/
public static void initializeDatabaseAdapters(Context context) {
if (mDbHelper != null) { //close if open
Expand Down Expand Up @@ -171,6 +179,83 @@ public static void initializeDatabaseAdapters(Context context) {
mBudgetsDbAdapter = new BudgetsDbAdapter(mainDb, mBudgetAmountsDbAdapter, mRecurrenceDbAdapter);
}

private static void destroyDatabaseAdapters() {
if (mSplitsDbAdapter != null) {
try {
mSplitsDbAdapter.close();
mSplitsDbAdapter = null;
} catch (IOException ignore) {
}
}
if (mTransactionsDbAdapter != null) {
try {
mTransactionsDbAdapter.close();
mTransactionsDbAdapter = null;
} catch (IOException ignore) {
}
}
if (mAccountsDbAdapter != null) {
try {
mAccountsDbAdapter.close();
mAccountsDbAdapter = null;
} catch (IOException ignore) {
}
}
if (mRecurrenceDbAdapter != null) {
try {
mRecurrenceDbAdapter.close();
mRecurrenceDbAdapter = null;
} catch (IOException ignore) {
}
}
if (mScheduledActionDbAdapter != null) {
try {
mScheduledActionDbAdapter.close();
mScheduledActionDbAdapter = null;
} catch (IOException ignore) {
}
}
if (mPricesDbAdapter != null) {
try {
mPricesDbAdapter.close();
mPricesDbAdapter = null;
} catch (IOException ignore) {
}
}
if (mCommoditiesDbAdapter != null) {
try {
mCommoditiesDbAdapter.close();
mCommoditiesDbAdapter = null;
} catch (IOException ignore) {
}
}
if (mBudgetAmountsDbAdapter != null) {
try {
mBudgetAmountsDbAdapter.close();
mBudgetAmountsDbAdapter = null;
} catch (IOException ignore) {
}
}
if (mBudgetsDbAdapter != null) {
try {
mBudgetsDbAdapter.close();
mBudgetsDbAdapter = null;
} catch (IOException ignore) {
}
}
if (mBooksDbAdapter != null) {
try {
mBooksDbAdapter.close();
mBooksDbAdapter = null;
} catch (IOException ignore) {
}
}
if (mDbHelper != null) {
mDbHelper.close();
mDbHelper = null;
}
}

public static AccountsDbAdapter getAccountsDbAdapter() {
return mAccountsDbAdapter;
}
Expand Down Expand Up @@ -299,8 +384,25 @@ public static String getDefaultCurrencyCode() {
* @see #getDefaultCurrencyCode()
*/
public static void setDefaultCurrencyCode(@NonNull String currencyCode) {
PreferenceManager.getDefaultSharedPreferences(context).edit()
.putString(getAppContext().getString(R.string.key_default_currency), currencyCode)
setDefaultCurrencyCode(context, currencyCode);
}

/**
* Sets the default currency for the application in all relevant places:
* <ul>
* <li>Shared preferences</li>
* <li>{@link Money#DEFAULT_CURRENCY_CODE}</li>
* <li>{@link Commodity#DEFAULT_COMMODITY}</li>
* </ul>
*
* @param context the context.
* @param currencyCode ISO 4217 currency code
* @see #getDefaultCurrencyCode()
*/
public static void setDefaultCurrencyCode(@NonNull Context context, @NonNull String currencyCode) {
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putString(context.getString(R.string.key_default_currency), currencyCode)
.apply();
Money.DEFAULT_CURRENCY_CODE = currencyCode;
Commodity.DEFAULT_COMMODITY = mCommoditiesDbAdapter.getCommodity(currencyCode);
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/org/gnucash/android/db/BookDbHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class BookDbHelper extends SQLiteOpenHelper {

public static final String LOG_TAG = "BookDbHelper";

private Context mContext;
private final Context mContext;

/**
* Create the books table
Expand Down Expand Up @@ -117,6 +117,11 @@ public void onCreate(SQLiteDatabase db) {
new TransactionsDbAdapter(mainDb, new SplitsDbAdapter(mainDb)));

String rootAccountUID = accountsDbAdapter.getOrCreateGnuCashRootAccountUID();
try {
accountsDbAdapter.close();
helper.close();
} catch (IOException ignore) {
}
book.setRootAccountUID(rootAccountUID);
book.setActive(true);
insertBook(db, book);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import org.gnucash.android.model.BaseModel;
import org.gnucash.android.util.TimestampHelper;

import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -44,7 +46,7 @@
*
* @author Ngewi Fet <[email protected]>
*/
public abstract class DatabaseAdapter<Model extends BaseModel> {
public abstract class DatabaseAdapter<Model extends BaseModel> implements Closeable {
/**
* Tag for logging
*/
Expand Down Expand Up @@ -833,4 +835,23 @@ public void enableForeignKey(boolean enable) {
public void endTransaction() {
mDb.endTransaction();
}

@Override
public void close() throws IOException {
if (mInsertStatement != null) {
mInsertStatement.close();
mInsertStatement = null;
}
if (mReplaceStatement != null) {
mReplaceStatement.close();
mReplaceStatement = null;
}
if (mUpdateStatement != null) {
mUpdateStatement.close();
mUpdateStatement = null;
}
if (mDb.isOpen()) {
mDb.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ public static void createDefaultAccounts(final String currencyCode, final Activi
@Override
public void onTaskComplete() {
AccountsDbAdapter.getInstance().updateAllAccounts(DatabaseSchema.AccountEntry.COLUMN_CURRENCY, currencyCode);
GnuCashApplication.setDefaultCurrencyCode(currencyCode);
GnuCashApplication.setDefaultCurrencyCode(activity, currencyCode);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private void selectExportFile() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference.getKey().equals(getString(R.string.key_default_currency))) {
GnuCashApplication.setDefaultCurrencyCode(newValue.toString());
GnuCashApplication.setDefaultCurrencyCode(preference.getContext(), newValue.toString());
String fullname = CommoditiesDbAdapter.getInstance().getCommodity(newValue.toString()).getFullname();
preference.setSummary(fullname);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public boolean onPreferenceClick(Preference preference) {
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.setSummary(newValue.toString());
if (preference.getKey().equals(getString(R.string.key_default_currency))) {
GnuCashApplication.setDefaultCurrencyCode(newValue.toString());
GnuCashApplication.setDefaultCurrencyCode(preference.getContext(), newValue.toString());
}

if (preference.getKey().equals(getString(R.string.key_default_export_email))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void onClick(View view) {
}
}

GnuCashApplication.setDefaultCurrencyCode(mCurrencyCode);
GnuCashApplication.setDefaultCurrencyCode(view.getContext(), mCurrencyCode);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(FirstRunWizardActivity.this);
SharedPreferences.Editor preferenceEditor = preferences.edit();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ public void setUp() throws Exception {
initAdapters(null);
}

@After
public void after() throws Exception {
mAccountsDbAdapter.close();
mCommoditiesDbAdapter.close();
mSplitsDbAdapter.close();
mTransactionsDbAdapter.close();
}

/**
* Initialize database adapters for a specific book.
* This method should be called everytime a new book is loaded into the database
Expand Down
Loading