001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.test.log;
016    
017    import java.util.Map;
018    import java.util.concurrent.ConcurrentHashMap;
019    
020    import org.junit.Assert;
021    
022    /**
023     * @author Shuyang Zhou
024     */
025    public class ConcurrentAssertUtil {
026    
027            public static void caughtFailure(String message) {
028                    Thread currentThread = Thread.currentThread();
029    
030                    if (currentThread != _thread) {
031                            _concurrentFailureMessages.put(currentThread, message);
032    
033                            _thread.interrupt();
034                    }
035                    else {
036                            Assert.fail(message);
037                    }
038            }
039    
040            public static void endAssert() {
041                    _thread = null;
042    
043                    try {
044                            for (Map.Entry<Thread, String> entry :
045                                            _concurrentFailureMessages.entrySet()) {
046    
047                                    Thread thread = entry.getKey();
048    
049                                    Assert.fail(
050                                            "Thread " + thread + " caught concurrent failure: " +
051                                                    entry.getValue());
052                            }
053                    }
054                    finally {
055                            _concurrentFailureMessages.clear();
056                    }
057            }
058    
059            public static void startAssert() {
060                    _thread = Thread.currentThread();
061            }
062    
063            private static final Map<Thread, String> _concurrentFailureMessages =
064                    new ConcurrentHashMap<Thread, String>();
065            private static volatile Thread _thread;
066    
067    }