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.callback;
016    
017    import com.liferay.portal.kernel.test.rule.callback.BaseTestCallback;
018    import com.liferay.portal.kernel.test.util.TestPropsValues;
019    
020    import org.junit.Assert;
021    import org.junit.runner.Description;
022    
023    /**
024     * @author Shuyang Zhou
025     */
026    public class CITimeoutTestCallback extends BaseTestCallback<Long, Object> {
027    
028            public static final CITimeoutTestCallback INSTANCE =
029                    new CITimeoutTestCallback();
030    
031            @Override
032            public void afterClass(Description description, Long startTime) {
033                    long testTime = System.currentTimeMillis() - startTime;
034    
035                    if (testTime <= TestPropsValues.CI_TEST_TIMEOUT_TIME) {
036                            return;
037                    }
038    
039                    String message =
040                            description.getClassName() + " spent " + testTime +
041                                    "ms and surpassed the timeout threshold " +
042                                            TestPropsValues.CI_TEST_TIMEOUT_TIME + "ms.";
043    
044                    System.setProperty(_CI_TIMEOUT_TEST_CLASS_MESSAGE, message);
045    
046                    Assert.fail(
047                            message + " Marked it as failed and aborting subsequent tests.");
048            }
049    
050            @Override
051            public Long beforeClass(Description description) {
052                    String message = System.getProperty(_CI_TIMEOUT_TEST_CLASS_MESSAGE);
053    
054                    if (message != null) {
055                            Assert.fail(
056                                    "Abort running " + description.getClassName() + " due to : " +
057                                            message);
058                    }
059    
060                    return System.currentTimeMillis();
061            }
062    
063            private CITimeoutTestCallback() {
064            }
065    
066            private static final String _CI_TIMEOUT_TEST_CLASS_MESSAGE =
067                    "CI_TIMEOUT_TEST_CLASS_MESSAGE";
068    
069    }