Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010 the original author or authors.
   *
   * 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.callbackparams.sandbox;
 
 import java.util.List;
 import java.util.Map;

Author(s):
Henrik Kaipe
 
 public class ExpectedExceptionSweeps extends AdaptiveRule {
 
     private Map<Class<? extends Throwable>, Collector<?>> sweeps =
             new LinkedHashMap();
 
     public static ExpectedExceptionSweeps create() {
         return new ExpectedExceptionSweeps();
     }
 
     public ExpectedExceptionSweeps sweep(Collector<?> msgCollector,
             Class<? extends ThrowableexceptionType) {
         .put(exceptionTypemsgCollector);
         return this;
     }
 
     public Object evaluate(TestRun testrunthrows Throwable {
         final Object returnValue;
         try {
             returnValue = testrun.executeTestMethod();
 
         } catch (Throwable thrown) {
             assertExpectedWasThrown(thrown);
             return null;
         }
         assertNothingWasExpected();
         return returnValue;
     }
 
     private void assertExpectedWasThrown(Throwable actualthrows Throwable {
         List<StringexpectedMessages = new ArrayList();
         Class<? extends ThrowableexpectedClass =
                 collectExpectations(expectedMessages);
 
         if (null == expectedClass
                 || false == expectedClass.isAssignableFrom(actual.getClass())) {
             throw actual;
 
         } else if (expectedMessages.contains(actual.getMessage())) {
             /* ALLES GUT */
             return;
 
         } else {
             throw new AssertionError("Message of expected exception of "
                     + expectedClass + " was supposed to be <one of "
                     + expectedMessages + "> but was <"
                     + actual.getMessage() + '>');
         }
     }
 
     private void assertNothingWasExpected() {
         Class<?> expected = collectExpectations(new ArrayList<String>());
         if (null != expected) {
             throw new AssertionError("Expected testrun exception of class <"
                     + expected.getName()
                     + "> but no exception was thrown");
         }
     }
 
     private Class<? extends ThrowablecollectExpectations(
             Collection<Stringmessages) {
         assert messages.isEmpty();
 
         for (Class<? extends Throwableexpected : .keySet()) {
             .get(expected).collect(messages);
             if (false == messages.isEmpty()) {
                 return expected;
             }
         }
 
         return null;
     }
New to GrepCode? Check out our FAQ X