1b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot// Copyright 2010 Google Inc. All Rights Reserved. 2b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 3b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotpackage org.junit.runners.model; 4b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 5b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport java.util.ArrayList; 6b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport java.util.Collections; 7b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport java.util.List; 8b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 9b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot/** 10b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * Collects multiple {@code Throwable}s into one exception. 11b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot */ 12b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotpublic class MultipleFailureException extends Exception { 13b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot private static final long serialVersionUID= 1L; 14b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 15b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot private final List<Throwable> fErrors; 16b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 17b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public MultipleFailureException(List<Throwable> errors) { 18b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot fErrors= new ArrayList<Throwable>(errors); 19b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 20b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 21b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public List<Throwable> getFailures() { 22b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot return Collections.unmodifiableList(fErrors); 23b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 24b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 25b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot @Override 26b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public String getMessage() { 27b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot StringBuilder sb = new StringBuilder( 28b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot String.format("There were %d errors:", fErrors.size())); 29b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot for (Throwable e : fErrors) { 30b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot sb.append(String.format("\n %s(%s)", e.getClass().getName(), e.getMessage())); 31b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 32b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot return sb.toString(); 33b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 34b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 35b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot /** 36b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * Asserts that a list of throwables is empty. If it isn't empty, 37b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * will throw {@link MultipleFailureException} (if there are 38b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * multiple throwables in the list) or the first element in the list 39b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * (if there is only one element). 40b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * 41b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * @param errors list to check 42b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * @throws Throwable if the list is not empty 43b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot */ 44b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot @SuppressWarnings("deprecation") 45b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public static void assertEmpty(List<Throwable> errors) throws Throwable { 46b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot if (errors.isEmpty()) 47b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot return; 48b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot if (errors.size() == 1) 49b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot throw errors.get(0); 50b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 51b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot /* 52b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * Many places in the code are documented to throw 53b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * org.junit.internal.runners.model.MultipleFailureException. 54b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * That class now extends this one, so we throw the internal 55b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * exception in case developers have tests that catch 56b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * MultipleFailureException. 57b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot */ 58b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot throw new org.junit.internal.runners.model.MultipleFailureException(errors); 59b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 60b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot} 61