Java tutorial
/* * Copyright 2012 the hegemon 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 com.cueup.hegemon; import com.google.common.base.Joiner; import com.google.common.collect.Lists; import org.apache.commons.lang.exception.ExceptionUtils; import org.junit.Assert; import java.util.List; /** * Utilities for tests. */ public class TestUtils { private TestUtils() { } /** * Utility class that collects errors from sub-threads. */ private static class ErrorCollector implements Thread.UncaughtExceptionHandler { private final List<String> errors = Lists.newArrayList(); @Override public synchronized void uncaughtException(Thread thread, Throwable throwable) { this.errors.add(thread.getName() + ": " + ExceptionUtils.getFullStackTrace(throwable)); } public void assertNoErrors() { if (!this.errors.isEmpty()) { Assert.fail(Joiner.on("\n\n").join(this.errors)); } } } public static void runConcurrent(int count, Runnable r) throws InterruptedException { List<Thread> threads = Lists.newArrayList(); ErrorCollector errorCollector = new ErrorCollector(); for (int i = 0; i < count; i++) { Thread t = new Thread(r); t.setName("testThread" + i); t.setUncaughtExceptionHandler(errorCollector); threads.add(t); } for (Thread t : threads) { t.start(); } for (Thread t : threads) { t.join(); } errorCollector.assertNoErrors(); } }