|
6 | 6 | import com.path.android.jobqueue.Params; |
7 | 7 | import com.path.android.jobqueue.config.Configuration; |
8 | 8 | import com.path.android.jobqueue.di.DependencyInjector; |
| 9 | +import com.path.android.jobqueue.log.CustomLogger; |
9 | 10 | import com.path.android.jobqueue.test.jobs.DummyJob; |
10 | 11 | import static org.hamcrest.CoreMatchers.*; |
11 | 12 | import org.hamcrest.*; |
12 | 13 | import org.junit.Test; |
13 | 14 | import org.junit.runner.RunWith; |
14 | 15 | import org.robolectric.*; |
15 | 16 |
|
| 17 | +import java.util.concurrent.CountDownLatch; |
| 18 | +import java.util.concurrent.TimeUnit; |
16 | 19 | import java.util.concurrent.atomic.AtomicInteger; |
17 | 20 |
|
18 | 21 | @RunWith(RobolectricTestRunner.class) |
@@ -42,6 +45,55 @@ public void inject(BaseJob job) { |
42 | 45 | holder = getNextJobMethod(jobManager).invoke(); |
43 | 46 | MatcherAssert.assertThat("injection should be called for persistent job", holder.getBaseJob(), equalTo(injectedJobReference.getObject())); |
44 | 47 | MatcherAssert.assertThat("injection should be called two times for persistent job", injectionCallCount.get(), equalTo(3)); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testInjectorCrash() throws Exception { |
| 52 | + final String EXCEPTION_MESSAGE = "could not inject for whatever reason :)"; |
| 53 | + DependencyInjector dummyDependencyInjector = new DependencyInjector() { |
| 54 | + @Override |
| 55 | + public void inject(BaseJob baseJob) { |
| 56 | + throw new RuntimeException(EXCEPTION_MESSAGE); |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + final ObjectReference objectReference = new ObjectReference(); |
| 61 | + final CountDownLatch exceptionLatch = new CountDownLatch(1); |
| 62 | + CustomLogger customLogger = new CustomLogger() { |
| 63 | + @Override |
| 64 | + public boolean isDebugEnabled() { |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void d(String s, Object... objects) { |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void e(Throwable throwable, String s, Object... objects) { |
| 75 | + objectReference.setObject(throwable); |
| 76 | + exceptionLatch.countDown(); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void e(String s, Object... objects) { |
| 81 | + // |
| 82 | + } |
| 83 | + }; |
| 84 | + JobManager jobManager = createJobManager(new Configuration.Builder(Robolectric.application).injector(dummyDependencyInjector)); |
| 85 | + Throwable addException = null; |
| 86 | + try { |
| 87 | + jobManager.addJob(new DummyJob(new Params(0))); |
| 88 | + } catch (Throwable t) { |
| 89 | + addException = t; |
| 90 | + } |
| 91 | + MatcherAssert.assertThat("addJob should throw exception if dependency injector throws exception", addException, notNullValue()); |
| 92 | + jobManager.addJobInBackground(new DummyJob(new Params(0))); |
| 93 | + exceptionLatch.await(2, TimeUnit.SECONDS); |
| 94 | + MatcherAssert.assertThat("there should be a received exception", objectReference.getObject(), notNullValue()); |
| 95 | + MatcherAssert.assertThat("logged exception should be a runtime exception", objectReference.getObject(), instanceOf(RuntimeException.class)); |
| 96 | + MatcherAssert.assertThat("logged exception should have expected message", ((Throwable)objectReference.getObject()).getMessage(), is(EXCEPTION_MESSAGE)); |
45 | 97 |
|
46 | 98 | } |
47 | 99 | } |
0 commit comments