Home | History | Annotate | Download | only in internal
      1 /*
      2  * Copyright 2001-2009 OFFIS, Tammo Freese
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package org.easymock.internal;
     17 
     18 import java.io.Serializable;
     19 import java.lang.reflect.InvocationTargetException;
     20 
     21 import org.easymock.IAnswer;
     22 
     23 public class Result implements IAnswer<Object>, Serializable {
     24 
     25     private static final long serialVersionUID = 5476251941213917681L;
     26 
     27     private final IAnswer<?> value;
     28 
     29     private final boolean shouldFillInStackTrace;
     30 
     31     private Result(IAnswer<?> value, boolean shouldFillInStackTrace) {
     32         this.value = value;
     33         this.shouldFillInStackTrace = shouldFillInStackTrace;
     34     }
     35 
     36     public static Result createThrowResult(final Throwable throwable) {
     37         class ThrowingAnswer implements IAnswer<Object>, Serializable {
     38 
     39             private static final long serialVersionUID = -332797751209289222L;
     40 
     41             public Object answer() throws Throwable {
     42                 throw throwable;
     43             }
     44 
     45             @Override
     46             public String toString() {
     47                 return "Answer throwing " + throwable;
     48             }
     49         }
     50         return new Result(new ThrowingAnswer(), true);
     51     }
     52 
     53     public static Result createReturnResult(final Object value) {
     54         class ReturningAnswer implements IAnswer<Object>, Serializable {
     55 
     56             private static final long serialVersionUID = 6973893913593916866L;
     57 
     58             public Object answer() throws Throwable {
     59                 return value;
     60             }
     61 
     62             @Override
     63             public String toString() {
     64                 return "Answer returning " + value;
     65             }
     66         }
     67         return new Result(new ReturningAnswer(), true);
     68     }
     69 
     70     public static Result createDelegatingResult(final Object value) {
     71         class DelegatingAnswer implements IAnswer<Object>, Serializable {
     72 
     73             private static final long serialVersionUID = -5699326678580460103L;
     74 
     75             public Object answer() throws Throwable {
     76                 Invocation invocation = LastControl.getCurrentInvocation();
     77                 try {
     78                     return invocation.getMethod().invoke(value,
     79                             invocation.getArguments());
     80                 } catch (IllegalArgumentException e) {
     81                     throw new IllegalArgumentException("Delegation to object ["
     82                             + String.valueOf(value)
     83                             + "] is not implementing the mocked method ["
     84                             + invocation.getMethod() + "]", e);
     85                 } catch (InvocationTargetException e) {
     86                     throw e.getCause();
     87                 }
     88             }
     89 
     90             @Override
     91             public String toString() {
     92                 return "Delegated to " + value;
     93             }
     94         }
     95         return new Result(new DelegatingAnswer(), false);
     96     }
     97 
     98     public static Result createAnswerResult(IAnswer<?> answer) {
     99         return new Result(answer, false);
    100     }
    101 
    102     public Object answer() throws Throwable {
    103         return value.answer();
    104     }
    105 
    106     public boolean shouldFillInStackTrace() {
    107         return shouldFillInStackTrace;
    108     }
    109 
    110     @Override
    111     public String toString() {
    112         return value.toString();
    113     }
    114 }
    115