001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.tests.library.driver; 020 021import org.apache.reef.driver.task.FailedTask; 022import org.apache.reef.tests.library.exceptions.DriverSideFailure; 023import org.apache.reef.tests.library.exceptions.ExpectedTaskException; 024import org.apache.reef.util.Exceptions; 025import org.apache.reef.util.Optional; 026import org.apache.reef.wake.EventHandler; 027 028import javax.inject.Inject; 029 030/** 031 * A handler for FailedTask that will throw a DriverSideFailure unless the FailedTask was triggered by an 032 * ExpectedTaskException in the Task. 033 */ 034public final class ExpectedTaskFailureHandler implements EventHandler<FailedTask> { 035 036 @Inject 037 public ExpectedTaskFailureHandler() { 038 } 039 040 /** 041 * Checks whether the FailedTask was caused by a ExpectedTaskException. 042 * 043 * @param failedTask 044 * @throws org.apache.reef.tests.library.exceptions.DriverSideFailure if the FailedTask wasn't triggered by a 045 * ExpectedTaskException 046 */ 047 048 @Override 049 public void onNext(final FailedTask failedTask) { 050 final Optional<Throwable> reasonOptional = failedTask.getReason(); 051 if (!reasonOptional.isPresent()) { 052 throw new DriverSideFailure("Received a FailedTask, but it did not contain an exception."); 053 } else if (!(Exceptions.getUltimateCause(reasonOptional.get()) instanceof ExpectedTaskException)) { 054 throw new DriverSideFailure("Received a FailedTask, but the ExpectedTaskException isn't the ultimate cause.", 055 reasonOptional.get()); 056 } 057 failedTask.getActiveContext().get().close(); 058 } 059}