All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.googlecode.gwt.test.GwtStubberImpl Maven / Gradle / Ivy

There is a newer version: 0.63
Show newest version
package com.googlecode.gwt.test;

import org.mockito.internal.stubbing.StubberImpl;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.googlecode.gwt.test.mockito.GwtStubber;

class GwtStubberImpl extends StubberImpl implements GwtStubber {

   private static class FailureAnswer implements Answer {

      private final Throwable result;

      public FailureAnswer(Throwable result) {
         this.result = result;
      }

      @SuppressWarnings("unchecked")
      public T answer(InvocationOnMock invocation) {
         Object[] arguments = invocation.getArguments();
         AsyncCallback callback = (AsyncCallback) arguments[arguments.length - 1];
         callback.onFailure(result);
         return null;
      }

   }

   private static class SuccessAnswer implements Answer {

      private final T result;

      public SuccessAnswer(T result) {
         this.result = result;
      }

      @SuppressWarnings("unchecked")
      public T answer(InvocationOnMock invocation) {
         Object[] arguments = invocation.getArguments();
         AsyncCallback callback = (AsyncCallback) arguments[arguments.length - 1];
         callback.onSuccess(result);
         return null;
      }

   }

   public  GwtStubber doFailureCallback(final Throwable exception) {
      doAnswer(new FailureAnswer(exception));
      return this;
   }

   public  GwtStubber doSuccessCallback(T object) {
      doAnswer(new SuccessAnswer(object));
      return this;
   }

}