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

org.xj4.TestWrapperStatement Maven / Gradle / Ivy

/**
 * Copyright 2008 Peach Jean Solutions
 * 
 * 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.xj4;

import java.util.List;
import java.util.ArrayList;

import org.junit.internal.runners.model.MultipleFailureException;
import org.junit.runners.model.Statement;
import org.xj4.spi.TestWrapper;

/**
 * A statement that wraps another Statement with a list of TestWrappers.  First, the before method is called on all
 * wrappers, in order.  Then, if none of these methods has thrown an exception, the statement is invoked.  Afterwards,
 * the after method is called on all wrappers in reverse order.  Any exceptions generated during this process are
 * stored and rethrown as a MultipleFailureException after all after methods are invoked.
 */
class TestWrapperStatement extends Statement {

  private Statement statement;
  private List wrappers;

  public TestWrapperStatement(Statement statement, List wrappers) {
    this.statement = statement;
    this.wrappers = wrappers;
  }

  @Override
  public void evaluate() throws Throwable {
    List errors = new ArrayList();
    errors.clear();
    try {
      for(TestWrapper wrapper: wrappers) {
        try {
          wrapper.doBefore();
        } catch (Throwable e) {
          errors.add(e);
        }
      }
      if(errors.isEmpty()) {
        statement.evaluate();
      }
    } catch (Throwable e) {
      errors.add(e);
    } finally {
      for (TestWrapper wrapper: wrappers) {
        try {
          wrapper.doAfter();
        } catch (Throwable e) {
          errors.add(e);
        }
      }
    }
    if (errors.isEmpty()) {
      return;
    } else if (errors.size() == 1) {
      throw errors.get(0);
    } else {
      throw new MultipleFailureException(errors);
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy