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

Allow easier testing #680

Merged
merged 1 commit into from
Aug 11, 2018
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
7 changes: 4 additions & 3 deletions acra-core/src/main/java/org/acra/ACRA.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,10 @@ public static void init(@NonNull Application app, @NonNull CoreConfiguration con
}

if (isInitialised()) {
log.w(LOG_TAG, "ACRA#init called more than once. Won't do anything more.");
return;
log.w(LOG_TAG, "ACRA#init called more than once. This might have unexpected side effects. Doing this outside of tests is discouraged.");
if(DEV_LOGGING) log.d(LOG_TAG, "Removing old ACRA config...");
((ErrorReporterImpl) errorReporterSingleton).unregister();
errorReporterSingleton = StubCreator.createErrorReporterStub();
}

//noinspection ConstantConditions
Expand Down Expand Up @@ -236,7 +238,6 @@ public static void init(@NonNull Application app, @NonNull CoreConfiguration con
/**
* @return true is ACRA has been initialised.
*/
@SuppressWarnings("unused")
public static boolean isInitialised() {
return errorReporterSingleton instanceof ErrorReporterImpl;
}
Expand Down
10 changes: 5 additions & 5 deletions acra-core/src/main/java/org/acra/builder/ReportExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public final void execute(@NonNull final ReportBuilder reportBuilder) {
if (!administrator.shouldStartCollecting(context, config, reportBuilder)) {
blockingAdministrator = administrator;
}
} catch (Throwable t) {
} catch (Exception t) {
ACRA.log.w(LOG_TAG, "ReportingAdministrator " + administrator.getClass().getName() + " threw exception", t);
}
}
Expand All @@ -142,7 +142,7 @@ public final void execute(@NonNull final ReportBuilder reportBuilder) {
if (!administrator.shouldSendReport(context, config, crashReportData)) {
blockingAdministrator = administrator;
}
} catch (Throwable t) {
} catch (Exception t) {
ACRA.log.w(LOG_TAG, "ReportingAdministrator " + administrator.getClass().getName() + " threw exception", t);
}
}
Expand All @@ -157,7 +157,7 @@ public final void execute(@NonNull final ReportBuilder reportBuilder) {
if (!administrator.shouldFinishActivity(context, config, lastActivityManager)) {
finishActivity = false;
}
} catch (Throwable t) {
} catch (Exception t) {
ACRA.log.w(LOG_TAG, "ReportingAdministrator " + administrator.getClass().getName() + " threw exception", t);
}
}
Expand Down Expand Up @@ -185,7 +185,7 @@ public final void execute(@NonNull final ReportBuilder reportBuilder) {
if (ACRA.DEV_LOGGING) ACRA.log.d(LOG_TAG, "Not sending crash report because of ReportingAdministrator " + blockingAdministrator.getClass().getName());
try {
blockingAdministrator.notifyReportDropped(context, config);
} catch (Throwable t) {
} catch (Exception t) {
ACRA.log.w(LOG_TAG, "ReportingAdministrator " + blockingAdministrator.getClass().getName() + " threw exeption", t);
}
}
Expand All @@ -198,7 +198,7 @@ public final void execute(@NonNull final ReportBuilder reportBuilder) {
if (!administrator.shouldKillApplication(context, config, reportBuilder, crashReportData)) {
endApplication = false;
}
} catch (Throwable t) {
} catch (Exception t) {
ACRA.log.w(LOG_TAG, "ReportingAdministrator " + administrator.getClass().getName() + " threw exception", t);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public final void collect(@NonNull Context context, @NonNull CoreConfiguration c
if (shouldCollect(context, config, field, reportBuilder)) {
collect(field, context, config, reportBuilder, target);
}
} catch (Throwable t) {
} catch (Exception t) {
target.put(field, (String) null);
throw new CollectorException("Error while retrieving " + field.name() + " data:" + t.getMessage(), t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ public CrashReportDataFactory(@NonNull Context context, @NonNull CoreConfigurati
Collector.Order o2;
try {
o1 = c1.getOrder();
} catch (Throwable t) {
} catch (Exception t) {
o1 = Collector.Order.NORMAL;
}
try {
o2 = c2.getOrder();
} catch (Throwable t) {
} catch (Exception t) {
o2 = Collector.Order.NORMAL;
}
return o1.ordinal() - o2.ordinal();
Expand All @@ -89,7 +89,7 @@ public CrashReportData createCrashData(@NonNull final ReportBuilder builder) {
if(ACRA.DEV_LOGGING)ACRA.log.d(LOG_TAG, "Collector " + collector.getClass().getName() + " completed");
}catch (CollectorException e){
ACRA.log.w(LOG_TAG, e);
}catch (Throwable t) {
}catch (Exception t) {
ACRA.log.e(LOG_TAG, "Error in collector " + collector.getClass().getSimpleName(), t);
}
}));
Expand All @@ -113,7 +113,7 @@ public void collectStartUp() {
//catch absolutely everything possible here so no collector obstructs the others
try {
((ApplicationStartupCollector) collector).collectApplicationStartUp(context, config);
} catch (Throwable t) {
} catch (Exception t) {
ACRA.log.w(ACRA.LOG_TAG, collector.getClass().getSimpleName() + " failed to collect its startup data", t);
}
}
Expand Down
4 changes: 2 additions & 2 deletions acra-core/src/main/java/org/acra/legacy/ReportConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ void convert() {
//reports without these keys are probably invalid
IOUtils.deleteFile(report);
}
} catch (Throwable e) {
} catch (Exception e) {
try {
//If this succeeds the report has already been converted, happens e.g. on preference clear.
persister.load(report);
if (ACRA.DEV_LOGGING) ACRA.log.d(LOG_TAG, "Tried to convert already converted report file " + report.getPath() + ". Ignoring");
} catch (Throwable t) {
} catch (Exception t) {
//File matches neither of the known formats, remove it.
ACRA.log.w(LOG_TAG, "Unable to read report file " + report.getPath() + ". Deleting", e);
IOUtils.deleteFile(report);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class ErrorReporterImpl implements Thread.UncaughtExceptionHandler, Share
private final ReportExecutor reportExecutor;
private final Map<String, String> customData = new HashMap<>();
private final SchedulerStarter schedulerStarter;
private final UncaughtExceptionHandler defaultExceptionHandler;


/**
Expand All @@ -76,7 +77,6 @@ public ErrorReporterImpl(@NonNull Application context, @NonNull CoreConfiguratio
final CrashReportDataFactory crashReportDataFactory = new CrashReportDataFactory(context, config);
crashReportDataFactory.collectStartUp();

final Thread.UncaughtExceptionHandler defaultExceptionHandler;
defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);

Expand Down Expand Up @@ -157,7 +157,7 @@ public void uncaughtException(@Nullable Thread t, @NonNull Throwable e) {
.endApplication()
.build(reportExecutor);

} catch (Throwable fatality) {
} catch (Exception fatality) {
// ACRA failed. Prevent any recursive call to ACRA.uncaughtException(), let the native reporter do its job.
ACRA.log.e(LOG_TAG, "ACRA failed to capture the error - handing off to native error reporter", fatality);
reportExecutor.handReportToDefaultExceptionHandler(t, e);
Expand Down Expand Up @@ -222,4 +222,8 @@ public void onSharedPreferenceChanged(@NonNull SharedPreferences sharedPreferenc
setEnabled(SharedPreferencesFactory.shouldEnableACRA(sharedPreferences));
}
}

public void unregister() {
Thread.setDefaultUncaughtExceptionHandler(defaultExceptionHandler);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public boolean hasPermission(@NonNull String permission) {

try {
return pm.checkPermission(permission, context.getPackageName()) == PackageManager.PERMISSION_GRANTED;
} catch (Throwable e) {
} catch (Exception e) {
// To catch RuntimeException("Package manager has died") that can occur on some version of Android,
// when the remote PackageManager is unavailable. I suspect this sometimes occurs when the App is being reinstalled.
return false;
Expand All @@ -84,7 +84,7 @@ public PackageInfo getPackageInfo() {
} catch (PackageManager.NameNotFoundException e) {
ACRA.log.w(LOG_TAG, "Failed to find PackageInfo for current App : " + context.getPackageName());
return null;
} catch (Throwable e) {
} catch (Exception e) {
// To catch RuntimeException("Package manager has died") that can occur on some version of Android,
// when the remote PackageManager is unavailable. I suspect this sometimes occurs when the App is being reinstalled.
return null;
Expand Down
76 changes: 76 additions & 0 deletions acra-core/src/test/java/org/acra/ACRATest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2018
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.acra;

import android.app.Application;
import android.content.Context;
import android.support.annotation.NonNull;
import org.acra.builder.ReportBuilder;
import org.acra.collector.StacktraceCollector;
import org.acra.config.CoreConfiguration;
import org.acra.config.CoreConfigurationBuilder;
import org.acra.config.ReportingAdministrator;
import org.acra.data.CrashReportData;
import org.acra.plugins.SimplePluginLoader;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;

import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.*;

/**
* @author lukas
* @since 02.07.18
*/
@RunWith(RobolectricTestRunner.class)
public class ACRATest {

@Test
public void init() {
Application application = RuntimeEnvironment.application;
CoreConfigurationBuilder builder = new CoreConfigurationBuilder(application).setPluginLoader(new SimplePluginLoader(StacktraceCollector.class, TestAdministrator.class));
ACRA.init(application, builder);
ACRA.getErrorReporter().handleException(new RuntimeException());
}

@Test(expected = AssertionError.class)
public void failing() {
Application application = RuntimeEnvironment.application;
CoreConfigurationBuilder builder = new CoreConfigurationBuilder(application).setPluginLoader(new SimplePluginLoader(FailingTestAdministrator.class));
ACRA.init(application, builder);
ACRA.getErrorReporter().handleException(new RuntimeException());
}

public static class TestAdministrator implements ReportingAdministrator {
@Override
public boolean shouldSendReport(@NonNull Context context, @NonNull CoreConfiguration config, @NonNull CrashReportData crashReportData) {
assertTrue(crashReportData.containsKey(ReportField.STACK_TRACE));
assertThat(crashReportData.getString(ReportField.STACK_TRACE), containsString("RuntimeException"));
return false;
}
}

public static class FailingTestAdministrator implements ReportingAdministrator {
@Override
public boolean shouldStartCollecting(@NonNull Context context, @NonNull CoreConfiguration config, @NonNull ReportBuilder reportBuilder) {
fail("Intended failure to test if assertions work");
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void onReceive(@NonNull Context context, @NonNull Intent intent) {
}
}

} catch (Throwable t) {
} catch (Exception t) {
ACRA.log.e(LOG_TAG, "Failed to handle notification action", t);
}
}
Expand Down