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.rule;
016    
017    import com.liferay.portal.kernel.util.StringBundler;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.test.rule.callback.LogAssertionTestCallback;
020    
021    import java.lang.Thread.UncaughtExceptionHandler;
022    
023    /**
024     * @author Shuyang Zhou
025     */
026    public class LogAssertionUncaughtExceptionHandler
027            implements UncaughtExceptionHandler {
028    
029            public LogAssertionUncaughtExceptionHandler(
030                    UncaughtExceptionHandler uncaughtExceptionHandler) {
031    
032                    _uncaughtExceptionHandler = uncaughtExceptionHandler;
033            }
034    
035            @Override
036            public void uncaughtException(Thread thread, Throwable throwable) {
037                    if (_uncaughtExceptionHandler != null) {
038                            _uncaughtExceptionHandler.uncaughtException(thread, throwable);
039                    }
040    
041                    StringBundler sb = new StringBundler(4);
042    
043                    sb.append("Uncaught exception in ");
044                    sb.append(thread);
045                    sb.append(StringPool.COMMA_AND_SPACE);
046                    sb.append(throwable);
047    
048                    LogAssertionTestCallback.caughtFailure(
049                            new AssertionError(sb.toString(), throwable));
050            }
051    
052            private final UncaughtExceptionHandler _uncaughtExceptionHandler;
053    
054    }