Search in sources :

Example 1 with DependencyInjector

use of com.path.android.jobqueue.di.DependencyInjector in project android-priority-jobqueue by path.

the class InjectorTest method testInjector.

@Test
public void testInjector() throws Exception {
    Configuration.Builder builder = new Configuration.Builder(Robolectric.application);
    final JobManagerTestBase.ObjectReference injectedJobReference = new JobManagerTestBase.ObjectReference();
    final AtomicInteger injectionCallCount = new AtomicInteger(0);
    DependencyInjector dependencyInjector = new DependencyInjector() {

        @Override
        public void inject(BaseJob job) {
            injectedJobReference.setObject(job);
            injectionCallCount.incrementAndGet();
        }
    };
    builder.injector(dependencyInjector);
    JobManager jobManager = createJobManager(builder);
    jobManager.stop();
    jobManager.addJob(new DummyJob(new Params(4)));
    MatcherAssert.assertThat("injection should be called after adding a non-persistent job", injectionCallCount.get(), equalTo(1));
    jobManager.addJob(new DummyJob(new Params(1).persist()));
    MatcherAssert.assertThat("injection should be called after adding a persistent job", injectionCallCount.get(), equalTo(2));
    JobHolder holder = getNextJobMethod(jobManager).invoke();
    MatcherAssert.assertThat("injection should NOT be called for non persistent job", holder.getBaseJob(), not(injectedJobReference.getObject()));
    MatcherAssert.assertThat("injection should be called once for non persistent job", injectionCallCount.get(), equalTo(2));
    holder = getNextJobMethod(jobManager).invoke();
    MatcherAssert.assertThat("injection should be called for persistent job", holder.getBaseJob(), equalTo(injectedJobReference.getObject()));
    MatcherAssert.assertThat("injection should be called two times for persistent job", injectionCallCount.get(), equalTo(3));
}
Also used : DependencyInjector(com.path.android.jobqueue.di.DependencyInjector) Configuration(com.path.android.jobqueue.config.Configuration) BaseJob(com.path.android.jobqueue.BaseJob) Params(com.path.android.jobqueue.Params) JobManager(com.path.android.jobqueue.JobManager) JobHolder(com.path.android.jobqueue.JobHolder) DummyJob(com.path.android.jobqueue.test.jobs.DummyJob) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 2 with DependencyInjector

use of com.path.android.jobqueue.di.DependencyInjector in project android-priority-jobqueue by path.

the class InjectorTest method testInjectorCrash.

@Test
public void testInjectorCrash() throws Exception {
    final String EXCEPTION_MESSAGE = "could not inject for whatever reason :)";
    DependencyInjector dummyDependencyInjector = new DependencyInjector() {

        @Override
        public void inject(BaseJob baseJob) {
            throw new RuntimeException(EXCEPTION_MESSAGE);
        }
    };
    final ObjectReference objectReference = new ObjectReference();
    final CountDownLatch exceptionLatch = new CountDownLatch(1);
    CustomLogger customLogger = new CustomLogger() {

        @Override
        public boolean isDebugEnabled() {
            return false;
        }

        @Override
        public void d(String s, Object... objects) {
        }

        @Override
        public void e(Throwable throwable, String s, Object... objects) {
            objectReference.setObject(throwable);
            exceptionLatch.countDown();
        }

        @Override
        public void e(String s, Object... objects) {
        // 
        }
    };
    JobManager jobManager = createJobManager(new Configuration.Builder(Robolectric.application).injector(dummyDependencyInjector).customLogger(customLogger));
    Throwable addException = null;
    try {
        jobManager.addJob(new DummyJob(new Params(0)));
    } catch (Throwable t) {
        addException = t;
    }
    MatcherAssert.assertThat("addJob should throw exception if dependency injector throws exception", addException, notNullValue());
    jobManager.addJobInBackground(new DummyJob(new Params(0)));
    exceptionLatch.await(2, TimeUnit.SECONDS);
    MatcherAssert.assertThat("there should be a received exception", objectReference.getObject(), notNullValue());
    MatcherAssert.assertThat("logged exception should be a runtime exception", objectReference.getObject(), instanceOf(RuntimeException.class));
    MatcherAssert.assertThat("logged exception should have expected message", ((Throwable) objectReference.getObject()).getMessage(), is(EXCEPTION_MESSAGE));
}
Also used : DependencyInjector(com.path.android.jobqueue.di.DependencyInjector) Configuration(com.path.android.jobqueue.config.Configuration) BaseJob(com.path.android.jobqueue.BaseJob) Params(com.path.android.jobqueue.Params) CustomLogger(com.path.android.jobqueue.log.CustomLogger) JobManager(com.path.android.jobqueue.JobManager) CountDownLatch(java.util.concurrent.CountDownLatch) DummyJob(com.path.android.jobqueue.test.jobs.DummyJob) Test(org.junit.Test)

Aggregations

BaseJob (com.path.android.jobqueue.BaseJob)2 JobManager (com.path.android.jobqueue.JobManager)2 Params (com.path.android.jobqueue.Params)2 Configuration (com.path.android.jobqueue.config.Configuration)2 DependencyInjector (com.path.android.jobqueue.di.DependencyInjector)2 DummyJob (com.path.android.jobqueue.test.jobs.DummyJob)2 Test (org.junit.Test)2 JobHolder (com.path.android.jobqueue.JobHolder)1 CustomLogger (com.path.android.jobqueue.log.CustomLogger)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1